明解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", , 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 ;
}
练习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 < ) std->height = ;
if (std->weight > ) std->weight = ;
} 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 ;
}
练习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 = { , , };
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 ;
}
练习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 = ; i < n - ; i++) {
for (j = n - ; j > i; j--)
if (a[j - ].height > a[j].height)
swap_Student(&a[j - ], &a[j]);
}
} int main(void)
{
int i; Student std[] = { { }, { }, { }, { }, { },
}; for (i = ; i < NUMBER; i++) { printf("姓名 身高 体重 奖学金\n ");
scanf("%s %i %lf %d", &std[i].name, &std[i].height,& std[i].weight, &std[i].schols);
} for (i = ; 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 == ) {
puts("\n按身高排序。");
for (i = ; i < NUMBER; i++)
printf("%-8s %6d%6.1f%7ld\n",
std[i].name, std[i].height, std[i].weight, std[i].schols);
}
return ;
}
练习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 ; /* 无法行驶 */
c->pt = dest; /* 更新当前位置(向dest移动) */
c->fuel -= d; /* 更新燃料(减去行驶距离d所消耗的燃料) */
return ; /* 成功行驶 */
} int main(void)
{
Car mycar = { { 0.0, 0.0 }, 90.0 };
int move_method;
double x_distance;
double y_distance;
while () {
int select;
Point dest; /* 目的地的坐标 */ put_info(mycar); /* 显示当前位置和剩余燃料 */ printf("开动汽车吗【Yes···1 / No···0】:");
scanf("%d", &select);
if (select != ) break; printf("两种方法,1输入目的地,2输入X方向和Y方向的行驶距离:");
scanf("%d", &move_method);
switch (move_method)
{
case :
printf("目的地的X坐标:"); scanf("%lf", &dest.x);
printf(" Y坐标:"); scanf("%lf", &dest.y);
break;
case :
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 ;
}
明解C语言 入门篇 第十二章答案的更多相关文章
- 明解C语言 入门篇 第五章答案
练习5-1 /* 依次把1.2.3.4.5 赋值给数组的每个元素并显示(使用for语句) */ #include <stdio.h> int main(void) { int i; ]; ...
- 明解C语言 入门篇 第四章答案
练习4-1 #include <stdio.h> int main(void) { int no; int x; do{ printf("请输入一个整数:"); sca ...
- 明解C语言 入门篇 第三章答案
练习3-1 #include <stdio.h> int main() { int x; int y; puts("请输入两个整数."); printf("整 ...
- 明解C语言 入门篇 第二章答案
练习2-1 #include <stdio.h> int main() { int x; int y; int percent; puts("请输入两个整数"); pr ...
- 明解C语言 入门篇 第一章答案
练习1-1 #include <stdio.h> int main() { int a; a = 15; int b; b = 37; int c; c = a - b; printf(& ...
- 明解C语言 入门篇 第八章答案
练习8-1 #include<stdio.h> #define diff(x,y)(x-y) int main() { int x; int y; printf("x=" ...
- 明解C语言 入门篇 第十三章答案
练习13-1 /* 打开与关闭文件 */ #include <stdio.h> int main(void) { ]; FILE* fp; printf("请输入你要打开的文件& ...
- 明解C语言 入门篇 第十一章答案
练习11-1 /* 用指针实现的字符串的改写 */ #include <stdio.h> int main(void) { "; printf("p = \" ...
- 明解C语言 入门篇 第十章答案
练习10-1 #include <stdio.h> void adjust_point(int*n) { ) *n = ; ) *n = 0; } int main() { int x; ...
随机推荐
- html背景音乐
标签<audio> 参用属性 autoplay="autoplay"自动播放 controls="controls",在页面内显示显示控件,如播放按 ...
- 【计算机网络】WebSocket实现原理分析
1.介绍一下websocket和通信过程? 1.1 基本概念 [!NOTE] Websocket是应用层第七层上的一个应用层协议,它必须依赖 HTTP 协议进行一次握手 ,握手成功后,数据就直接从 T ...
- Springboot访问静态资源&WebJars&图标&欢迎页面
目录 概述 1.访问WebJar资源 2.访问静态资源 3.favicon.ico图标 4.欢迎页面 概述 使用Springboot进行web开发时,boot底层实际用的就是springmvc,项目中 ...
- github上星星1万多的python教程推荐收藏
简单的说,Python是一个“优雅”.“明确”.“简单”的编程语言. 学习曲线低,非专业人士也能上手 开源系统,拥有强大的生态圈 解释型语言,完美的平台可移植性 支持面向对象和函数式编程 能够通过调用 ...
- 移动端适配方案(rem+flex)
为什么用rem不用px? 主流:各大网站的移动版绝大多数都是用的rem. 移动端屏幕分辨率差别太大:最低适配的iPhone6,分辨率仅为750*1334.而现在市面上大多数手机,都达到了1080* ...
- whistle手机调试工具使用简单教程
npm全局安装 npm install -g whistle 全局启动 w2 start 启动之后,输入127.0.0.1:8899 就可以访问到whistle调试界面了: 我们主要常用几个功能: 1 ...
- UITableViewStyleGrouped 类型 tableView sectionHeader 高度问题
UITableViewStyleGrouped 类型的 tableView 在适配的时候出现很大的问题.记录一下 按照之前的方法,只需要执行以下的代码就能够很好的解决 section == 0 的时候 ...
- 【转载】不可不知的 Android strings.xml 那些事
相信 strings.xml 已经是大家在 Android 开发中最熟悉的文件之一了,但其实它也有很多需要注意的地方和一些小技巧,知道了这些可以让你的 Android 应用更加规范易用,大家来看看吧. ...
- ACM-ICPC 2018 南京赛区网络预赛 I. Skr(回文树)
题意 https://nanti.jisuanke.com/t/A1955 求所有本质不同的回文串转成数后的和. 思路 如果了解回文树的构造原理,那么这题就很简单了,回文树每个结点代表一个回文串,每添 ...
- drf扩展知识点总结视图