練習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,%p\n", takao.name, &takao.name); printf("身高 = %d,%p\n", takao.height,&takao.height); printf("體重 = %.1f,%p\n", takao.weight, &takao.weight); printf("獎學金 = %ld,%p\n", takao.schols, &takao.schols); return 0; }
練習12-2
/* 擁有超能力的洋子(在結構體中引入typedef名) */ #include <stdio.h> #define NAME_LEN 64 /* 姓名的字符數 */ /*=== 表示學生的結構體 ===*/ typedef struct student { char name[NAME_LEN]; /* 姓名 */ int height; /* 身高 */ float weight; /* 體重 */ long schols; /* 獎學金 */ } Student; /*--- 將std指向的學生的身高變為180cm,體重變為80kg ---*/ void hiroko(Student* std) { if (std->height < 180) std->height = 180; if (std->weight > 80) std->weight = 80; } int main(void) { int height, weight, schols; printf("身高是:體重是:獎學金是:"); scanf("%d%d%d", &height, &weight, &schols); Student sanaka = { "Sanaka", height, weight, 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> /*=== xyz結構體 ===*/ struct xyz { int x; long y; double z; }; /*--- 返回具有{x,y,z}的值的結構體xyz ---*/ struct xyz scan_xyz() { int x; long y; double z; struct xyz temp; printf("x=,y=,z="); scanf("%d%ld%lf", &x, &y, &z); temp.x = x; temp.y = y; temp.z = z; return temp; } int main(void) { struct xyz s = { 0, 0, 0 }; s = scan_xyz(); printf("xyz.x = %d\n", s.x); printf("xyz.y = %ld\n", s.y); printf("xyz.z = %f\n", s.z); return 0; }
練習12-4
/* 將5名學生的身高按升序排列 */ #include <stdio.h> #include <string.h> #define NUMBER 5 /* 學生人數 */ #define NAME_LEN 64 /* 姓名的字符數 */ /*=== 表示學生的結構體 ===*/ typedef struct { char name[NAME_LEN]; /* 姓名 */ int height; /* 身高 */ double weight; /* 體重 */ int schols; /* 獎學金 */ } Student; /*--- 將x和y指向的學生進行交換 ---*/ void swap_Student(Student* x, Student* y) { Student temp = *x; *x = *y; *y = temp; } /*--- 將學生數組a的前n個元素按身高進行升序排列 ---*/ 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]); } } int main(void) { int i; Student std[] = { {0 }, { 0 }, { 0 }, { 0 }, { 0 }, }; for (i = 0; i < NUMBER; i++) { printf("姓名 身高 體重 獎學金\n "); scanf("%s %i %lf %d", &std[i].name, &std[i].height,& std[i].weight, &std[i].schols); } 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); sort_by_height(std, NUMBER); /* 按身高進行升序排列 */ int choice; printf("是否按身高排列,是->1,否->0\n"); scanf("%d", &choice); if (choice == 1) { 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); } 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; }