明解C语言 入门篇 第十二章答案

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

原文地址:https://www.cnblogs.com/nightswatch-candle/p/11939231.html

时间: 2024-08-23 04:29:19

明解C语言 入门篇 第十二章答案的相关文章

明解C语言 入门篇 第二章答案

练习2-1 1 #include <stdio.h> 2 3 int main() { 4 int x; 5 int y; 6 int percent; 7 8 puts("请输入两个整数"); 9 printf("整数x:"); 10 scanf("%d", &x); 11 printf("整数y:"); 12 scanf("%d", &y); 13 percent = (x

明解C语言 入门篇 第七章答案

练习7-1 #include <stdio.h> int main() { int n; printf("%d\t%d\t%d\n", sizeof 1,sizeof(unsigned)-1,sizeof n+2 ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4再减去-1所以显示为3,最后是n+2为6 printf("%d\t%d\t%d\n", sizeof +1, sizeof(double) - 1, sizeof(n

【Android的从零单排开发日记】之入门篇(十二)——Android组件间的数据传输

组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制数据. 一.Intent数据传递 那么首先是简单的跳转.我们可以借助bundle这个容器来存放我们想要传递的数据. Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 Bu

明解C语言 中级篇 第一章答案

练习1-1 #include <stdio.h> #include<stdlib.h> int main() { srand(time(0)); int anw = rand() % 7; printf("您的签运是:"); switch (anw) { case 0:printf("大吉"); break; case 1:printf("吉"); break; case 2:printf("小吉");

明解C语言 中级篇 第三章答案

练习3-1 /* 猜拳游戏(其四:分割函数/显示成绩)*/ #include <time.h> #include <stdio.h> #include <stdlib.h> int human; /* 玩家的手势 */ int comp; /* 计算机的手势 */ int win_no; /* 胜利次数 */ int lose_no; /* 失败次数 */ int draw_no; /* 平局次数 */ char* hd[] = { "石头", &q

明解C语言 中级篇 第四章答案

练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /*--- 生成4个不同数字的组合并存入数组x ---*/ void make4digits(int x[]) { int i, j, val; for (i = 0; i < 4; i++) { do { val =

第五部分 架构篇 第十二章 MongoDB Replica Sets 架构(简介)

1.Replication简介 MongoDB Replication即是在多个服务器中同步复制数据,数据复制的目的为提供冗余和提高数据的可用性,数据复制的操作即在不同的数据库服务器上保存多份复制的数据集,以此来避免单机故障进而丢失数据,复制机制也允许你恢复硬件故障和服务中断,进而起到数据的容灾和备份作用. 在MongoDB中,一个复制集就是一组mongod实例,一个mongod实例作为primary也就是所谓的master服务,接收所有的写操作,其他的mongod实例作为secondaries

第五部分 架构篇 第十四章 MongoDB Replica Sets 架构(自动故障转移/读写分离实践)

说明:该篇内容部分来自红丸编写的MongoDB实战文章. 1.简介 MongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余,多机器中同一时刻只有一台是用于写操作,正是由于这个情况,为了MongoDB提供了数据一致性的保障,担当primary角色的服务能把读操作分发给Slave(详情请看前两篇关于Replica Set成员组成和理解). MongoDB高可用分为两种: Master-Slave主从复制:只需要在某一个服务启动时加上-master参数,而另外一个服务加上-slave与-so

【three.js详解之一】入门篇

[three.js详解之一]入门篇 开场白 webGL可以让我们在canvas上实现3D效果.而three.js是一款webGL框架,由于其易用性被广泛应用.如果你要学习webGL,抛弃那些复杂的原生接口从这款框架入手是一个不错的选择. 博主目前也在学习three.js,发现相关资料非常稀少,甚至官方的api文档也非常粗糙,很多效果需要自己慢慢敲代码摸索.所以我写这个教程的目的一是自己总结,二是与大家分享. 本篇是系列教程的第一篇:入门篇.在这篇文章中,我将以一个简单的demo为例,阐述thre