#include <math.h>
#include <stdio.h>
#define sqr(n) ((n) * (n))
typedef struct{
double x;
double y;
} Point;
typedef struct{
Point pt;
double fuel;
} Car;
double distance_of(Point pa, Point pb){
return sqrt(sqr(pa.x - pb.x) + sqr(pa.y - pb.y));
}
void put_info(Car c){
printf("現在位置 : (%.2f, %.2f)\n", c.pt.x, c.pt.y);
printf("残り燃料 : %.2fリットル\n", c.fuel);
}
int move(Car *c, Point dest){
double d = distance_of(c->pt, dest);
if(d > c->fuel) return 0;
c->pt = dest;
c->fuel -= d;
return 1;
}
int main(){
Car mycar = {{0.0, 0.0}, 90.0};
while(true){
int select;
Point dest;
put_info(mycar);
printf("移動しますか? yes:1/no:0 -> "); scanf("%d", &select);
if(select != 1) break;
printf("目的地を入力:1 / 移動距離を入力:2 -> "); scanf("%d", &select);
if(select == 1){
printf("目的地のx座標 : "); scanf("%lf", &dest.x);
printf("目的地のy座標 : "); scanf("%lf", &dest.y);
if(!move(&mycar, dest)) printf("移動できません\n");
}else if(select == 2){
printf("x方向の移動距離 : "); scanf("%lf", &dest.x);
printf("y方向の移動距離 : "); scanf("%lf", &dest.y);
dest.x += mycar.pt.x;
dest.y += mycar.pt.y;
if(!move(&mycar, dest)) printf("移動できません\n");
}
}
return 0;
}
演習 12-4
#include <stdio.h>
#include <string.h>
#define NAME_LEN 64
typedef struct{
char name[NAME_LEN];
int height;
float weight;
long schols;
} Student;
void swap_Student(Student *x, Student *y){
Student tmp = *x;
*x = *y;
*y = tmp;
}
void sort_by_height(Student a[], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(a[j - 1].height > a[j].height) swap_Student(&a[j - 1], &a[j]);
}
}
}
void sort_by_name(Student a[], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(strcmp(a[j - 1].name, a[j].name) > 0) swap_Student(&a[j - 1], &a[j]);
}
}
}
int main(){
int i, n;
Student std[256];
printf("何人の情報を入力しますか?"); scanf("%d", &n);
for(i = 0; i < n; i++){
printf("氏名 : "); scanf("%s", std[i].name);
printf("身長 : "); scanf("%d", &std[i].height);
printf("体重 : "); scanf("%f", &std[i].weight);
printf("奨学金 : "); scanf("%ld", &std[i].schols);
}
printf("身長を昇順にソート:1, 名前を昇順にソート:2 -> "); scanf("%d", &i);
if(i == 1){
sort_by_height(std, n);
printf("身長順にソートしました\n");
}
else if(i == 2){
sort_by_name(std, n);
printf("名前順にソートしました\n");
}
for(i = 0; i < n; i++){
printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols);
}
return 0;
}
#include <string.h>
#define NAME_LEN 64
typedef struct{
char name[NAME_LEN];
int height;
float weight;
long schols;
} Student;
void swap_Student(Student *x, Student *y){
Student tmp = *x;
*x = *y;
*y = tmp;
}
void sort_by_height(Student a[], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(a[j - 1].height > a[j].height) swap_Student(&a[j - 1], &a[j]);
}
}
}
void sort_by_name(Student a[], int n){
int i, j;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(strcmp(a[j - 1].name, a[j].name) > 0) swap_Student(&a[j - 1], &a[j]);
}
}
}
int main(){
int i, n;
Student std[256];
printf("何人の情報を入力しますか?"); scanf("%d", &n);
for(i = 0; i < n; i++){
printf("氏名 : "); scanf("%s", std[i].name);
printf("身長 : "); scanf("%d", &std[i].height);
printf("体重 : "); scanf("%f", &std[i].weight);
printf("奨学金 : "); scanf("%ld", &std[i].schols);
}
printf("身長を昇順にソート:1, 名前を昇順にソート:2 -> "); scanf("%d", &i);
if(i == 1){
sort_by_height(std, n);
printf("身長順にソートしました\n");
}
else if(i == 2){
sort_by_name(std, n);
printf("名前順にソートしました\n");
}
for(i = 0; i < n; i++){
printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols);
}
return 0;
}
演習 12-3
#include <stdio.h>
typedef struct xyz{
int x;
long y;
double z;
}xyz;
xyz scan_xyz(){
xyz tmp;
printf("x : "); scanf("%d", &tmp.x);
printf("y : "); scanf("%ld", &tmp.y);
printf("z : "); scanf("%lf", &tmp.z);
return tmp;
}
int main(){
xyz s;
s = scan_xyz();
printf("xyz.x : %d\n", s.x);
printf("xyz.y : %ld\n", s.y);
printf("xyz.z : %lf\n", s.z);
return 0;
}
typedef struct xyz{
int x;
long y;
double z;
}xyz;
xyz scan_xyz(){
xyz tmp;
printf("x : "); scanf("%d", &tmp.x);
printf("y : "); scanf("%ld", &tmp.y);
printf("z : "); scanf("%lf", &tmp.z);
return tmp;
}
int main(){
xyz s;
s = scan_xyz();
printf("xyz.x : %d\n", s.x);
printf("xyz.y : %ld\n", s.y);
printf("xyz.z : %lf\n", s.z);
return 0;
}
演習 12-2
#include <stdio.h>
#define NAME_LEN 64
typedef struct student{
char name[NAME_LEN];
int height;
float weight;
long schols;
}Student;
void hiroko(Student *std){
if(std->height < 180) std->height = 180;
if(std->weight > 80) std->weight = 80;
}
int main(){
Student sanaka;
printf("氏名 : "); scanf("%s", sanaka.name);
printf("身長 : "); scanf("%d", &sanaka.height);
printf("体重 : "); scanf("%f", &sanaka.weight);
printf("奨学金 : "); scanf("%ld", &sanaka.schols);
hiroko(&sanaka);
printf("氏名 = %s\n", sanaka.name);
printf("身長 = %d\n", sanaka.height);
printf("体重 = %.1f\n", sanaka.weight);
printf("奨学金 = %ld\n", sanaka.schols);
return 0;
}
#define NAME_LEN 64
typedef struct student{
char name[NAME_LEN];
int height;
float weight;
long schols;
}Student;
void hiroko(Student *std){
if(std->height < 180) std->height = 180;
if(std->weight > 80) std->weight = 80;
}
int main(){
Student sanaka;
printf("氏名 : "); scanf("%s", sanaka.name);
printf("身長 : "); scanf("%d", &sanaka.height);
printf("体重 : "); scanf("%f", &sanaka.weight);
printf("奨学金 : "); scanf("%ld", &sanaka.schols);
hiroko(&sanaka);
printf("氏名 = %s\n", sanaka.name);
printf("身長 = %d\n", sanaka.height);
printf("体重 = %.1f\n", sanaka.weight);
printf("奨学金 = %ld\n", sanaka.schols);
return 0;
}
演習 12-1
#include <stdio.h>
#define NAME_LEN 64
struct student{
char name[NAME_LEN];
int height;
float weight;
long schols;
};
int main(){
struct student takao = {"Takao", 173, 86.2};
printf("氏名 = %s, adress = %p\n", takao.name, &takao.name);
printf("身長 = %d, adress = %p\n", takao.height, &takao.height);
printf("体重 = %.1f, adress = %p\n", takao.weight, &takao.weight);
printf("奨学金 = %ld, adress = %p\n", takao.schols, &takao.schols);
return 0;
}
#define NAME_LEN 64
struct student{
char name[NAME_LEN];
int height;
float weight;
long schols;
};
int main(){
struct student takao = {"Takao", 173, 86.2};
printf("氏名 = %s, adress = %p\n", takao.name, &takao.name);
printf("身長 = %d, adress = %p\n", takao.height, &takao.height);
printf("体重 = %.1f, adress = %p\n", takao.weight, &takao.weight);
printf("奨学金 = %ld, adress = %p\n", takao.schols, &takao.schols);
return 0;
}
登録:
投稿 (Atom)