大连海事大学C语言与Windows程序设计6道课后题答案

合集下载

C语言程序设计教程课后习题答案

C语言程序设计教程课后习题答案

第1章1-3 CAB4 .c .obj .exe5 /* */6 ;7 算法8 ①中级语言:C语言具有高级语言的先进思想又能直接对存储器进行操作,能进行位运算,能实现汇编语言的大部分功能,生成目标代码质量高,程序执行效率高。

②结构化语言:C语言用函数作为程序模块,以实现程序的模块化,语言简洁、紧凑,具有结构化的特点。

③可移植性好:C语言不包含依赖硬件的输入输出机制,使C语言本身不依赖于硬件系统,可移植性好。

9 #include<>main( ) { ; }10 #include “”main(){printf(“This is my first C Program!”);}第2章1.yes2.-33.2,14.1)a!=b||a<=c 2)x>=4||x<=-45.x>20&&x<30||x<-1006.#include <>main(){int x;printf(“please input an integar:”);scanf("%d",&x);if(x%5==0&&x%7==0) printf("yes\n");else printf("no\n");}7.#include <>main(){int year,month;printf("please input the year and month:");scanf("%d%d",&year,&month);switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf("this month have 31 days.");break;case 4:case 6:case 9:case 11:printf("this month have 30 days.");break;case 2:if(year%4==0&&year%100!=0||year%400==0){printf("this month have 29 days.");break;}else{printf("this month have 28 days.");break;}}}8.#include <>main(){float money;int year;printf("\nplease input the money and the year:");scanf("%f%d",&money,&year);if(year==1) money+=money**12*year;if(year==2) money+=money**12*year;if(year==3||year==4) money+=money**12*year;if(year>=5&&year<=7) money+=money**12*year;if(year>=8) money+=money**12*year;printf("the money is:%f",money);}第3章1#include ""main(){ float x[10],sum=0;int i=0;printf("please input 10 numbers(-10e6<x<10e6)\n");while(i<10){ scanf("%f",&x[i]);sum=sum+1/x[i];i=i+1;}printf("the sum of 10 numbers is %.2f\n",sum);}2#include ""main(){ int x[100],sum=0,aver,i=0;printf("please input numbers until 0\n");scanf("%d",&x[i]);while(x[i]!=0){ sum=sum+x[i];i=i+1;scanf("%d",&x[i]);}printf("the sum is %d\n",sum);printf("the average is %.2f\n",float(sum)/i);}3#include ""#include ""main(){ int i=1,j,f;long int s=0;while(i<=10){ for(f=1,j=1;j<=i;j++)f=f*j;s=s+pow(-1,i-1)*f;i=i+1;}printf("s= %ld\n",s);}4#include ""#include ""main(){ int i=0;float s=;while(2*i+1<=101){ s=s+pow(-1,i)/float(2*i+1);i=i+1;}printf("s= %f\n",s);}5#include ""#include""main(){ int i,j,k,n=0;for(i=1;i<=9;i++)for(j=0;j<=9;j++)for(k=0;k<=9;k++)if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)printf("the %d:%d\n",++n,i*100+j*10+k); printf("the all is %d.\n",n--);}6#include ""main(){ int n,i,j;printf("please input n:");scanf("%d",&n);for(i=0;i<n;i++){ for(j=0;j<n;j++)printf("*");printf("\n");}}7#include ""main(){ char a[81];int i,j=0,k=0,m=0;printf("please input a $(length<=80)\n");for(i=0;(a[i]=getchar())!='\n';i++){ if(a[i]>=65&&a[i]<=90)j++;else if(a[i]==32)k++;else m++;}printf("the upper letter is %d\n",j);printf("the blank is %d\n",k);printf("the other is %d\n",m);}8#include ""main(){ int i,j,k,m=0;for(i=0;i<=20;i++){ for(j=0;j<=50;j++)for(k=0;k<=100;k++)if(i*5+j*2+k==100)printf("the %d wag is 1:%d,2:%d,5:%d\n",++m,k,j,i);}printf("the wags is %d\n",m--);}9#include ""main(){ int i,j,k,m=0;for(i=0;i<=3;i++){ for(j=0;j<=3;j++)for(k=0;k<=6;k++)if(i+j+k==8)printf("the %d wag is white:%d,red:%d,black:%d\n",++m,i,j,k); }printf("the wags is %d\n",m--);}10#include ""main(){ long int n;int i,j,k;printf("please inpur n:");scanf("%ld",&n);if(n%2==1||n<=1){ printf("the input is error.\n");return(1);}for(i=2;i<=n/2;i++){ for(j=2;j<i;j++)if(i/j*j==i)break;if(j==i){for(k=2;k<n-i;k++)if((n-i)/k*k==(n-i))break;if(k==n-i)printf("%ld=%d+%d\n",n,i,n-i);}}}11#include ""main(){int i,j,k,m;for(i=1;i<=9;i++)for(j=0;j<=9;j++)for(k=0;k<=9;k++)for(m=0;m<=9;m++)if((i*1000+j*100+k*10+m)*9==(i+j*10+k*100+m*1000)) printf("the number is:%d\n",i*1000+j*100+k*10+m);}第4章1-5 ABDCB 6-10 BCBCA 11-13 ABA14#include ""main(){float a1,b1,a2,b2,re1,im1,re2,im2,re3,im3;printf("Please input the first number:");scanf("%f %f",&a1,&b1);printf("Please input the second number:");scanf("%f %f",&a2,&b2);re1=a1+a2;im1=b1+b2;printf("(%f+j%f)+(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re1,im1);re2=a1-a2;im2=b1-b2;printf("(%f+j%f)-(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re2,im2);re3=a1*a2-b1*b2;im3=a1*b2+a2*b1;printf("(%f+j%f)*(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re3,im3);}第5章1-5 CACCD 6-7 DA8#include ""main(){char str[100];int num1=0,num2=0,i=0;scanf("%s",str);while(str[i]!='\0'){if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')num1++;else if(str[i]>='0'&&str[i]<='9')num2++;i++;}printf("num1=%d,num2=%d",num1,num2);}9笔者在程序中直接给数组元素赋了值,读者可以修改为用scanf函数读入数值;另外,还可以用第7章宏定义的方法设定数组的长度,从而实现对元素个数不同的数组进行排序。

C语言程序设计课后答案

C语言程序设计课后答案

C 语言程序设计习题答案习题一 C 语言程序设计概述一、名词解释(1)程序P1 (2)程序设计P1 (3)机器语言P1 (4)汇编程序P2(5)高级语言P2 (6)编译程序P3 (7)解释程序P3 (8)算法P4(9)结构化的程序设计P9二、简述题1. 设计程序时应遵循哪些基本原则?P4答:正确性、可靠性、简明性、有效性、可维护性、可移植性。

2. 算法的要素是什么?算法具有哪些特点?答:算法的要素是:操作与控制结构;算法的特点有:有穷性、确定性、有效性、有零个或多个输入、有一个或多个输出。

3. 算法的表示形式有哪几种?答:算法的表示形式有:自然语言、传统流程图、伪代码、结构化的流程图(N_S 流程图,盒图)。

4. 有哪三种基本结构?答:三种基本结构是:顺序结构、选择结构和循环结构。

5. 传统流程图与N-S 流程图最大的区别是什么?答:N-S 流程图去掉了在传统流程图中常用的流程线,使得程序的结构显得更加清晰、简单。

三、用传统流程图、N-S 图分别表示求解以下问题的算法。

1. 有3个数a ,b ,c ,要求按由大到小的顺序把它们输出。

2. 依次将10个数输入,求出其中最大的数 和最小的数并输出。

3. 求1+2+3+…+100的值。

4. 求1×2×3×…×10的值。

5. 求下列分段函数的值。

6. 求100~200之间的所有素数。

7. 求一元二次方程ax 2+bx+c=0的根。

分别考虑d=b 2-4ac 大于0、等于0和小于0三种情况。

四、注释下面C 程序的各个组成部分。

main() /*主函数 */{ /*程序开始 */int a,k,m; /*定义三个用来存放整数的变量 */a=10; /*将整数10赋值给变量a */k=2; /*将整数2赋值给变量k */m=1; /*将整数1赋值给变量1 */a=(k+m)*k/(k-m); /*先求出算术表达式的值,并将其赋值给变量a */printf("%d\n",a); /*在屏幕上打印出变量a 的值 */} /*程序结束 */习题二 数据类型、运算符与表达式一、选择题1~10:BCDCB DDBCA11~20: ADDAA DBADC21~28: DABAD CDDY= 3X (X<1) 4X-1 (X=1) 5(X-1)+6 (1<X<5) 6-3X (X ≥5) 输入一个数给x X<=1 Yes no X<1 x<5 Yes no yes no Y=3x y=4x-1 y=5x+1 y=6-3x 输出s 的值 i =100 当i<=200时 n=2; flag=1; 当n< i 时i 能否被n 整除?yes no flag=0 n = n+1 flag=1?yes no输出i 的值i = i+1二、填空题1.字母L 或字母l2.字符或%c 、整数或%d3.在程序运行过程中,其值可以在一定的范围内变化的量。

c语言程序设计教材全部答案

c语言程序设计教材全部答案

A) float
B) char
C) int
D) double
设 C 语言中,一个 int 型数据在内存中占 2 个字节,则 unsigned int 型数据的取值范围
为C A) 0~255
。 B) 0~32767
C) 0~65535
D) 0~2147483647
若运行时给变量 x 输入 12,则以下程序的运行结果是

A) A%2==1
B) !(A%2==0)
C) !(A%2)
D) A%2
设有:int a=1,b=2,c=3,d=4,m=2,n=2;执行(m=a>b)&&(n=c>d)后 n 的值为
A) 1
B) 2
C) 3
D) 4
B。
以下程序的运行结果是 B
main()
{ int a,b,d=241;
a=d/100%9;
(2)已知底和高,计算三角形的面积。 #include “stdio.h” void main()
{ int bottom,high,area; /*bottom 表示底,high 表示高,area 表示面积*/ bottom=4; high=6; area=bottom*high/2; printf(“the area is %d\n”,area);
int m,i,s; for(m=2;m<=1000;m++)
{ s=0; for(i=1;i<=m/2;i++)
if(m%i==0) s+=i; if(m==s)
printf("%d\t",m); }
printf("\n"); }

Windows前6章答案

Windows前6章答案

Windows前6章答案习题1答案:一、1正确2错误(c语言)3正确4错误5错误(非功能键)6正确二、1c2a3a4a5c三、1wm_keydown,onkeydown2cmyview,cmydoc,cmyapp,cmainframe3wm_rbuttondown,onrbuttondown()四、1.windows提供更多了大量原订义的用c语言撰写的函数,这些函数就叫作“api 函数”。

2.为了描述事件的各种信息,windows定义了一个结构,这个结构就叫做“消息”。

3.消息与消息响应函数之间一一对应的关系称为“消息映射”。

五、1.onlbuttondown(uintnflags,cpointpoint)messagebox(\鼠标左键被按下\2.onrbuttondown(uintnflags,cpointpoint)messagebox(\鼠标右键被按下\六、1.通过类凡塘将按下鼠标右键消息wm_rbuttondown对应的消息响应函数onrbuttondown()添加到视图类中。

在onrbuttondown函数中添加messagebox函数,用来在窗口中弹出消息框。

messagebox函数第一个参数是编程者自己名字的字符串,第二个参数是学号的字符串,第三个参数是mb_okcancel。

2.参照基准1.2。

3.在视图类的ondraw函数中输入pdc->textout函数,函数中前两个参数是输出文字的位置的横、纵坐标,第三个参数是名字学号字符串。

4.在视图类中定义一个cstring类的变量text,在视图类的构造函数中初始化该变量为空串。

通过类凡塘将按下鼠标左键消息wm_lbuttondown对应的消息响应函数onlbuttondown()添加到视图类中。

在onlbuttondown函数中先将text赋值为编程者自己的名字和学号,再调用api函数invalidate(),自动调用ondraw()函数。

(完整版)C语言程序设计课后习题答案

(完整版)C语言程序设计课后习题答案
(1)变量c1、c2应定义为字符型或整形?或二者皆可?
(2)要求输出c1和c2值的ASCII码,应如何处理?用putchar函数还是printf函数?
(3)整形变量与字符变量是否在任何情况下都可以互相代替?如:charc1,c2;与intc1,c2;是否无条件地等价?
解 :#include<stdio.h> void main()
printf(“Verygood!\n”);printf(“\n”);
printf(“**************************”);
}
2.编写一个C程序,输入a、b、c三个值,输出其中最大值。解:
#include<stdio.h> void main()
{
int a,b,c,max;
printf(“请输入三个数a,b,c:\n”);
解 :#include<stdio.h> #include<math.h> void main()
{
double P, r=0.1, n=10;
P=pow((1+r), n);
printf(“%lf\n”, P);
}
3.请编程序将“China”译成密码,译码规律是用原来字母后面的第4个字母代替原来的字母。
printf(“c1=%d c2=%d\n”,c1,c2); printf(“c1=%c c2=%c\n”,c1,c2);
}
第四章
3.写出下面各逻辑表达式的值。设a=3,b=4,c=5。
(1)a+b>c&&b==c
(2)a||b+c&&b-c (3)!(a>b)&&!c||1

C语言程序设计课后习题答案Word版

C语言程序设计课后习题答案Word版

第1章(P18)一.单项选择题1.B2.B3.B4.C5.A6.C7.D8.C二. 填空题1. main2. 反斜杠3. 常量、变量、运算符、函数调用、表达式、保留字4. 一 , main , main5. 326. 由字母或下划线开头的字母、数字、下划线组成的一串符号第2章(P39)一.选择题1. D2.B3.B4.B5.D6.A7.B8.D9.A 10.B 11.C 12.C 13.B 14.B15.A 16.D二.填空题1. 控制代码、ASCII码字符集中的任意字符、特殊字符、换行符、Tab符号、左退一格符号、换页符号、响玲符号2. 其值可以发生变化的量 , 变量名 , 变量值 , 存储单元 , short int , -32768~32767 ,long int , -231~(231-1) , unsigned short , 0~65535 , unsigned long , 0~(232-1)3. 自增、自减 , 加和减 , +、-、×、/、% , 自增、自减4. 不同类型混合运算时,由编译系统自动完成5. (1)7 (2)6 (3)7 (4)1 (5)10 (6)0 (7)1 (8)0 (9)0 (10)0三. 运行程序题1. 27.0000002.13.700000第3章(P55)一.选择题1.A2.A3.C4.B5.C6.B二. 填空题1. 回车 , 输入的字符 , 从标准设备(键盘)读入一个字符2. 按用户指定的格式从键盘上把数据输入到指定的变量中 , 格式字符串开头标志 , 控制输入数据的格式 , 输入八进制整数 , 输入单个字符 , 输入字符串3. 一个字符 , #include<stdio.h>4. 按用户指定的格式 , 把指定的数据输出到屏幕上显示 , 格式字符串开头的标志 , 指定输出格式 , 以八进制形式输出无符号整数 , 以小数形式输出单、双精度数 , 输出单个字符 , 输出字符串 , 以%f、%e中较短的宽度输出单、双精度实数.5. (1) -200 , 2500 (2)i=-200 , j=2500 (3)i=-200j=25006. 12 , 0 , 07. 分号8. 100(回车) 100(空格)25.8(空格)1.89234 100(TAB)25.8(TAB)1.8923425.81(回车)1.892349. x=127 , x= 127 , x= 177 , x= 12710. a=513.789215 , a= 513.79 , a= 153.78921500 , a= 513.78921500三. 运行程序题1. a=27; b=15; c=32. p=73. 5 105,34.73125134.73, 34.7312A,65computer, computer4. 575, 767.856400,-789.12402367.86, -789.12,67.856400,-789.124023,67.856400,-789.1240236.78564e+01, -7.9e+02A,65,101,411234567,1234567,d68765529,177771,fff9,-7COMPUTER, COM第四章(P76)一.选择题1.D2.C3.D4.C5.B6.D7.B8.A9.B 10.A 11.C 12.D 13.A 14.A 15.D 16.A 17.A 18.B 二.填空题1.结构化控制、结构、程序的性能、运行2.执行循环体一次、再判断表达式的值、判断、执行3.switch语句、循环语句、循环体三.运行程序题1.(1)0 (2)1 (3)1 (4)0 (5)12.#$#$#$&3.3667784. *************************四.完善程序题1.fabs(t)、t、-s2.=’*’、’\n’、YES第五章(P100)一.选择题1.B2.A3.B4.C5.C6.A7.B8.A9.C二.填空题1.20、0、192.数组名3.越界三.运行程序题1.3572.*************************3.18104.输出一个3×3矩阵的转置矩阵四.完善程序题1.k、-12.&a[i][j]、a[i][i]+a[i][2-i]3.ndigit[10]、ndigit[ch-‘0’]++第六章(P130)一.选择题1.D2.D3.B4.A5.D6.A7.A8.B9.B 10.A 11.B 12.D 13.A 14.A 二.填空题1.函数内部2.函数内部、局部3.return、void4.fun()、extern三.运行程序题1.122.93.10214.215.a=5,b=3a*b=15a+b=86.11101121011310114101151017.238.2226四.修改程序题1.在主函数前加上:float mul(float a,float b);把float mul(float a,b)改为float mul(float a,float b) 2.把z=fabs(x-y);改为z=abs(x-y);3.把输入的整数转换为字符串逆序输出第七章(P160)一、选择题A D D C DB D B A C二、填空题1、取内容、取地址2、3、+33、xyz、x4、1002、1004、1008、1001、10065、286、4、a[2][0]7、6、a[3]三、运行程序题1、porm2、ga3、88884、307四、完善程序题1、++i、i2、0、z第八章(P194)一、选择题D BCBCBDBC二、运行程序题1、36、40、412、133、zhao4、1,1二、填空题1、成员、用指针访问成员2、343、12、6.0000004、34、125、2、36、struct node *next7、num!=p1->info && p1->link=NULL、head=p1->link、p2->link=p1->link第九章(P209)一、选择题1-8 CC A DCDC ( 3错. 32->64)二、填空题1、72、PR(x); PR(y);三、运行程序题1、932、2 123、a=14,b=15,c=04、Hid Ted5、R=2.5 circ=15.707963 area=19.634954第10章(P219)一、B A A A DB AC A C二、1. ~ << >> & ^ |2. 0x22 0xbbbb 0x bb99 001101100 最高补0 最高补101011011 11011011三、1. 12342.15765375765四、 1. 12 <<2. 15 -1第11章一、 B C D B BC C B D二、 1. “w”“rb”“ab+”“rt”“wt+”2. 缓冲文件系统非缓冲文件系统三、完善程序题1、rewind(fp);fp2、"a+";"r";fp1四、编写程序题1、从键盘输入一个字符串(以“#”作为结束符),把文本输出到磁盘文件e.dat中。

程序设计基础教程(c语言版)课后答案

程序设计基础教程(c语言版)课后答案

程序设计基础教程(c语言版)课后答案课后答案及解析z习题解答课后答案及解析目录1.2 习题解答 (3)1.2.1 选择题 (3)1.2.2 填空题 (3)1.2.3 编程题 (4)2.2 习题解答 (5)2.2.1 选择题 (5)2.2.2 填空题 (7)2.2.3 编程题 (8)3.2 习题解答 (11)3.2.1 选择题 (11)3.2.2 填空题 (12)3.2.3 编程题 (12)4.2 习题解答 (15)4.2.1 选择题 (15)4.2.2 填空题 (17)4.2.3 编程题 (18)5.2 习题解答 (29)5.2.1 选择题 (29)5.2.2 填空题 (31)5.2.3 编程题 (33)6.2 习题解答 (37)6.2.1 选择题 (37)6.2.2 填空题 (41)6.2.3 编程题 (43)7.2 习题解答 (67)7.2.1 选择题 (67)7.2.2 填空题 (68)7.2.3 编程题..................................................................................................68 2课后答案及解析1.2 习题解答1.2.1 选择题1、B在一个C程序中必须有且只能有一个main函数,而且main 函数可以在任何地方出现.2、CC 语言中合法以的标识符组成为:字母,数字,下划线,且数字不能打头,亦不能为关键字。

A中,-sub 不合法。

B 中4d 不合法。

D 中void 为关键字。

3、4、C转义字符中,第一个\”输出“,第二\\输出\,第三个\b退格,把前一个\去掉了,第四个\ 输出,第五个\t跳格,第六个\”输出”,第七个\n输出换行。

5、C本题将10进制17,分别按8、16进制输出。

8的进制21=2*8+1*1=17(10, 16制11=1*16+1*1=17(10)1.2.2 填空题1、主一个C源程序中至少包括一个主函数,其他函数没有限制。

c语言《程序设计基础》课后习题参考答案与解析

c语言《程序设计基础》课后习题参考答案与解析

《程序设计基础》习题参考答案与部分解析第1章 C 语言概述一、填空a) C源程序的基本单位是函数。

b) 一个C程序中至少应包括一个main函数。

c) 在C语言中,输出操作是有库函数printf( )函数完成。

二、单选题1、A2、C3、B解析:第1题答案:A 。

因为一个C程序总是从main函数开始执行的,而不论main函数在程序中的位置。

且到main函数结束。

第2题答案:C 。

因为main函数没有限制必须位于程序的最前面。

C程序书写自由,一行内可写几个语句。

在对一个C 程序进行编译的过程中,无法检查注释当中的拼写错误。

不过C语言本身并没有输入输出语句,输入输出是由函数完成的。

第3题答案:B。

因为一个C语言程序是由若干个函数组成的。

但至少包含一个main 函数,且main函数的位置不限。

三、编程题1、编写一个输出“Welcome to C!”信息的小程序。

解:程序如下#include “stdio.h”main( ){printf(“Welcome to C!”) ;}2、已知三角形的三边长分别为3,4,5,试用海轮公式编程求其面积。

海伦公式为:S△= ,其中s= (a+b+s)/2 ;解:程序如下#include “math.h”#include “stdio.h”main( ){int a , b , c ; /* a ,b,c 3个整型变量表示三角形的3条边。

*/float s ,s1 ; /* s1作为面积变量,s 作为中间变量是都应该是实形*/a=3 ; b= 4; c=5 ;s= (a+b+c)/2.0 ;s1= sqrt(s*(s-a)*(s-b)*(s-c)); /* sqrt函数完成开平方根功能。

*/printf(“area=%f\n”,s1);}第2章程序设计基础知识一、单选题1、C2、A3、C4、A5、C6、C7、D8、C9、D 10、A 11、D 12、A 13、C 14、C 15、B A16、B 17 D解析:1. 答案:C。

c程序设计教程课后习题答案

c程序设计教程课后习题答案

c程序设计教程课后习题答案在编写C语言程序时,理解并完成课后习题是掌握编程知识的重要步骤。

以下是一些典型的C语言程序设计教程课后习题答案的示例,这些答案涵盖了基础语法、数据结构、函数、指针等概念。

习题1:变量声明和初始化编写一个C程序,声明一个整型变量`x`和一个浮点型变量`y`,并将它们分别初始化为10和3.14。

```c#include <stdio.h>int main() {int x = 10;double y = 3.14;printf("x = %d\n", x);printf("y = %f\n", y);return 0;}```习题2:条件语句编写一个程序,判断一个整数变量`a`的值,如果`a`大于10则输出"Greater than 10",如果小于10则输出"Less than 10",如果等于10则输出"Equal to 10"。

```c#include <stdio.h>int main() {int a;printf("Enter an integer: ");scanf("%d", &a);if (a > 10) {printf("Greater than 10\n");} else if (a < 10) {printf("Less than 10\n");} else {printf("Equal to 10\n");}return 0;}```习题3:循环编写一个程序,使用`for`循环打印从1到10的整数。

```c#include <stdio.h>int main() {for (int i = 1; i <= 10; ++i) {printf("%d ", i);}printf("\n");return 0;}```习题4:数组编写一个程序,声明一个整数数组`arr`,包含5个元素,初始化为1, 2, 3, 4, 5,并打印数组中的所有元素。

C语言课后题答案

C语言课后题答案

C语言c语言程序设计教程(第2版)课后题及模拟题参考答案习题1 31-1 填空题 31-2 思考题 31-3 编程题 3习题2 42-1 单选题 42-2 思考题 4习题3 53-1 选择题 53-2 填空题 53-3 编程题 5习题4 74-1单选题74-2填空题74-3 编程题7习题5 105-1单选题105-2填空题105-3 编程题10习题6 136-1单选题136-2填空题136-3 编程题13习题7 157-1单选题157-2填空题157-3 编程题15习题8 168-1单选题168-2填空题168-3 编程题16习题9 189-1单选题189-2填空题189-3 编程题18习题10 2210-1单选题2210-2填空题2210-3 编程题22习题11 2411-1单选题2411-2填空题24习题12 2512-1单选题2512-2 填空题25实验篇26实验1 熟悉Visual C++6.0可视化集成开发环境26实验2 顺序结构程序设计26实验3 选择结构程序设计26实验4 循环结构程序设计26实验5 函数28实验6 数组32实验7 指针33实验8 结构体和共用体35实验9 文件36实验10 综合编程36模拟试卷(一)参考答案37模拟试卷(二)参考答案38习题11-1 填空题1. 函数2. 主函数main(),主函数main()3. 主函数main()4. 函数首部,函数体5. {, }6. /*, */7. 顺序结构,选择结构,循环结构8. .c, .obj, .exe1-2 思考题1. 结构化程序设计是指:为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。

其基本结构包括顺序结构、选择结构和循环结构三种。

2. 算法是对具体问题求解步骤的一种描述。

计算机算法的表达工具通常采用以下几种方法:(1)用自然语言表示算(2)用流程图表示算法(3)用伪代码表示算法(4)用程序设计语言表示算法3. 语言简洁、紧凑,使用方便、灵活; 支持结构化程序设计;运算符丰富;数据类型丰富;较强的编译预处理功能;C语言的可移植性好;C语言本身既有一般高级语言的优点,又有低级(汇编)语言的特点;语法限制不太严格,程序设计自由度大。

《C语言程序设计》课后习题参考答案

《C语言程序设计》课后习题参考答案
}
运行结果:13
4.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第12次落地时,第12次反弹多高?按四舍五入的方法精确到小数点后面四位。
#include "stdio.h"
main()
{
float hn,sn=100.0;
int i;
hn=sn/2;
for(i=2; i<=12; i++)
#include <math.h>
main()
{
int sum=0, i, j, yes;
for(i=2; i<=500; i++)
{
yes=1;
for(j=2; j<=sqrt(i); j++)
if(i%j==0)
{
yes=0;
break;
}
if(yes) sum+=i;
}
printf("%d\n", sum);
源代码
#include <stdio.h>
void main()
{
int a[5]={3,-5,8,2,9};
int sum=0;
float average = 0;
int max = a[0];
int min = a[0];
int i;
for(i=0; i<5;i++)
sum += a[i];
average = (float)sum/5;
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
void main( )

大学C语言课本课后习题相应答案及详细解答

大学C语言课本课后习题相应答案及详细解答

5-1#include"stdio.h" void main(){char c1,c2;printf("c1:");c1=getchar();if(c1>='a'&&c1<='z') c2=c1-32;else if(c1>='A'&&c1<='Z') c2=c1+32;else c2=c1;printf("=>%c\n",c2);}5-2#include"stdio.h"void main(){char c;printf("c:");c=getchar();if(c>='A'&&c<='Z')if(c=='A') printf("没有前面的字母!");else if(c=='Z') printf("没有后面的字母!!");else printf("前面的字母=%c,后面的字母=%c",c-1,c+1);printf("\n");} 5-3#include"stdio.h"void main(){int s;char g;printf("s:");scanf("%d",&s);if(s>=0&&s<=100){switch(s/10){case 9:case 10: g='A';break;case 8: g='B';break;case 7: g='C';break;case 6: g='D';break;default: g='E';}printf("g=%c\n",g);}else printf("成绩不在百分制范围内!\n"); }5-4#include"stdio.h"void main(){int y,m,d,dok;int yy,mm,dd;printf("y,m,d:");scanf("%d,%d,%d",&y,&m,&d);switch(m){case 1: case 3: case 5: case 7: case 8: case 10: case 12:if(d>0&&d<=31) dok=4;else dok=0;break;case 4: case 6: case 9: case 11:if(d>0&&d<=30) dok=3;else dok=0;break;case 2: if(y%4==0&&y%100!=0||y%400==0)if(d>0&&d<=29)dok=2;else dok=0;elseif(d>0&&d<=28)dok=1;else dok=0;break;default: dok=0;}if(dok==0) printf("月份或日期不对!\n");else{switch(dok){case 1: if(d==28){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case2:if(d==29){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case3:if(d==30){yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;case 4:if(d==31)if(m==12){yy=y+1;dd=1;mm=1;}else{yy=y;dd=1;mm=m+1;}else{yy=y;dd=d+1;mm=m;}break;}printf("Tomorrow:%d年%d月%d日\n",yy,mm,dd);}}5-5#include"stdio.h"void main(){int a,b,c,t;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);if(a+b>c&&b+c>a&&a+c>b){if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}if(a==b&&b==c) printf("等边三角形.");else if(a==b||b==c||a==c) printf("等腰角形.");else if(c*c==a*a+b*b) printf("直角三角形.");else printf("任意三角形.");printf("\n");}else printf("不能构成三角形!\n");}6-1#include"stdio.h"void main(){int a,b,m,n,k;printf("m,n:");scanf("%d,%d",&m,&n);a=m;b=n;k=a%b;while(k){a=b;b=k;k=a%b;}printf("%d\n",(m*n/b));}6-2#include"stdio.h"void main(){int n;int i,j,s;printf("n=");scanf("%d",&n);for(i=1;i<=n;i++){s=0;for(j=1;j<i;j++)if(i%j==0) s=s+j;if(s==i) {printf("%d:",i);for(j=1;j<i;j++)if(i%j==0) printf("%d ",j);printf("\n");}}} 6-3#include"stdio.h"#include"math.h"void main(){double x,y,zd,zx,x1,x2;zd=zx=50;for(x=0;x<=2;x=x+0.2){y=2*pow(x,3)-3*pow(x,4)+6*pow(x,5)-4*x+50;if(y>zd){ zd=y;x1=x;}if(y<zx){ zx=y;x2=x;}}printf("x=%f,max=%f\n",x1,zd);printf("x=%f,min=%f\n",x2,zx);}6-4#include"stdio.h"void main(){double x,sum=0,a,b,c;int i;printf("x=");scanf("%lf",&x);a=x;b=1.0;c=1.0;for(i=1;i<=10;i++){sum=sum+a/(b*c);a=a*x*x;b=b+2;c=c*i;}printf("y(%.2lf)=%lf\n",x,sum);}7-1/*选择排序*/#include"stdio.h"void main(){int i,j,n,k,temp;int a[10];printf("n(<10):");scanf("%d",&n);printf("Original:");for(i=0;i<n;i++) scanf("%d",&a[i]);for(i=0;i<n-1;i++){ /*趟*/k=i;for(j=i+1;j<n;j++) /*比较次数*/if(a[j]<a[k]) k=j;/*<升序,>?*/if(k!=i){temp=a[i];a[i]=a[k];a[k]=temp;}}printf("Ordered:");for(i=0;i<n;i++) printf("%d ",a[i]);printf("\n");}7-2#include"stdio.h"void main(){int a[3][3];int i,j,s=1;printf("Input:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%d",&a[i][j]);if(i==j) s=s*a[i][j];}printf("s=%d\n",s);} 7-3/*杨辉三角*/#include"stdio.h"void main(){int x[7][7];int i,j;for(i=0;i<7;i++) {x[i][0]=1;x[i][i]=1;}for(i=2;i<7;i++)for(j=1;j<i;j++)x[i][j]=x[i-1][j]+x[i-1][j-1];for(i=0;i<7;i++){for(j=0;j<=i;j++)printf("%3d",x[i][j]);printf("\n");}}7-4#include<stdio.h>#include<string.h>void main(){char str[21];int i,j;printf("str:");gets(str);for(i=0,j=strlen(str)-1;i<=j;i++,j--)if(str[i]!=str[j]) break;if(i>j) printf("%s是回文\n",str);else printf("%s不是回文\n",str);}7-5/*输入一维数组的10个元素,并将最小值与第1个数交换,最大值与最后一个数交换,然后输出交换后的结果*/#include<stdio.h>void main(){int a[10],i,zx,zd;printf("Input:\n");zx=zd=0;for(i=0;i<10;i++){scanf("%d",&a[i]);if(a[i]<a[zx]) zx=i;if(a[i]>a[zd]) zd=i;}if(zx!=0){int t;t=a[0];a[0]=a[zx];a[zx]=t;}if(zd!=9){int t;t=a[9];a[9]=a[zd];a[zd]=t;}for(i=0;i<10;i++)printf("%d ",a[i]);printf("\n");} 8-2#include"stdio.h"double xexp(double x,int n){double c;if(n==0) c=1.0;else c=x*xexp(x,n-1);return c;}void main(){int n;double x;printf("x:");scanf("%lf",&x);printf("n:");scanf("%d",&n);printf("Result=%g\n",xexp(x,n));}8-3#include"stdio.h"int find(int x[],int n,int y){int i,pos=-1;for(i=0;i<n;i++)if(x[i]==y) pos=i;return pos;}void main(){int a[10]={11,1,13,15,18,7,19,27,3,8};int i,y,pos;for(i=0;i<10;i++) printf("%d ",a[i]);printf("\ny:");scanf("%d",&y);pos=find(a,10,y);if(pos==-1) printf("Not found!\n");else printf("Position=%d\n",pos);}8-1#include"stdio.h"#include"conio.h" //getch()#include"stdlib.h" //srand(),rand(),system("cls") #include"time.h" //time()void main(){void init(int a[][5],int m,int n);void input(int a[][5],int m,int n);void output(int a[][5],int m,int n);int min(int b[][5],int m,int n);int a[5][5],ch='0';while(1){system("cls"); //清屏printf("1.初始化 2.键盘输入0.结束程序:");printf("\n");if(ch=='0') break;else if(ch=='1'){init(a,5,5);output(a,5,5);}else if(ch=='2'){input(a,5,5);output(a,5,5);}else printf("Error!\n");printf("Min element:%d\n",min(a,5,5));printf("按任意键继续!\n");getch();}}void init(int a[][5],int m,int n){int i,j;srand(time(0)); //time(0)表示以当前的时间做种子,增加每次运行的随机性for(i=0;i<5;i++)for(j=0;j<5;j++)a[i][j]=rand()%100; //随机数范围:0~32767,将它控制在0~99的范围}void input(int a[][5],int m,int n){int i,j;printf("Input Array 5X5:\n");for(i=0;i<m;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);}void output(int a[][5],int m,int n){int i,j;printf("Output Array 5X5:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%2d ",a[i][j]);printf("\n");}}int min(int b[][5],int m,int n){int i,j,zx;zx=b[0][0];for(i=0;i<m;i++)for(j=0;j<n;j++)if(i==j&&b[i][j]<zx) zx=b[i][j];return zx;}8-4#include"stdio.h"float pave,nave;void saver(float a[],int n){int i,z,f;float psum,nsum;psum=nsum=0.0;z=f=0;for(i=0;i<n;i++)if(a[i]<0) {nsum=nsum+a[i];z++;}else if(a[i]>0) {psum=psum+a[i];f++;}else continue;pave=(z!=0?psum/z:0.0);nave=(f!=0?nsum/f:0.0);}void main(){float a[10]={1.0,11.0,3.0,-1.5,-5.5,-2};saver(a,10);printf("pave=%.1f,nave=%.2f\n",pave,nave); } 8-5#include"stdio.h"#include"math.h"void p1(int a,int b){printf("has two equal roots:%\n",-b/(2*a));}void p2(int a,int b, int disc){float x1,x2;x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("Has distant real roots:% and %\n",x1,x2);}void p3(int a,int b, int disc){float real,image;real=-b/(2*a);image=sqrt(-disc)/(2*a);printf("Has complex roots:\n");printf("%+%8.4fi\n",real,image);printf("%-%8.4fi\n",real,image);}void main(){ int a,b,c,disc;printf("a,b,c:");scanf("%d,%d,%d",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-6) p1(a,b);else if(disc>1e-6) p2(a,b,disc);else p3(a,b,disc);}8-6#include"stdio.h"#include"stdlib.h"#include"conio.h"#include"time.h"void main(){void printaverage(int score[][5],int m,int n);void printname(int score[][5],int m,int n);int score[10][5];int i,j;srand(time(0));for(i=0;i<10;i++)for(j=0;j<5;j++)score[i][j]=50+rand()%50+1;printf("Output Students' score:\n");printf("Course 1 2 3 4 5\n");printf("-------------------------\n");for(i=0;i<10;i++){printf("No.%2d:",i+1);for(j=0;j<5;j++)printf("%3d ",score[i][j]);printf("\n");}printaverage(score,10,5);printname(score,10,5);} void printaverage(int score[][5],int m,int n){int i,j,sum;printf("\nAverage score:\n");for(i=0;i<m;i++){sum=0;for(j=0;j<n;j++)sum=sum+score[i][j];printf("No.%d:%.1f\n",i+1,sum/5.0);}}void printname(int score[][5],int m,int n){int i,j,max0,row0;for(j=0;j<5;j++){max0=score[0][j];for(i=0;i<10;i++)if(score[i][j]>max0){max0=score[i][j];row0=i;}printf("Course %d,max score=%d,student:No.%d\n",j+1,max0,row0+1);}}大学英语I词汇练习题Choose the best one to complete each sentence.1. She felt like ____C___ frustration, but she was determined not to lose her self-control.A. to cry out ofB. to cry forC. crying out ofD. crying for2. The method he used turned out to be ___A____ in improving the students' English.A. effectiveB. abilityC. responseD. explicit3. Measures had to be taken in face of the housing problem that ____C____ in the city.A. foundedB. raisedC. aroseD. produced4. Without electronic computers, much of today's advanced technology ___D____.A. haven't been achievedB. wouldn't be achievedC. hadn't been achievedD. wouldn't have been achieved5. Jane said to her husband, "Don't worry. There is no cause for __B______ about our daughter'sability to manage herself."A. careB. concernC. attentionD. love6. The tap won't ____A____, and there is water all over the floor.A. turn offB. turn downC. turn onD. turn up7. Wearing the right shoes and clothes ____B____ being fit can make all the difference.A. in additionB. as well asC. alsoD. too8. She didn't try to do anything for her daughter, and ____B____ it's too late now.A. in caseB. in any caseC. at this caseD. in case of9. The girl said she hated _____C___ he smiled at her.A. that wayB. this wayC. the wayD. all the way10. I'll call the hotel. I'll tell them we'll ____A____ tomorrow morning and stay there for two nights.A. check inB. check outC. check offD. check over11. My father didn't go to New York; the doctor suggested that he ___C____ there.A. not to goB. won'tC. not goD. not to go to12. She _____C____ him to find answers to her problems.A. learned fromB. came intoC. leaned onD. subjected to13. The large wings of that bird ______B_____ it to fly high and fast.A. makeB. enableC. forceD. realize14. If he ____D___ the policeman honestly, he would not have been arrested.A. would answerB. answerC. should answerD. had answered15. I intended ___A____ you last Sunday, but I had no time.A. to have called onB. calling onC. to be calling onD. to be called on16. If you try to learn too many things at a time, you may get ____D___.A. alarmedB. scaredC. surprisedD. confused17. I haven't read this book, and my brother hasn't ____A____.A. eitherB. neitherC. alsoD. too18. No sooner ____B_____ than the truck started off.A. his luggage was loadedB. had his luggage been loadedC. loaded his luggageD. his luggage was being loaded19. At the end of the game, the whole crowd ____C____ their feet and cheered wildly.A. emerged fromB. rose fromC. got toD. stood up20. Darren has decided to give ___D_____ football at the end of this season.A. inB. forC. offD. up21. Neither his friends nor his mother ____B____ his marriage to that girl.A. acceptB. acceptsC. agreeD. agrees22. Most people believe that he is quite _____A___ of lying to get out of trouble.A. capableB. enabledC. ableD. skilled23. We were told that most of our luggage would be _____B___ by sea.A. approachedB. transportedC. handledD. communicated24. The Broadcasting Museum also offers Saturday workshops to _____B___ children with the world of radio.A. contriveB. acquaintC. acquireD. admit25. Without a passport, leaving the country is ____D___.A. out of questionB. without questionC. in the questionD. out of the question26. She states her views very ___A______.A. definitelyB. definiteC. infiniteD. infinitely27. You need to ____D____ the employers that you can do the job.A. convictB. confuseC. confirmD. convince28.The little boy was so lucky to have ___B_____ the earthquake.A. surviveB. survived C .surviving D. survival29. It is ___B___ that later in life you will see each other again.A. likeB. likelyC. likedD. seemed30.Women’s social ____B____ has changed much over the years.A. stateB. statusC. statesD. statement31.The novel _____D____ the imagination of thousands of readers.A. caterB. cateredC. captureD. captured32.There are good chances of ______D______ working in this firm.A. prospectB. portionC. positionD. promotion33.If you are ______A_____ a night owl, having the first class start in the afternoon might be thebest thing that could ever happen to you.A. more ofB. more thanC. much moreD. more34. The conference discussed the fair ______A_____of income and wealth.A. distributionB. distributeC. contributionD. contribute35. I was so ___C__ in today's history lesson that I didn't understand anything.A. amazedB. confusingC. confusedD. amazing36. ___B___ in England, Anne Bradstreet both admired and imitated several English poets.A. Having born and educatedB. Born and educatedC. Since born and educatedD. To be born and educated37. You will see this product ___B___ wherever you go.A. to be advertisedB. advertisedC. advertiseD. advertising38. I never forget my __B__ with you for the first time.A. to meetB. meetingC. to have metD. having to be meeting39.She was so angry that she left like __B__ something at him.A. to throwB. throwingC. to have thrownD. having thrown40. We are ___C__ in different kinds of extracurricular activities on campus.A. having involvedB. to involveC. involvedD. involving41.Let me assure you that it was not done ______D_____.A. intensiveB. intensivelyC. intentionalD. intentionally42.This medicine can ____A____ the pain in no time.A. lessenB. lessC. looseD. loosen43.Advertising is often the most ____A____ method of product promotion.A. effectiveB. effectedC. emotionalD. efficient44.It is at 5 a.m. _C___ the train from Beijing to Guangzhou will arrive.A. forB. withC. thatD. when45.Things might have been much worse if the mother _B___ on her right to keep the baby.A. has been insistingB. had insistedC. would insistD. insisted46.Had he worked harder, he __B__ the exams.A. must have got throughB. would have got throughC. permitted are freshmenD. are permitted freshmen47.God knows, how can I ____A____ six exams a week?.A. endureB. tolerateC. bear D .suffer48.All their attempts ______C______ the child from the burning building were in vain.A. rescuedB. to have rescueC. to rescueD. in rescuing49,Could you find someone ______C_______ for me to play tennis with?A. to fitB. fitC. fittingD. suit50. The smooth space flight marks a big ______D______ for Chinese scientists.A. breakB. breakingC. breakupD. breakthrough51 Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduringpanies will have to do more than this if they are to ___B_____ the earthquake.A. survivedB. surviveC. survivesD. surviving53. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore54. There are good chances of ______D______ in this firm.A. moveB. motiveC. motionD. promotion55. She tried to ______D______ our friendship.A. underminedB. keptC. maintainedD. undermine56. The new airport will ______C___ tourism.A. helpedB. useC. facilitateD. benefited57. They were full of ____B____ when they saw my new car.A. jealousB. envyC. pitifulD. loves58. This medicine can ____A____ the pain in no time.A. lessenB. lessC. leastD. little59.He _______A_____ her on her last physics paper.A. complimentedB. complimentC. completeD. completed60. Mrs. Hill is _______B____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep61. Let me assure you that it was not done ______C_____.A. internationalB. purposeC. intentionallyD. intentional62.The novel _____D_____ the imagination of thousands of readers.A. seizeB. catchC. captureD. captured63. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under64. He came over and ____B____ a piece of paper into my hand.A. shoveB. shovedC. shovesD. shoving65. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied66.The doctor tried to help ____D____ the function of his limbs.A. storeB. storesC. restoresD. restore67. Women’s social ____B____ has changed much over the years.A. placeB. statusC. landD. work68. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whose69. A dropped cigarette is being ____C____ for the fire.A. firedB. fireC. blamedD. blame70. Advertising is often the most _____D___ method of promotion.A. effectB. affectC. affectedD. effective71.In time you’ll _____A_____ the beauty of this language.A. appreciateB. appreciatesC. appreciatedD. appreciating72. ______B___ is his favorite toy is hard for the kid to decide.A. whoB. whichC. howD. where73. After dinner, I took a ____A____ around the park.A. strollB. divingC. runD. walking74. The boy really enjoyed the useful and ____B____ work in science.A. creationB. creativeC. createdD. creator75. Some professions are _____A_______ with people who have not grown up.A. stuffedB. stuffC. stiffD. stiffed76. Economic crises destroy the capitalist system, and they grow in size and ____A____.A. durationB. duringC. endureD. enduring77. The new airport will ____C_____ tourism.A. helpedB. useC. facilitateD. benifited78. Let me assure you that it was not done _____C______.A. internationalB. purposeC. intentionallyD. intentional79. The girl student whose father is a senior engineer used to ___A____ abroad.A. studyB. studyingC. studiesD. studied80. Advertising is often the most ____D____ method of promotion.A. effectB. affectC. affectedD. effective81. It was he ___A___ knew he didn’t need to be afraid to make mistakes at that moment.A. whoB. whomC. whichD. whosepanies will have to do more than this if they are to _____B___ the earthquake.A. survivedB. surviveC. survivesD. surviving83. The baby felt ____C____ by her parents.A. hateB. loveC. ignoredD. ignore84. If you yell ___A____ someone, you should apologize.A. atB. outC. ofD. under85. Mrs. Hill is ______B_____ on Tom’s marrying Susan.A. awareB. keenC. kindD. keep86. _____A____ college takes hard work!A. SurvivingB. SurviveC. SurvivesD. To surviving87. It is absolutely ____A___ for the human beings to do something to save the earth.A. necessaryB. necessarilyC. unnecessaryD. unnecessarily88. An employer is more ____D____ to choose the candidate with high grades.A. likeB. probablyC. possiblyD. likely89. I don’t like this young woman. She seems a social ___D____.A. flowerB. workerC. negotiatorD. butterfly90. How should this ___B____ girl see the coldness of the letter?A. blindB. innocentC. warmD. pretty91. Be careful not to ___B____ yourself in their local conflicts.A. attendB. involveC. take part inD. participate92. Home prices are holding ____C___ in most regions, and people live with steady income.A. flexibleB. fierceC. stableD. shortage93. He was ______C______ by a newspaper reporter at that time.A. being interviewB. interviewC. being interviewedD. to interview94. They were full of ____C____ when they saw my new car.A. enviousB. envilyC. envyD. envies95. ________C_________, it is a very stable pattern producing results through effort .A. ContraryB. ConverseC. On the contraryD. Diverse96. The teacher doesn’t permit _____B____ in classA. smokeB. smokingC. to smokeD. to have a smoke97. The man in the corner confessed to ___A____ a lie to the manager of the company.A. have toldB. be toldC. being toldD. having told98. I never forget __B____ you for the first time.A. to meetB. meetingC. to have metD. having to be meeting.99. _______A___ a man has, ___________ he wants.A. The more, the moreB. More, moreC. The more, moreD. More, the more100. I see no _____B____ of his success.A. possibleB. possibilityC. possibilitiesD. probable101. He could be ____B____ about everything else in the world, but not about Manet, his loving child.A. visualB. criticalC. favoriteD. essential102. Some see themselves as the provider of ideas, ____C____ others view their role as essentially managerial.A. whenB. thereforeC. whileD. otherwise103. The idea ____A____ to him so much that he took it without hesitation.A. appealedB. interestedC. drewD. attracted104. The teacher evaluated the performance of each of his students who ____B____ evaluated his performance.A. by turnB. in turnC. at turnD. on turn105. Disease _____A____ during the journey and many passengers had to be rushed to hospital for treatment.A. broke outB. broke downC. started offD. started out106. The products have been _____A_____ to strict tests before leaving the factory.A. subjectedB. adjustedC. objectedD. constricted107. Our television set is out of order. Could you come and _____C_____ it for us?A. see throughB. see outC. see toD. see over108. _____C_____ they have taken matters into their hands, the pace of events has quickened.A. As thatB. So thatC. Now thatD. For that109. Our neighbor said if we made more noise he would _____B____ us to the police.A. inform ofB. complain aboutC. report toD. care for110. People working in the government should not _____A______ business affairs that might affect their political decisions.A. engage inB. hope forC. choose betweenD. pick on111. It's not easy to ______B______ to the top in show business.A. make outB. make itC. make upD. make them112. Carter is in charge of the office while I'm ______C______.A. leavingB. on leavingC. on leaveD. on relief113. We must ____B____ our attention on the question of reducing our cost.A. payB. focusC. absorbD. promote 114. Snap judgments, if ___B_____, have usually been considered signs of immaturity or lack of common sense.A. taking seriouslyB. taken seriouslyC. take seriouslyD. to be taken seriously115. Being with his family for a few days, I gained one or two insights ____A____ the reason he behaves the way he does.A. intoB. ofC. onD. off116. ___D___ your age or knowledge of the language, you'll be 100% involved in your studies from the first lesson to the last.A. As a result ofB. In spiteC. Despite ofD. Regardless of117. He said he had caught a bad cold and told me to ____D____ him.A. stay atB. stay upC. stay inD. stay away from118. Good managing of a company ___A_____ great efforts.A. calls forB. calls outC. calls inD. calls at119. I've looked ___C_____ all my papers but I still can't find the contract.A. uponB. outC. throughD. in120. When I'm going out in the evening I use the bike if I can, ___B_____ the car.A. regardless ofB. more thanC. other thanD. rather than121. We can't speak ___A____ our teacher.A. too highly ofB. too high ofC. highly of tooD. to highly of122. No one in our class ____D___ in sports than he.A. are more interestedB. are much interestedC. is much interestedD. is more interested123. As a teacher you have to ____B____ your methods to suit the needs of slower children.A. enlargeB. adjustC. affectD. afford124. ________A___ the hotel before soldiers armed with pistols and clubs charged into the crowd.A. Scarcely had they leftB. Scarcely they had leftC. Scarcely they leftD. Scarcely left they125. In the film, the peaceful life of a monk _____D_____ the violent life of a murderer.A. is compared withB. is compared toC. is contrasted toD. is contrasted with126 I didn't realize the food problem was so _____B___ in this city; with winter coming, many people would starve to death without more help.A. essentialB. criticalC. explicitD. effective127. Female socialization places importance on getting along with others, _____B___ male socialization places importance on becoming independent.。

C语言程序设计课后习题参考答案

C语言程序设计课后习题参考答案

高等院校计算机基础教育规划教材《C++程序设计》课后习题参考答案――大学习题1参考答案一、选择题1. A2. D二、填空题1. BASIC、FORTRAN、AL_GOL60和COBOL2. 83. 关键字4. 编辑、编译、和运行三、简答题1.答:(1)C语言具有结构化的控制语句。

C语言提供了结构化程序所必需的基本控制语句,实现了对逻辑流的有效控制。

(2)C语言具有丰富的数据结构类型。

C语言除提供整型、实型、字符型等基本数据类型外,还提供了用基本数据类型构造出的各种复杂的数据结构,如数组、结构、联合等。

C语言还提供了与地址密切相关的指针类型。

此外,用户还可以根据需要自定义数据类型。

(3)C语言具有丰富的运算符。

C语言提供了多达34种运算符,丰富的数据类型与丰富的运算符相结合,使C语言的表达力更具灵活性,同时也提高了执行效率。

(4)C语言简洁、紧凑,使用方便、灵活,程序书写自由,有9种控制语句。

(5)C语言既具有高级语言的功能,又具有低级语言的许多功能,通常被称为中级计算机语言。

它既是成功的系统描述语言,又是通用的程序设计语言。

(6)C语言与汇编语言相比,可移植性好。

(7)功能强大。

C语言具有低级语言的一些功能,所以,生成目标代码质量高,程序执行效率高。

现在许多系统软件都用C语言来描述,可以大大提高了编程效率。

2.答:运行一个C语言程序,一般需要经过如下几个步骤:①上机输入并编辑源程序;②编译源程序;③与库函数连接;④生成可执行目标程序;⑤运行目标程序。

3.答:(1)操作系统的设计与实现。

C语言是一种应用非常广泛的结构化高级程序设计语言,既适合编写应用软件,又适合编写系统软件。

(2)工业控制。

由于C语言具有简洁、灵活、代码效率高、能进行位操作等优点,C语言大量应用在单板机、单片机上,以及嵌入式领域等。

(3)图形图像处理。

C语言在存管理和进程控制方面有丰富的指令,而且它能提供快速运行的代码,因而C语言适合进行图形程序设计。

(word完整版)C语言程序设计课后习题1-8参考答案

(word完整版)C语言程序设计课后习题1-8参考答案

C语言程序设计课后习题1—8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分?各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。

运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。

数据运算包括算术运算和逻辑运算。

控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的.存储器是计算机中具有记忆能力的部件,用来存放程序和数据.输入设备是用来输入程序和数据的部件。

输出设备正好与输入设备相反,是用来输出结果的部件。

2、简述计算机的工作原理。

答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。

整个过程由控制器进行控制。

3、计算机软件系统分为哪几类?答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。

系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。

这类软件一般与具体应用无关,是在系统一级上提供的服务。

系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。

另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。

在操作系统的基础上运行。

4、什么叫软件?说明软件与硬件之间的相互关系。

答:软件是指计算机程序及有关程序的技术文档资料。

两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。

在不太严格的情况下,认为程序就是软件。

硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作.在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。

大学生C语言课后习题全部答案详解

大学生C语言课后习题全部答案详解
}#include<stdio.h>
\
main()
{
int y,m,d,yt,mt,dt,age;
printf("请按格式2010-12-10输入你的生日");
scanf("%d-%d-%d",&y,&m,&d);
printf("请按格式2010-12-10输入今天的日期");
scanf("%d-%d-%d",&yt,&mt,&dt);printf(源自| 0——退出|\n");
printf("|------------------------|\n");
printf("请输入选项\n");
ch=getch();
putch(ch);
}
#include<stdio.h>
#include<conio.h>
main()
{
//定义
int a=0,b=0,he=0,cha=0,ji=0;
{printf("%4d",i);
n++;
}
}
printf("\n");
printf("%d",n);
}#include<stdio.h>
#include<math.h>
main()
{
int n=0,s=1,i=0;
printf("input:");
scanf("%d",&n);
for(i=1;i<=n;i++)

C语言课后习题答案(第1-8章)

C语言课后习题答案(第1-8章)

C语言课后习题答案(第1-8章)1.有的编程题答案有多种,只给出一种参考答案。

2.结合课本学习编程思想与技巧。

第一章C语言概述一、简答题1.(1)语言简洁、紧凑,使用方便、灵活;(2)运算符丰富;(3)具有丰富的数据类型;(4)具有结构化的控制语句;(5)语法限制不太严格,程序设计自由度大;(6)C语言允许直接访问物理地址,能进行位(bit)操作,能实现汇编语言的大部分功能,可以直接访问硬件;(7)生成目标代码质量高,程序执行效率高;(8)用C 语言写的程序可移植性好,基本上不作修改就能用于各种型号的计算机和各种操作系统。

2.顺序结构,分支结构,循环结构3.所谓算法就是为解决一个问题而采取的方法和步骤。

算法的特性:有穷性、确定性、输入、输出、可行性。

表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。

二、算法1.瓶子A里盛有醋瓶子B里盛有酱油有一个空瓶C将A中的醋倒入C将B中的酱油倒入A将C中的醋倒入B2.输入一个数放在a中max = a以下步骤重复9次:输入一个数放在a中如果a>max,max=a打印max的值3.如果a<b< bdsfid="85" p=""></b<>temp=aa=bb=temp如果c>atemp = aa=cc=temp否则如果c>btemp=bb=cc=temp打印a,b,c的值4.i=1sum=0以下程序循环100遍: sum=sum+i i=i+1 打印sum 的值5.如果(n 除以3的余数为0 并且 n 除以5的余数为0)n 能够同时被3和5整除否则 n 不能够同时被3和5整除 6.i=101以下语句循环50遍:j=2 flag=1 当j<(i 除以2的整数部分)时循环以下语句:如果i 除以j 的余数为零flag=0 退出该循环如果flag==1 打印i 的值 i=i+2 7.如果m做以下循环,直到m,n 能够被i 整除:如果m 能够被i 整除并且n 能够被i 整除 i 即是m 和n 的最大公约数跳出循环否则 i=i-1 打印i 的值8.data=b*b —4*a*c 如果data >0否则如果data=0 x1=x2=—b/2 否则无实数解三、编程题1.main () { a data b x 2)(1+-=adata b x 2)(2--=printf(“How do you do?\n”);printf(“##############################\n”);}2.main(){int a,b,c;scanf(“%d%d%d”,&a,&b,&c);printf(“sum=%d”,a+b+c);}第二章数据类型与表达式一、单项选择题1. B2. C3. A4. A5. D6. B7.A8.C9.D二、写出下列程序的运行结果1.aabb cc abcAN2.ab97983. 9,11,9,104.a=2,b=3,c=25.s1=2.500000s2=3三、编程题main(){char c1='c',c2='h',c3='i',c4='n',c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}第三章顺序程序设计选择题1)B 2)D 3)D 4)A 5)A 6)B 7)B 8)A 9)D 10)C 11)D 12)A填空题1)b 2)261 3)25 21 37 4)abc 5)5.0,4,c=3 6)3编程题#define PI 3.1415926main( ){float h, r, l, sv, sz ;printf(“请输入圆的半径r,圆柱高h:\n”) ;scanf(“%f,%f”,&r, &h) ;l=2* PI *r ;s= PI *r*r ;sv=3.0/4.0* PI *r*r*r ;sz= PI *r*r*h ;printf(“圆周长为:l=%6.2f, 圆面积为:s=%6.2f\n”, l, s) ;printf(“求体积为:sv=%6.2f, 圆柱体积为:sz=%6.2f\n”, sv, sz) ;}2、main( ){float c, f ;printf(“请输入一个华氏温度:\n”) ;scanf(“%f”, &f) ;c=5.0/9.0*(f-32) ;printf(“摄氏温度为:%6.2f\n”, c) ;}第四章循环程序设计一、选择题1.D2.D3.D4.C5.D6.A7.B8.B9.A 10.D.11.B 12.A 13.B 14.C 15.C 16.A 17.A 18.B 19.D 20.D21.A二、填空题1.5858582.03.184.89215.20,06.67.1,3,28.Year%400==0year%100!=0count++三、编程题1.main(){long a;scanf(“%ld”,&a);if (a>=10000&&a<=99999) printf(“这是个五位数”);else if (a>=1000&&a<=9999) printf(“这是个四位数”);else if (a>=100&&a<=999) printf(“这是个三位数”);else if (a>=10&&a<=99) printf(“这是个二位数”);else if (a>=0&&a<=9)printf(“这是个一位数”);elseprintf(“输入错误”);}3.main(){int a;int g,s,b;//个位、十位、百位printf("水仙花数有:\n"); for(a=100;i<=999;a++){g=a%10;s=a/10%10;b=a/100;if (a==g*g*g+s*s*s+b*b*b) printf("%d ",a);}printf("\n");}4、main(){int i,n;printf("input n:");scanf("%d",&n);printf("\n%d=",n);for(i=2;i<=n;i++){while(i!=n)if(n%i==0){printf("%d*",i);n/=i;} else break;}printf("%d\n",n);}5、#include “stdio.h”main(){char c;int w,x,y,z;w=x=y=z=0;while((c=getchar())!=’\n’){if((c>’a’&&c<’z’)||(c>’A’&&c<’Z’)) w++;else if (c==’’)x++;else if (c>’0’&&c<’9’)y++;else z++;}printf(“英文字母个数:%d\n”,w); printf(“空格个数:%d\n”,x);printf(“数字个数:%d\n”,y);printf(“其他字母个数:%d\n”,z); }6、main(){float h=100,t=100;int j;for(j=1;j<10;j++){t=t/2;h=h+t*2;}printf(“共经过:%f 米\n”,h); printf(“第十次反弹%f 米\n”,t/2); }第五章数组一、单项选择题1. B2. A3. C4. C5. D6. B7.C8.B9.C 10. B二、写出下列程序的运行结果1.S=3682.*************************3.S1=18S2=104.!margorP5.数字0的出现次数是:3数字1的出现次数是:2数字2的出现次数是:2数字3的出现次数是:2数字4的出现次数是:0数字5的出现次数是:1数字6的出现次数是:1数字7的出现次数是:1数字8的出现次数是:1数字9的出现次数是:1 三、编程题1.#include "stdio.h" main(){int a[11];int i,j,t;printf("input 10 number:\n");for (i=1;i<11;i++)scanf("%d",&a[i]);printf("\n");for (i=1;i<=9;i++)for (j=1;j<=10-i;j++)if (a[j]<a[j+1])< bdsfid="320" p=""></a[j+1])<> {t=a[j];a[j]=a[j+1];a[j+1]=t;}printf("the sorted number is:\n");for (i=1;i<11;i++)printf("%4d",a[i]);}2.#include "stdio.h"main(){int a[11],i,b;printf("imput 10 sorted number:\n");for (i=0;i<10;i++)scanf("%d",&a[i]);printf("input an integer:\n");scanf("%d",&b);for (i=9;i>=0&&a[i]>b;i--)a[i+1]=a[i];a[i+1]=b;printf("sorted numbers:\n");for (i=0;i<11;i++)printf("%5d",a[i]);}3.#include "stdio.h"main(){int a[5],i,j,t;printf("input 5 integer:\n");for (i=0;i<5;i++) /*输入5个整数*/scanf("%d",&a[i]);for (i=0,j=4;i<="" bdsfid="348" p="">{t=a[i];a[i]=a[j];a[j]=t;}for (i=0;i<5;i++) /*逆序存放后重新输出*/printf("%5d",a[i]);}4.#include "stdio.h"main(){int i,j,a[10][10]={{1},{1,1}};for (i=2;i<10;i++) /*给二维数组每个元素赋值*/ {a[i][0]=1; for (j=1;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];}for (i=0;i<10;i++) /*输出二维数组*/{for (j=0;j<=i;j++)printf("%-5d",a[i][j]);printf("\n");}}5.#include "stdio.h"main(){int a[3][4],i,j,max,row,col;printf("input 3*4 matrix:\n");for (i=0;i<3;i++) /*输入3*4矩阵*/for (j=0;j<4;j++)scanf("%d",&a[i][j]);max=a[0][0]; row=0; col=0;for (i=0;i<3;i++) /*寻找矩阵中的最大值及其行列号*/ for (j=0;j<4;j++)if (max<a[i][j])< bdsfid="376" p=""></a[i][j])<>{max=a[i][j]; row=i, col=j;}printf("\n数组中最大的数是%d,其行号是%d,列号是%d\n",max,row,col);}6.#define N 3#define M 4#include "stdio.h"main(){int a[N][M],i,j,k,max,row,col;printf("input %d*%d matrix: \n",N,M);for (i=0;i<="" bdsfid="387" p="">for (j=0;j<m;j++)< bdsfid="389" p=""></m;j++)<>scanf("%d",&a[i][j]);for (i=0;i<="" bdsfid="392" p="">{max=a[i][0]; row=i; col=0;for(j=1;j<a[i][j])<="" *求矩阵中每一行的最大值及其所行列号*="" bdsfid="395" if="" p="">{max=a[i][j]; col=j;}for (k=0;k<max)<="" *判断每一行的最大值在其所在列是否最大*="" bdsfid="398" if="" p="">break;if (k==N) /*得到鞍点*/{printf("the point is %d,row=%d,col=%d\n",max,row,col);break;}}if (i==N) /*没有鞍点*/printf("no point\n");}7.#include "stdio.h"{int num=0,word=0;/*word=0表示未出现单词,如出现单词就置word为1.num用来统计单词个数*/char c;printf("please input a string :\n");while ((c=getchar())!='\n')if (c==' ')word=0;else if (word==0){word=1; num++;}printf("There are %d words in the line\n",num);}8.#include "stdio.h"main(){int i,j,uppn,lown,dign,span,othn;/*uppn,lown,dign,span,othn分别存放英文大写字母、小写字母、数字、空格和其他字符的个数*/char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}9.#include "stdio.h"{int i,j;char str1[20],str2[20];printf("input two strings:\n");gets(str1);gets(str2);j=strlen(str1); /*求字符串1的长度*/for (i=0;str2[i]!='\0';i++,j++) /*字符串合并*/ str1[j]=str2[i];str1[j]='\0'; /*加上字符串结束标志*/puts(str1);}10.#include "stdio.h"main(){int i,n;char str1[20],str2[20];printf("input two strings:(no more than 20 characters)\n"); gets(str1); gets(str2);n=strlen(str1)printf("%d\n",str1[i]-str2[i]);}第六章函数与编译预处理一、单项选择题1. C2. B3. B4. B5. A6. B7.A8.C9.D 10. A二、写出下列程序的运行结果1.92.4,B8,B3.-4.0000004.a=6,b=55.48三、编程题1.main(){ int prime(int n);int n;printf("input n(n>0):\n");scanf("%d",&n);if (prime(n))printf("%d is a sushu\n",n);else printf("%d is not a sushu\n",n); }int prime(int n){int flag=1,i;for (i=2;i<=n/2&&flag==1;i++)if (n%i==0) flag=0;return(flag);}2.#define N 3convert(int array[3][3]){ int i,j,t;for (i=0;i<n-1;i++)< bdsfid="507" p=""></n-1;i++)<> for (j=i+1;j<n;j++)< bdsfid="509" p=""></n;j++)<> {t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}main(){int i,j;int a[N][N];printf("input a:\n");for (i=0;i<n;i++)< bdsfid="520" p=""></n;i++)<>for (j=0;j<n;j++)< bdsfid="522" p=""></n;j++)<> scanf("%d",&a[i][j]);printf("Array a:\n");for (i=0;i<n;i++)< bdsfid="526" p=""></n;i++)<> {for (j=0;j<n;j++)< bdsfid="528" p=""></n;j++)<> printf("%5d",a[i][j]);printf("\n");}convert(a);printf("a de zhuanzhi is:\n");for (i=0;i<n;i++)< bdsfid="535" p=""></n;i++)<>{for (j=0;j<n;j++)< bdsfid="537" p=""></n;j++)<>printf("%5d",a[i][j]);printf("\n");}}3. #include#includemain(){char str[100];printf("input a string:\n") ;gets(str);inverse(str);printf("the reversed string is:%s\n",str);}inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str)-1;i<j;i++,j--)< bdsfid="558" p=""></j;i++,j--)<>{t=str[i];str[i]=str[j];str[j]=t;}4. #includeconcat(char str1[],char str2[]){ int i=0,j;while (str1[i]!='\0')i++;for (j=0;str2[j]!='\0';i++,j++)str1[i]=str2[j];str1[i]='\0';}main(){char str1[100],str2[100];gets(str1);gets(str2);concat(str1,str2);puts(str1);}5. main(){char str[80];printf("input a string (4 ge shu zi zi fu):\n"); scanf("%s",str); insert(str);printf("result is:\n%s\n",str);}insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}6. #include "stdio.h"int i,ndight,nwhite,nletter,nother;count(char str[]){ndight=nwhite=nletter=nother=0;for (i=0;str[i]!='\0';i++)if (str[i]>='0'&&str[i]<='9')ndight++;else if ((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) nletter++;else if (str[i]==' ')nwhite++;else nother++;}main(){char text[80];printf("input a string:\n");gets(text);count(text);printf("ndight=%d,nletter=%d,nwhite=%d,nother=%d\n",n dight,nletter,nwhite,nother); }7. #define N 10#includesort(char str[]){int i,j;char t;for (i=1;i<n;i++)< bdsfid="625" p=""></n;i++)<>for (j=0;j<n-i;j++)< bdsfid="627" p=""></n-i;j++)<> if (str[j]>str[j+1]){t=str[j];str[j]=str[j+1] ;str[j+1]=t;}}main(){char str[N];int i ;printf("Input 10 ge zi fu:\n");gets(str);printf("The sorted result:\n") ;for(i=0;i<n;i++)< bdsfid="639" p=""></n;i++)<> printf("%c",str[i]);}8.这题较复杂#include#include#define N 10void input_e(int num[],char name[N][8]){int i;for (i=0;i<n;i++)< bdsfid="651" p=""></n;i++)<> { printf("input gong hao:");scanf("%d",&num[i]);printf("input name:");getchar();gets(name[i]);}for (i=0;i<n;i++)< bdsfid="659" p=""></n;i++)<> printf("%5d%10s\n",num[i],name[i]);}void sort(int num[],char name[N][8]) /*选择法排序*/{int i,j,min,temp1;char temp2[8];for (i=0;i<n-1;i++)< bdsfid="666" p=""></n-1;i++)<> {min=i;for (j=i+1;j<n;j++)< bdsfid="669" p=""></n;j++)<>if (num[j]temp1=num[i];strcpy(temp2,name[i]);num[i]=num[min];strcpy(name[i],name[min]);num[min]=temp1;strcpy(name[min],temp2);}printf("the sorted result:\n");for (i=0;i<n;i++)< bdsfid="681" p=""></n;i++)<>printf("%5d%10s\n",num[i],name[i]);}void search(int n,int num[],char name[N][8]) /*折半查找法*/ { int top,bott,mid,find;bott=N-1;if ((nnum[N-1]))find=-1;while ((find==0)&&(top<=bott)){mid=(bott+top)/2;if (n==num[mid]){find=1; printf("%d name is:%s\n",n,name[mid]);}else if (n<num[mid])< bdsfid="695" p=""></num[mid])<> bott=mid-1;else top=mid+1;}if ((find==-1)||(find==0))printf("%d is not found.\n",n);}main(){int num[N],number,c,flag;char name[N][8];input_e(num,name);sort(num,name);for (flag=1;flag;){printf("please input chazhao de gonghao:"); /*输入查找的工号*/ scanf("%d",&number);search(number,num,name);printf("continue Y/N?"); /*是否继续查找*/getchar();c=getchar();if (c=='N'||c=='n')flag=0;}}9. #include "stdio.h"#define MAX 10main(){char str[MAX];char c;int i;i=0;printf("input number(16 jinzhi): "); /*输入一个十六进制的数*/while((c=getchar())!='\n'&&i<max)< bdsfid="727" p=""></max)<>{ str[i]=c;i++;printf("result is :%d\n",htod(str));}int htod(char s[]){int i,n;n=0;for(i=0;s[i]!='\0';i++){if (s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if (s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if (s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}10. #define SW AP(a,b) t=a;a=b;b=t main(){int a,b,t;printf("input a,b:") ;scanf("%d,%d",&a,&b);SW AP(a,b);printf("result:a=%d,b=%d\n",a,b);}11. #define SURPLUS(a,b) ((a)%(b)) main(){int a,b;printf("input a,b:");scanf("%d,%d",&a,&b);printf("result is: %d\n",SURPLUS(a,b)); } 12. main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",max(a,b,c));}int max(int x,int y,int z){int t;t=(x>y ? x : y);return(t>z?t:z);}#define MAX(x,y) ((x)>(y)?(x):(y)) main(){int a,b,c;printf("input a,b,c:");scanf("%d,%d,%d",&a,&b,&c);printf("max=%d\n",MAX(MAX(a,b),c)); }13. #include "stdio.h"#define CHANGE 1#define MAX 80main(){char str[MAX];int i;printf("input a string:\n");gets(str);#if (CHANGE){ for (i=0;str[i]!='\0';i++)if (str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z')str[i]=str[i]+1;else if (str[i]=='z'||str[i]=='Z')str[i]=str[i]-25;}#endifprintf("%s\n",str);}第七章指针一、选择题1)A 2)D 3)D 4)C 5)B 6)B 7)B 8)C 9)B 10)C 11)A 12)A 13)A 14)C 15)B 16)A 17)C 18)B 19)D 20)B二、阅读下面程序,写出程序运行结果1)abcdeedcba 2)1113151719 3)(TurboC中是11,9 7,11 )(VisualC是9,9 7,11)4)3 6 5)6385三、编程题1、main( ){ int a[10],i,temp,*p=a;printf("Please input array a:\n"); for(i=0;i<10;i++) scanf("%d",&a[i]);printf("array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);for(i=0;i<5;i++){ temp=p[i];p[i]=p[10-i-1];p[10-i-1]=temp;}printf("\n Now array a:\n");for(i=0;i<10;i++)printf("%4d",a[i]);}2、main( ){ int a[3][3],*p,i,j;printf("please input matrix:\n"); for(i=0;i<3;i++) for(j=0;j<3;j++)scanf("%d",&a[i][j]);p=&a[0][0];move(p);printf("\n Now matrix:\n");for(i=0;i<3;i++){ for(j=0;j<3;j++)printf("%4d",a[i][j]);printf("\n");}}move(int *q){ int i,j,t;for(i=0;i<3;i++)for(j=i;j<3;j++){ t=*(q+3*i+j);*(q+3*i+j)=*(q+3*j+i); *(q+3*j+i)=t;}}3、#include#include。

C语言程序设计习题与参考答案

C语言程序设计习题与参考答案

习题与参考答案第1章 C语言程序设计基础思考与练习一、填空题1、C程序是由构成的,一个C程序中至少包含。

2、C程序的注释是由和所界定的文字信息组成的。

3、开发一个C程序要经过编辑、、和运行4个环节。

4、C语言中,头文件的扩展名是。

5、C语言源程序的扩展名;经过编译后,生成文件的后缀是;经过连接后,生成文件的扩展名是。

二、简答题1、简述C语言程序的运行步骤。

2、软件编程中的可读性原则一般包括那些内容?3、什么是算法?算法的特点有哪些?4、参照本章例题,编写一个C程序,输出以下内容。

**************************************************Hello,world!**************************************************5、写出算法。

已知圆的半径,求周长和面积。

第1章思考与练习参考答案一、填空题1、函数、一个主函数main()2、/*、*/3、编译、连接4、.h5、.c、.obj、.exe二、简答题1、简述C语言程序的运行步骤。

答:(1)编辑C程序源文件;(2)编译,将编辑好的源程序文件“*.c”,翻译成二进制目标代码文件“*.obj”;(3)连接,将编译生成的各个目标程序模块和系统或第三方提供的库函数“*.lib”连接在一起,生成可以脱离开发环境、直接在操作系统下运行的可执行文件“*.exe”;(4)运行程序。

如果运行出错,这说明程序处理的逻辑存在问题,需要再次回到编辑环境针对程序出现的逻辑错误进一步检查、修改源程序,重复编辑→编译→连接→运行的过程,直到取得预期结果为止。

2、软件编程中的可读性原则一般包括那些内容?答:1.编程规范概要(1)程序结构清晰,简单易懂,单个函数的程序行数不得超过100行。

(2)打算干什么,要简单,直截了当,代码精简,避免垃圾程序。

(3)尽量使用标准库函数和公共函数。

(4)不要随意定义全局变量,尽量使用局部变量。

C语言程序设计教程——课后习题答案

C语言程序设计教程——课后习题答案
printf("%d / %d---> shang=%d,yushu=%d",x,y,temp2,x-y*temp2);
getch();
}
9、
#include<stdio.h>
void main()
{float x,y,m=0,n=0;
scanf("%f,%f",&x,&y);
n=(x-2)*(x-2);
3:(x<30&&x>20)||(x<-100)
4:***a=25,b=14,c=19***
5:37
6:if(a<=b) printf("1");
else printf("2");
7、
#include<stdio.h>
void main()
{char a,b,t1,t2;
scanf("%c,%c",&a,&b);
4、大小写字母在c语言中是有区别的。
5、除main函数和标准库函数以外,用户可以自己编写函数,程序一般由多个函数组成,这些函数制定实际所需要做的工作。
例如:
void main()
{
int a,b,c,s;
a=8;b=12;c=6;
s=a+b*c;
printf("s=%d\n",s);
}
3、c语言的特点:
}
10、void main()
{
int i,j,k;
scanf("%d,%d,%d",&i,&j,&k);
((i%2 != 0?1:0) + (j%2 != 0?1:0)+(k%2 != 0?1:0)) == 2?printf("YES\n"):printf("NO\n");
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

大连海事大学C语言与Windows程序设计6道课后题答案1.(1)设计具有以下功能的程序:从键盘上输入10个非负整数,统计并输出最大数和最小数,当输入负数时结束输入。

#include <iostream.h>void main(){int a, max=-1, min=32767,num=1;cout<<"Please input a number:"<<endl;do{cin>>a;if(a<0) break; //输入负数结束输入if(a>max) max =a;if(a<min) min =a;num++;}while (a>=0&&num<=10);if(max>=0)cout<<"Max="<<max<<endl<<"Min="<<min<<endl;elsecout<<"No data!";}1.(2)编程序实现求和:S=1-1/3+1/5-1/7+……+1/n。

要求:n值在程序运行时从键盘输入。

#include<stdio.h>void main(){inti,n,sign=1;float sum=0.0;printf("请输入n:");scanf("%d",&n);for(i=1;i<=n;i+=2){sum+=sign*(1.0/i);sign=-sign;}printf("\nsum=%f\n",sum);}2.设计具有以下功能的程序:从键盘输入10个整数,然后排序。

(要求:利用函数调用实现并以数组作为函数的参数)3.编写几何点(二维平面上)的类Point,包括位置属性(二维坐标x,y),成员函数包括:点的位置获取函数GetX()和GetY(),点的位置设置函数SetX()和SetY(),点的位置移动函数MoveTo()点的信息打印函数Display()。

void main(){Point p(100,100);p.Display();p.MoveTo(200,200);cout<<"after moving…"<<endl;p.Display();}程序输出结果如下:X: 100Y: 100after moving…X: 200Y: 200#include <iostream.h>class Point{private:int X;int Y;public:Point(int X, int Y){this->X = X;this->Y = Y;}Point(){this->X = 0;this->Y = 0;}~Point(){}intGetX(){return this->X;}intGetY(){return this->Y;}voidSetX(int X){this->X = X;}voidSetY(int Y){this->Y = Y;}voidMoveTo(int X, int Y){SetX(X);SetY(Y);}void Display(){cout<<"X: "<<GetX()<<endl;cout<<"Y: "<<GetY()<<endl;}};4.编写几何图形圆的类Circle,包括两个属性:圆心O(用上题中的Point类实现)和半径R。

成员函数包括:圆心位置获取函数GetO()半径获取函数GetR()半径位置设置函数SetR()圆的位置移动函数MoveTo()圆的半径设置函数SetR()圆的信息打印函数Display()void main(){Point p(100,100);Point p2(200,200);Circle c(p, 100);c.Display();c.MoveTo(p2);cout<<"after moving"<<endl;c.Display();c.SetR(200);cout<<"after altering r"<<endl;c.Display();}程序输出结果如下:Circle: (100,100),100after movingCircle: (200,200),100after altering rCircle: (200,200),200#include <iostream.h>class Point{private:int X;int Y;public:Point(int X, int Y){this->X = X;this->Y = Y;}Point(){this->X = 0;this->Y = 0;}~Point(){}intGetX(){return this->X;}intGetY(){return this->Y;}voidSetX(int X){this->X = X;}voidSetY(int Y){this->Y = Y;}voidMoveTo(int X, int Y){SetX(X);SetY(Y);}/* void Display(){cout<<"X: "<<GetX()<<endl;cout<<"Y: "<<GetY()<<endl;}*/};/////////////////////////class Circle{private:Point O;int R;public:Circle(Point& p, int R){O.MoveTo(p.GetX(),p.GetY());this->R = R;}~Circle(){}Point GetO(){return this->O;}intGetR(){return this->R;}voidMoveTo(Point& p){O.MoveTo(p.GetX(), p.GetY());}voidSetR(int R){this->R = R;}void Display(){cout<<"Circle: ("<<O.GetX()<<","<<O.GetY()<<"),"<<R<<endl;}};void main(){Point p(100,100);Point p2(200,200);Circle c(p, 100);c.Display();c.MoveTo(p2);cout<<"after moving"<<endl;c.Display();c.SetR(200);cout<<"after altering r"<<endl;c.Display();}5.编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。

该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类TimeDate,它是前面两个类为基类的派生类。

void main(){TimeDate date1, date2(1998, 8, 12, 12, 45, 10);date1.SetDate(1998, 8, 7);date1.SetTime(10, 30, 45);cout<<"The date1 date and time is:";date1.GetDT();cout<<"The date1 date is:";date1.GetDate();cout<<"The date1 time is:";date1.GetTime();cout<<"The date2 date and time is:";date2.GetDT();}#include<iostream.h>class Date{public:Date() {}Date(int y, int m, int d) { SetDate(y, m, d); }voidSetDate(int y, int m, int d){Year = y;Month = m;Day = d;}voidGetDate(){cout<<Year<<"/"<<Month<<"/"<<Day<<endl;}protected:int Year, Month, Day;};class Time{public:Time() {}Time(int h, int m, int s) { SetTime(h, m, s); }voidSetTime(int h, int m, int s){Hours = h;Minutes = m;Seconds = s;}voidGetTime(){cout<<Hours<<":"<<Minutes<<":"<<Seconds<<endl;}protected:int Hours, Minutes, Seconds;};classTimeDate:public Date, public Time{public:TimeDate(){}TimeDate(int y, intmo, int d, int h, int mi, int s):Date(y, mo, d), Time(h, mi, s) {}voidGetDT(){cout<<Year<<"/"<<Month<<"/"<<Day<<"_"<<Hours<<":"<<Minutes<<":"<< Seconds<<endl;}};void main(){TimeDate date1, date2(1998, 8, 12, 12, 45, 10);date1.SetDate(1998, 8, 7);date1.SetTime(10, 30, 45);cout<<"The date1 date and time is:";date1.GetDT();cout<<"The date1 date is:";date1.GetDate();cout<<"The date1 time is:";date1.GetTime();cout<<"The date2 date and time is:";date2.GetDT();}6.生成一个Object抽象类,在其中声明double CalArea()为纯虚函数,从Object派生出:Rect类(其中包含成员变量Point topleft,Point bottomright。

相关文档
最新文档