已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C 语言习题答案 第 1 章 1选择题 12 345 CBABD 2填空题 (1)main (2) main (3) 有穷性、确定性、有零个或多个输入、有一个或多个输出、有效 性 (4) 顺序、分支和循环 (5) 自顶向下,逐步细化、模块化设计、限制使用 goto 语句 第 2 章 1选择题 12 345 67 890 12 34 5 ACCDA DDBBA BBACB 2、填空题 (1) 数字、字母、下划线 (2)0 (3) 4、8 (4) (a+b)*c/(a-b) (5) 60 (6 )16 (7 ) 9 (8)6、4、2 (9 )0 (10)10、6 (11 )12、4 (12)5.500000 (13 ) 0 (14)16 (15 ) 6.6 3、编程题 (1 )编写一个程序求各种类型数据的存储长度。 #include main() printf(“int:%d byten“,sizeof(int); printf(“short int:%d byten“,sizeof(short int); printf(“long int:%d byten“,sizeof(long int); printf(“float:%d byten“,sizeof(float); printf(“double:%d byten“,sizeof(double); printf(“long double:%d byten“,sizeof(long double); printf(“char:%d byten“,sizeof(char); (2 ) #include #define RAT 1.60934 main() float k; printf(“input the km:“); scanf(“%f“, printf(“mile:%fn“,k/RAT); 第 3 章 1选择题 12 345 67 890 (1) (10) :DDCDD DCDCC 2解析题 (1) x=170,x=170,x=252,x=aa,x=170 x=170,x=170,x= 170,x=%6d a=513.789185,a=513.79,a=513.78918457,a=513.78918457 (2) a=3b=7x=8.5y=71.82c1=Ac2=a? 3编程题 (1) main() int x,y; scanf(“%d%d“, printf(“商数=%d,余数=%d“,x/y,x%y); (2) main() double x,y,z,avg; scanf(“%lf%lf%lf“, avg=(x+y+z)/3; printf(“%.1f“,avg); 第 4 章 1选择题 (1)(10) CCAAD CCABD 2填空题 (1)a0 (6) a+bc printf(“please input a number:“); scanf(“%d“, if(x%2=0) printf(“x is a even number“); else printf(“x is a odd number“); (2) #include main() int x,y; printf(“please input a number:“); scanf(“%d“, if(x-5 printf(“please input a number:“); scanf(“%d“, switch(a/10) case 0: case 1: case 2:m=1;break; case 3:m=2;break; case 4:m=3;break; case 5:m=4;break; default:m=5;break; printf(“%d“,m); (4) #include main() float price,tax; printf(“please input the price of product:“); scanf(“%f“, if(price=10000) tax=price*0.05; else if(price=5000) tax=price*0.03; else if(price=1000) tax=price*0.02; else tax=0; printf(“%f“,tax); (5) #include main() float score; printf(“please input the score of student:“); scanf(“%f“, if(score=85) printf(“VERY GOOD“); else if(score=60) printf(“GOOD“); else printf(“BAD“); (6 ) # include main () int x,y,z; printf(“input two number:“); scanf(“%d%d“, if(x99 | y99) printf(“input data error!n“); else z=(x/10)*1000+(y/10)*100+(x%10)*10+y%10; printf(“%dn“,z); 第 5 章 1选择题 (1)d (2) c (3)a (4)d (5)a (6)d (7)d (8)b (9)d (10)b (11)c (12)a (13)d (14)d (15)c 2填空题 (1) fahr celsius=5.0/9*(fahr-32) fahr+=step (2) 5、4、6 (3) 3*i-2 (4) y- z*=x (5) 85 2 (6) j+ i%j=0 j=i(或 j=i) (7)sum=eps 第三处改正:r*2 (4) 第一处改正: float n,t,sum; t=1; sum=0; n=1; s=1.0; while(n main() int i; long int n=1; for(i=1;i main() int n,i,j; scanf(“%d“, for(i=1;i main() int h10,h7,h5; for(h10=0; h100) x1=(x2+1)*2; /*第一天的桃子数是第 2 天桃子数加 1 后的 2 倍*/ x2=x1; day-; printf(“the total is %dn“,x1); /代码二 main() int i=9,sum=1; for(; i0; sum=2*(sum+1),i-); printf( “sum=%dn “,sum); (7)牛顿迭代法 一般地: f(x)=a0xn+a1xn-1+.+an-1x+an=0 求 f(x)在 X0 附近的根 计算公式:Xn+1=Xn-f(Xn)/f (Xn) 精度:= |Xn+1-Xn| #include main() float x,x0,f,f1; x=1.5; /可改为 scanf(“%f“, do x0=x; f=2*x0*x0*x0-4*x0*x0+3*x0-6; f1=6*x0*x0-8*x0+3; / f 的导数,即它的切线 x=x0-f/f1; / 切线与 x 轴的交点 while(fabs(x-x0)=1e-5); printf(“The root is %8.7fn“,x); (8) 利用辗除法,直到 b 为 0 为止 main() int p,r,n,m,temp; printf(“input two integer n,m:“); scanf(“%d,%d“, if(n0 n%10+0x30 或 n%10+0 sj=0 (4 ) si 、 s0,s1 、str,s1、s2,str 4.改错题 (1) 第一处改正: n=strlen(str) 第二处改正:strj= 第三处改正:strj!= (2)第一处改正:ch=stri 第二处改正:(j=0) srand(unsigned) time(NULL); for (i=0;i #define N 5 main() int aN,max,min,i,sum=0,count=0; float av; for(i=0; imax)max=ai; if(aiav)count+; printf(“max:%dnmin:%dnav:%fncount:%d“,max,min,av,count); (3 ) #include main() int score30; /*=61,62,63,64,65,66,67,68,69,70, 71,72,73,74,75,76,77,78,79,80, 81,85,89,90,33,44,55,91,92,93; /不必赋初值,这是为调试程序 方便*/ int i,count90=0,count80=0,count70=0,count60=0; for(i=0;i=90) count90+; count80+; count70+; count60+; else if(scorei=80) count80+; count70+; count60+; else if(scorei=70) count70+; count60+; else if(scorei=60) count60+; printf(“059:%dn6069:%dn7079:%dn8089:%dn90100:%d“,30-count60,count60- count70,count70-count80,count80-count90,count90); printf(“n60 or higher:%dn70 or higher:%dn80 or higher:%dn90 or higher:%dn“,count60,count70,count80,count90); (4 ) 1、 #include main() char a45; int i,j,k; for(i=0;i main() char a59; int i,j,k; for(i=0;i # define N 4 main() int i,j,aN=21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10; printf(“nThe original array:n“); for(i=0;iy?x:y) (8) * # * * # * * # * (9 ) str =abdef (10) low=1e-6)改为 if(fabs(x1-x0)=1e-6) 4 、编程题 (1) #include long act(long n) if (n=1|n=0) return 1; else return n*act(n-1); main() int n=3; printf(“n!=%ld“,act(n); (2 ) #include void mmax(float a,int n); main() float a6; int i; for(i=1; ia(int)a0) a0=n; mmax(a,n-1); (3 ) #include #include int start; int fun(char str); main() char a40; int i,num; gets(a); num=fun(a); if(num=-1) printf(“input string error or no have An“); else printf(“%dn“,num); for(i=start; i #include void fun(char str,int a)/不使用数组时,可使用全局变量保存统计结果 int i=0; a0=a1=a2=a3=0; while(stri!=0) if(stri=A p-) *p=*(p-1); *array=array_end; m-; if(m0) move(array,n,m); /*递归调用,当循环次数 m 减至 0 时,停止调用*/ (2) /*习题 9-4-2 */ #include #include #define TOTAL 6 int mseek(char*str,char xstr,int n) int i; for(i=0; i #include void fun(char *str,int num4) int i; for(i=0; i=a void change(int *p); printf(“Input matrix:n“); for(i=0;i*(p+5*i+j) pmin=p+5*i+j; temp=*(p+12); /*将最大值换给中心元素*/ *(p+12)=*pmax; *pmax=temp; temp=*p; /*将最小值换给左上角元素*/ *p=*pmin; *pmin=temp; pmin=p+1; for(i=0;i*(p+5*i+j) pmin=p+5*i+j; temp=*pmin; /*将第二最小值换给右上角元素*/ *pmin=*(p+4); *(p+4)=temp; pmin=p+1; for(i=0;i*(p+5*i+j) pmin=p+5*i+j; /*将第三最小值换给左下角元素*/ temp=*pmin; *pmin=*(p+20); *(p+20)=temp; pmin=p+1; for(i=0;i*(p+5*i+j) pmin=p+5*i+j; temp=*pmin; /*将第四最小值换给右下角元素*/ *pmin=*(p+24); *(p+24)=temp; (5) /*习题 9-4-5 */ /*可以专门编写一个函数求各学生的平均分,存到 aver4数组*/ #include void avcour1(float score5); void fali2(int num4,float score45); void good(int num4,float score45); main() int i,j,num4;/数组 num 代表学号 float score45; printf(“Input NO. and scores: n“); for(i=0;i=2) printf(“%5d“,numi); for(k=0;k85.0) n+; sum+=scoreij; averi=sum/5; if(n=5)|(averi=90) printf(“%5d“,numi); for(k=0;k double sigma(double (*fn)(double),double l,double u) double sum=0,d; for(d=l; d %sn“,i,month_name(i); /*调用指针函数 month_name()*/ char *month_name(int n) /*定义一个指针函数 month_name(),返回一个指向字符串的指针*/ static char *name=“Illegal Month“, “January“, “February“, “March“, “April“, “May“, “June“, “July“, “August“, “September“, “October“, “November“, “December“; return(n12)?name0:namen); (8) /*习题 9-4-8 */ #include #include #define N 10 main() void sort(char *p); int i; char *pN,strN20; for(i=0;i0) temp=*(p+j); *(p+j)=*(p+j+1); *(p+j+1)=temp; (9) /*习题 9-4-9 */ #include #define LINEMAX 20 /*定义字符串的最大长度*/ main() void sort(char *p); int i; char *p,*pstr5,str5LINEMAX; for(i=0;i0) /*比较后交换字符串地址*/ temp=*(p+i); *(p+i)=*(p+j); *(p+j)=temp; (10) void StrOR(char xx80,int maxline) int i,righto,j,s,k; char temp80; for(i=0; i=0; j-) k=0; memset(temp,0,80); if(xxij=o) righto=j; for(s=righto+1; sage; q-name,q-age (3 ) p!=NULL c+ p-next (4 ) int month; int day; ; main() struct data a; int monthnum12=31,28,31,30,31,30,31,31,30,31,30,31; int i,sum=0; scanf(“%d%d%d“, for(i=0;i #include struct study float chinese; float maths; float english; float avg; ; main() struct study student; scanf(“%f%f%f“, student.avg=(student.chinese+student.maths+student.english)/3; printf(“average score is %fn“,student.avg); (3) #include #include struct study int num; float chinese; float maths; float english; float avg; ; main() struct study s3; struct study *p; for(p=s;pnum), p-avg=(p-chinese+p-maths+p-english)/3; for(p=s;pnum,p-chinese,p-maths,p-english,p-avg); (4) #include #include #define N 3 typedef struct char dm5; /*产品代码 */ char mc11; /* 产品名称 */ int dj; /* 单价 */ int sl; /* 数量 */ long je; /* 金额*/ PRO; void SortDat(PRO sell,int n) int i,j; PRO xy; for(i=0; i0|strcmp(selli.mc,sellj.mc)=0 selli=sellj; sellj=xy; void main() PRO sellN; int i; for(i=0; i #include main() int *pi; int i,j,t,n; printf(“输入整数个数 :“); scanf(“%d“, pi=(int *)malloc(n*sizeof(int); printf(“输入整数 :“); for(i=0; ipij) t=pii; pii=pij;pij=t; for(i=0; i #include struct stu int num; char name20; int age; struct stu *next; ; void list(struct stu *head) struct stu *p; printf(“The list records are:n“); p=head; if(head!=NULL) do printf(“%dt%st%dn“,p-num,p-name,p-age); p=p-next; while(p!=NULL); else printf(“The list is null“); struct stu * insert(struct stu *head,struct stu *stud) struct stu *p0,*p1,*p2; p1=head; /*p1 指向链表第一个节点*/ p0=stud; /*p0 指向要插入的节点 */ if(head=NULL) /*原链表是空表*/ head=p0; p0-next=NULL; /*p0 作为头指针*/ else while(p1!=NULL) p1=p1-next; /*p1 指针后移*/ if(p1!=NULL) if(head=p1) head=p0;/*插入链表开头*/ else p2-next=p0; /*插入到 p2 节点之后 */ p0-next=p1; else p2-next=p0; p0-next=NULL; /*插入到最后*/ return head; main() struct stu *newstu,*head; head=NULL; int num; scanf(“%d“, while(num!=0) newstu=(struct stu *)malloc(sizeof(struct stu); newstu-num=num; scanf(“%s“,newstu-name); scanf(“%d“, head=insert(head,newstu); scanf(“%d“, list(head); (7) #include void partition(unsigned long int num) union a unsigned short int part2; unsigned long int w; n,*p; p= n.w=num; printf(“long int=%lxn“,num); printf(“low part num=%0x,high part num=%0xn“,p-part0,p-part1); main() unsigned long int x; x=0x23456789; /这是为了调试方便,应改为 scanf 函数 partition(x); 第 11 章 一、选择 1.B 2.A 3.B 4.C 5.B 6.C 7.D 8.C 9.C、D 10.A 11.A 12.C 13.B 二、填空 1 : fopen(fname,“w“) ch 2: “r“ fgetc(fp) 或 getc(fp) 3: “bi.dat“ 第二处改为: !feof(fp) 或 !=0 改为=0 2: 第一处改为: rewind(fp) 第二处改为: fgetc(fp)!= n 0后面加上 char str100; int i=0; if(fp=fopen(“myfile“,“w“)=NULL) printf(“Can not open the file.n“); exit(0); printf(“Input a string:n“); gets(str); while(stri!=!) if(stri=a char name8; int score3; float ave; stu5; main() int i,j,sum; FILE *fp; for(i=0;i #include main() char s80; int a; FILE *fp; if(fp=fopen(“test“,“w“)=NULL) printf(“Cannot open file.n“); exit(1); fscanf(stdin,“%s%d“,s, /相当于 scanf fprintf(fp,“%s %d“,s,a); fclose(fp); if(fp=fopen(“test“,“r“)=NULL) printf(“Cannot open file.n“); exit(1); fscanf(fp,“%s %d“,s, fprintf(stdout,“%s %dn“,s,a); /相当于 printf fclose(fp); (4) #include #include main() FILE *fp; int i,j,n,i1; char c100,t,ch; if(fp=fopen(“A“,“r“)=NULL) printf(“Can not open the file.n“); exit(0); printf(“nfile A:n“); for(i=0;(ch=fgetc(fp)!=EOF;i+) ci=ch; putchar(ci); fclose(fp); i1=i; if(fp=fopen(“B“,“r“)=NULL) printf(“n Can not open the file.“); exit(0); printf(“nfile B:n“); for(i=i1;(ch=fgetc(fp)!=EOF;i+) ci=ch; putchar(ci); fclose(fp); n=i; for(i=0;icj) t=ci; ci=cj; cj=t; printf(“n file C:n“); fp=fopen(“c“,“w“); for(i=0;i main() FILE *fp1,*fp2; char ch; fp1=fopen(“file1.c“,“r“); fp2=fopen(“file2.c“,“w“); ch=fgetc(fp1); while(!feof(fp1) putchar(ch); ch=fgetc(fp1); rewind(fp1); while(!feof(fp1) fputc(fgetc(fp1),fp2); fclose(fp1); fclose(fp2); (6) #include main() FILE *fp; long position; fp=fopen(“data.txt“,“w“); position=ftell(fp); printf(“position=%ldn“,position); fprintf(fp,“Sample datan“); position=ftell(fp); printf(“positio
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 耳道清洁:老年耳部护理的基础步骤
- 医学医美咨询师职业健康案例教学课件
- 脓毒症患者心理韧性的培养与护理策略
- 失智老人行为管理与干预
- 护理人力资源配置与排班优化策略
- 手术间分区管理规定
- 面部轻度爆炸伤恢复期护理重点:细节决定恢复
- 基于PDCA循环的护理培训质量持续改进
- 产后恶露观察与护理指南
- 市场营销活动策划案模板高效执行
- 基于PLC的自动洗车控制系统设计
- 2025年AI影像诊断开发合同协议
- 2025年铁路招聘考试题库及答案
- 隆鼻护理查房
- 食品安全管理员考试题库及答案(2025版)
- 2025新《食品安全法》解读及案例分析讲座课件
- 团队协作效能评估工具
- 瓜子二手车保卖合同(标准版)
- 边际贡献培训课件
- DB11-T 808-2020 市政基础设施工程资料管理规程
- 消防排烟系统安装施工方案
评论
0/150
提交评论