明解C语言(入门篇)第十二章

1、练习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;
}

2、练习12-2

#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={};
	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;
}

3、练习12-3

#include<stdio.h>

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\n%ld\n%lf\n", s2.a, s2.b, s2.c);

    return 0;
}

4、练习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_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;
}

5、练习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) + (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;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值