《明解C语言》第三版 (入门篇) 第十二章练习答案

练习12-1

#include<stdio.h>

#define NAME_LEN 64

struct student{
	char name[NAME_LEN];
	int height;
	float weight;
	long schools;
};

int main(void)
{
	struct student takao={"Takao",173,86.32};
	
	printf("姓名的地址=%p\n",&takao.name);
	printf("身高的地址=%p\n",&takao.height);
	printf("体重的地址=%p\n",&takao.weight);
	printf("奖学金的地址=%p\n",&takao.schools);
	
	return 0;
}

练习12-2

#include<stdio.h>

#define NAME_LEN 128

struct student{
	char name[NAME_LEN];
	int height;
	float weight;
	long schools;
};

int main(void)
{
	struct student takao={};
	printf("请输入姓名为:");	scanf("%s",&takao.name);
	printf("请输入身高为:");	scanf("%d",&takao.height);
	printf("请输入体重为:");	scanf("%f",&takao.weight);
	printf("请输入奖学金为:");	scanf("%ld",&takao.schools);
		
    printf("姓名 = %s\n", takao.name);
    printf("身高 = %d\n", takao.height);
    printf("体重 = %.1f\n", takao.weight);
    printf("奖学金 = %ld\n", takao.schools);
	
	return 0;
}

练习12-3

#include <stdio.h>
#include <string.h>
struct xyz {
	int x;
	long y;
	double z;
};

struct xyz xyz_of(int a,long b,double c) {
	struct xyz s;
	s.x = a;
	s.y = b;
	s.z = c;

	return s;
};

int main()
{
	int a;
	long b;
	double c;
	struct xyz s_1;

	puts("请输入a,b,c的值,整形,长整型,浮点型");
	printf("a = ");  scanf("%d",&a);
	printf("b = ");  scanf("%ld", &b);
	printf("c = ");  scanf("%lf", &c);

	s_1 = xyz_of(a,b,c);

	printf("%d\n%ld\n%lf\n", s_1.x,s_1.y,s_1.z);

	return 0;
}

练习12-4


#include <stdio.h>
#include <string.h>

#define NUMBER 5
#define NAME 32

typedef struct {
    char  name[NAME];    /* 姓名 */
    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; 
    int tmp;

    puts("请输入0或者1。(0---按身高排序,1---按姓名排序)");
    printf("你选择: ");    scanf("%d", &tmp);

    if(tmp == 0)
    {
        for (i = 0; i < NUMBER; i++)
        {
            for (j = 0; j < NUMBER; j++)
            {
                if (a[j].height > a[j + 1].height)
                {
                    swap_Student(&a[j],&a[j + 1]);
                }
            }
        }
    }

    if(tmp == 1)
    {
        for (i = 0; i < NUMBER; i++)
        {
            for (j = 0; j < NUMBER; j++)
            {
                if (a[j].name > a[j + 1].name)
                {
                    swap_Student(&a[j],&a[j + 1]);
                }
            }
        }
    }

}

int main()
{
    int i;

    Student std[] = { 
        {0},{0},{0},{0},{0},
    };

    printf("依次输入姓名,身高,体重,奖学金。(以一个空格隔开)\n");
    for (i = 0; i < NUMBER; i++) {
        scanf("%s %d %f %ld",std[i].name, &std[i].height, &std[i].weight, &std[i].schols);
    } 

    printf("你输入的是\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);
    }

    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);
    }

    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漏洞嵌入式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值