天津理工大学c语言上机报告4.docx
《天津理工大学c语言上机报告4.docx》由会员分享,可在线阅读,更多相关《天津理工大学c语言上机报告4.docx(8页珍藏版)》请在冰豆网上搜索。
![天津理工大学c语言上机报告4.docx](https://file1.bdocx.com/fileroot1/2023-4/20/50a8e898-449e-43d1-87ab-1d008520cfd6/50a8e898-449e-43d1-87ab-1d008520cfd61.gif)
天津理工大学c语言上机报告4
天津理工大学-c语言上机报告4
实验四
(2009-9-24)
-、实验名称:
程序控制结构。
二、实验目的:
(1)掌握c语言基本程序结构(顺序、分支和循环)及其应用;
(2)结合程序理解一些常用的算法(穷举、迭代和递推等);
(3)进一步熟悉VisualC++6.0开发环境下调试程序的基本步骤。
三、实验项目
(1)
源程序:
#include
intmain()
{
intx;
inty;
〃输入(Input)
printf("Enteraninteger:
\n");scanf("%d",&x);
〃处理(Process)if(x<1)
y=x;elseif(x<6)
y=3*x-2;
else
y=4*x-8;
〃输出(Output)printf("y=%d\n",y);return0;
}
(2)采用if语句时,源程序为:
#includeintmain()
{
intscore;
〃输入(Input)
printf("Enterthescore:
\n");
scanf("%d",&score);
//处理(Process)和输出(Output)if(score>100||score<0)
printf("lllegal\n");elseif(score>=90)
printf("Excellent\n");
elseif(score>=80)
printf("Good\n");
elseif(score>=70)
printf("Middle\n");elseif(score>=60)
printf("Passed\n");else
printf("Failed\n");
return0;
采用
}
swtich语句时,源程序为:
#includeintmain()intscore;
//输入(Input)printf("Enterthescore:
\n");
scanf("%d",&score);
//处理(Process)和输出(Output)
switch(score/10)
{
case10:
case9:
printf("Excellent\n");break;
case&
printf("Good\n");break;
case7:
printf("Middle\n");break;
case6:
printf("Passed\n");break;
case5:
case4:
case3:
case2:
case1:
case0:
printf("Failed\n");break;
default:
printf("lllegal\n");break;
}return0;
}
(3)源程序为:
#include
intmain()
{
intdata1,data2;
charop;
doubleresult;
〃输入(Input)
printf("Entertheexpression:
data1opdata2\n");scanf("%d%c%d",&data1,&op,&data2);
//处理(Process)
switch(op)
case・+-
resu-fHda5r」+da5r2
break
case.':
resu-fHda5rrda5r2
break
case示-
resu-fHdaEr-tdaErN
break
case5
=K(da5r2H0)
resu-fHda5rwda5r2
e-se
宀
prinffcuMdedbyzero一kr=
refumr
break-
二彗圧(Oufpuf)prinff(=%d%c%dH%7・2f\n=daErIJopda5r2二esu-口
return0;
}intmain()
{
intline;
intloop;
//处理(Process)和输出(Output)line=0;
for(loop=0;loopv=1000;loop=loop+1)
if(loop%13==0&&loop%7==0)
{
printf("%d",loop);
++line;
if(line%10==0)//每行打印10个数
printf("\n");
}
printf("\n");
return0;
}
(5)源程序为:
#include
intmain()
{
intloop;
intnum;
intden;
inttmp;
doublesum;
〃处理(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;
}
II输出(Output)
printf("sum=%7.2f\n",sum);
return0;
}
(6)
源程序:
#inelude
intmain()intloop;
intnum;
intn_even;
intsum;
II输入和处理(Process)sum=0;
n_even=0;
for(loop=1;loopv=10;loop=loop+1)
seanf("%d",&num);
—if(num%2==0)
{
sum+=num;
++n_even;
}
}//输出(Output)
printf("偶数之和sum=%d\n",sum);
printf("偶数的平均数ave=%7.2f\n",sum*10/n_even);
return0;
intmain()
{
intloop;
doublefn;
doublefact;
doublesum;
//处理(Process)sum=0;
fn=1.0;
loop=1;
fact=1;
while(fn>=1e-6)
{
sum+=fn;
fact*=loop;fn=1/fact;
loop=loop+1;
}//输出(Output)
printf("sum=%7.5f\n",sum);
return0;
}