资源描述
流程控制实验
一、 实验名称 流程操纵实验
二、实验目的
1.把握复合语句、if语句、switch语句的使用,熟练把握for、while、do-while三种差不多的循环操纵语句的使用,把握重复循环技术,了解转移语句与标号语句。
2.练习循环结构for、while、do-while语句的使用。
3.练习转移语句和标号语句的使用。
4.使用codeblocks开发环境中的调试功能:单步执行、设置断点、观看变量值。
三、 实验任务
1.源程序改错题
下面是运算s=n!的源程序,在那个源程序中存在若干语法和逻辑错误。要求在运算机上对那个例子程序进行调试修改,使之能够正确完成指定任务。例如,8!=40320。
#include <stdio.h>
void main()
{
int i,n,s=1;
printf("Please enter n:");
scanf("%d",n);
for(i=1,i<=n,i++)
s=s*i;
printf("%d! = %d",n,s);
}
2.源程序修改替换题
(1)修改第1题,分别用while和do-while语句替换for语句。
(2)修改第1题,输入改为“整数S”,输出改为“满足n!≥S的最小整数n”。例如输入整数40310,输出结果为n=8。
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语句来实现。
(2)编写一个程序,将输入的一行字符复制到输出,复制过程中将一个以上的空格字符用一个空格字符代替。
(3)打印如下杨辉三角形。
1 /*第0行 */
1 1 /*第1行 */
1 2 1 /*第2行 */
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
每个数据值能够由组合运算(表示第i行第j列位置的值),而的运算如下:
(i=0,1,2,…)
(j=0,1,2,3,…,i)
本程序中为了打印出金字塔成效,要注意空格的数目。一位数之间是3个空格,两位数之间有2个空格,3位数之间只有一个空格,程序编制过程中要注意区分。
(4)编写一个程序,将用户输入的任意正整数逆转,例如,输入1234,输出4321。
四、 实验步骤及结果
1.源程序改错题
更换后的正确程序:
#include <stdio.h>
int main(),
{
int i,n,s=1;
printf("Please enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
s=s*i;
printf("%d!=%d",n,s);
return 0;
}
2.源程序修改替换题
(1)while语句型替换程序
#include <stdio.h>
int main()
{
int i=1,n,s=1;
printf("Please enter n:");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
}
printf("%d!=%d",n,s);
return 0;
}
(2)do-while语句型替换程序
#include <stdio.h>
int main()
{
int i=1,n,s=1;
printf("Please enter n:");
scanf("%d",&n);
do
{
s=s*i;
i++;
}while(i<=n);
printf("%d!=%d",n,s);
return 0;
}
3.编程设计题
(1)税金收取问题程序:
If语句型:
#include<stdio.h>
int main()
{
double income,tax,a,b;
inx:
printf("Please enter the income:\n");
scanf("%lf",&income);
a=income/1000;
if(a>=0)
{if(a>=5)
b=0.25;
if(a>=4)
b=0.20;
if(a>=3)
b=0.15;
if(a>=2)
b=0.10;
if(a>=1)
b=0.05;
else
b=0.00;
tax=income*b;
printf("the tax is %.1lf",tax); }
else
{
printf("Enter error!\n");
goto inx;
}
}
运行结果:
Switch语句型:
#include<stdio.h>
int main()
{
double income,tax,a,b;
inx:
printf("Please enter the income:\n");
scanf("%lf",&income);
a=income/1000;
if(a<0)
{
printf("Enter error!\n");
goto inx;
}
else
switch((int)(a)){
case 0:b=0.00;break;
case 1:b=0.05;break;
case 2:b=0.10;break;
case 3:b=0.15;break;
case 4:b=0.20;break;
default:b=0.25;break;
} tax=income*b;
printf("The tax is %.1lf",tax);
return 0;
}
运行结果:
(2)字符复制输出和多个空白字符删除:
#include<stdio.h>
int main()
{
int flag=1;
char c;
printf("please intput some chars:\n");
while((c=getchar())!='\n')
{
switch(c)
{case' ':if(flag){putchar(c);
flag=0;}
break;
default:putchar(c);flag=1;
}
}
if((c=getchar())=='\n')
printf("%c",c);
return 0;
}
运行结果:
(3)杨辉三角:
#include<stdio.h>
#define M 10
#define N 10
int main()
{
int a[M][N],i,j;
for(i=0;i<M;i++)
{
for(j=N;j>=i;j--)
printf("%2c",' ');
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%3d ",a[i][j]);
if(i==j)
printf("\n");
}
}
}
运行结果:
(4)数字逆转:
#include<stdio.h>
int main()
{
long x, digit;
printf("please input a positive integer:\n");
scanf("%ld",&x);
do
{
digit=x%10;
printf("%ld",digit);
x/=10;
}while(x!=0);
printf("\n");
return 0;
}
运行结果:
五、 实验体会
通过这次C语言实验,我更进一步认识到了C语言学习理论、实践相结合的重要性。同时我也深深认识到if语句、if-else语句、switch、for、while以及do-while等在c语言实现中的重要作用,我会更加努力学习理论知识,时不时的上机实践来查找自己的不足进而是自己在C语言的学习上取得专门大的进步,提高自己的实践能力。
展开阅读全文