12.1
#include
#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("姓名的地址为%p\n", &takao.name);
printf("身高的地址为%p\n", &takao.height);
printf("体重的地址为%p\n", &takao.weight);
printf("奖学金的地址为%p\n", &takao.schols);
return 0;
}
12.2
#include
#define NAME_LEN 64
struct student
{
char name[NAME_LEN];
int height;
float weight;
long schols;
};
void hiroko(struct student *std)
{
if (std->height < 180)
std->height = 180;
if (std->weight > 80)
std->weight = 80;
}
int main(void)
{
struct student sanako;
printf("姓名: "); scanf("%s", sanako.name);
printf("身高: "); scanf("%d", &sanako.height);
printf("体重: "); scanf("%f", &sanako.weight);
printf("奖学金: "); scanf("%ld", &sanako.schols);
hiroko(&sanako);
puts("超能力~");
printf("姓名 = %s\n", sanako.name);
printf("身高 = %d\n", sanako.height);
printf("体重 = %.1f\n", sanako.weight);
printf("奖学金 = %ld\n", sanako.schols);
return 0;
}
12.3
#include
struct xyz{
int a;
long b;
double c;
};
struct xyz scan_xyz(int a, long b, double c){
struct xyz s1;
s1.a = a;
s1.b = b;
s1.c = c;
return s1;
};
int main(void)
{
int a;
long b;
double c;
struct xyz s2 = {0, 0, 0};
printf("请输入a, b, c的值。\n");
printf("a: "); scanf("%d", &a);
printf("b: "); scanf("%ld", &b);
printf("c: "); scanf("%lf", &c);
s2 = scan_xyz(a, b, c);
printf("%d %ld %lf\n", s2.a, s2.b, s2.c);
return 0;
}
12.4
#include
#include
#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_or_name(Student a[], int n)
{
int i, j, temp;
puts("请输入0或者1。(0---按身高排序,1---按姓名排序)");
printf("选择: "); scanf("%d", &temp);
if (temp == 0)
{
for (i = 0; i < n - 1; i++)
{
for (j = n; j > i; j--)
{
if (a[j - 1].height > a[j].height)
swap_Student(&a[j - 1], &a[j]);
}
}
}
if (temp == 1)
{
for (i = 0; i < n - 1; i++)
{
for (j = n; j > i; j--)
{
if (a[j - 1].name[0] > a[j].name[0])
swap_Student(&a[j - 1], &a[j]);
}
}
}
}
int main(void)
{
int i;
Student std[NUMBER];
printf("请依次输入姓名,身高,体重,奖学金。(数据之间以一个空格隔开)\n");
for (i = 0; i < NUMBER; i++)
{
printf("学生%d: ", i + 1);
scanf("%s %d %f %ld",
std[i].name, &std[i].height, &std[i].weight, &std[i].schols);
}
sort_by_height_or_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);
}
return 0;
}
12.5
#include
#include
#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) + (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(void)
{
Car mycar = {{0.0, 0.0}, 90.0};
while (1)
{
int select, temp;
double x, y;
Point dest;
put_info(mycar);
printf("开动汽车吗[Yes---1/No---0] : ");
scanf("%d", &select);
printf("选择输入[目的地坐标---1/行驶距离---0] : ");
scanf("%d", &temp);
if (select != 1)
break;
else
{
if (temp == 1)
{
printf("目的地的X的坐标: "); scanf("%lf", &dest.x);
printf("目的地的Y的坐标: "); scanf("%lf", &dest.y);
}
if (temp == 0)
{
printf("X轴方向的行驶距离: "); scanf("%lf", &x);
dest.x = mycar.pt.x + x;
printf("Y轴方向的行驶距离: "); scanf("%lf", &y);
dest.x = mycar.pt.y + y;
}
}
if (!move(&mycar, dest))
puts("\a燃料不足无法行驶。");
}
return 0;
}