已解锁-C语言课后习题答案

合集下载

C语言课后习题参考答案参考资料

C语言课后习题参考答案参考资料

第2章部分习题参考答案、单项选择题:DABDD DAAAB BCD2020/4/8 1第3章部分习题参考答案•、单项选择题:CACBB CDBBB ADCCB DCCBB :、填空题:1. a!"b I I av=c2. x>20 &&x<30 I I x<-1003. 04. 非。

5. 16. x%3==0&&x%7==0 或x%21==02020/4/8 2第3章习题三、编程题2、#include <stdio.h>#include <math.h>void main(){ float a,b,c,p,s;printf(”请输入三角形的三边长:H);sea nf (” %f %f )&a,&b,&c);if(a+b>c && a+c>b && b+c>a){ p=(a+b+c)/2;s=sqrt(p*(p-a)*(p-b)*(p-c));printf(H三角形面积为:%.2f\n n,s);}else printf(”输入的边长不能构成三角形!\n”); } 2020/4/8 33、#include <stdio.h>void main(){int x,y;scanf(”%d”,&x);if(x>・5 &&XVO) y=x-l;else if(x==O) y=x;else if(x>0 && x<8) y=x+l;else y=10;printf(”y=%d\n”,y);2020/4/8ch=getchar(); 第3章习题」4,大写转小写,小写转大写,其他原样输出。

#indude <stdio.h> void main() { char ch;scanf(,,%c ,,z &ch); if(di>='A'&&chv='Z') ch=ch+32;else if(ch>='a'&& chv='Z)ch=ch-32;printf(,,%c\n ,,z ch);if(ch>=A && ch<=,Z I) printf(,,%c\n ,\ch+32); else if(ch>='a' && ch<='z')printf(”%c\n”,ch ・32);else printf(,,<Vbc\n ,,z ch);2020/4/85第3章_习题」#indude<stdio.h>8. void main(){ int year,flag=l;double money,rate,total;printf("请输入存款本金和年限:");scanf(,,%lf(Vbd,\&money,&year);if(year= = 1) rate=0.00215;else if(year= = 2) rate=0.0023;else if(year==3) rate=0.00245;else if(year==5) rate=0.00275;else if(year==8) rate=0.0032;else flag=O;if(flag= = l){ total=money* (1+rate* 12*yea r);printf("总金额为:%・2lf\rT,total);2020/4/8}else printf(”输入的期限不正确!\n");#indude <stdio.h>void main(){ int year,flag = l; double money,rate,total;printf(H请输入存款本金和年限:H);scanf("%lf%cT,&money,&year);switch (year){ case 1: rate=0.00215; break;case 2: rate=0.0023; break;case 3: rate=0.00245; break;case 5: rate=0.00275; break;case 8: rate=0.0032; break;default: printf("输入的期限不正确!\n,flag=O;?if(flag= = l){ total=money* (1+rate* 12*year);printf(”总金额为:%・2lf\rT,total);}} 2020/4/8 7第4章部分习题参考答案・、单项选择题BCDBD BCDBB CCCCC BACDD二、编程题2・编程输出1^200之间所有能被3整除、且个位数为6的整数。

C语言课后习题答案

C语言课后习题答案
printf(“第十次反弹 %f 米\n”,t/2);
}
第五章 数 组
一、单项选择题
1. B 2. A 3. C 4. C 5. D 6. B 7.C 8.B 9.C 10. B
二、写出下列程序的运行结果
1.S=368
2.
*****
*****
2.顺序结构,分支结构,循环结构
3.所谓算法就是为解决一个问题而采取的方法和步骤。
算法的特性:有穷性、确定性、输入、输出、可行性。
表示算法的方式:(1)用自然语言表示;(2)用流程图表示;(3)用N—S流程图表示;(4)用伪代码表示;(5)用计算机语言表示。
二、算法
1. 瓶子A里盛有醋
}
第二章 数据类型与表达式
一、单项选择题
1. B 2. C 3. A 4. A 5. D 6. B 7.A 8.C 9.D
二、写出下列程序的运行结果
1.
aabb cc abc
AN
2.
ab
9798
3. 9,11,9,10
b=temp
如果 c>a
temp = a
a=c
c=temp
否则
如果 c>b
temp=b
b=c
c=temp
打印a,b,c的值
4. i=1
sum=0
以下程序循环100遍:
sum=sum+i
i=i+1
跳出循环
否则
i=i-1
打印i的值
8. data=b*b—4*a*c
如果data>0
否则 如果data=0

最新c语言课后习题答案汇总

最新c语言课后习题答案汇总

c语言课后习题答案第二章习题2.什么叫做结构化算法?为什么要提倡结构化算法?答:结构化算法是由一些基本结构顺序组成的。

在基本结构之间不存在向前或向后的跳转,流程的转移只存在于一个基本的结构范围内。

一个非结构化的算法可以用一个等价的结构化算法代替,其功能不变。

跟结构化算法比较起来,非结构化算法有以下缺点:流程不受限制的随意转来转去,使流程图豪无规律使人在阅读的时候难以理解算法的逻辑.难以阅读,也难以修改,从而使算法的可靠性和可维护性难以保证。

4.第三章习题1.#include<stdio.h>#include<math.h>void main(){unsigned int n;float p,p1,r=0.09;scanf("%u",&n);p=pow(1+r,n);p1=(p-1)*100;printf("%5.2f%%\n",p1);}运行结果:输入,回车,见结果:2.#include<stdio.h>#include<math.h>int main(){int bj=1000;float r1,r2,r3,r5,r0,lx1,lx2,lx3,lx4,lx5;r1=0.0414;r2=0.0468;r3=0.0540;r5=0.0585;r0=0.0072;lx1=bj*r5;lx2=bj*(1+r2)*r3;lx3=bj*(1+r3)*r2;lx4=bj*pow(1+r1,5);lx5=bj*r0*5;printf("lx1=%f lx2=%f lx3=%f lx4=%f lx=5%f\n",lx1,lx2,lx3,lx4,lx5); return 0;}运行结果:3.#include<stdio.h>#include<math.h>int main(){long d,p;d=300000;p=6000;double m,r=0.01;m=log(p/(p-d*r))/log(1+r);printf("%4.2f",m);return 0;}运行结果:4.#include<stdio.h>int main(){int c1,c2;c1=97;c2=98;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}运行结果:①;②;③结果不变。

C语言课后题编程标准答案(可编辑修改word版)

C语言课后题编程标准答案(可编辑修改word版)

C 语言课后题编程答案所有章节的课后习题的选择题和填空题大家必须熟练掌握,编程题掌握到第九章即可。

希望大家通过课后的编程题熟悉典型的编程算法,掌握基本的编程思路,注意编程细节。

第3 章3-17编写程序,把560 分钟换算成用小时和分钟表示,然后进行输出。

#include<stdio.h>main(){int h,m。

h=560/60。

m=560%60。

printf(“560 分钟可转换成%d 小时%d 分钟”,h,m)。

}程序总结:(1)只要在程序中用到系统提供的库函数,必须把库函数所在的头文件用#include 命令包含进来。

否则库函数的使用无效。

输入输出库函数的头文件是:stdio.h。

数学函数的头文件是:math.h。

字符处理函数的头文件是:ctype.h。

字符串处理函数的头文件是:string.h。

(2)注意C 语言中的“/”和“%”两种运算符。

“/”的运算结果取决于两操作数的类型。

比如:1/2=0(与数学中不同,结果与操作数的类型一致,所以结果只取商值),1.0/2=0.5(除之前2 要自动类型转换成2.0,因为只有同类型的操作数才能进行除运算), 1/2.0=0.5, 1.0/2.0=0.5。

“%”的两个操作数都必须是整数。

(3)printf(“560分钟可转换成%d 小时%d 分钟”,h,m)。

printf 第一个参数要尽量详细,这样可以提高输出结果的可读性,恰当好处的添加提示性信息,可以提高程序的质量。

(4)i nt h,m。

变量起名要有艺术性,尽量做到见名知意。

3-18编写程序,输入两个整数,1500 和350,求出它们的商和余数并进行输出。

#include<stdio.h>main(){int a,b,m,n。

printf(“请输入两个整数:\n”)。

scanf(“%d%d”,&a,&b)。

m=a/b。

n=a%b。

printf(“%d 除以%d 商%d 余%d”,a,b,m,n)。

c语言课后习题答案

c语言课后习题答案

c语言课后习题答案C语言课后习题答案C语言作为一门广泛应用于编程领域的高级编程语言,对于学习者来说是必不可少的一门课程。

在学习过程中,课后习题是巩固知识、提高编程能力的重要环节。

然而,有时候我们可能会遇到一些难以解答的问题,或者对于某些题目的解答存在疑惑。

在这篇文章中,我将为大家提供一些C语言课后习题的答案,并解释一些常见问题。

1. 编写一个程序,从键盘输入一个整数,判断该数是否为素数。

解答:```c#include <stdio.h>int isPrime(int num) {if (num <= 1) {return 0;}for (int i = 2; i * i <= num; i++) {if (num % i == 0) {return 0;}}return 1;}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (isPrime(num)) {printf("%d是素数。

\n", num);} else {printf("%d不是素数。

\n", num);}return 0;}```以上代码中,我们定义了一个函数`isPrime`,用于判断一个数是否为素数。

在`main`函数中,我们从键盘输入一个整数,并调用`isPrime`函数进行判断。

如果返回值为1,则说明该数是素数,否则不是素数。

2. 编写一个程序,计算并输出1到100之间所有能被3整除但不能被5整除的数之和。

解答:```c#include <stdio.h>int main() {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 3 == 0 && i % 5 != 0) {sum += i;}}printf("1到100之间所有能被3整除但不能被5整除的数之和为:%d\n", sum);return 0;}```以上代码中,我们使用`for`循环遍历1到100之间的所有整数,使用条件判断语句`if`筛选出能被3整除但不能被5整除的数,并将它们累加到`sum`变量中。

c语言课后习题参考答案

c语言课后习题参考答案

第1章习题参考答案1、选择题(1)C (2)C (3)D (4)B (5)D(6)D (7)C (8)A (9)A (10)C2、填空题(1)函数、一个主函数、函数。

(2) /*、*/ 。

(3) 连接。

(4) # 。

(5) main 。

(6) /*……*/ 。

(7) .h 。

(8) F5 。

(9) .exe (10) .exe 、 .obj 、.exe 。

3、程序设计题(1) #include <stdio.h>void main(){printf(" *\n");printf(" * S *\n");printf(" *\n");}(2) #include <stdio.h>void main(){printf("*****************\n");printf("Your are welcome!\n");printf("-----------------\n");}(3) #include <stdio.h>void main(){char c1,c2;printf("请输入c1和c2的值:");c1=getchar();scanf(" %c",&c2);printf("c1=%c\n",c1);printf("c2=");putchar(c2);printf("\n");}第2章习题参考答案1、简述算法的概念。

答:算法就是为解决一个具体的问题而使用的确定的、有限的方法和步骤。

2、算法应具备哪些特性?答:算法应具备有穷性、确定性、可行性、输入、输出等五种特性。

3、分别说明什么是自然语言描述法?什么是框图语言描述法?什么是伪语言描述法?什么是N-S框图语言描述法?它们的优缺点各是什么?答:①用人们日常使用的语言来描述算法,称为算法的自然语言描述法。

C语言课后全部答案

C语言课后全部答案

第一章C语言初步一、选择题1.C2.D3.D4.A5.D6.B7.B 8.B 9.C 10.A二、思考与应用题1.A.字符串常量B.字符串常量C.整型常量D.浮点型常量(double)2.A.long doubleB.long intC.floatD.字符型常量3.A.整型常量B.字符型常量C.字符串常量D.字符串常量4.FirstExample: 10,w is Yz is 5.125.int main(void){return 0;}6.#include<stdio.h>int main(void){print f("Hello World");return 0;}7.#include<stdio.h>int main(void){printf("We are to learn correct");printf("C language here");return 0;}/*main*/8.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c;/*Statements*/printf("The end of the program."); return 0;}/*main*/9.#include<stdio.h>int main(void){/*Local Definition*/int a;float b;char c,d;/*Statements*/printf("The end of the program."); return 0;}/*main*/10.#include<stdio.h>int main(void){/*Local Definition*/int a;char b,c,d;float e,f;/*Statements*/printf("The end of the program."); return 0;}/*main*/11.A.char option;B.int sum=0;C.float produce=1;12.A.short code;B.float salesTax=.0825;C.double sum=0;13.#include<stdio.h>int main(void){float cost=172.53;printf("The sales total is: $ %6.2f\n",cost);printf("^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^");return 0;}14.#include<stdio.h>int main(void){printf("******\n");printf("******\n");printf("******\n");printf("******\n");return 0;}15.#include<stdio.h>int main(void){printf("*\n");printf("**\n");printf("***\n");printf("****\n");return 0;}16.#include<stdio.h>int main(void){int x1=1;int x2=10;int x3=100;int x4=1000;int x5=10000;printf("%d %d %d %d %d\n",x1,x2,x 3,x4,x5);printf("%f %f %f %f %f\n",x1,x2,x 3,x4,x5);return 0;}17.#include<stdio.h>int main(void){int x;printf("Please enter an integer:\n");scanf("%d",&x);printf("The number as a character:%c\n",x);printf("The number as a decimal:%d\n",x);printf("The number as a float:%f\n",x);return 0;}三、编程题1.#include<stdio.h>int main(void){int x1;int x2;int x3;printf("Please enter three numbers:");scanf("%d%d%d",&x1, &x2, &x3);printf("\nYour numbers forword:\n");printf("%8d\n%8d\n%8d\n",x1,x2,x3 );printf("Your numbers reversed:\n");printf("%8d\n%8d\n%8d\n",x3,x2,x1 );return 0;}2.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;int x10;printf("Please enter 10 numbers:\n");scanf("%d%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9, &x10);printf("\nYour numbers are:\n");printf("%8d%8d\n",x1,x10);printf("%8d%8d\n",x2,x9);printf("%8d%8d\n",x3,x8);printf("%8d%8d\n",x4,x7);printf("%8d%8d\n",x5,x6);return 0;}3.#include<stdio.h>int main(void){int x1;int x2;int x3;int x4;int x5;int x6;int x7;int x8;int x9;printf("Input:\n");printf(" ");scanf("%d%d%d%d%d%d%d%d%d",&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8, &x9);printf("\nOutput\n");printf("%8d,%d,%d\n",x1,x2,x3);printf("%8d,%d,%d\n",x4,x5,x6);printf("%8d,%d,%d\n",x7,x8,x9);return 0;}第二章表达式与简单程序设计一、选择题1.A2.D3.C4.B5.B6.D7.B二、思考与应用题1.A.2 B.8 C.8 D.0 E.11 2.A.4,4B.4,4 C.4,4 D.2,3 E.4,53.A.12 B.-27 C.14 D.2 E.4 4.A.22.52 B.4.5 C.13.333333 D.0.882353 E.0.075.ABCEF6.A.5 B.2 C.4 D.1 E.7.A.5 B.2 C.4 D.-3 E.6 8.A.5 B.294 C.4 D.29 E.9 9. 50 50 100 2510.#include<stdio.h>int main(void){float x;float y;float p;float s;float total;printf("Please enter x and y:\n");scanf("%f%f",&x,&y);p=x*y;s=x+y;total=s*s+p*(s-x)*(p+y);printf("total=%f",total);return 0;}11.#include<stdio.h>int main(void){int x,y,z;printf("Please enter two integers:\n");scanf("%d%d",&x,&y);z=x*y;printf("%d*%d=%d",x,y,z);return 0;}12.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x%10;printf("%d",y);return 0;}13.#include<stdio.h>int main(void){float x;int y;printf("Please enter a floating point number:\n");scanf("%f",&x);y=(int)x/10%10;printf("%d",y);return 0;}14.#include<stdio.h>int main(void){float x;float y;float c;float s;p rintf("Please enter length and width:\n");s canf("%f%f",&x,&y);c=2*(x+y);s=x*y;p rintf("The circumference is %f\n",c);p rintf("The measure of area is %f",s);r eturn 0;}15.#include<stdio.h>int main(void){int a;f loat r;p rintf("Please enter an angle(integer):\n");s canf("%d",&a);r=a/57.295779;p rintf("%d°is %.5f radians",a,r);r eturn 0;}16.#include<stdio.h>int main(void){float c;float F;printf("Please enter centigrade:\n");scanf("%f",&c);F=32+(c*180.0/100.0);printf("Fahrenheit is %f ",F);return 0;}17.(1)double m,v,KinEn;KinEn=m*v*v/2;(2)double b,c,res;res=(b+c)/(2*b*c);18.(1)#include<stdio.h>int main(void){int x=25;x=x+5;printf("%d\n",x);x=x+5;printf("%d\n",x);return 0;}(2)#include<stdio.h>int main(void){int x=10;x=x+2;printf("%d\n",x);x=x+2;printf("%d\n",x);return 0;}(3)#include<stdio.h>int main(void){int x=32;x=x*2;printf("%d\n",x);x=x*2;printf("%d\n",x);return 0;}三、编程题1.A.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/12;printf("%f inches is %f feet ",x,y);return 0;}B.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/36;printf("%f inches is %f yards ",x,y);return 0;}C.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x*2.54;printf("%f inches is %f centimeters ",x,y);return 0;D.#include<stdio.h>int main(void){float x;float y;printf("Please enter an number(inch):\n");scanf("%f",&x);y=x/39.37;printf("%f inches is %f meters ",x,y);return 0;}2.#include<stdio.h>int main(void){int fib1;int fib2;int fib3;fib1=13+21;fib2=21+fib1;fib3=fib1+fib2;printf("%d,%d,%d",fib1,fib2,fib3);return 0;}3.#include<stdio.h>int main(void){int x;int b1;int b2;int b3;int b4;int b5;printf("Please enter an integer(0-32767):\n");scanf("%d",&x);b1=x%10;b2=x/10%10;b3=x/100%10;b4=x/1000%10;b5=x/10000%10;printf("%4d%4d%4d%4d%4d\n",b5,b4,b3,b2,b1);printf("%4d%4d%4d%4d%\n",b4,b3,b2,b1);printf("%4d%4d%4d\n",b3,b2,b1);printf("%4d%4d\n",b2,b1);printf("%4d",b1);return 0;}第三章分支程序设计一、选择题1.C2.D3.A4.B5.B6.A7.C8.C 9.A二、思考与应用题1.A.0 B.1 C.1 D.0 E.0 2.A.x>=y B.x<y C.x!=y D.x==y E.x>y3.A.0 B.1 C.1 D.1 E.0 4.x=4,y=3,z=25.x=4,y=1,z=26.x=4,y=2,z=27.x=4,y=1,z=28.x=0,y=0,z=19.x=2,y=0,z=210.x=0,y=0,z=111.x=0,y=0,z=112.x=-1,y=0,z=013.x=0,y=1,z=014.x=1,y=3,z=115.x=1,y=3,z=116.x=4,y=3,z=017.if(aChar=='E'){c++;printf("Value is E\n");}18.switch(ch){case ‘E’:case ‘e’:countE++;break;case ‘A’:case ‘a’:countA++;break;case ‘I’:case ‘i’:countI++;break;default:printf("Error-Not A,E,or I\a\n"); }三、编程题1.#include<stdio.h>main( ){int x,x1,x2,x3;char grade;printf("please enter three text scores(0-100):\n");scanf("%d%d%d",&x1,&x2,&x3);x=(x1+x2+x3)/3;if(x>=90)grade='A';else if(x>=70&&x<90)if(x3>90)grade='A';elsegrade='B';else if(x>=50&&x<70)if((x2+x3)/2>70)grade='C';elsegrade='D';else if(x<50)grade='F';printf("%c\n",grade);}2.#include<stdio.h>main( ){int year;printf("please enter a year:\n");scanf("%d",&year);if(year%4==0&&year%100!=0||year%400==0)printf("%d is a leap year!\n",year);elseprintf("%d is a non-leap year!\n",year);}3.#include<stdio.h>main( ){i ntyearNow,monthNow,dayNow,year,month,day,x;printf("Please enter the date today(month day year):\n");scanf("%d%d%d",&monthNow,&dayNow,&yearNow);printf("Please enter your birthday(month day year):\n");s canf("%d%d%d",&month,&day,&year);if(monthNow<month)x=yearNow-year-1;else if(monthNow==month)if(dayNow<day)x=yearNow-year-1;elsex=yearNow-year;elsex=yearNow-year;printf("You are %d years old!\n",x);}4.#include<stdio.h>#include<stdlib.h>#include<time.h>main( ){int number,Ynumber;srand(time(NULL));number=rand()%20+1;printf("I am thinking of a number between 1 and 20.\n");printf("Can you guess what it is?");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{if(Ynumber>number)printf("Your guess is high.Try again:");elseprintf("Your guess is low.Try again:");scanf("%d",&Ynumber);if(Ynumber==number)printf("Congratulations!You did it.");else{printf("sorry.The number was %d.\n",number);printf("You should have gotten it by now.\n");printf("Better luck next time.");}}}}}}5.#include<stdio.h>main( ){int year,month,day,x=0,y,z;printf("Please enter your birthday:(year month day)\n");scanf("%d%d%d",&year,&month,&day);y=((year-1)*365+((year-1)/4)-((year-1)/100) +((year-1)/400))%7;switch(month){case 12:x=x+30;case 11:x=x+31;case 10:x=x+30;case 9:x=x+31;case 8:x=x+31;case 7:x=x+30;case 6:x=x+31;case 5:x=x+30;case 4:x=x+31;case 3:x=x+28;case 2:x=x+31;case 1:x=x+0;}if(year%4==0&&year%100!=0||year%400==0)day++;z=(x+day+y)%7;switch(z){case 0:printf("The day of your birthday is Sunday!");break;case 1:printf("The day of your birthday is Monday!");break;case 2:printf("The day of your birthday is Tuseday!");break;case 3:printf("The day of your birthday is Wednesday!");break;case 4:printf("The day of your birthday is Thursday!");break;case 5:printf("The day of your birthday is Friday!");break;case 6:printf("The day of your birthday is Saturday!");break;}}6.#include<stdio.h>#include<math.h>main( ){int a,b,c,p;float x1,x2;printf("Please enter a,b,c:\n");scanf("%d%d%d",&a,&b,&c);p=b*b-4*a*c;if(a==0)if(b==0)printf("There is no solution!");else{x1=-(float)c/b;printf("The solution is x1=%f",x1);}elseif(p<0)printf("There is no real solution!");else{x1=-b+sqrt(p)/(2*a);x2=-b-sqrt(p)/(2*a);printf("The solutions are x1=%f,x2=%f",x1,x2);}}。

C语言课后习题参考答案

C语言课后习题参考答案

C语言课后习题参考答案第一章:基础知识问题1:C语言的发展历史及特点C语言是由贝尔实验室的肯·汤普逊和丹尼斯·里奇在20世纪70年代初开发的一种高级程序设计语言。

它以其简洁、高效和可移植性而闻名。

C语言被广泛应用于系统级编程、嵌入式系统和科学计算等领域,并成为其他编程语言的基础。

问题2:C语言的基本数据类型及其存储范围C语言的基本数据类型包括字符型(char)、整型(int)、浮点型(float)、双精度浮点型(double)和空类型(void)。

它们的存储范围如下:- char:-128 到 127 或 0 到 255- int:-32,768 到 32,767 或 0 到 65,535- float:3.4E-38 到 3.4E38- double:1.7E-308 到 1.7E308- void:无特定存储范围问题3:如何在C语言中输出语句?在C语言中,可以使用printf函数来输出语句。

它的基本语法为:```cprintf("要输出的内容");```问题4:定义变量的语法规则在C语言中,定义变量的语法规则如下:```c数据类型变量名;```例如,定义一个整型变量x:```cint x;```问题5:如何进行变量的赋值?变量的赋值可以使用赋值运算符(=)。

例如,将一个整型常量10赋值给变量x:```cx = 10;```第二章:控制结构问题1:if语句的语法结构if语句用于根据条件执行不同的代码块。

它的基本语法结构如下:```cif (条件) {// 条件为真时执行的代码}```问题2:switch语句的用法及语法结构switch语句用于根据不同的表达式值执行相应的代码块。

它的基本用法及语法结构如下:```cswitch (表达式) {case 值1:// 当表达式的值等于值1时执行的代码break;case 值2:// 当表达式的值等于值2时执行的代码break;default:// 当表达式的值与所有case都不匹配时执行的代码break;}```问题3:循环语句的分类及语法结构C语言中的循环语句包括for循环、while循环和do-while循环。

C语言课后习题答案解析

C语言课后习题答案解析

C语言课后习题答案-第四版-第一章5、请参照本章例题,编写一个C语言程序,输出以下信息:****************************V e r y G o o d !****************************#include <stdio.h>int main ( ){printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}6、编写一个C语言程序,输入a,b,c三个值,输出其中最大值。

#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d%d%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d%d%d",&a,&b,&c);max=a>b?a:b;max=max>c?max:c;printf("The largest number is %d\n",max); return 0;}第3章1、假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比。

大学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.。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

第1章程序设计和C语言1什么是运算机程序1什么是运算机语言1语言的进展及其特点3最简单的C语言程序5最简单的C语言程序举例6语言程序的结构10运行C程序的步骤与方式12程序设计的任务141-5 #include <>int main ( ){ printf ("**************************\n\n");printf(" Very Good!\n\n");printf ("**************************\n");return 0;}1-6#include <>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}第2章算法——程序的灵魂16什么是算法16简单的算法举例17算法的特性21如何表示一个算法22用自然语言表示算法22用流程图表示算法22三种大体结构和改良的流程图26用N S流程图表示算法28 用伪代码表示算法31用运算机语言表示算法32结构化程序设计方式34习题36第章最简单的C程序设计——顺序程序设计37 顺序程序设计举例37数据的表现形式及其运算39常量和变量39数据类型42整型数据44字符型数据47浮点型数据49如何确信常量的类型51运算符和表达式52语句57语句的作用和分类57最大体的语句——赋值语句59数据的输入输出65输入输出举例65有关数据输入输出的概念67用printf函数输出数据68用scanf函数输入数据75字符数据的输入输出78习题823-1 #include <>#include <>int main(){float p,r,n;r=;n=10;p=pow(1+r,n);printf("p=%f\n",p);return 0;}3-2-1#include <>#include <>int main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=;r3=;r2=;r1=;r0=;p1=p*((1+r5)*5); #include <>#include <>int main(){float d=300000,p=6000,r=,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%\n",m);return 0;}3-4#include <>int main(){int c1,c2;c1=197;c2=198;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}3-5#include <>int main(){int a,b;float x,y;char c1,c2;scanf("a=%d b=%d",&a,&b);scanf("%f %e",&x,&y);scanf("%c%c",&c1,&c2);printf("a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n",a,b,x,y,c1,c2); return 0;}3-6#include <>int 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("passwor is %c%c%c%c%c\n",c1,c2,c3,c4,c5); return 0;}3-7#include <>int main (){float h,r,l,s,sq,vq,vz;float pi=;printf("请输入圆半径r,圆柱高h∶");scanf("%f,%f",&r,&h); #include <> int main(){ int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n" ,x,y);}else if(x<10) /* 1=<x<10 */{ y=2*x-1;printf("x=%d, y=2*x-1=%d\n",x,y);}else /* x>=10 */{ y=3*x-11;printf("x=%d, y=3*x-11=%d\n",x,y);}return 0;}4-7-1#include <>int main()int x,y;printf("enter x:");scanf("%d",&x);y=-1;if(x!=0)if(x>0)y=1;elsey=0;printf("x=%d,y=%d\n",x,y); return 0;}4-7-2#include <>int main(){int x,y;printf("please enter x:");scanf("%d",&x);y=0;if(x>=0)if(x>0) y=1;else y=-1;printf("x=%d,y=%d\n",x,y); return 0;}4-8#include <>int main(){ float score;char grade;printf("请输入学生成绩:");scanf("%f",&score);while (score>100||score<0){printf("\n 输入有误,请重输");scanf("%f",&score);}switch((int)(score/10)){case 10:case 9: grade='A';break;case 8: grade='B';break;case 7: grade='C';break;case 6: grade='D';break;case 5:case 4:case 3:case 2:case 1:case 0: grade='E';}printf("成绩是%,相应的品级是%c\n ",score,grade);return 0;}4-9#include <>#include <>int main(){int num,indiv,ten,hundred,thousand,ten_thousand,place; .=%d\n",sn); return 0;}5-6#include <>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%\n",s);return 0;}5-7#include <>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/{s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/{s3=s3+1/k;}printf("sum=%\n",s1+s2+s3);return 0;}5-8#include <>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9-1#define M 1000 /*概念寻觅范围*/#include <>int main(){int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是不是完数*/ {n=0; /* n用来累计a的因子的个数*/s=a; /* s用来寄存尚未求出的因子之和,开始时等于a */ for (i=1;i<a;i++) /* 检查i是不是a的因子*/if (a%i==0) /* 若是i是a的因子*/{n++; /* n加1,表示新找到一个因子*/s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和*/switch(n) /* 将找到的因子赋给k1...k9,或k10 */{case 1:k1=i; break; /* 找出的笫1个因子赋给k1 */case 2:k2=i; break; /* 找出的笫2个因子赋给k2 */case 3:k3=i; break; /* 找出的笫3个因子赋给k3 */case 4:k4=i; break; /* 找出的笫4个因子赋给k4 */case 5:k5=i; break; /* 找出的笫5个因子赋给k5 */case 6:k6=i; break; /* 找出的笫6个因子赋给k6 */case 7:k7=i; break; /* 找出的笫7个因子赋给k7 */case 8:k8=i; break; /* 找出的笫8个因子赋给k8 */case 9:k9=i; break; /*找出的笫9个因子赋给k9 */case 10:k10=i; break; /* 找出的笫10个因子赋给k10 */}}if (s==0){printf("%d ,Its factors are ",a);if (n>1) printf("%d,%d",k1,k2); /* n>1表示a至少有2个因子*/if (n>2) printf(",%d",k3); /* n>2表示至少有3个因子,故应再输出一个因子*/if (n>3) printf(",%d",k4); /* n>3表示至少有4个因子,故应再输出一个因子*/if (n>4) printf(",%d",k5); /* 以下类似*/if (n>5) printf(",%d",k6);if (n>6) printf(",%d",k7);if (n>7) printf(",%d",k8);if (n>8) printf(",%d",k9);if (n>9) printf(",%d",k10);printf("\n");}}return 0;}5-9-2#include <>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m);for (i=1;i<m;i++)if (m%i==0) printf("%d ",i);printf("\n");}}return 0;}5-10#include <>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%\n",s);return 0;}5-11#include <>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共通过的米数*/hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共通过%f米\n",sn);printf("第10次反弹%f米\n",hn);return 0;}5-12#include <>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <>#include <>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of % is %\n",a,x1);return 0;5-14#include <>#include <>int main(){double x1,x0,f,f1;x1=;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %\n",x1); return 0;}5-15#include <>#include <>int main(){float x0,x1,x2,fx0,fx1,fx2;do{printf("enter x1 & x2:");scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5);printf("x=%\n",x0);return 0;5-16#include <>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");}for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k);return 0;}第6章利用数组处置批量数据142如何概念和引用一维数组142如何概念一维数组143如何引用一维数组元素144一维数组的初始化145一维数组程序举例146如何概念和引用二维数组148 如何概念二维数组149如何引用二维数组的元素150 二维数组的初始化151二维数组程序举例152字符数组154如何概念字符数组154字符数组的初始化155如何引用字符数组中的元素155 字符串和字符串终止标志156 字符数组的输入输出159利用字符串处置函数161字符数组应用举例165习题1686-1#include <>#include <>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0;}printf("\n");for (i=2,n=0;i<=100;i++){ if(a[i]!=0){printf("%5d",a[i]);n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j;printf("array a:\n");for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:");scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n");for (i=0;i<11;i++)printf("%5d",a[i]);printf("\n");return 0;}6-5#include <>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) n",number);;printf("continu or not(Y/N)?");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1);gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++){if (text[i][j]>='A'&& text[i][j]<='Z')upp++;else if (text[i][j]>='a' && text[i][j]<='z')low++;else if (text[i][j]>='0' && text[i][j]<='9')dig++;else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}6-11#include <>int main(){ char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))tran[j]=219-ch[j];elsetran[j]=ch[j];j++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))ch[j]=219-ch[j];elsech[j]=ch[j];j++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1); return 0;}6-14#include <>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu);return 0;}6-15#include <>#include <>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}第7章用函数实现模块化程序设计170什么缘故要用函数170如何概念函数172什么缘故要概念函数172概念函数的方式173挪用函数174函数挪用的形式174函数调历时的数据传递175函数挪用的进程177函数的返回值178对被挪用函数的声明和函数原型179 函数的嵌套挪用182函数的递归挪用184数组作为函数参数192数组元素作函数实参193数组名作函数参数194多维数组名作函数参数197局部变量和全局变量199局部变量199全局变量200变量的存储方式和生存期204动态存储方式与静态存储方式204 局部变量的存储类别205全局变量的存储类别208存储类别小结212关于变量的声明和概念214内部函数和外部函数215内部函数215外部函数215习题2187-1-1#include <>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("",h);l=lcd(u,v,h);printf("",l);return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h) {return(u*v/h);}7-1-2#include <>int Hcf,Lcd;int main(){void hcf(int,int);void lcd(int,int);int u,v;scanf("%d,%d",&u,&v); hcf(u,v);lcd(u,v);printf("",Hcf);printf("",Lcd);return 0;}void hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}Hcf=v;}void lcd(int u,int v){Lcd=u*v/Hcf;7-2#include <>#include <>float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %*x*x+%*x+%=0\n",a,b,c); disc=b*b-4*a*c;printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q); }return 0;}void greater_than_zero(float a,float b){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b)p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n); elseprintf("%d is not a prime.\n",n); return 0;}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);}7-4#include <>#define N 3int array[N][N];int main(){ void convert(int array[][3]);int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]); printf("\noriginal array :\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}return 0;}void convert(int array[][3]){int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}#include <>#include <>int main(){void inverse(char str[]);char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str);return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j;for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7-8#include <>#include <>int main(){char str[80];void insert(char []);printf("input four digits:");scanf("%s",str);insert(str);return 0;}void insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}printf("output:\n%s\n",str);}7-9#include <>int letter,digit,space,others;int main(){void count(char []);char text[80];printf("input string:\n");gets(text);printf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n",letter,digit,space,others); return 0;}void count(char str[]){int i;for (i=0;str[i]!='\0';i++)if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z'))letter++;else if (str[i]>='0' && str [i]<='9')digit++;else if (str[i]==32)space++;elseothers++;}7-10#include <>#include <>int main(){int alphabetic(char);int longest(char []);int i;char line[100];printf("input one line:\n");gets(line);printf("The longest word is :");for (i=longest(line);alphabetic(line[i]);i++)printf("%c",line[i]);printf("\n");return 0;}int alphabetic(char c){if ((c>='a' && c<='z')||(c>='A'&&c<='z'))return(1);elsereturn(0);}int longest(char string[]){int len=0,i,length=0,flag=1,place=0,point; for (i=0;i<=strlen(string);i++)if (alphabetic(string[i]))if (flag){point=i;flag=0;}elselen++;else{flag=1;if (len>=length){length=len;place=point;len=0;}}return(place);}7-11#include <>#include <>#define N 10char str[N];int main(){void sort(char []);int i,flag;for (flag=1;flag==1;){printf("input string:\n");scanf("%s",&str);if (strlen(str)>N)printf("string too long,input again!");elseflag=0;}sort(str);printf("string sorted:\n");for (i=0;i<N;i++)printf("%c",str[i]);printf("\n");return 0;}void sort(char str[]){int i,j;char t;for(j=1;j<N;j++)for (i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7-12#include <>#include <>int main(){float solut(float a,float b,float c,float d); float a,b,c,d;printf("input a,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d); printf("x=%\n",solut(a,b,c,d));return 0;}float solut(float a,float b,float c,float d) {float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-3);return(x);}7-13#include <>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();void input_stu(void);void aver_stu(void);void aver_cour(void);input_stu();aver_stu();aver_cour();printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n"); for(i=0;i<N;i++){printf("\n NO %2d ",i+1);for(j=0;j<M;j++)printf("%",score[i][j]);printf("%\n",a_stu[i]);}printf("\naverage:");for (j=0;j<M;j++)printf("%",a_cour[j]);printf("\n");h=highest();printf("highest:% NO. %2d course %2d\n",h,r,c);printf("variance %\n",s_var());return 0;}void input_stu(void){int i,j;for (i=0;i<N;i++){printf("\ninput score of student%2d:\n",i+1);for (j=0;j<M;j++)scanf("%f",&score[i][j]);}}void aver_stu(void){int i,j;float s;for (i=0;i<N;i++){for (j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/;}}void aver_cour(void){int i,j;float s;for (j=0;j<M;j++){s=0;for (i=0;i<N;i++)s+=score[i][j];a_cour[j]=s/(float)N;}}float highest(){float high;int i,j;high=score[0][0];for (i=0;i<N;i++)for (j=0;j<M;j++)if (score[i][j]>high){high=score[i][j];r=i+1;c=j+1;}return(high);}float s_var(void){int i;float sumx,sumxn;sumx=;sumxn=;for (i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }7-*14#include <>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();void input_stu(void);void aver_stu(void);void aver_cour(void);input_stu();aver_stu();aver_cour();printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n"); for(i=0;i<N;i++){printf("\n NO %2d ",i+1);for(j=0;j<M;j++)printf("%",score[i][j]);printf("%\n",a_stu[i]);}printf("\naverage:");for (j=0;j<M;j++)printf("%",a_cour[j]);printf("\n");h=highest();printf("highest:% NO. %2d course %2d\n",h,r,c);printf("variance %\n",s_var());return 0;}void input_stu(void){int i,j;for (i=0;i<N;i++){printf("\ninput score of student%2d:\n",i+1);for (j=0;j<M;j++)scanf("%f",&score[i][j]);}}void aver_stu(void){int i,j;float s;for (i=0;i<N;i++){for (j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/;}}void aver_cour(void) {int i,j;float s;for (j=0;j<M;j++){s=0;for (i=0;i<N;i++)s+=score[i][j];a_cour[j]=s/(float)N;}}float highest(){float high;int i,j;high=score[0][0];for (i=0;i<N;i++)for (j=0;j<M;j++)if (score[i][j]>high){high=score[i][j];r=i+1;c=j+1;}return(high);}float s_var(void){int i;float sumx,sumxn; sumx=;sumxn=;for (i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N));}7-15#include <>#include <>#define N 10int main(){void input(int [],char name[][8]);void sort(int [],char name[][8]);void search(int ,int [],char name[][8]);int num[N],number,flag=1,c;char name[N][8];input(num,name);sort(num,name);while (flag==1){printf("\ninput number to look for:");scanf("%d",&number);search(number,num,name);printf("continue ot not(Y/N)?");getchar();c=getchar();if (c=='N'||c=='n')flag=0;}return 0;}void input(int num[],char name[N][8]){int i;for (i=0;i<N;i++){printf("input NO.: ");scanf("%d",&num[i]);printf("input name: ");getchar();gets(name[i]);}}void sort(int num[],char name[N][8])。

C语言课后习题答案

C语言课后习题答案

C语言课后习题答案以下是C语言课后习题的答案:1. 编写程序,将输入的两个整数进行相加并输出结果。

```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入第一个整数:");scanf("%d", &num1);printf("请输入第二个整数:");scanf("%d", &num2);sum = num1 + num2;printf("两个整数的和为:%d\n", sum);return 0;}```2. 编写程序,判断输入的一个整数是否为奇数或偶数,并输出结果。

```c#include <stdio.h>int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if(num % 2 == 0) {printf("%d是偶数。

\n", num);} else {printf("%d是奇数。

\n", num);}return 0;}```3. 编写程序,计算输入数字的阶乘并输出结果。

```c#include <stdio.h>int main() {int num, factorial = 1, i;printf("请输入一个正整数:");scanf("%d", &num);if (num < 0) {printf("错误!请输入一个非负数。

\n");} else {for (i = 1; i <= num; ++i) {factorial *= i;}printf("%d的阶乘为:%d\n", num, factorial);}return 0;}```4. 编写程序,判断输入的年份是否为闰年,并输出结果。

(完整版)C语言课后答案

(完整版)C语言课后答案

第3章三、编程题1.编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。

#include "stdio.h"#define PI 3.1415void main(){float r,area,circumference;scanf("%f",&r);area=PI*r*r;circumference=2*r*PI;printf("area=%6.2f\ncircumference=%6.2f\n",area,circumference);}2.编写程序,输出下面结果,注意,双引号也要输出:“I'm a student!”#include <stdio.h>void main(){printf("\"I\'m a student!\"\n");}3.编写程序,输入一个小写字母,将其转换为大写字母输出。

例如输入b ,则输出B 。

提示:小写字母和对应的大写字母的ASCII 码值相差32。

void main(){char ch;ch=getchar();ch-=32;putchar(ch);//printf("%c",ch);}4.编写程序,输入一个华氏温度f ,输出其相应的摄氏温度c 。

华氏温度和摄氏温度的转换公式为:)32f (95c -=#include <stdio.h>void main(){float f,c;scanf(“%f”,&f);c=5.0*(f-32)/9;printf(“华氏温度%5.2f 转换为摄氏温度为:%5.2f\n”,f,c);}第4章三、编程题1.输入一个整数,判断这个整数是奇数还是偶数(提示:整数的奇偶性可以利用取余运算符%判定)。

#include <stdio.h>void main(){int a;scanf("%d",&a);if(a%2)printf("奇数\n");elseprintf("偶数\n");}2.编写程序,输入一个24小时制的时间,转换为12小时制时间后进行输出。

C语言课后习题参考答案

C语言课后习题参考答案

C语言课后习题答案第一章习题答案一、选择题1~5:BDCDA 6~10:DABBB 11~12:CC二、填空题1、main()2、函数首部,函数体3、函数4、编辑、编译、连接、运行5、.cpp、.obj、 .exe6、; 或分号三、编程题#include <stdio.h> /* 包含标准库的信息*/void main() /* 定义名为main 的函数,它不接受参数值*/ { /* main函数的语句都被括在花括号中*/ printf("hello, world\n");/* main 函数调用库函数printf 以显示字符序列,其中\n代表换行符*/ }第二章习题答案一、选择题1~5:CBABB 6~10:CDCDD 11~15:CADBC 16~20:BDAAD二、填空题1、整型,实型,字符型,枚举类型2、13、94、123535145、2,16、2,27、10 20 08、a=149、2,3,110、double第三章习题答案一、选择题1~5:CBBBC 6~10:DDDBB二、填空题1、控制语句,表达式语句,复合语句2、;3、{}4、15、a6、c:dec=120,oct=170,hex=78,ASCII=x7、32767,327678、10,A,109、3 310、(1) 123.456001(2) □□□□□□□ 123.456(3) 123.4560(4) 8765.456700(5) □□□□□□ 8765.457(6) 8765.4567(7) 8765.4567三、编程题1、参考答案:#include <stdio.h>void main(){int a,b,c;long int u,n;float x,y,z;char c1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf("\n");printf("a=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c' or %d(ASCII)\n",c1,c1);printf("c2='%c' or %d(ASCII)\n",c2,c2);}2、参考答案:#include <stdio.h>void main(){float a, b, c, t;printf("please input a,b,c:\n");scanf ("%f, %f, %f", &a, &b, &c);t=(a+b+c)/3;printf ("average of %6.2f、%6.2f and %6.2f is %6.2f\n",a,b,c,t); }3、参考答案:#include <stdio.h>main(){int R, d;float s;printf("请输入圆的半径");scanf("%d",&R);d=2*R;c=2*3.14159*R;printf("d=%d,c=%f\n",d,c);}4、参考答案:#include <stdio.h>void main(){int h, f, x, y; /* x为鸡的数量,y为兔的数量*/printf("请输入鸡兔的总头数h,总脚数f:");scanf("%d%d", &h, &f);x=(4*h-f)/2;y=(f-2*h)/2;printf("笼中有鸡%d 只,有兔%d只。

(完整word版)C语言课后习题答案(最终)

(完整word版)C语言课后习题答案(最终)

第0章习题1. 将下列十进制数分别转化为二进制数、八进制数和十六进制数:(1)128 (2)511 (3)1024 (4)65535 (5)1048575答:(1)10000000、200、80(2)111111111、777、1FF(3)10000000000、2000、400(4)1111111111111111、177777、FFFF(5)11111111111111111111、3777777、FFFFF2. 将下列二进制数转化为十进制数和十六进制数:(1)1100110101B (2)101101.1011B答:(1)821、335(2)45.6875、2D.B3。

写出下列数的原码、反码、补码:15、-20、—27/32答:(1)00001111、00000000、00001111(2)10010100、11101011、11101100(3)1。

1101100、1。

0010011、1.00101004. 16位无符号定点整数的数值表示范围为多少?8位补码的表示范围是多少?16位补码的表示范围是多少?答:0~65535、—128~127、—32768~327675.1968年Dijkstra提出结构化程序设计的思想的原因是什么?简要回答结构化程序设计的经典定义.答:结构化程序设计概念的提出主要是源于程序结构的层次性与模块化使得构造出来的软件具有良好的可理解性和可维护性,随着软件规模的扩大与复杂性的提高,程序的可维护性成为程序设计者们关注的重要问题之一。

如果一个程序的代码块仅仅通过顺序、选择和循环这3种基本控制结构进行连接,并且每个代码块只有一个入口和一个出口,则称这个程序是结构化的。

6.C程序在内存中存储在哪儿?计算机的内存空间是如何分区的?分区存放不同类型的数据的目的是什么?答:C语言程序属于应用程序,程序代码本身存放在应用程序区,程序运行时处理的数据存放在应用程序数据区。

计算机的内存空间主要分为3个区:系统程序区、应用程序区和数据区,其中数据区又分为系统程序数据区和应用程序数据区两类。

C语言教材课后习题参考答案.docx

C语言教材课后习题参考答案.docx
printf( “\n”);
}
}
}
4、
main()
{int i, a, min, max;
scanf(“%d" , &a);
min=a;max=a;
for(i=2;i<=100;i++)
{ scanf(“,&a);
if (a<min) min=a;
if (a>max) inax=a;
}
printf (<<Max=%d, Min=%d\nv, max, min);
3、存储单元、符号地址、内存地址
4、十、十六、八
5、double(双精度型)
6、8
7、5.500000
8、a二-32768
9、+0017,021,0x11
三、写程序运行结果
3257
32 57
7.8& -345.12,7. 8765,-345.1230
7.87654e+00,-3. 5e+02
a,97, 141,61
main()
{int x, y;
for(x=20;x<=100;x++)
for (y=20;y<=100;y++)
if(x*x+y二二1053&&x+y*y二二873)
printf (i(x=%dy=%d\n” , x, y);
}
7、
main()
{
long int k, g, s=0;
printf ("Please input an integer:,z);
Printf(is not a quadratic);

(完整版)C语言课后题编程标准答案

(完整版)C语言课后题编程标准答案

C语言课后题编程答案所有章节的课后习题的选择题和填空题大家必须熟练掌握,编程题掌握到第九章即可。

希望大家通过课后的编程题熟悉典型的编程算法,掌握基本的编程思路,注意编程细节。

第3章3-17 编写程序,把560分钟换算成用小时和分钟表示,然后进行输出。

#include<stdio.h>main(){int h,m。

h=560/60。

m=560%60。

printf(“560分钟可转换成%d小时%d分钟”,h,m)。

}程序总结:(1)只要在程序中用到系统提供的库函数,必须把库函数所在的头文件用#include命令包含进来。

否则库函数的使用无效。

输入输出库函数的头文件是:stdio.h。

数学函数的头文件是:math.h。

字符处理函数的头文件是:ctype.h。

字符串处理函数的头文件是:string.h。

(2)注意C语言中的“/”和“%”两种运算符。

“/”的运算结果取决于两操作数的类型。

比如:1/2=0(与数学中不同,结果与操作数的类型一致,所以结果只取商值),1.0/2=0.5(除之前2要自动类型转换成 2.0,因为只有同类型的操作数才能进行除运算), 1/2.0=0.5, 1.0/2.0=0.5。

“%”的两个操作数都必须是整数。

(3)printf(“560分钟可转换成%d小时%d分钟”,h,m)。

printf第一个参数要尽量详细,这样可以提高输出结果的可读性,恰当好处的添加提示性信息,可以提高程序的质量。

(4)int h,m。

变量起名要有艺术性,尽量做到见名知意。

3-18 编写程序,输入两个整数,1500和350,求出它们的商和余数并进行输出。

#include<stdio.h>main(){int a,b,m,n。

printf(“请输入两个整数:\n”)。

scanf(“%d%d”,&a,&b)。

m=a/b。

n=a%b。

printf(“%d除以%d商%d余%d”,a,b,m,n)。

c语言课后习题答案

c语言课后习题答案

c语言课后习题答案C语言课后习题答案一、选择题1. C语言中,用于定义变量的关键字是( A )。

A. intB. floatC. doubleD. char2. 下列选项中,哪一个是C语言的合法标识符?( C )A. 2variableB. variable2C. _variable2D. variable#23. 在C语言中,用于实现条件判断的关键字是( B )。

A. ifB. switchC. caseD. default二、填空题1. C语言中,一个整型变量(int)通常占用的字节数是______。

答案:4(在32位系统中)2. 在C语言中,若要声明一个字符数组,可以使用以下语法:______。

答案:char arrayName[size];3. 当需要在C语言中进行文件操作时,通常使用包含的头文件是______。

答案:<stdio.h>三、简答题1. 请简述C语言中数组的定义和初始化方法。

答案:在C语言中,数组是一种基本的数据结构,用于存储具有相同类型的元素集合。

数组的定义格式为:类型名数组名[元素个数];初始化可以使用花括号将初始值包围起来,如:int arr[5] = {1, 2, 3, 4, 5}。

2. 描述C语言中函数的定义和调用过程。

答案:函数是C语言中实现代码复用的一种方式。

函数定义包括返回类型、函数名、参数列表和函数体。

例如:int add(int a, int b) { return a + b; } 函数调用则是在代码中使用函数名和参数列表来执行函数,如:int result = add(3, 5);四、编程题1. 编写一个C语言程序,实现两个整数的加法运算,并输出结果。

```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两个整数的和是:%d\n", sum);return 0;}```2. 编写一个C语言程序,实现字符串的复制功能。

C语言课后习题答案

C语言课后习题答案

C语言课后习题答案第1章 C语言概述一、选择题答案: 1.A 6.B 二、填空题答案: 1.函数一、选择题答案: 1.B 6.C 11.B 16.C2.D 7.C 12.B 17.B3.D 4.B 8.B 9.A 13.D 14.D 18.D 19.A5.D 10.D 15.A 20.C2.目标3.编译程序4.分号2.B 7.D3.C 8.B4.C5.D第2章数据类型、运算符和表达式二、填空题答案: 1.2 度6.307.4,2一、选择题答案: 1.C 6.A 二、填空题答案:1.scanf printf 2.顺序、选择、循环 3.1 4.3 5.9,10 6.10,20 7.213 8.空格、Tab键、回车键2.C 7.D3.C 4.A 8.D 9.C5.A 10.D8.42.6,4,23.184.5 5.double或双精第3章顺序结构第4章选择结构一、选择题答案:1.B 6.C2.D 7.C3.D 8.B4.B 9.B5.B二、程序改错题1.输入两个实数,在fun()函数中按数值由小到大输出它们。

(保留2位小数)答案:=======(答案1)======= float t;=======(答案2)======= if(a>b)=======(答案3)=======printf(\2.编写一个程序计算某年某月有几天。

(注意要区分闰年)答案:=======(答案1)======= switch(mm)=======(答案2)======= break;=======(答案3)======= default : 三、程序填空题1.功能:输出a、b、c三个变量中的最小值。

答案:=======(答案1)======= &c=======(答案2)======= c:t1四、程序设计题1.功能:对任意输入的x,用下式计算并输出 y 的值。

说明:因为关于函数调用的知识在第7章讲解,所以在现阶段本程序的设计可以改用一个主函数来完成。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

e 1
1 1 1 1! 2! n!
int main() { int n,i,j,s=0;
int main() { int n=1; float e=1,t=1,s=1.0;
printf("请输入 n :\n"); scanf("%d",&n); while(fabs(t)>=1e-6) { for(i=1;i<=n;i++) for(j=1;j<=i;j++) s+=j; e+=t; n++; s*=n; t=1/s; printf("s=%d\n",s); }
if(ge%2==1) sum+=ge*ge; if(shi%2==1) sum+=shi*shi; if(bai%2==1) sum+=bai*bai;
}
return 0;
C 语言程序设计 课后习题 参考程序 }
由宋飞、张斌编写 习题 5.7
(西安邮电学院)
第5页
编写一个程序,求 e 的近似值,当通项小于 10-7 时停止 习题 5.4 编写一个程序,求 s=1+(1+2)+(1+2+3)+ …… +(1+2+3+4+ …… +n) 的值。 #include <stdio.h> #include <stdio.h> #include <math.h> 计算。
第一章 概述
习题 1.5 编写一个 C 语言程序,要求输出以下信息: *************** How are you ! *************** }
/* 输出 */ printf(“最小的数:%d\n”,min);
return 0;
#include <stdio.h>
第三章 算法和基本程序设计
#include <stdio.h>
int main() { char c;
int main()
C 语言程序设计 课后习题 参考程序 { double x,y; }
由宋飞、张斌编写 return 0;
(西安邮电学院)
第3页
printf("请输入 x:"); scanf("%lf",&x);
习题 4.5 计算器程序。用户输入运算数和四则运算符,输出运算结 果。
由宋飞、张斌编写
(西安邮电学院)
第2页
printf("请输入一个字母:"); scanf("%c",&c);
printf("转换后:%c\n",c+32);
printf(“请输入 a,b,c,d :”); scanf(“%d,%d,%d,%d”,&a,&b,&c,&d); }
return 0;
average =(a+b+c+d)/4.0;
/* 判断最小值 */ min=x; if(y<min)min=y; if(z<min)min=z;
习题 3.5 编写程序,输入四个数,并求出它们的平均值。
#include <stdio.h>
C 语言程序设计 课后习题 参考程序 int main() { int a,b,c,d; float average;
习题 3.4 编写程序,输入三角形的 3 个边长 a,b,c,求三角形的面 积 area。
int main() { printf(“***************\n”); printf(“How are you !\n”); printf(“***************\n”);
area S ( S a)( S b)( S c)
printf(“请输入运算符(+ - * /):”); scanf(“%c”,&t);
printf(“请输入 a,b:”); scanf(“%d,%d”,&a,&b);
printf("y=%lf\n",y); switch (t) return 0; } { case‘+’ : res=a+b;break; case‘-‘ : res=a-b;break; 习题 4.4 用条件表达式的嵌套来完成此题:学习成绩≥90 分的同 学用 A 表示,70~89 分之间的用 B 表示,60~69 分之间用 C 表示,60 分以下的用 D 表示。 printf(“结果:%f”,res); #include <stdio.h> return 0; int main() { int mark; char res; 习题 4.6 企业发放的奖金根据利润提成。利润(I)低于或等于 10 万元时, 奖金可提成 10%; 利润高于 10 万元, 低于 20 万元时, printf(“请输入成绩:”); scanf(“%d”,&mark); 低于 10 万元的部分按 10%提成,高于 10 万元的部分,可提成 7.5%;20 万到 40 万之间时,高于 20 万的部分,可提成 5%; 40 万到 60 万之间时,高于 40 万的部分,可提成 3%;60 万 if(mark>=90) res=’A’; else if(mark>=70) res=’B’; else if(mark>=60) res=’C’; else res=’D’; #include <stdio.h> printf(“结果 : %c\n”,res); int main() 到 100 万之间时,高于 60 万的部分可提成 1.5%,高于 100 万时,超过 100 万的部分按 1%提成。从键盘输入当月的利润, 求应发放的奖金总数。 } } case‘*’ : res=a*b;break; case‘/’ : res=(float)a/b;break;
其中,S=(a+b+c)/2。
return 0; }
#include <math.h> #include <stdio.h>
习题 1.6 编写一个 C 语言程序, 从键盘输入 x,y,z 三个变量, 并输 出其中的最小值。
int main() { float a,b,c,area,s;
#include <stdio.h>
int main() { int a,b,c,d; int t; /* 临时变量 */
int main() { int x,y,z,num;
printf(“请输入 a,b,c,d:”); scanf(“%d,%d,%d,%d”,&a,&b,&c,&d);
if(a>b) {t=a;a=b;b=t;} /*数据交换方法 */ printf("请输入一个数:"); scanf("%d",&num); if(a>c) {t=a;a=c;c=t;} if(a>d) {t=a;a=d;d=t;} if(b>c) {t=b;b=c;c=t;} /* 分离个位,十位,百位的方法 */ x=num%10; y=(num/10)%10; z=(num/100)%10; printf(“结果:%d,%d,%d,%d\n”,a,b,c,d); if(b>d) {t=b;b=d;d=t;} if(c>d) {t=c;c=d;d=t;}
第四章 选择分支结构程序设计
习题 4.2 输入四个数 a、b、c、d,把这四个置由小到大输出。
printf(“平均值: %f\n”,average);
return 0; } #include <stdio.h>
习题 3.6 从键盘上输入一个整数,分别输出它的个位数,十位数和 百位数。 #include <stdio.h>
C 语言程序设计 课后习题 参考程序 { long i,sum;
由宋飞、张斌编写
(西安邮电学院)
第4页
if(qian%2==1) sum+=qian*qian; if(wan%2==1) sum+=wan*wan; printf("总合是 %d\n",sum); printf("请输入利润 I:"); scanf("%ld",&i); } if(i<100000) sum=i*0.1; else if(i<200000) sum=10000+(i-100000)*0.075; else if(i<400000) sum=17500+(i-200000)*0.05; else if(i<600000) sum=27500+(i-400000)*0.03; else if(i<1000000) sum=33500+(i-600000)*0.015; else sum=39500+(i-1000000)*0.01; printf("请输入一个数:\n"); scanf("%d",&n); printf("奖金总数:%ld\n",sum); if(n>=0) printf("X 是正数\n"); return 0; else printf("X 是负数\n"); if(n%2==1) printf("X 是奇数\n"); int main() { int n,data; #include <stdio.h> 习题 5.2 编写程序,判断从键盘输入的整数 X 的正负性和奇偶性。 return 0;
相关文档
最新文档