练习12-1
#include <stdio.h>
#define NAME_LEN 64
struct student {
char name[NAME_LEN];
int height;
float weight;
double schlors;
};
int main(void)
{
struct student takao = { "Takao",173,86.3 };
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("奖学金是%d,其地址为%p\n", takao.schlors,&(takao.schlors));
return 0;
}
练习12-2
#include <stdio.h>
#define NAME_LEN 64
typedef struct student {
char name[NAME_LEN];
int height;
float weight;
double scholars;
}Student;
void hiroko(Student *std)
{
if ((*std).height < 180) (*std).height = 180;
if ((*std).weight > 80.0) (*std).weight = 80.0;
}
int main(void)
{
Student tmp ;
printf("请输入姓名:"); scanf("%s", tmp.name);
printf("请输入身高:"); scanf("%d", &tmp.height);
printf("请输入体重:"); scanf("%f", &tmp.weight);
printf("请输入奖学金:"); scanf("%ld", &tmp.scholars);
putchar('\n');
hiroko(&tmp);
printf("姓名%s\n", tmp.name);
printf("身高%d\n", tmp.height);
printf("体重%.1f\n", tmp.weight);
printf("奖学金%ld\n", tmp.scholars);
return 0;
}
练习12-3
#include <stdio.h>
struct xyz {
int x;
long y;
double z;
};
struct xyz xyz_of(int x, long y, double z)
{
struct xyz tmp;
tmp.x = x;
tmp.y = y;
tmp.z = z;
return tmp;
}
int main(void)
{
struct xyz s;
int x;
long y;
double z;
printf("请输入int型x:"); scanf("%d", &x);
printf("请输入long型数值y:"); scanf("%ld", &y);
printf("请输入double型z:"); scanf("%lf", &z);
s = xyz_of(x, y, z);
printf("x=%d\n", s.x);
printf("y=%ld\n", s.y);
printf("z=%.3f\n", s.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 scholars;
}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, j;
Student std[NUMBER];
for (i = 0; i < NUMBER; i++) {
printf("第%d名同学姓名:",i+1); scanf("%s", std[i].name);
printf("第%d名同学身高:",i+1); scanf("%d", &std[i].height);
printf("第%d名同学体重:",i+1); scanf("%f", &std[i].weight);
printf("第%d名同学奖学金:",i+1); scanf("%ld", &std[i].scholars);
putchar('\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].scholars);
do {
printf("请选择操作方式:【0...按照身高排序/1...按照名字的顺序排序】");
scanf("%d", &j);
} while (j < 0 || j>1);
if (j == 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].scholars);
}
else {
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].scholars);
}
return 0;
}
练习12-5
#include <stdio.h>
#include <math.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(void)
{
Car mycar = { 0.0,0.0,90.0 };
Point dest = { 0.0,0.0 };
while (1) {
int select;
int num;
double x, y;
put_info(mycar);
printf("请选择前往方式:【直接输入目的地---1/输入X方向和Y方向---2】");
scanf("%d", &num);
if (num == 1) {
printf("开动汽车吗【Yes---1/No---0】:");
scanf("%d", &select);
if (select != 1) break;
printf("目的地的X坐标:"); scanf("%lf", &dest.x);
printf(" Y坐标:"); scanf("%lf", &dest.y);
if (!move(&mycar, dest))
puts("\a燃料不足,无法行驶。");
}
else {
printf("开动汽车吗【Yes---1/No---0】:");
scanf("%d", &select);
if (select != 1) break;
printf("沿X方向行驶距离:"); scanf("%lf", &x);
printf("沿Y方向行驶距离:"); scanf("%lf", &y);
dest.x += x;
dest.y += y;
if (!move(&mycar, dest))
puts("\a燃料不足,无法行驶。");
}
}
return 0;
}