C语言程序设计教程答案 杨路明 郭浩志.doc_第1页
C语言程序设计教程答案 杨路明 郭浩志.doc_第2页
C语言程序设计教程答案 杨路明 郭浩志.doc_第3页
C语言程序设计教程答案 杨路明 郭浩志.doc_第4页
C语言程序设计教程答案 杨路明 郭浩志.doc_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C语言程序设计教程习题答案总主编 杨路明主审 郭浩志文件摘自 CSDN习题11、算法描述主要是用两种基本方法:第一是自然语言描述,第二是使用专用工具进行算法描述2、c语言程序的结构如下: 1、c语言程序由函数组成,每个程序必须具有一个main函数作为程序的主控函数。 2、/*与*/之间的内容构成c语言程序的注释部分。 3、用预处理命令#include可以包含有关文件的信息。 4、大小写字母在c语言中是有区别的。 5、除main函数和标准库函数以外,用户可以自己编写函数,程序一般由多个函数组成,这些函数制定实际所需要做的工作。 例如: void main() int a,b,c,s; a=8;b=12;c=6; s=a+b*c; printf(s=%dn,s); 3、c语言的特点: 1、c语言具有结构语言的特点,程序之间很容易实现段的共享; 2、c语言的主要结构成分为函数,函数可以在程序中被定义完成独立的任务,独立地编译成代码,以实现程序的模块化。 3、c语言运算符丰富,运算包含的范围很广; 4、c语言数据类型丰富。 5、c语言允许直接访问物理地址,即可直接对硬件进行操作,实现汇编语言的大部分功能; 6、c语言语法限制不太严格,程序设计自由度大,这样是c语言能够减少对程序员的束缚; 7、用c语言编程,生成的目标代码质量高,程序执行效率高,可移植性好;4、合法标识符:AB12、leed_3、EF3_3、_762、PAS、XYZ43K2 不合法标识符:a*b2、8stu、D.K.Jon、if、ave#xy、#_DT5、C.D5、F2:将当前编辑器中文件存盘 F10:调用主菜单 F4:程序运行到光标所在行 Ctrl+F9:当前编辑环境下,进行编译、连接且运行程序; Alt+F5:将窗口切换到DOS下,查看程序运行结果6、 (1): * welcome you very good * (2): please input three number;5,7,8 max number is:87、main8、User screen、Alt+F59、标识符必须是字母或下划线开头,大小写字母含义不同。由数字、字母和下划线组成; 关键字是一种语言中规定具有特定含义的标识符。关键字不能作为变量或函数名来使用,用户只能根据系统的规定使用它们。10、选择主菜单File项下拉子菜单中Save项或直接按F2键存盘。习题21、符合C语法规定的常数为:0x1e、abn、1.e52、(1): 错误如下:int x,y=5,z=5,aver; x=7; aver = (x+y+x)/3; 结果如下:AVER=5 (2): 错误如下:char c1=a,c2=b,c3=c; printf(a=%db=%cendn,a,b); 结果如下:a=3b=Aend aabcc abc3、4、(1):9,11,9,10 (2):3,1,0,0 (3):11,19,31,15、(1):0 (2):0 (3):9.500000 (4):90 (5):10 (6):10 (7):65 (8):4 (9):4.500000 (10):1 (11):0 (12):20 (13):06、(5)7、求x的绝对值8、c(max=ab?a:b)?c:max;9、B10、D习题31、输入函数scanf的参数错误,应该为:scanf(%f,&k);2、|1234 1234 |3、ff104、1,3,15、原字符串 左边加空格再加字符串本省,字符个数总和为5个6、scanf(%d,%d,%c,%c,&a1,&a2,&c1,&c2);7、printf(a+b=%dn,a+b); printf(a-b=%dn,a-b); printf(a*b=%dn,a*b); printf(a/b=%dn,a/b); printf(float)a/b=%fn,(float)a/b); printf(a%b=%dn,a%b);8、void main() float r; float s,c; printf(please input the number:); scanf(%f,&r); if(r=0) s = 3.14*r*r; c = 2*3.14*r; printf(s = %f, c = %fn,s,c); elseprintf(you input number is error!); 9、void main() int n; printf(please input the number:); scanf(%d,&n); if(n=100 & n c | a+c b | b+c a) s = (a+b+c)/2; Area = sqrt(s*(s-a)*(s-b)*(s-c); printf(%fn,Area); else printf(you input the number is error!n);习题41:02:203: (x20)|(x-100)4:*a=25,b=14,c=16*5:376:if(a=b) printf(1);else printf(2);7、#includevoid main()char a,b,t1,t2;scanf(%c,%c,&a,&b);t1=ab?a:b;t2=ab?a:b;if(t1-t2)%2=0)printf(%c,%c,a+1,b+1);elseprintf(%c,%c,a-1,b-1);getch();8、#includevoid main()int temp1=0,temp2=0,x,y,i=1;printf(Please input (x,y): );scanf(%d,%d,&x,&y);while(i*y) shang=%d,yushu=%d,x,y,temp2,x-y*temp2);getch();9、#includevoid main()float x,y,m=0,n=0;scanf(%f,%f,&x,&y);n=(x-2)*(x-2);m=(y-2)*(y-2);if(m+n)=1)printf(%.3f,%.3f)In the yuan,x,y);elseprintf(%.3f,%.3f)out of the yuan,x,y);getch();10、#includevoid main()int temp=0,month,year;printf(Please input (year,month): );scanf(%d,%d,&year,&month);if(year%400=0)|(year%4=0&year%100!=0)temp=1;if(month=2)if(temp)printf(%d year %d month have 29 ,year,month);else printf(%d year %d month have 28 ,year,month);else if(month%2=0)printf(%d year %d month have 30 ,year,month);else printf(%d year %d month have 31 ,year,month);getch();11、switch(a/10)case 5:m=4;break;case 4:m=3;break;case 3:m=2;break;case 2:m=1;break;default:m=5; 12、方法一:#includevoid main()int x,y;scanf(%d,&x);if(x-5)y=x-1;else if(x=0)y=x;else if(x0&x8)y=x+1;printf(%d,y);getch();方法二:#includevoid main()int x,y;scanf(%d,&x);if(x-5)if(x=0)y=x;else if(x0&x8) y=y=x+1;else y=x-1;printf(%d,y);else printf(Input error!);getch();方法三:#includevoid main()int x,y,i;scanf(%d,&x);if(x-5)if(x=0)i=1;else if(x0&x8) i=2;else i=3;else i=4;switch(i)case 1:y=x;printf(%d,y);break;case 2:y=x+1;printf(%d,y);break;case 3:y=x-1;printf(%d,y);break;case 4:printf(Input error!);break;getch();习题51、void main() int n,value;int i,count=0;float average = 0;long int sum = 0;scanf(%d,&n);for(i = 0; i = A & ch = a & ch = 0 & ch = 9)sz+;ch = getchar();printf(zm = %d ; sz = %dn,zm,sz); 3、 void main() long i_value;int sum = 0;int temp = 0;scanf(%ld,&i_value);if(i_value = A & ch = a & ch 500)break;printf(the %d years later complete!n,count); 6、 void main() int i,temp=0,bit=0;for(i = 1 ; i = 999; i+)if(i%3 = 0)temp = i;for(;temp != 0;)bit = temp%10;if(bit = 5)printf(%d,i);break;temp = temp/10;printf(b n); 7、#include int main(void) int steps = 29, i = 1; while ( steps % 7 ) steps = (30 * +i) - 1; printf(Total steps: %dn, steps); return 0;8、main() int i,j,k,n; printf(the narcissus number is:n); for(n=100;n1000;n+) i=n/100; j=n/10-i*10; k=n%10; if(i*i*i+j*j*j+k*k*k=n) printf(%dn,n); 9、main() float i,j,temp,n,sum; int t; i=2;j=1;sum=0; scanf(%d,&t); for(n=1;n=t;n+) sum=sum+i/j; temp=i;i=i+j;j=temp; printf(2/1+3/2+5/3+8/5+13/8+.=%fn,sum);10、void main() int nWidth,a,b; scanf(%d,&nWidth); for(a=(nWidth%2);a=nWidth;a+=2) for(b=0;b=nleft&bnright) chOut =*; printf(%c,chOut); printf(n); 11、void main() int i=1,j=1; for(i=1;i=9;i+) for(j=1;j=i;j+) printf(%d*%d=%d,i,j,i*j); printf(n) 12、#include stdio.hvoid main() char c; /count1是正数的个数 /count2是负数的个数 int count1=0,count2=0; /sum1是正数之和 /sum2是负数之和 int sum1=0,sum2=0; int flage = 0; c=getchar(); while(c != *) if(c = -)flage = 1; if(flage = 0) sum1+=(c-48);count1+; else if(c != -)sum2-=(c-48);flage=0;count2+; c = getchar(); printf(%d,%dn,count1,count2); printf(%f,%fn,sum1/count1,sum2/count2);13、void main() int i,j; int s = 0; for(i = 100 ; i = 999; i+) s = 0;for(j=1; j i-1 ; j+)if( i%j = 0 ) s = s + j;if(s = i)printf(%5d,i);14、#include #include void main()int n;long k = 1;float e = 1;n = 1;clrscr();while ( fabs(1.0/k) = 0.000001)n+;e = e+ 1.0/k;k = k * n;printf(%fn,e);15、#include math.hmain()float x0,x1,x2,f0,f1,f2; x1=-10; f1=2*x1*x1*x1-4*x1*x1+3*x1; x2=10; f2=2*x2*x2*x2-4*x2*x2+3*x2; do x0=(x1+x2)/2; f0=2*x0*x0*x0-4*x0*x0+3*x0; if(f0*f1)1e-6); printf(_2*x*x*x-4*x*x+3*x=0_n); printf(the root is %fn,x0);习题61、#includeint divisor(int a,int b)int r;while(r=a%b)!=0)a=b;b=r;return b;int multiple(int a,int b)int d;d=divisor(a,b);return a*b/d;void main()int a,b,c,d;printf(intput (a,b): );scanf(%d,%d,&a,&b);c=divisor(a,b);d=multiple(a,b);printf(ndivisor=%dttmultiple=%d,c,d);2、#includevoid tongji(char a)int b3=0,0,0,i=0;while(ai!=0)if(ai=65)|(ai=97)b0+;else if(ai=48) b1+;elseb2+;i+;printf(zimu have: %d; shuzi have: %d; qita have: %d,b0,b1,b2);getch();void main()char a100;printf(Please input a string: );gets(a);tongji(a);3、#includeint flower(int n)int x=0,i,j,k;i=(n%10); j= (n/10%10);k=(n/100);x=i*i*i+j*j*j+k*k*k;if(x=n) return 1;else return 0;void main()int i,n;printf(Please intput n: );scanf(%d,&n);if(n999|n100)printf(Input error!);elsefor(i=100;in;i+)if(flower(i)printf(%d ,i);getch();4、#include#define SWAP(a,b) t=b;b=a;a=t;main()float x,y,t;printf(Enter two number (x,y): );scanf(%f,%f,&x,&y);SWAP(x,y);printf(nnExchanged:x=%f,y=%f,x,y);getch();5、#includeint fib(int n)int p;if(n=0) p=0;else if(n=1) p=1;else p=fib(n-1)+fib(n-2);return p;void main()int n;printf(Please input fib: );scanf(%d,&n);printf(nnfibonacci=%d,fib(n);6、#includelong fac(int n)long f;if(n=0)f=1;elsef=n*fac(n-1);return f;void main()int m,n;long f;printf(Please input (m,n): );scanf(%ld,%ld,&m,&n);f=fac(n+m)+fac(n);printf(nnfibonacci=%ld,f);7、#includevoid list()int i,j;for(i=1;i=9;i+) for(j=1;j=i;j+)printf(%d*%d=%d ,i,j,i*j); printf(n);void main()list();8、#includeint he(int n)int i,s=0;for(i=1;in;i+) if(n%i=0)s+=i;return s;void main()int i,j;for(i=1;i30000;i+)for(j=1;j30000;j+)if(he(i)=j)&(he(j)=i)&(i!=j) printf(%d%d ,i,j);9、#include#define max 100struct worklong sid;char name15;workermax;int size=0;struct work *set_list()doprintf(nnPlease input (sid,name): );scanf(%ld,%s,&workersize.sid,);size+;while(workersize-1.sid!=0);return worker;void paixu(struct work a)int i,j,k;long t;char v15;for(i=0;isize-1-2;i+)k=i;for(j=i;jaj.sid)k=j;if(k!=i)t=ai.sid; ai.sid=aj.sid; aj.sid=t;strcpy(v,);strcpy(,);strcpy(,v);for(i=0;i=size-2;i+)printf(sid: %ldtname:%snn,ai.sid,);getch();void select(struct work a)int i=0,found=1;long num;printf(Input select sid: );scanf(%ld,&num);for(i=0;i=size-2;i+)if(ai.sid=num) found=0;printf(%ld: %s,num,);break;if(found)printf(nnNO this sid!);getch();void main()struct work *p;p=set_list();paixu(p);select(p);10、#includefloat jiecheng(int n)long s=1;int i;for(i=1;i=n;i+)s=s*i;return s;float chengfang(float x,int n)float s=1;int i;for(i=1;i=n;i+)s=s*x;return s;float f(float x,int n)float s=1,t=0;int i=1,j=-1;for(i=1;i=0 ak=temp break k=8 (2) %s stri=Z cstri+26-a+ i+a4、/*题目:求一组成绩的平均分数以及高于平均分的成绩。*/#include#define max 100void main()float amax,s=0,average=0;int i,n;printf(Please input N: ); /*输入要处理元素的个数*/ scanf(%d,&n);printf(nPlease input %d ge shu: ,n); /*输入90,85,92,77,80,62*/for(i=0;in;i+) scanf(%f,&ai); s+=ai; average = s / n;printf(naverage = %.2fn,average); /*输出81.00*/for(i=0;iaverage) printf(%.2f ,ai); /*输出90.00 ,85.00 ,92.00*/5、/*题目:编写程序,输入一组整数,将他们排序后由小到大输出。*/#include#define max 100void main()int amax,j,i,k=0,t=0,n=0;printf(Please input N: ); /*输入要处理元素的个数*/ scanf(%d,&n);for(i=0;in;i+) scanf(%d,&ai);for(i=0;in-1;i+) /*比较n-1遍*/k=i;for(j=i+1;jaj)k=j; if(k!=i) /*元素排序前下标与排序后下标不符,则交换其值*/ t=ai; ai=ak; ak=t;printf(nn);for(i=0;in;i+) printf(%d ,ai);6、/*题目:从键盘输入一个4x4的整数矩阵,以主对角线()为对称轴*/*将左下角元素中较大者替换右上角元素,并将右上角含对称轴输出。*/#include#includevoid main()int d44=0,0,0,0,i,j;clrscr();printf(nPlease input 16 num: );for(i=0;i4;i+) for(j=0;j4;j+) scanf(%d,&dij);for(i=0;i4;i+) for(j=0;jdji) /*左下角元素大于右上角元素则交换*/ dji=dij;for(i=0;i4;i+)printf(nnnnn);/*为了保持每行的间隔与每列的相同*/for(j=0;j=i) printf(t%d,dij);/*为了使位数不同的数输出依然保持三角型*/ else printf(t);7、*题目:输入一个3x4的二维数组,然后分别按行和按列输出。*/#include#includevoid main()int d34=0,0,0,0,i,j;printf(Please input 12 num: );for(i=0;i3;i+) for(j=0;j4;j+) scanf(%d,&dij);printf(nnAn hang: );for(i=0;i3;i+) for(j=0;j4;j+) printf(%d ,dij);printf(nnAn lie: );for(i=0;i4;i+) for(j=0;j3;j+) printf(%d ,dji);8、/*题目:编写程序,将两个字符串连接起来,不用strcat函数。*/#include#includevoid main() char s150,s250; int i=0,j=0,t=0; printf(Input one: ); gets(s1); while(s1i!=0) i+; /*统计s1中字符个数*/ printf(nnInput second: ); gets(s2); while(s2j!=0) j+; /*统计s2中字符个数*/ for(t=0;t=j;t+) /*把s2中字符连接到s1后面*/ s1i=s2t; i+; printf(nnone + second: ); puts(s1);9、/*输入一行字符串,统计字符对ab的个数。*/#include#include#define MAX 100void main()char strMAX;int i=0,flage=0;int count=0;printf(nPlease input a string: );gets(str);while(stri)if(stri=a) flage=1;else if(stri=b) if(flage=1) count+;flage=0;else flage=0;i+; printf(the double char ab count is %dn,count);10、#include /*相对于第一种方法,这种方法能把同为最大的字符串全部打印出来。*/#includeint paixu(int a,int n) /*构造对长度为n的数组排序的函数*/int i,j,t,k,temp; for(i=0;in-1;i+)k=i;for(j=i+1;jaj)k=j; if(k!=i) t=ai; ai=ak; ak=t;temp=an-1;return temp; /*返回最大字符串的长度的值*/void main()char str150,str250,str350,str450,str550,str650;char str750,str850,str950,str1050;int count10=0,temp10=0,i,j;int paixu(int a,int n);for(i=1;i=10;i+) /*输入10个字符串*/ printf(nPlease input %d string: ,i); if(i=1) gets(str1);while(str1count0)count0+;temp0+; if(i=2) gets(str2);while(str2count1)count1+;temp1+; if(i=3) gets(str3);while(str3count2)count2+;temp2+; if(i=4) gets(str4);while(str4count3)count3+; temp3+; if(i=5) gets(str5);while(str5count4)count4+; temp4+; if(i=6) gets(str6);while(str6count5)count5+; temp5+; if(i=7) gets(str7)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论