《C语言程序设计教程》(主编黄迪明、余勤)第八章课后习题答案

来源:互联网 发布:sql insert触发器实例 编辑:程序博客网 时间:2024/05/22 06:44

       在阅读代码前,先说一下这本书的在版编目(CIP)数据:C语言程序设计教程/黄迪明、余勤主编.--北京:国防工业出版社,2006.5    ISBN 7-118-04516-0

       所有代码均在 VS2013中成功运行。第一次写博客,若有不妥之处,非常欢迎您提出问题,若能提出改进意见,不胜感激!



8.1  重写8.4程序,使用swap()函数将输入数据的高位字节和低位字节交换后返回。main()函数调用这个函数,实现程序的功能。(下面的程序为源程序文件,目前不了解该程序,过一段时间再将重写的程序补充上来)

#include<stdio.h>void main(){printf("将输入数据的高位字节和低位字节交换后返回。\n\n");union body{struct BYTE{unsigned char l, h;}byte;unsigned int word;}a, b;printf("Enter data?");scanf("%x",&a.word);b.byte.h = a.byte.l;b.byte.l = a.byte.h;printf("%x-->%x\n",a.word,b.word);getch();}


8.5  编写一个程序,定义modem变量为位域结构struct bits类型,并初始化b0为1,b1为1,b2为1,b3为0,b4为1,要求程序输出modem变量中每个位域成员的值。

#include<stdio.h>void main(){        printf("编写一个程序,定义modem变量为位域结构struct bits类型,并初始化b0为1,b1为1,b2为1,b3为0,b4为1,要求程序输出modem变量中每个位域成员的值。\n\n");       struct bits{int b0 : 1;int b1 : 1;int b2 : 1;int b3 : 1;int b4 : 1;};struct bits modem;modem.b0 = 0;modem.b1 = 0;modem.b2 = 0;modem.b3 = 0;modem.b4 = 0;printf("modem->b0:%d\tmodem->b1:%d\tmodem->b2:%d\tmodem->b3:%d\tmodem->b4:%d\t", modem.b0, modem.b1, modem.b2, modem.b3, modem.b4);getch();}


8.7  使用带参数的宏MXA(a,b),编写从a,b,c三个数中找出最大数的程序。

#include<stdio.h>#define MAX(a,b)((a)>(b)?(a):(b))void main(){        printf("使用带参数的宏MXA(a,b),编写从a,b,c三个数中找出最大数的程序。\n\n");       int a = 0, b = 0, c = 0;printf("请输入a的值:");scanf("%d",&a);printf("请输入b的值:");scanf("%d", &b);printf("请输入c的值:");scanf("%d", &c);printf("最大的数为:%d",MAX(c,MAX(a,b)));getch();}


8.8  定义一个求两个数中最小值的宏MIN(a,b),并写一个测试该宏定义的程序。

    (1)定义程序的文件

//该定义的程序#include<stdio.h>#include"head2.h"#define MIN(a,b)((a)<(b)?(a):(b))void main(){printf("最小的数为:%d", MIN(c, d));getch();}
          (2)测试文件

//测试文件int c = 2, d = 4;

具体情况如下图所示:




8.9  使用带参数的宏,编写输入两个整数,求其相除的商和余数的程序。

#include<stdio.h>#define SHANG(a,b)((a)/(b))#define YUSHU(a,b)((a)%(b))void main(){printf("  使用带参数的宏,编写输入两个整数,求其相除的商和余数的程序。\n\n");int a = 0, b = 0;printf("请输入被除数:");scanf("%d", &a);printf("请输入除数:");scanf("%d", &b);printf("a/b=%d\n", SHANG(a, b));printf("a%%b=%d", YUSHU(a, b));getch();}


                                                                                             业精于勤荒于嬉,行成于思毁于随。撸代码的你,好好加油吧!


阅读全文
0 0