C语言实验报告参考答案原.doc_第1页
C语言实验报告参考答案原.doc_第2页
C语言实验报告参考答案原.doc_第3页
C语言实验报告参考答案原.doc_第4页
C语言实验报告参考答案原.doc_第5页
已阅读5页,还剩25页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

C语言实验报告参考答案实验一 熟悉C语言程序开发环境及数据描述四、程序清单1编写程序实现在屏幕上显示以下结果: The dress is long The shoes are big The trousers are black答案:#includemain()printf(The dress is longn);printf(The shoes are bign);printf(The trousers are blackn);2编写程序:(1) a=150,b=20,c=45,编写求a/b、a/c(商)和a%b、a%c(余数)的程序。(2)a=160,b=46,c=18,d=170, 编写求(a+b)/(b-c)*(c-d)的程序。答案:(1)#includemain()int a,b,c,x,y;a=150;b=20;c=45;x=a/b;y=a/c;printf(a/b的商=%dn,x);printf(a/c的商=%dn,y);x=a%b;y=a%c;printf(a/b的余数=%dn,x);printf(a/c的余数=%dn,y);(2)#includemain()int a,b,c,d;float x;a=160;b=46;c=18;d=170;x=(a+b)/(b-c)*(c-d);printf(a+b)/(b-c)*(c-d)=%fn,x);3. 设变量a的值为0,b的值为-10,编写程序:当ab时,将b赋给c;当a=b时,将0赋给c。(提示:用条件运算符)答案:#includemain()int a,b,c;a=0;b=-10;c= (ab) ? b:a;printf(c = %dn,c);五、调试和测试结果1.编译、连接无错,运行后屏幕上显示以下结果: The dress is long The shoes are big The trousers are black2、(1) 编译、连接无错,运行后屏幕上显示以下结果:a/b的商=7a/c的商=3a/b的余数=10a/c的余数=15(2) 编译、连接无错,运行后屏幕上显示以下结果:(a+b)/(b-c)*(c-d)=-1064.00003. 编译、连接无错,运行后屏幕上显示以下结果:c =-10实验二 顺序结构程序设计四、程序清单1键盘输入与屏幕输出练习问题1 D 。问题2 改printf(%c,%c,%dn,a,b,c);这条语句 改成:printf(%c %c %dn,a,b,c);问题3 改scanf(%c%c%d,&a,&b,&c);这条语句 改为:scanf(%c,%c,%d,&a,&b,&c);问题4 改printf(%c,%c,%dn,a,b,c);这条语句 改成:printf(%c %c %dn,a,b,c);问题5 把scanf(%c%c%d,&a,&b,&c);和printf(%c,%c,%dn,a,b,c);改成scanf(%c%*c%c%*c%d,&a,&b,&c);printf(%c,%c,%dn,a,b,c);2(1)从键盘输入两个八进制数,计算两数之和并分别用十进制和十六进制数形式输出。#include int main() int a,b,c; scanf(%d%d,&a,&b);c = a + b;printf(%dn,c);printf(%xn,c);return 0;2(2)编写程序:从键盘输入两个实数a和x,按公式计算并输出y的值: #include#includeint main() float a,x,y; scanf(%f%f,&a,&x);y = pow(a,5) + sin(a*x) + exp(a*x) + log(a+x);printf(y=%fn,y);return 0;五、调试和测试结果2(1) 输入: 12 14 输出:26 1a2(2) 输入:1 0输出:2.000000实验三 选择结构程序设计四、设计流程(算法描述) (请写出上机内容2(3)题的算法描述) 主要是两两比较,然后得出最大的数五、程序清单(1)输入一个整数,若大于等于0,输出提示信息“is positive”,否则输出“is negative”。#include#includemain() int a;scanf(%d,&a);if(a=0)printf(the number is positven);elseprintf(the number is negetiven);return 0;(2)输入两个整数a和b,若a=b时,求其积c并显示;若ab时,求其商c并显示。#include main() int a,b,c;scanf(%d%d,&a,&b);if(a=b)printf(c=%dn,a*b);elseprintf(c=%dn,a/b);return 0;(3)输入a、b、c三个整数,输出最大数。#includemain() int a,b,c,x;scanf(%d%d%d,&a,&b,&c);if(a=b)x=a;elsex=b;if (xc)x=c;printf(the max number is:%dn,x);return 0;六、调试和测试结果2(1) 输入: 2 输出:the number is positve 输入: 0 输出:the number is positve输入: -2 输出:the number is negetive2(2) 输入: 3 2 输出:c=6输入: 2 3 输出:c=02(3) 输入:3 2 1 输出:the max number is:3 输入:2 3 1 输出:the max number is:3输入:1 2 3 输出:the max number is:3实验四 循环结构程序设计四、设计流程(算法描述)(请写出上机内容2的算法描述)首先求出每一个给定数的所有因子和,然后从2到5000循环,那一个数x与因子之和相等,就是完数。五、程序清单1编写程序:求1+2+3+100和12+22+33+1002。#include#includeint main() int i,j,sum;sum = 0;for (i=1;i=100;i+)sum += i;printf(the sum is:%dn,sum);sum =0;for(i=1;i=100;i+)j=pow(i,2);sum +=j;printf(the square sum is:%dn,sum);return 0; 2一个数如果恰好等于它的因子之和,这个数就称为“完数”,编写程序找出25000中的所有完数。 #include#include main() int i,j,sum=0;for(i=2;i=5000;i+) /遍历从2到5000的所有数sum = 0; for (j=1;j=i/2;j+) /找出给定整数X的所有因子和if(i%j = 0)sum +=j;if(i = sum) /sum为因子和,如果和i相等,则输出printf(%d ,i);return 0; 3.编写程序:计算sinx的近似值,精确到10-6。 其实 所以程序#include #include main()float x,sinx,i,t;printf(请输入一个x值(弧度值):);scanf(%f,&x);sinx=0; t=x;i=1;while(fabs(t)=1e-6) sinx=sinx+t; t=t*(-x*x/(2*i*(2*i+1); i+;printf(sin(%.2f)=%.6fn,x,sinx);六、调试和测试结果1:结果:the sum is:5050 the square sum is:3383502:结果:6 28 4963、输入0,输出sin(0.00)=0.000000 输入1.57,输出sin(1.57)=1.000000 输入0.5,输出sin(0.50)=0.479426实验五 函数和编译预处理四、设计流程(算法描述)(请写出上机内容2的算法描述)求素数的方法就是:给定一个大于3的数x,从2到X的平方根遍历,只要有数可以被x整除,就不是素数五、程序清单1编写自定义函数long power(int m,int n),计算的值。利用此函数编程序实现:从键盘输入两个整数m和n,计算出的值。 #includelong power(int m,int n)/要返回的是long型int i;long s;/因为是要返回的数,所以这里也定义为long型s=1;for(i=1;i=n;i+)s *=m;return s;int main(void)int m,n;scanf(%d%d,&m,&n);printf(s=%ldn,power ( m,n);return 0; 2编写自定义函数prime(int x),判断x是否为素数。利用此函数编写程序找出35000中的所有素数,并输出素数的个数。#include#includeint prime(int m)int i,k;k=sqrt(m);for(i=2;ik)return 1;return 0;main() int i,k; k=0; for(i=3;i=5000;i+) if(prime(i)=1)k+;printf(%d is a prime muber n,i); printf(共有%d个素数n,k); 3. 编写自定义函数count(int x),计算x的因子个数。利用此函数找出并输出11000中有奇数个不同因子的整数。#include#includeint count(int x)int sum,i;sum =0;/记住因子的个数for(i=1;i=x/2;i+)if(x%i = 0)sum +=1;return sum+1;int main(void) int i,y; for(i=1;i=100;i+) y=count(i); if(y%2=1)printf(%dt,i); return 0; 六、调试和测试结果1.输入:2 3 输出:s=82.输出:共有668个素数2. 3、输出结果为:实验六 数组四、设计流程(算法描述)(请写出上机内容1的算法描述)设置两个变量分别指示头和尾。第一个和最后一个元素值互换,然后头和尾变量向里移动,最终到两变量相遇为止。五、程序清单1编写程序:从键盘输入一串整数保存到数组中,调用函数antitone()将数组反序输出。自定义函数void antitone(int a,int n)实现将数组中的n个数据按逆序存放。 void antitone(int a,int n)int i,j;int k;i=0;j=n-1;while(ij)k=ai;ai=aj;aj=k;i +=1;j -=1;2已知某数列的前两项为2和3,其后每一项为其前两项之积。编程实现:从键盘输入一个整数x,判断并输出x最接近数列的第几项?#include#includevoid Mad(int a,int n)int i;a0=2;a1=3;for(i=2;iai;i+);k1 = abs(x-ai-1);k2 = abs(x-ai);if(k1k2)printf(the most similar x number is:%dn,ai);elseprintf(the most similar x number is:%dn,ai-1);return 0; 3.编程实现:输入10个学生5门课的成绩并完成如下功能(1)求每个学生的平均分;(2)求每门课程的平均分。 #include#include#define num 10typedef struct studentchar name20;float math;float englis;float computer;float Chinese;float history;STUDENT;int main(void)STUDENT stunum;int i;float score,sum,average;char s10;float scoreMath,scoreEng,scoreCom,scoreChi,scoreHis;for(i=0;inum;i+)printf(Name: );gets();printf(math score: );scanf(%f,&score);stui.math = score;printf(englis score: );scanf(%f,&score);stui.englis = score;printf(computer score: );scanf(%f,&score);puter = score;printf(Chinese score: );scanf(%f,&score);stui.Chinese = score;printf(history score: );scanf(%f,&score);stui.history = score;gets(s);/功能是接受最后一个回车符,然后下一次gets();才能起到作用/求每个学生的平均分数for(i=0;inum;i+)sum=0;sum +=stui.math;sum +=stui.englis;sum +=puter;sum +=stui.Chinese;sum +=stui.history;average = sum/5;printf(%ss average score is:%fn,,average);/求每门课的平均成绩scoreMath=0;scoreEng=0;scoreCom=0;scoreChi=0;scoreHis=0;for(i=0;inum;i+)scoreMath += stui.math;scoreEng += stui.englis;scoreCom += puter;scoreChi += stui.Chinese;scoreHis += stui.history;printf(maths average score is:%fn,scoreMath/num);printf(engliss average score is:%fn,scoreEng/num);printf(computers average score is:%fn,scoreCom/num);printf(Chineses average score is:%fn,scoreChi/num);printf(historys average score is:%fn,scoreHis/num);return 0;实验七 数组和函数四、程序清单 (请写出上机内容2中函数的源代码)void fun(int ttMN,int ppN) int i,j,max; for(j=0; jN; j+ ) max=tt0j; for(i=1;imax)max=ttij; ppj=max; 五、调试和测试结果(写出上机内容1中填空的内容)(1) (1) sum=0 (2) tii (3) 1 (2) (1) 1 (2) i (3) ap+i 实验八 指针(1)四、程序清单(请写出上机内容2中的函数)求出每个位上的数字,然后放在千位上的数字乘以1000,放在百位上的数字乘以100,放在10位上的数字乘以10,然后相加。void fun(int a,int b,long *c) int a10,a1,b10,b1;a10=a/10;a1=a%10;b10=b/10;b1=b%10;*c = a10 * 1000 + b1 * 100 + a1 *10 + b10;五、调试和测试结果(请写出上机内容1的输出结果)1(1) 输出结果为:8,7,7,8(2) 6(3) (1)x=10 y=20(2)x=20 y=10 (4) 【1】int *p 【2】&ai 【3】 pi 输入:1 2 3 4 5 6 输出: 1 2 3 4 5 6实验九 指针(2)设计流程(算法描述)(请写出上机内容2中的算法描述)当 *(x+i)!=0i=0*(x+i)= =yTreturn 1i=i+1return 0F五、程序清单1已知一个整型数组a5,其各元素值为4,6,8,10,12。使用指针编程求数组元素之积。#include int main(void)int a=4,6,8,10,12,sum;int *p;sum=1;for(p=a;pave=0; for(i=0;iave+=a-si; a-ave/=N;五、调试和测试结果(请写出上机内容1的填空结果)上机内容1的填空结果(1) -sno (2) -name (3) &t实验十一 共用体与枚举 文件四、程序清单(请写出上机内容2中的程序源代码)#include #include #include int main(void) int i,sum;FILE *fd;char s10,*p,ch;if( (fd=fopen(D:shi.txt,wt)=NULL)printf(creat the file failedn);exit(0);elsefor(i=1;i100;i+)if( (i%3 =0) & (i%5 = 0) )printf(%d, ,i);itoa(i,s,10);/转换成字符串fputs(s,fd);fputc( ,fd);printf(n);fclose(fd);/提取字符转换成数字输入if( (fd=fopen(D:shi.txt,rt)=NULL)printf(open the file failedn);exit(0);elsep=s;sum=0;doch=fgetc(fd);if(ch = )i=atoi(s);sum +=i;printf(%d ,i);strset(s,0);p=s;else*p=ch;p+;while(ch != EOF);printf(数的和是:%dn,sum);fclose(fd); return 0;实验十二 参考答案实验十二参考答案:(可根据情况,弄清楚一个模块即可)题目:设某班有n位同学,每位同学的数据包括以下内容:学号(长整型)、姓名(字符串)、数学成绩(整型)、程序设计成绩(整型)。设计程序完成以下五项功能:新建数据档案、添加数据、删除数据、对输入的数据进行排序和查询。注:输入数据时,要求学号不能相同,姓名可以相同。设计思路:1)程序运行时,首先显示主菜单(模块)如下:1程序运行时,首先显示主菜单如下:1新建数据2添加数据3删除数据4排序5查询6退出用户输入序号后,程序进行相应操作。2)在主菜单中选择序号4,弹出子菜单选择排序方式,子菜单如下:1数学成绩排序2程序设计成绩排序3总分排序。4返回主菜单 选择子菜单的序号后,程序能正确运行并在屏幕上显示按要求排序后的相关信息。3在主菜单中选择序号5,弹出子菜单选择查询方式,子菜单如下:1学号查询2姓名查询3数学成绩查询4程序设计成绩查询5总分查询6返回主菜单请按序号选择相应操作在子菜单中选择序号后,程序按以下方式工作。(1)学号查询:输入学号后,若该学号存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:查询到满足条件的结果后,查询即可结束)(2)姓名查询:输入姓名后,若该姓名存在则显示与其相关的所有信息,否则显示找不到的提示信息;(提示:使用字符串比较函数进行比较)(3)按科目查询:输入指定分数,程序运行后显示该科目中考试成绩大于等于指定分数的同学的学号、姓名以及该科成绩并统计满足条件的人数;(4)总分查询:输入指定分数,程序运行后显示总分成绩大于等于指定分数的同学的学号、姓名以及各科成绩并统计满足条件的人数。C源程序清单如下:#include stdio.h#include stdlib.h#include string.h#include conio.h#include mem.h#include ctype.h#include alloc.h#define N 2 typedef struct z1char no11;char name15;int scoreN;float sum;float average;int order;struct z1 *next;STUDENT;/*Functions*/STUDENT *init(); /*initialize*/STUDENT *create();STUDENT *delete(STUDENT *h);STUDENT *searchno(STUDENT *h);void print(STUDENT *h);void search(STUDENT *h);void save(STUDENT *h);STUDENT *load();STUDENT *insert(STUDENT *h);STUDENT *sort(STUDENT *h);STUDENT *index(STUDENT *h);int menu_select(); /*menu*/*main*/main()int i;STUDENT *head;head=init();clrscr();for(;)switch(menu_select()case 1:head=init();break;case 2:head=create();break;case 3:head=delete(head);break;case 4:print(head);break;case 5:search(head);break;case 6:head=searchno(head);break;case 7:save(head);break;case 8:head=load(); break;case 9:head=insert(head); break;case 10:head=sort(head);break;case 11:case 12:case 13:head=index(head);break;case 0:exit(0); menu_select()char *menu=*MENU*, 1. Init list, 2. Enter list, 3. Delete a record from list, 4. print list , 5. Search record by name, 6. Search record by Number, 7. Save the file, 8. Load the file, 9. insert record to list , 10. sort by total scores, 11. sort by maths scores, 12. sort by program scores, 13. index on number, 0. Quit;char s3;int c,i;gotoxy(1,25);printf(press any key continue.n);getch();clrscr();gotoxy(1,1);textcolor(YELLOW);textbackground(BLACK);gotoxy(10,2);putch(0xc9);for(i=1;i44;i+)putch(0xcd);putch(0xbb);for(i=3;i20;i+)gotoxy(10,i);putch(0xba);gotoxy(54,i);putch(0xba);gotoxy(10,20);putch(0xc8);for(i=1;i44;i+)putch(0xcd);putch(0xbc);window(11,3,53,19);clrscr();for(i=0;i16;i+)gotoxy(10,i+1);cprintf(%s,menui);textbackground(BLACK);window(1,1,80,25);gotoxy(10,21);doprintf(n Enter you choice(013):);scanf(%s,s);c=atoi(s);while(c14);return c;STUDENT *init()return NULL;STUDENT *create()int i; int s;STUDENT *h=NULL,*info;for(;)info=(STUDENT *)malloc(sizeof(STUDENT);if(!info)printf(nout of memory);return NULL;inputs(enter no:(10 digitals .enter 0 to exit),info-no,11);if(info-no0=0) break; /*when the first number is 0,break*/inputs(enter name:(name,15);printf(please input scores n);s=0; /*s is sum,begins with 0*/for(i=0;iscorei); /* socre0 stores maths scores,socore1 stores program scores*/if(info-scorei100|info-scoreiscorei100|info-scoreiscorei;info-sum=s;info-order=0;info-next=h;h=info;return(h);inputs(char *prompt, char *s, int count)char p255;doprintf(prompt);scanf(%s,p);if(strlen(p)count)printf(n too long! n);while(strlen(p)count);strcpy(s,p);/*Print infor*/void print(STUDENT *h)int i=0;STUDENT *p;clrscr();p=h;printf(nnn*STUDENT*n);printf(|rec| NO. | name | maths | program | sum |order|n);printf(|-|-|-|-|-|-|-|n);while(p!=NULL)i+;printf(|%3d|%-10s|%-15s|%7d|%9d|%4.2f| %3d |n,i,p-no,p-name,p-score0,p-score1,p-sum,p-order);p=p-next;printf(*end*n);STUDENT *delete(STUDENT *h)STUDENT *p,*q;char s11;clrscr();printf(please enter the number you want to delete n);scanf(%s,s);q=p=h;while(strcmp(p-no,s)&p!=NULL)q=p;p=p-next;if(p=NULL)printf(nlist no %s studentn,s);elseprintf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n);printf(|-|-|-|-|-|-|n);printf(|%-10s|%-15s|%7d|%9d|%4.2f| %3d |n,p-no,p-name,p-score0,p-score1,p-sum,p-order);printf(*end*n);getch();if(p=h)h=p-next;elseq-next=p-next;free(p);printf(n have deleted No %s studentn,s);return(h);STUDENT *searchno(STUDENT *h)STUDENT *p,*q;char s11;clrscr();printf(please enter the number you want to search n);scanf(%s,s);q=p=h;while(strcmp(p-no,s)&p!=NULL)q=p;p=p-next;if(p=NULL)printf(n %s No Found!n,s);elseprintf(n %s Found!n,s);printf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n);printf(|-|-|-|-|-|-|n);printf(|%-10s|%-15s|%7d|%9d|%4.2f| %3d |n,p-no,p-name,p-score0,p-score1,p-sum,p-order);printf(*end*n);getch();return(h);void search(STUDENT *h)STUDENT *p;char s15;clrscr();printf(please enter name for searchn);scanf(%s,s);p=h;while(strcmp(p-name,s)&p!=NULL)p=p-next;if(p=NULL)printf(n %s No Found!n,s);elseprintf(n %s Found!n,s);printf(nnn*STUDENT*n);printf(| NO. | name | maths | program | sum |order|n);printf(|-|-|-|-|-|-|n);printf(|%-10s|%-15s|%7d|%9d|%4.2f| %3d |n,p-no,p-name,p-score0,p-score1,p-sum,p-ord

温馨提示

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

评论

0/150

提交评论