C语言程序设计实验报告2

2026/4/27 22:46:01

eaintheaccidentinvestigation,managementandreporting,eachpostshouldbedevelopedunderthissystemspecialistscheck,cleartheexaminations,time,cyclesandotherrelevantregulations.Strengtheningsitesupervisionandexamination,todetectandinvestigateillegalcommand,illegaloperationsandviolationsofoperatingrules.Secondsafetyreferstotheproductionsite,technologymanagement,equipment,facilities,andsooncanleadtoaccidentsrisksexist.1,accordingtotheextentofthesecurityrisks,solvingisdividedintoa,b,andclevelsofdifficulty;A-level:difficult,miningdifficulties,shallbereportedtothecompany'sproblems.B-class:difficulttoresolvedif 实验2 流程控制实验

2.1 实验目的

(1)掌握复合语句、if语句、switch语句的使用,熟练掌握for、while、do-while三种基本的循环控制语句的使用,掌握重复循环技术,了解转移语句与标号语句。 (2)熟练运用for、while、do-while语句来编写程序。 (3)练习转移语句和标号语句的使用。

(4)使用集成开发环境中的调试功能:单步执行、设置断点、观察变量值。 2.2 实验内容及要求 2.2.1.源程序改错

下面是计算s=n! 的源程序,在这个源程序中存在若干语法和逻辑错误。要求在计算机上对这个例子程序进行调试修改,使之能够正确完成指定任务。例如,8!=40320。 1 #include 2 int main() 3 { int i,n,s=1;

4 printf(\5 scanf(\6 for(i=1,i<=n,i++) 7 s=s*i;

8 printf(\9 }

10 return 0; 解答:

(1) 错误修改:

1) 输入字符格式错误,正确形式为:

scanf(“%d”,&n);

2) return 0;与}的相对位置错误,正确形式为:

return0; }

(2) 错误修改后的程序: #include

int main() { int i,n,s=1;

printf(\

scanf(\ for(i=1,i<=n,i++) s=s*i;

printf(\ return 0;

}

(3) 运行结果:

图1 2.2.1源程序改错测试图 2.2.2.源程序修改替换

(1)修改第1题,分别用while和do-while语句替换for语句。

iculties,shallconsistofminingorganizationstosolveproblems.C-class:fromsegmentsandbusinessrisksthatmustbeaddressedintheDepartment.2,open-pitmineunsafetypesinclude:electrical,transport,blasting,fire,andotherslope.3,accordingtotheseverityofthehazardfilledinbyunittroubleshooting,registrationform(seeatteaintheaccidentinvestigation,managementandreporting,eachpostshouldbedevelopedunderthissystemspecialistscheck,cleartheexaminations,time,cyclesandotherrelevantregulations.Strengtheningsitesupervisionandexamination,todetectandinvestigateillegalcommand,illegaloperationsandviolationsofoperatingrules.Secondsafetyreferstotheproductionsite,technologymanagement,equipment,facilities,andsooncanleadtoaccidentsrisksexist.1,accordingtotheextentofthesecurityrisks,solvingisdividedintoa,b,andclevelsofdifficulty;A-level:difficult,miningdifficulties,shallbereportedtothecompany'sproblems.B-class:difficulttoresolvedif 替换为While语句

1)在判断while语句前,要先定义i=1,再执行while语句。替换后的程序如下所示: #include int main() {

int i,n,s=1;

printf(\ scanf(\ i=1;

while (i<=n) {

s=s*i; i++; }

printf(\ return 0; }

2)运行截图及说明

图2 2.2.2(1)while语句测试图 替换为do-while语句

1) 要先定义i=1,再执行while语句。替换后的程序如下所示: #include int main() {

int i,n,s=1;

printf(\ scanf(\ i=1; do{ s=s*i; i++;

}while(i<=n);

printf(\ return 0; }

2) 运行截图及说明

iculties,shallconsistofminingorganizationstosolveproblems.C-class:fromsegmentsandbusinessrisksthatmustbeaddressedintheDepartment.2,open-pitmineunsafetypesinclude:electrical,transport,blasting,fire,andotherslope.3,accordingtotheseverityofthehazardfilledinbyunittroubleshooting,registrationform(seeatteaintheaccidentinvestigation,managementandreporting,eachpostshouldbedevelopedunderthissystemspecialistscheck,cleartheexaminations,time,cyclesandotherrelevantregulations.Strengtheningsitesupervisionandexamination,todetectandinvestigateillegalcommand,illegaloperationsandviolationsofoperatingrules.Secondsafetyreferstotheproductionsite,technologymanagement,equipment,facilities,andsooncanleadtoaccidentsrisksexist.1,accordingtotheextentofthesecurityrisks,solvingisdividedintoa,b,andclevelsofdifficulty;A-level:difficult,miningdifficulties,shallbereportedtothecompany'sproblems.B-class:difficulttoresolvedif 图3 2.2.2(2)do-while语句测试图

(2)修改第1题,输入改为“整数S”,输出改为“满足n!≥S的最小整数n”。例如输入整数40310,输出结果为n=8。

1)解题思路:先定义S,x=1,i=1,输入S,判断x<=S,如果为真,则有x=x*i,i=i+1,再返回判断x<=s,直至值为假,输出i-1的值。源程序为: #include int main() {

int S;

int x=1,i=1;

printf(\请输入整数S:\scanf(\while(x<=S) {

x=x*i; i=i+1; }

printf(\满足n!>=S的最小整数n是%d\return 0; }

2)运行截图及说明

图4 2.2.2(3)程序测试图

2.2.3.程序设计

(1) 假设工资税金按以下方法计算:x < 1000元,不收取税金;1000 ≤ x < 2000,收取5%的税金;2000 ≤ x < 3000,收取10%的税金;3000 ≤ x < 4000,收取15%的税金;4000 ≤ x < 5000,收取20%的税金;x>5000,收取25%的税金。输入工资金额,输出应收取税金额度,要求分别用if语句和switch语句来实现。 解答: if语句:

1) 解题思路:

1. 定义浮点数工资金额x,应收取税金额为y。 2. 输入工资金额x,判断x属于哪一个收入范围。 3. 如果x<1000,则y=0。

4. 如果x>=1000且x<2000,则y=5%*(x-1000)=0.05*x-50。

5. 如果x>=2000且x<3000,则y=5%*1000+10%*(x-2000)=0.1x-150。

6. 如果x>=3000且x<4000,则y=5%*1000+10%*1000+15%*(x-3000)=0.15x-300。 7. 如果x>=4000且x<5000,则y=5%*1000+10%*1000+15%*1000+20%*(x-4000)

=0.2x-500。 8. 如果

x>=5000,y=5%*1000+10%*1000+15%*1000+20%*1000+25%*(x-5000)=0.25x-750。 9. 输出y,结束。 2) 程序清单

#include

iculties,shallconsistofminingorganizationstosolveproblems.C-class:fromsegmentsandbusinessrisksthatmustbeaddressedintheDepartment.2,open-pitmineunsafetypesinclude:electrical,transport,blasting,fire,andotherslope.3,accordingtotheseverityofthehazardfilledinbyunittroubleshooting,registrationform(seeatteaintheaccidentinvestigation,managementandreporting,eachpostshouldbedevelopedunderthissystemspecialistscheck,cleartheexaminations,time,cyclesandotherrelevantregulations.Strengtheningsitesupervisionandexamination,todetectandinvestigateillegalcommand,illegaloperationsandviolationsofoperatingrules.Secondsafetyreferstotheproductionsite,technologymanagement,equipment,facilities,andsooncanleadtoaccidentsrisksexist.1,accordingtotheextentofthesecurityrisks,solvingisdividedintoa,b,andclevelsofdifficulty;A-level:difficult,miningdifficulties,shallbereportedtothecompany'sproblems.B-class:difficulttoresolvedif int main() {

float x,y;

printf(\请输入工资金额x:\ scanf(\ if(x<1000) y=0;

else if(x>=1000&&x<2000) y=0.05*x-50; else if(x>=2000&&x<3000) y=0.10*x-150; else if(x>=3000&&x<4000) y=0.15*x-300; else if(x>=4000&&x<5000) y=0.20*x-500; else y=0.25*x-750;

printf(\应收取税金额度为%f元\

} 3) 测试 测试用例 用例1 用例2 用例3 用例4 用例5 用例6 输入的工资金额x 500 1500 2500 3500 4500 5500 理论输出结果 0 25 100 225 400 625

图5 2.2.3(1)用例1测试图

图6 2.2.3(1)用例2测试图

图7 2.2.3(1)用例3测试图

iculties,shallconsistofminingorganizationstosolveproblems.C-class:fromsegmentsandbusinessrisksthatmustbeaddressedintheDepartment.2,open-pitmineunsafetypesinclude:electrical,transport,blasting,fire,andotherslope.3,accordingtotheseverityofthehazardfilledinbyunittroubleshooting,registrationform(seeatt


C语言程序设计实验报告2.doc 将本文的Word文档下载到电脑
搜索更多关于: C语言程序设计实验报告2 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219