1、 C语言程序设计试验汇报 一、 试验名称 步骤控制试验 二、试验目标 1.掌握复合语句、if语句、switch语句使用,熟练掌握for、while、do-while三种基础循环控制语句使用,掌握反复循环技术,了解转移语句和标号语句。 2.练习循环结构for、while、do-while语句使用。 3.练习转移语句和标号语句使用。 4.使用codeblocks开发环境中调试功效:单步实施、设置断点、观察变量值。 三、 试验任务 1.源程序改错题 下面是计算s=n!源程序,在这个源程序中存在若干语法和逻辑错误。要求
2、在计算机上对这个例子程序进行调试修改,使之能够正确完成指定任务。比如,8!=40320。
#include
3、整数40310,输出结果为n=8。 3.编程设计题 (1)假设工资税金按以下计算方法计算:x<1000元,不收税金;1000<=x<,收取5%税金;<=x<3000,收取10%税金;3000<=x<4000,收取15%税金;4000<=x<5000,收取20%税金;x>5000, 收取25%税金。编写一个程序,输入工资金额,输出应收取税金,要求用if和switch语句来实现。 (2)编写一个程序,将输入一行字符复制到输出,复制过程中将一个以上空格字符用一个空格字符替换。 (3)打印以下杨辉三角形。 1
4、 /*第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
5、 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)编写一个程序,将用户输入任意正整数逆转,比如,输入1
6、234,输出4321。
四、 试验步骤及结果
1.源程序改错题
更改后正确程序:
#include
7、rintf("Please enter n:");
scanf("%d",&n);
while(i<=n)
{
s=s*i;
i++;
}
printf("%d!=%d",n,s);
return 0;
}
(2)do-while语句型替换程序
#include
8、 s=s*i;
i++;
}while(i<=n);
printf("%d!=%d",n,s);
return 0;
}
3.编程设计题
(1)税金收取问题程序:
If语句型:
#include
9、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; } } 运行结果: Sw
10、itch语句型:
#include
11、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
12、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
13、 #define M 10
#define N 10
int main()
{
int a[M][N],i,j;
for(i=0;i
14、}
}
运行结果:
(4)数字逆转:
#include






