练习12-1
#include <stdio.h> #define NAME_LEN 64 /* 姓名的字符数 */ struct student { char name[NAME_LEN]; /* 姓名 */ int height; /* 身高 */ float weight; /* 体重 */ long schols; /* 奖学金 */ }; int main(void) { struct student takao = { "Takao", 173, 86.2 }; printf("姓名 = %s\n", takao.name); printf("身高 = %d\n", takao.height); printf("体重 = %.1f\n", takao.weight); printf("奖学金 = %ld\n", takao.schols); printf("takao.name 的地址是%p\n",&(takao.name)); printf("takao.height的地址是%p\n", &(takao.height)); printf("takao.weight的地址是%p\n", &(takao.weight)); printf("takao.schols的地址是%p\n", &(takao.schols)); 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(void) { Student sanaka;// = { "Sanaka", 175, 62.5, 73000 }; printf("input name:"); scanf("%s",sanaka.name); printf("input height:"); scanf("%d", &(sanaka.height)); printf("input weight:"); scanf("%f", &(sanaka.weight)); printf("input schols:"); 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-3
#include <stdio.h> #define NAME_LEN 64 /* 姓名的字符数 */ struct xyz { int x; long y; double z; }; struct xyz scan_xyz(int x, long y,double z) { struct xyz xyz_value; xyz_value.x = x; xyz_value.y = y; xyz_value.z = z; return xyz_value; } int main(void) { int x; long y; double z; struct xyz xyz_value; printf("input x:"); scanf("%d",&x); printf("input y:"); scanf("%ld", &y); printf("input z:"); scanf("%lf", &z); xyz_value = scan_xyz(x, y, z); printf("xyz_value.x = %d\n",xyz_value.x); printf("xyz_value.y = %ld\n", xyz_value.y); printf("xyz_value.z = %lf\n", xyz_value.z); return 0; }
练习12-4
#include <stdio.h> #include <string.h> #define NUMBER 5 /* 学生人数 */ #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 temp = *x; *x = *y; *y = temp; } void sort_by_height(Student a[], int n) { int i, j; for (i = 0; i < n - 1; 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 - 1; i++) { for (j = n - 1; j > i; j--) if (a[j - 1].name > a[j].name) swap_Student(&a[j - 1], &a[j]); } } int main(void) { int i; int sort_type; Student std[NUMBER];//[] = { // { "Sato", 178, 61.2, 80000 }, /* 佐藤 */ // { "Sanaka", 175, 62.5, 73000 }, /* 佐中 */ // { "Takao", 173, 86.2, 0 }, /* 高尾 */ // { "Mike", 165, 72.3, 70000 }, /* 平木 */ // { "Masaki", 179, 77.5, 70000 }, /* 真崎 */ //}; printf("please input student name,height,weight,schols,use space to separate,use enter to input next student.\n"); for(i=0;i<NUMBER;i++) { printf("student %d:",i+1); scanf("%s %d %f %ld",std[i].name,&(std[i].height),&(std[i].weight),&(std[i].schols)); } printf("\n---------------------------------\n"); for (i = 0; i < NUMBER; i++) printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols); printf("press 0 to sort by height,press 1 to sort by name:"); scanf("%d",&sort_type); switch(sort_type) { case 0: sort_by_height(std, NUMBER); /* 按身高进行升序排列 */ puts("\n按身高排序。"); for (i = 0; i < NUMBER; i++) printf("%-8s %6d%6.1f%7ld\n",std[i].name, std[i].height, std[i].weight, std[i].schols); break; case 1: sort_by_name(std, NUMBER); /* 按姓名进行升序排列 */ puts("\n按姓名排序。"); for (i = 0; i < NUMBER; i++) printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols); break; } return 0; }
练习12-5
#include <math.h> #include <stdio.h> #define sqr(n) ((n) * (n)) typedef struct { double x; /* X坐标 */ double y; /* 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; /* 更新当前位置(向dest移动) */ c->fuel -= d; /* 更新燃料(减去行驶距离d所消耗的燃料) */ return 1; /* 成功行驶 */ } int main(void) { Car mycar = { { 0.0, 0.0 }, 90.0 }; int move_method; double x_distance; double y_distance; while (1) { int select; Point dest; /* 目的地的坐标 */ put_info(mycar); /* 显示当前位置和剩余燃料 */ printf("开动汽车吗【Yes···1 / No···0】:"); scanf("%d", &select); if (select != 1) break; printf("两种方法,1输入目的地,2输入X方向和Y方向的行驶距离:"); scanf("%d",&move_method); switch(move_method) { case 1: printf("目的地的X坐标:"); scanf("%lf", &dest.x); printf(" Y坐标:"); scanf("%lf", &dest.y); break; case 2: printf("X方向行驶距离:"); scanf("%lf",&x_distance); printf("Y方向行驶距离:"); scanf("%lf",&y_distance); dest.x = x_distance + mycar.pt.x; dest.y = y_distance + mycar.pt.y; break; } if (!move(&mycar, dest)) puts("\a燃料不足无法行驶。"); } return 0; }