下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 2010C语言实验报告参考答案

2010C语言实验报告参考答案.doc

2010C语言实验报告参考答案

240404295
2011-07-02 0人阅读 举报 0 0 暂无简介

简介:本文档为《2010C语言实验报告参考答案doc》,可适用于其他资料领域

C语言实验报告参考答案C语言实验报告参考答案实验一熟悉C语言程序开发环境及数据描述四、程序清单.编写程序实现在屏幕上显示以下结果:ThedressislongTheshoesarebigThetrousersareblack答案:#include<stdioh>main(){printf("Thedressislongn")printf("Theshoesarebign")printf("Thetrousersareblackn")}.改错题(将正确程序写在指定位置)正确的程序为:#include<stdioh>main(){printf("商品名称    价格n")printf("TCL电视机   ¥n")printf("美的空调   ¥n")printf("SunRose键盘  ¥n")}.编写程序:a=,b=,c=,编写求ab、ac(商)和ab、ac(余数)的程序。答案:#include<stdioh>main(){inta,b,c,x,ya=b=c=x=aby=acprintf("ab的商=dn",x)printf("ac的商=dn",y)x=aby=acprintf("ab的余数=dn",x)printf("ac的余数=dn",y)}设变量a的值为b的值为编写程序:当a>b时将b赋给c当a<=b时将a赋给c。(提示:用条件运算符)答案:#include<stdioh>main(){inta,b,ca=b=c=(a>b)b:aprintf("c=dn",c)}五、调试和测试结果编译、连接无错运行后屏幕上显示以下结果:ThedressislongTheshoesarebigThetrousersareblack、编译、连接无错运行后屏幕上显示以下结果:ab的商=ac的商=ab的余数=ac的余数=编译、连接无错运行后屏幕上显示以下结果:c=实验二顺序结构程序设计四、程序清单.键盘输入与屏幕输出练习问题D。问题改printf("c,c,dn",a,b,c)这条语句改成:printf("ccdn",a,b,c)问题改scanf("ccd",a,b,c)这条语句改为:scanf("ccd",a,b,c)问题改printf("c,c,dn",a,b,c)这条语句改成:printf("’c’’c’dn",a,b,c)()从键盘输入两个八进制数计算两数之和并分别用十进制和十六进制数形式输出。#include<stdioh>intmain(){inta,b,cprintf("Enteraandb:")scanf("oo",a,b)c=abprintf("d:dn",c)printf("x:xn",c)return}()编写程序:从键盘输入两个实数a和x按公式计算并输出y的值:#include<stdioh>#include<mathh>intmain(){floata,x,yscanf("ff",a,x)y=pow(a,)sin(a*x)exp(a*x)log(ax)printf("y=fn",y)return}.改错题正确的程序为:#include<stdioh>main(){inta,b,c,sscanf("ddd",a,b,c)s=abcprintf("d=dddn",s,a,b,c)*输出s=abc*printf("ddd=dn",a,b,c,s)*输出abc=s*}五、调试和测试结果()输入:输出:a()输入:输出:实验三选择结构程序设计四、设计流程(算法描述)(请写出上机内容()题的算法描述)主要是两两比较然后得出最大的数五、程序清单()输入整数x和a计算并输出下列分段函数f(x)的值(保留位小数)请调用log函数求自然对数调用fabs函数求绝对值。程序为:#include<stdioh>#include<mathh>main(){intx,adoubleyprintf("Enteraandx:")scanf("dd",a,x)if(fabs(x)!=a)y=log(fabs((ax)(ax)))(*a)elsey=printf("a=d,f(d)=fn",a,x,y)}()输入a、b、c三个整数输出最大数。#include<stdioh>main(){inta,b,c,xscanf("ddd",a,b,c)if(a>=b)x=aelsex=bif(x<c)x=cprintf("themaxnumberis:dn",x)return}.改错题正确程序为:#include<stdioh>main(){doublenprintf("Entern:")scanf("lf",n)if(n<)printf("nislessthann")elseif(n==)printf("nisequalton")elseprintf("nisgreatern")}六、调试和测试结果()Enteraandx:a=f()=Enteraandx:a=f()=()输入:输出:themaxnumberis:输入:输出:themaxnumberis:输入:输出:themaxnumberis:实验四循环结构程序设计四、设计流程(算法描述)(请写出上机内容的算法描述)首先求出每一个给定数的所有因子和然后从到循环那一个数x与因子之和相等就是完数。五、程序清单.编写程序:求…和…。#include<stdioh>#include<mathh>intmain(){inti,j,sumsum=for(i=i<=i)sum=iprintf("thesumis:dn",sum)sum=for(i=i<=i){j=pow(i,)sum=j}printf("thesquaresumis:dn",sum)return}.一个数如果恰好等于它的因子之和这个数就称为“完数”编写程序找出~中的所有完数。#include<stdioh>#include<mathh>main(){inti,j,sum=for(i=i<=i)遍历从到的所有数{sum=for(j=j<=ij)找出给定整数X的所有因子和{if(ij==)sum=j}if(i==sum)sum为因子和如果和i相等则输出printf("d",i)}return}改错题正确的程序为:#include<stdioh>main(){intn=intfind=while(!find){if(n==n==n==n==){printf("n=dn",n)find=}n}}六、调试和测试结果:结果:thesumis:thesquaresumis::结果:实验五函数和预处理命令四、设计流程(算法描述)(请写出上机内容的算法描述)利用循环将m乘n次五、程序清单.编写自定义函数longpower(intm,intn)计算的值。利用此函数编程序实现:从键盘输入两个整数m和n计算出的值。#include<stdioh>longpower(intm,intn)要返回的是long型{intilongs因为是要返回的数所以这里也定义为long型s=for(i=i<=ni){s*=m}returns}intmain(void){intm,nscanf("dd",m,n)printf("s=ldn",power(m,n))return}、写出两个函数分别求两个整数的最大公约数和最小公倍数用主函数调用这两个函数并输出结果两个整数由键盘输入。.​ 源程序如下:intzdgys(intn,intn){inty,ifor(i=ni<=i)if(ni==ni==){y=ibreak}returny}intzxgbs(intn,intn){inty,ifor(i=ni<=n*ni)if(in==in==){y=ibreak}returny}main(){intn,n,tscanf("n=dn=d",n,n)if(n<n){t=nn=nn=t}printf("zdgys=dzxgbs=d",zdgys(n,n),zxgbs(n,n))}、改错题正确程序如下:#include<stdioh>intfact(intn)intmulti(intn)main(){intidoublesum,item,epseps=Esum=item=for(i=item>=epsi){item=fact(i)multi(*i)sum=sumitem}printf(“PI=lfn”,sum*)return}intfact(intn){intiintres=for(i=i<=ni)res=res*ireturnres}intmulti(intn){intiintres=for(i=i<=ni=i)res=res*ireturnres}六、调试和测试结果、输入:输出:s=、输入:n=n=输出:zdgys=zxgbs=、输出结果为:实验六数组四、设计流程(算法描述)(请写出上机内容的算法描述)设置两个变量分别指示头和尾。第一个和最后一个元素值互换然后头和尾变量向里移动最终到两变量相遇为止。五、程序清单.编写程序:从键盘输入一串整数保存到数组中调用函数antitone()将数组反序输出。自定义函数voidantitone(inta,intn)实现将数组中的n个数据按逆序存放。voidantitone(inta,intn){inti,jintki=j=nwhile(i<j){k=aiai=ajaj=ki=j=}}.已知某数列的前两项为和其后每一项为其前两项之积。编程实现:从键盘输入一个整数x判断并输出x最接近数列的第几项?#include<stdioh>#include<mathh>voidMad(inta,intn){intia=a=for(i=i<ni){ai=ai*ai}}intmain(void){inta,x,k,kintiMad(a,)产生序列printf("inputx:")scanf("d",x)i=for(x>aii)k=abs(xai)k=abs(xai)if(k>k)printf("x最接近第d项n",i)elseprintf("x最接近第d项n",i)return}、源程序如下:#include<stdioh>main(){charcinti,word=,num=,space=for(i=i<=i)scanf("c",ci)for(i=i<=i){if(ci=='')spaceif(ci>=''ci<='')numif(ci>'a'ci<'z')word}printf("字符:d数字:d空格:dn",word,num,space)}六、调试和测试结果、输入:输出:、输入:输出:x最接近第项、输入:sdkjjkmjk输出:字符:数字:空格:实验七数组和函数综合程序设计四、程序清单(请写出上机内容、中源程序)、​ 原程序求得的是下三角形经改进调试之后的程序为:#include<stdioh>#defineNmain(){inti,j,sum=intaNN={}printf("input×data:n")for(i=i<Ni){printf("Inputthedlinedata:n",i)for(j=j<Nj)scanf("d",aij)}for(i=i<Ni){for(j=j<Nj)printf("d",aij)printf("n")}for(i=i<Ni)for(j=Nj>=ij)sum=sumaijprintf("sum=dn",sum)}、voidfun(intttMN,intppN){inti,j,maxfor(j=j<Nj){max=ttjfor(i=i<Mi)if(ttij>max)max=ttijppj=max}}五、调试和测试结果(写出上机内容中填空的内容)、()sum=()tii()实验八指针四、程序清单(请写出上机内容中的函数)求出每个位上的数字然后放在千位上的数字乘以放在百位上的数字乘以放在位上的数字乘以然后相加。voidfun(inta,intb,long*c){inta,a,b,ba=aa=ab=bb=b*c=a*b*a*b}五、调试和测试结果(请写出上机内容的输出结果)()输出结果为:,,,()()()x=y=()x=y=()【】 int*p   【】 ai   【】pi输入:输出:实验九指针、函数和数组综合程序设计设计流程(算法描述)(请写出上机内容中的算法描述)五、程序清单.已知一个整型数组a其各元素值为,,,,。使用指针编程求数组元素之积。#include<stdioh>intmain(void){inta={,,,,},sumint*psum=for(p=ap<ap){sum*=*p}printf("dn",sum)return}.定义函数intf(char*x,chary)判断x所指的字符串中是否包含字符y若是则函数返回否则返回。intf(char*x,chary){char*pfor(p=x*p!=''p)if(*p==y){printf("cn",*p)return}return}定义函数voidf(floatx,int*y,float*z)将x的整数部分存于y所指的存储单元x的小数部分存于x所指的存储单元。voidf(floatx,int*y,float*z){*y=(int)x*z=x*y}六、调试和测试结果测试结果正确实验十结构体四、程序清单(请写出上机内容的源程序和上机内容中的函数)、上机内容的源程序#include<stdioh>#include<stringh>#defineFORMAT"dtstdtcn"structstudent{intnumcharnameintagecharsex}main(){voidinput(structstudentstu)voidstat(structstudentstu)structstudentstuintiinput(stu)for(i=i<i){printf(FORMAT,stuinum,stuiname,stuiage,stuisex)}stat(stu)}voidinput(structstudentstu){intifor(i=i<i){scanf("d",stuinum)getchar()scanf("s",stuiname)getchar()scanf("d",stuiage)getchar()scanf("c",stuisex)getchar()}}voidstat(structstudentstu){inti,c=,boy=,girl=for(i=i<i){if(stuiage<)c=if(stuisex=='m')boyelsegirl}printf("boytgirltage<n")printf("dtdtdn",boy,girl,c)}、voidfun(structSTREC*a){intia>ave=for(i=i<Ni)a>ave=a>sia>ave=N}、改错题正确的程序为:将printf("dscdn"*pnum*pnamepsexpage)改为:printf("dscdn"p>nump>namep>sexp>age)实验十一共用体、位运算和文件四、程序清单(请写出上机内容中的程序源代码)()​ 求以内能同时被和整除的自然数分别将它们输出到显示器屏幕和xtxt文件中。()用程序读出上述xtxt文件中的数据将它们输出到屏幕并求它们的和。#include<stdioh>#include<stringh>#include<stdlibh>intmain(void){inti,sumFILE*fdchars,*p,chif((fd=fopen("D:shitxt","wt"))==){printf("creatthefilefailedn")exit()}else{for(i=i<i){if((i==)(i==)){printf("d,",i)itoa(i,s,)转换成字符串fputs(s,fd)fputc('',fd)}}printf("n")fclose(fd)}提取字符转换成数字输入if((fd=fopen("D:shitxt","rt"))==){printf("openthefilefailedn")exit()}else{p=ssum=do{ch=fgetc(fd)if(ch==''){i=atoi(s)sum=iprintf("d",i)strset(s,'')p=s}else{*p=chp}}while(ch!=EOF)printf("数的和是:dn",sum)fclose(fd)}return}实验十二参考答案实验十二参考答案:(可根据情况弄清楚一个模块即可)题目:设某班有n位同学每位同学的数据包括以下内容:学号(长整型)、姓名(字符串)、数学成绩(整型)、程序设计成绩(整型)。设计程序完成以下五项功能:新建数据档案、添加数据、删除数据、对输入的数据进行排序和查询。注:输入数据时要求学号不能相同姓名可以相同。设计思路:).程序运行时首先显示主菜单(模块)如下:.程序运行时首先显示主菜单如下:.新建数据.添加数据.删除数据.排序.查询.退出用户输入序号后程序进行相应操作。).在主菜单中选择序号弹出子菜单选择排序方式子菜单如下:.数学成绩排序.程序设计成绩排序.总分排序。.返回主菜单选择子菜单的序号后程序能正确运行并在屏幕上显示按要求排序后的相关信息。.在主菜单中选择序号弹出子菜单选择查询方式子菜单如下:.学号查询.姓名查询.数学成绩查询.程序设计成绩查询.总分查询.返回主菜单请按序号选择相应操作 在子菜单中选择序号后程序按以下方式工作。()学号查询:输入学号后若该学号存在则显示与其相关的所有信息否则显示找不到的提示信息(提示:查询到满足条件的结果后查询即可结束)()姓名查询:输入姓名后若该姓名存在则显示与其相关的所有信息否则显示找不到的提示信息(提示:使用字符串比较函数进行比较)()按科目查询:输入指定分数程序运行后显示该科目中考试成绩大于等于指定分数的同学的学号、姓名以及该科成绩并统计满足条件的人数()总分查询:输入指定分数程序运行后显示总分成绩大于等于指定分数的同学的学号、姓名以及各科成绩并统计满足条件的人数。C源程序清单如下:#include"stdioh"#include"stdlibh"#include"stringh"#include"conioh"#include"memh"#include"ctypeh"#include"alloch"#defineNtypedefstructz{charnocharnameintscoreNfloatsumfloataverageintorderstructz*next}STUDENT*Functions*STUDENT*init()*initialize*STUDENT*create()STUDENT*delete(STUDENT*h)STUDENT*searchno(STUDENT*h)voidprint(STUDENT*h)voidsearch(STUDENT*h)voidsave(STUDENT*h)STUDENT*load()STUDENT*insert(STUDENT*h)STUDENT*sort(STUDENT*h)STUDENT*index(STUDENT*h)intmenuselect()*menu*******main*******main(){intiSTUDENT*headhead=init()clrscr()for(){switch(menuselect()){case:head=init()breakcase:head=create()breakcase:head=delete(head)breakcase:print(head)breakcase:search(head)breakcase:head=searchno(head)breakcase:save(head)breakcase:head=load()breakcase:head=insert(head)breakcase:head=sort(head)breakcase:case:case:head=index(head)breakcase:exit()}}}menuselect(){char*menu={"***************MENU***************","Initlist","Enterlist","Deletearecordfromlist","printlist","Searchrecordbyname","SearchrecordbyNumber","Savethefile","Loadthefile","insertrecordtolist","sortbytotalscores","sortbymathsscores","sortbyprogramscores","indexonnumber","Quit"}charsintc,igotoxy(,)printf("pressanykeycontinuen")getch()clrscr()gotoxy(,)textcolor(YELLOW)textbackground(BLACK)gotoxy(,)putch(xc)for(i=i<i)putch(xcd)putch(xbb)for(i=i<i){gotoxy(,i)putch(xba)gotoxy(,i)putch(xba)}gotoxy(,)putch(xc)for(i=i<i)putch(xcd)putch(xbc)window(,,,)clrscr()for(i=i<i){gotoxy(,i)cprintf("s",menui)}textbackground(BLACK)window(,,,)gotoxy(,)do{printf("nEnteryouchoice(~):")scanf("s",s)c=atoi(s)}while(c<||c>)returnc}STUDENT*init(){return}STUDENT*create(){intiintsSTUDENT*h=,*infofor(){info=(STUDENT*)malloc(sizeof(STUDENT))if(!info){printf("noutofmemory")return}inputs("enterno:(digitalsentertoexit)",info>no,)if(info>no=='')break*whenthefirstnumberis,break*inputs("entername:(<letters)",info>name,)printf("pleaseinputscoresn")s=*sissum,beginswith*for(i=i<NI){do{if(i==)printf("PleaseinputMathsscores:")if(i==)printf("PleaseinputProgramscores:")scanf("d",info>scorei)*socrestoresmathsscores,socorestoresprogramscores*if(info>scorei>||info>scorei<)printf("baddata,repeatinputn")}while(info>scorei>||info>scorei<)s=sinfo>scorei}info>sum=sinfo>order=info>next=hh=info}return(h)}inputs(char*prompt,char*s,intcount){charpdo{printf(prompt)scanf("s",p)if(strlen(p)>count)printf("ntoolong!n")}while(strlen(p)>count)strcpy(s,p)}*Printinfor*voidprint(STUDENT*h){inti=STUDENT*pclrscr()p=hprintf("nnn*******************************STUDENT**********************n")printf("|rec|NO|name|maths|program|sum|order|n")printf("||||||||n")while(p!=){iprintf("|d|s|s|d|d|f|d|n",i,p>no,p>name,p>score,p>score,p>sum,p>order)p=p>next}printf("**********************************end***************************n")}STUDENT*delete(STUDENT*h){STUDENT*p,*qcharsclrscr()printf("pleaseenterthenumberyouwanttodeleten")scanf("s",s)q=p=hwhile(strcmp(p>no,s)p!=){q=pp=p>next}if(p==)printf("nlistnosstudentn",s)else{printf("nnn******************************STUDENT**********************n")printf("|NO|name|maths|program|sum|order|n")printf("|||||||n")printf("|s|s|d|d|f|d|n",p>no,p>name,p>score,p>score,p>sum,p>order)printf("********************************end*****************************n")getch()if(p==h)h=p>nextelseq>next=p>nextfree(p)printf("nhavedeletedNosstudentn",s)}return(h)}STUDENT*searchno(STUDENT*h){STUDENT*p,*qcharsclrscr()printf("pleaseenterthenumberyouwanttosearchn")scanf("s",s)q=p=hwhile(strcmp(p>no,s)p!=){q=pp=p>next}if(p==)printf("nsNoFound!n",s)else{printf("nsFound!n",s)printf("nnn****************************STUDENT************************n")printf("|NO|name|maths|program|sum|order|n")printf("|||||||n")printf("|s|s|d|d|f|d|n",p>no,p>name,p>score,p>score,p>sum,p>order)printf("********************************end*****************************n")getch()}return(h)}voidsearch(STUDENT*h){STUDENT*pcharsclrscr()printf("pleaseenternameforsearchn")scanf("s",s)p=hwhile(strcmp(p>name,s)p!=)p=p>nextif(p==)printf("nsNoFound!n",s)else{printf("nsFound!n",s)printf("nnn****************************STUDENT**********************n")printf("|NO|name|maths|program|sum|order|n")printf("|||||||n")printf("|s|s|d|d|f|d|n",p>no,p>name,p>score,p>score,p>sum,p>order)printf("********************************end*****************************n")}}STUDENT*insert(STUDENT*h){STUDENT*p,*q,*infocharsints,iprintf("pleaseentertheNowhichthisrecordwillbelocatedbeforen")scanf("s",s)printf("npleasenewrecordn")info=(STUDENT*)malloc(sizeof(STUDENT))if(!info){printf("noutofmemory")return}inputs("enterno:(digitals)",info>no,)inputs("entername:(<letters)",info>name,)printf("pleaseinputscoresn")s=for(i=i<NI){do{if(i==)printf("PleaseinputMathsscores:")if(i==)printf("PleaseinputProgramscores:")scanf("d",info>scorei)if(info>scorei>||info>scorei<)printf("baddata,repeatinputn")}while(info>scorei>||info>scorei<)s=sinfo>scorei}info>sum=sinfo>order=info>next=p=hq=hwhile(strcmp(p>no,s)p!=){q=pp=p>next}if(p==)if(p==h)h=infoelseq>next=infoelseif(p==h){info>next=ph=info}else{info>next=pq>next=info}printf("nhaveinsertedsstudentn",info>name)return(h)}*SAVE*voidsave(STUDENT*h){FILE*fpSTUDENT*pcharoileprintf("Enteroilename,forexamplec:cstudenttxt:n")scanf("s",oile)if((fp=fopen(oile,"wb"))==){printf("cannotopenfilen")exit()}printf("nSavingfilen")p=hwhile(p!=){fwrite(p,sizeof(STUDENT),,fp)p=p>next}fclose(fp)printf("savesuccess!!n")}STUDENT*load(){STUDENT*p,*q,*h=FILE*fpcharinfileprintf("Enterinfilename,forexamplec:cstudenttxt:n")scanf("s",infile)if((fp=fopen(infile,"rb"))==){printf("cannotopenfilen")exit()}printf("nLoadingfile!n")p=(STUDENT*)malloc(sizeof(STUDENT))if(!p){printf("outofmemory!n")returnh}h=pwhile(!feof(fp)){if(!=fread(p,sizeof(STUDENT),,fp))breakp>next=(STUDENT*)malloc(sizeof(STUDENT))if(!p>next){printf("outofmemory!n")returnh}q=pp=p>next}q>next=fclose(fp)printf("Readdatasuccessful!n")returnh}*sort*STUDENT*sort(STUDENT*h){inti=STUDENT*p,*q,*t,*hh=h>nexth>next=while(h!=){t=hh=h>nextp=hq=hwhile(t>sumsump!=){q=pp=p>next}if(p==q){t>next=ph=t}else{t>next=pq>next=t}}p=hwhile(p!=){ip>order=ip=p>next}printf("sortsucess!!!n")returnh}*indexbynumber*STUDENT*index(STUDENT*h){STUDENT*p,*q,*t,*hh=h>nexth>next=while(h!=){t=hh=h>nextp=hq=hwhile(strcmp(t>no,p>no)>p!=){q=pp=p>next}if(p==q){t>next=ph=t}else{t>next=pq>next=t}}printf("indexsucess!!!n")returnh}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/29

2010C语言实验报告参考答案

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利