欢迎来到天天文库
浏览记录
ID:26221432
大小:79.53 KB
页数:6页
时间:2018-11-25
《天津理工大学-c语言上机报告4》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验四(2009-9-24)一、实验名称:程序控制结构。二、实验目的:(1)掌握C语言基本程序结构(顺序、分支和循环)及其应用;(2)结合程序理解一些常用的算法(穷举、迭代和递推等);(3)进一步熟悉VisualC++6.0开发环境下调试程序的基本步骤。三、实验项目(1)源程序:#includeintmain(){intx;inty;//输入(Input)printf("Enteraninteger:");scanf("%d",&x);//处理(Process)if(x<1)y=x;elseif(x<6)y=3*x-2
2、;elsey=4*x-8;//输出(Output)printf("y=%d",y);return0;}(2)6采用if语句时,源程序为:#includeintmain(){intscore;//输入(Input)printf("Enterthescore:");scanf("%d",&score);//处理(Process)和输出(Output)if(score>100
3、
4、score<0)printf("Illegal");elseif(score>=90)printf("Excellent");elsei
5、f(score>=80)printf("Good");elseif(score>=70)printf("Middle");elseif(score>=60)printf("Passed");elseprintf("Failed");return0;}采用swtich语句时,源程序为:#includeintmain(){intscore;//输入(Input)printf("Enterthescore:");scanf("%d",&score);//处理(Process)和输出(Output)switch
6、(score/10){case10:case9:printf("Excellent");break;case8:printf("Good");break;case7:printf("Middle");break;case6:6printf("Passed");break;case5:case4:case3:case2:case1:case0:printf("Failed");break;default:printf("Illegal");break;}return0;}(1)源程序为:#include7、>intmain(){intdata1,data2;charop;doubleresult;//输入(Input)printf("Entertheexpression:data1opdata2");scanf("%d%c%d",&data1,&op,&data2);//处理(Process)switch(op){case'+':result=data1+data2;break;case'-':result=data1-data2;break;case'*':result=data1*data2;break;case'/':if(data8、2!=0)result=data1/data2;6else{printf("Dividedbyzero!");return1;}break;}//输出(Output)printf("%d%c%d=%7.2f",data1,op,data2,result);return0;}(1)源程序为:intmain(){intline;intloop;//处理(Process)和输出(Output)line=0;for(loop=0;loop<=1000;loop=loop+1)if(loop%13==0&&loop%7==0){printf(9、"%d",loop);++line;if(line%10==0)//每行打印10个数printf("");}printf("");return0;}(2)源程序为:#includeintmain(){intloop;intnum;intden;inttmp;doublesum;6//处理(Process)sum=0;num=2;den=1;for(loop=1;loop<=20;loop=loop+1){sum+=num*1.0/den;tmp=num;num=num+den;den=tmp;}//输出(Outpu10、t)printf("sum=%7.2f",sum);return0;}(1)源程序:#includeintmain(){intloop;intnum;int
7、>intmain(){intdata1,data2;charop;doubleresult;//输入(Input)printf("Entertheexpression:data1opdata2");scanf("%d%c%d",&data1,&op,&data2);//处理(Process)switch(op){case'+':result=data1+data2;break;case'-':result=data1-data2;break;case'*':result=data1*data2;break;case'/':if(data
8、2!=0)result=data1/data2;6else{printf("Dividedbyzero!");return1;}break;}//输出(Output)printf("%d%c%d=%7.2f",data1,op,data2,result);return0;}(1)源程序为:intmain(){intline;intloop;//处理(Process)和输出(Output)line=0;for(loop=0;loop<=1000;loop=loop+1)if(loop%13==0&&loop%7==0){printf(
9、"%d",loop);++line;if(line%10==0)//每行打印10个数printf("");}printf("");return0;}(2)源程序为:#includeintmain(){intloop;intnum;intden;inttmp;doublesum;6//处理(Process)sum=0;num=2;den=1;for(loop=1;loop<=20;loop=loop+1){sum+=num*1.0/den;tmp=num;num=num+den;den=tmp;}//输出(Outpu
10、t)printf("sum=%7.2f",sum);return0;}(1)源程序:#includeintmain(){intloop;intnum;int
此文档下载收益归作者所有