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

下载本文档

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

文档简介

1、C语言程序设计教程习题答案总主编 杨路明主审 郭浩志文件摘自 CSDN习题11、算法描述主要是用两种基本方法:第一是自然语言描述,第二是使用专用工具进行算法描述2、c语言程序的结构如下: 1、c语言程序由函数组成,每个程序必须具有一个main函数作为程序的主控函数。 2、"/*"与"*/"之间的内容构成c语言程序的注释部分。 3、用预处理命令#include可以包含有关文件的信息。 4、大小写字母在c语言中是有区别的。 5、除main函数和标准库函数以外,用户可以自己编写函数,程序一般由多个函数组成,这些函数制定实际所需要做的工作。 例如: void m

2、ain() 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语言编程,生成的目标代

3、码质量高,程序执行效率高,可移植性好;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、Use

4、r 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',c

5、3='c' printf("a=%db='%c'"end"n",a,b); 结果如下:a=3b='A'"end" 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=a>b?a:

6、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(&q

7、uot;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); elsepr

8、intf("you input number is error!"); 9、void main() int n; printf("please input the number:"); scanf("%d",&n); if(n>=100 && n <= 999) printf("%d%d%d",n%10,(n/10)%10,n/100); elseprintf("you input number is error!");10、void main() int i

9、,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("YESn"):printf("NOn");11、void main() char a; scanf("%c",&a); printf("%c,%c,%c",a-1,a,a+1); printf("%d,%d,%d",a-1,a,a+1);12、void main

10、() float a,b,c,s,Area; scanf("%f,%f,%f",&a,&b,&c); if(a+b > 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: (x<30&&x>20)|(x<-100)4

11、:*a=25,b=14,c=16*5:376:if(a<=b) printf("1");else printf("2");7、#include<stdio.h>void main()char a,b,t1,t2;scanf("%c,%c",&a,&b);t1=a>b?a:b;t2=a<b?a:b;if(t1-t2)%2=0)printf("%c,%c",a+1,b+1);elseprintf("%c,%c",a-1,b-1);getch();8、#i

12、nclude<stdio.h>void main()int temp1=0,temp2=0,x,y,i=1;printf("Please input (x,y): ");scanf("%d,%d",&x,&y);while(i*y)<=x)if(x=(i*y) temp1=1;break;temp2=i;i+;if(temp1)printf("%d / %d = %d",x,y,i);elseprintf("%d / %d-> shang=%d,yushu=%d",x,y,t

13、emp2,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);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、#include<stdio.h>void

14、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 "

15、,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、方法一:#include<stdio.h>void main()int x

16、,y;scanf("%d",&x);if(x<0&&x>-5)y=x-1;else if(x=0)y=x;else if(x>0&&x<8)y=x+1;printf("%d",y);getch();方法二:#include<stdio.h>void main()int x,y;scanf("%d",&x);if(x<8&&x>-5)if(x=0)y=x;else if(x>0&&x<8) y=y=

17、x+1;else y=x-1;printf("%d",y);else printf("Input error!");getch();方法三:#include<stdio.h>void main()int x,y,i;scanf("%d",&x);if(x<8&&x>-5)if(x=0)i=1;else if(x>0&&x<8) i=2;else i=3;else i=4;switch(i)case 1:y=x;printf("%d",y);

18、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 < n; i+)scanf("%d",

19、&value);if(value%2 = 0)sum+=value;count+;average = sum / (float)count;printf("the average is %fn",average); 2、 #include "stdio.h" void main() char ch;int zm = 0, sz = 0;ch = getchar();while(ch != '*')if(ch >= 'A' && ch <= 'Z') | (ch >=

20、 'a' && ch < 'z') )zm+;if(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 < 0) printf("you inpu

21、t the int is error!n");return;for(;)temp = i_value%10;i_value = i_value/10;sum+=temp;printf("%d+",temp);if(i_value = 0)break;printf("b=%dn",sum); 4、 #include "stdio.h" void main() char ch;ch = getchar();while(ch != '.')if(ch >= 'A' && ch

22、<= 'Z')ch = ch + 32;putchar(ch);else if(ch >= 'a' && ch < 'z') )ch = ch - 32;putchar(ch);elseputchar(ch);ch = getchar();printf("n"); 5、 void main() float sum = 200;int count = 0;for(;)sum = sum+ sum*0.045;count+;if(sum > 500)break;printf("th

23、e %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 <stdio.h>int main(void) int steps = 29, i = 1;

24、 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;n<1000;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,su

25、m; 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<nWidth;b+) char chOut =&#

26、39; ' int nleft =(nWidth -a)/2; int nright = (nWidth+a)/2; if(b>=nleft&&b<nright) 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")

27、12、#include "stdio.h"void 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);f

28、lage=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 <stdi

29、o.h>#include <math.h>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.h"main()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*

30、x2; do x0=(x1+x2)/2; f0=2*x0*x0*x0-4*x0*x0+3*x0; if(f0*f1)<0) x2=x0; f2=f0; else x1=x0; f1=f0; while(fabs(f0)>1e-6); printf("_2*x*x*x-4*x*x+3*x=0_n"); printf("the root is %fn",x0);习题61、#include<stdio.h>int divisor(int a,int b)int r;while(r=a%b)!=0)a=b;b=r;return b;int

31、 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、#include<stdio.h>void tongji(char a)int b3=0,0,0,i=0;while(

32、ai!='0')if(ai<=90&&ai>=65)|(ai<=122&&ai>=97)b0+;else if(ai<=57&&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

33、);3、#include<stdio.h>int 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(n>999|n<100)printf("Input error!");elsefor(i=100;i<

34、;n;i+)if(flower(i)printf("%d ",i);getch();4、#include<stdio.h>#define S) t=b;b=a;a=t;main()float x,y,t;printf("Enter two number (x,y): ");scanf("%f,%f",&x,&y);S);printf("nnExchanged:x=%f,y=%f",x,y);getch();5、#include<stdio.h>int fib(int n)in

35、t 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、#include<stdio.h>long fac(int n)long f;if(n=0)f=1;elsef=n*fac(n-1);return f;void main()int m,n;long

36、 f;printf("Please input (m,n): ");scanf("%ld,%ld",&m,&n);f=fac(n+m)+fac(n);printf("nnfibonacci=%ld",f);7、#include<stdio.h>void 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

37、();8、#include<stdio.h>int he(int n)int i,s=0;for(i=1;i<n;i+) if(n%i=0)s+=i;return s;void main()int i,j;for(i=1;i<30000;i+)for(j=1;j<30000;j+)if(he(i)=j)&&(he(j)=i)&&(i!=j) printf("%d<->%d ",i,j);9、#include<stdio.h>#define max 100struct worklong si

38、d;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;i<size-1-2;i+)k=i;for

39、(j=i;j<size-2;j+)if(ai.sid>aj.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("Inpu

40、t 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、#include<stdio.h>float jiecheng(in

41、t 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<=n;i+)t=j*chengfang(x,2*i)/jiecheng(2*i);s+=t;j=j*j;return s;main()float s1=0,s2=0,s3=0,s4=0,x=5.6;int n=

42、7;s1=f(x,n);s2=f(x+2.3,n);s3=f(x-3.2,n+3);s4=s1/(s2+s3);printf("%f",s4);getch();习题71: (1) D (2) A (3) D (4) B (5) D2: (1) 1 5 4 4 6 7 8 2 3 4 (2) 63333: (1)a8=data; k>=0 ak=temp break k<=8 (2) "%s" stri<='Z' cstri+26-'a'+ i+'a'4、/*题目:求一组成绩的平均分数以及高

43、于平均分的成绩。*/#include<stdio.h>#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;i<n;i+) scanf("%f",&ai); s+=a

44、i; average = s / n;printf("naverage = %.2fn",average); /*输出81.00*/for(i=0;i<n;i+) if(ai>average) printf("%.2f ",ai); /*输出90.00 ,85.00 ,92.00*/5、/*题目:编写程序,输入一组整数,将他们排序后由小到大输出。*/#include<stdio.h>#define max 100void main()int amax,j,i,k=0,t=0,n=0;printf("Please inpu

45、t N: "); /*输入要处理元素的个数*/ scanf("%d",&n);for(i=0;i<n;i+) scanf("%d",&ai);for(i=0;i<n-1;i+) /*比较n-1遍*/k=i;for(j=i+1;j<n;j+) /*每遍比较n-(i+1)次*/ if(ak>aj)k=j; if(k!=i) /*元素排序前下标与排序后下标不符,则交换其值*/ t=ai; ai=ak; ak=t;printf("nn");for(i=0;i<n;i+) printf(&

46、quot;%d ",ai);6、/*题目:从键盘输入一个4x4的整数矩阵,以主对角线()为对称轴*/*将左下角元素中较大者替换右上角元素,并将右上角含对称轴输出。*/#include<stdio.h>#include<string.h>void main()int d44=0,0,0,0,i,j;clrscr();printf("nPlease input 16 num: ");for(i=0;i<4;i+) for(j=0;j<4;j+) scanf("%d",&dij);for(i=0;i<

47、4;i+) for(j=0;j<4;j+) if(dij>dji) /*左下角元素大于右上角元素则交换*/ dji=dij;for(i=0;i<4;i+)printf("nnnnn");/*为了保持每行的间隔与每列的相同*/for(j=0;j<4;j+) if(j>=i) printf("t%d",dij);/*为了使位数不同的数输出依然保持三角型*/ else printf("t");7、*题目:输入一个3x4的二维数组,然后分别按行和按列输出。*/#include<stdio.h>#inc

48、lude<string.h>void main()int d34=0,0,0,0,i,j;printf("Please input 12 num: ");for(i=0;i<3;i+) for(j=0;j<4;j+) scanf("%d",&dij);printf("nnAn hang: ");for(i=0;i<3;i+) for(j=0;j<4;j+) printf("%d ",dij);printf("nnAn lie: ");for(i=0;i

49、<4;i+) for(j=0;j<3;j+) printf("%d ",dji);8、/*题目:编写程序,将两个字符串连接起来,不用strcat函数。*/#include<stdio.h>#include<string.h>void main() char s150,s250; int i=0,j=0,t=0; printf("Input one: "); gets(s1); while(s1i!='0') i+; /*统计s1中字符个数*/ printf("nnInput second: &

50、quot;); 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<stdio.h>#include<string.h>#define MAX 100void main()char strMAX;int i=0,flage=0;int count=0;prin

51、tf("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<stdio.h> /*相对于第一种方法,这种方法能把同为最大的字符串全部打印出来。*/#include

52、<string.h>int paixu(int a,int n) /*构造对长度为n的数组排序的函数*/int i,j,t,k,temp; for(i=0;i<n-1;i+)k=i;for(j=i+1;j<n;j+) if(ak>aj)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(st

温馨提示

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

评论

0/150

提交评论