华师大C语言实验报告C实验报告2学号姓名.docx
《华师大C语言实验报告C实验报告2学号姓名.docx》由会员分享,可在线阅读,更多相关《华师大C语言实验报告C实验报告2学号姓名.docx(27页珍藏版)》请在冰豆网上搜索。
华师大C语言实验报告C实验报告2学号姓名
华东师范大学
上机实验报告
课程名称:
C语言程序设计
实验类型:
验证型
实验室名称:
*******
系别:
电子系
学号:
*******
专业:
通信工程
学生姓名:
*******
指导老师:
*******
学期:
2010年-2011第一学期
实验一嵌套结构
实验目的
(1)熟悉选择嵌套结构、循环嵌套结构、复合嵌套结构
(2)掌握嵌套结构的典型算法
实验内容
任务一 掌握使用if…else语句
1.题目要求
打开程序文件ex02_02.cpp,修改下面程序中的4个子函数中的代码 ,能按每一部分的要求得出相应的输出。
修改只限于增加圆括号,和代码的缩进方式。
当然代码的缩进方式对程序的执行没有影响,但可增加代码的可读性。
(也可能什么修改也不要做)。
#include
voidpaint_1(intx,inty);
voidpaint_2(intx,inty);
voidpaint_3(intx,inty);
voidpaint_4(intx,inty)
voidmain()
{
intx,y;
x=5,y=8;
paint_1(x,y);
paint_2(x,y);
paint_3(x,y);
y=7;
paint_4(x,y);
}
voidpaint_1(intx,inty)
{
printf("\nsection1:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
voidpaint_2(intx,inty)
{
printf("\nsection2:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
voidpaint_3(intx,inty)
{
printf("\nsection3:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
voidpaint_4(intx,inty)
{
printf("\nsection4:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
Section1:
假定x=5,y=8,输出为:
Section2:
假定x=5,y=8,输出为:
Section3:
假定x=5,y=8,输出为:
Section4:
假定x=5,y=7,输出为:
2.源程序清单
列出你修改后的四段if代码
voidpaint_1(intx,inty)
{
printf("\nsection1:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
voidpaint_2(intx,inty)
{
printf("\nsection2:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
{
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
}
voidpaint_3(intx,inty)
{
printf("\nsection3:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
{
printf("#####\n");
printf("$$$$$\n");
}
printf("&&&&&\n");
}
voidpaint_4(intx,inty)
{
printf("\nsection4:
\n");
if(y==8)
if(x==5)
printf("@@@@@\n");
else
{}
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
}
任务二编程个人所得税
P56习题3第2题
1.打开程序文件ex02_02.cpp
#include
doublecalTaxt(doublenumber);
intmain()
{
//输入一个工资
//调用calTax求解应缴个人所得税
//输出结果
}
doublecalTaxt(doublenumber)
{//计算应缴个人所得税
}
2.编写计算应缴个人所得税函数:
doublecalTaxt(doublenumber),返回应缴个人所得税。
在main函数中验证函数calTaxt的正确性。
源程序清单:
#include
doublecalTaxt(doublenumber);
intmain()
{doublenumber;
//输入一个工资
printf("number:
");
scanf("%lf",&number);
//调用calTax求解应缴个人所得税
doublecalTaxt(doublenumber);
//输出结果
printf("tax=%.2f\n",calTaxt(number));
return0;
}
doublecalTaxt(doublenumber)
{//计算应缴个人所得税
intrate;
if(number<=850)
rate=0;
else
if(number<=1350)
rate=5;
else
if(number<=2850)
rate=10;
else
if(number<=5850)
rate=15;
else
rate=20;
returnrate*(number-850)/100;
}
运行结果窗口
3.测试用例设计
1)number=750;
tax=0.00;
2)number=850;
tax=0.00;
3)number=1250;
tax=20.00;
4)number=1350;
tax=25.00;
5)number=1850;
tax=100.00;
6)number=2850;
tax=200.00;
7)number=3850;
tax=450.00;
8)number=5850;
tax=750.00;
9)number=6850;
tax=1200.00;
任务三编程统计成绩
创建程序文件ex02_03.cpp,完成下面功能,输入n个学生的学号和m门功课成绩求每位同学的总分和平均分,最后输出班级平均总分,最高总分和学号
1.源程序清单:
#include
intmain()
{
intn,m,i,j,y,z;
doublemax,sum,x,average,totle,averagetotle;
printf("n:
");
scanf("%d",&n);
printf("m:
");
scanf("%d",&m);
max=-1;
z=-1;
totle=0;
for(i=1;i<=n;i++)
{sum=0;
printf("y:
");
scanf("%d",&y);
for(j=1;j<=m;j++)
{
printf("x:
");
scanf("%lf",&x);
sum=sum+x;
}
printf("sum=%.2f",sum);
average=sum/m;
printf("average=%.2f\n",average);
if(sum>max)
{
max=sum;
z=y;
}
totle=totle+sum;
}
averagetotle=totle/n;
printf("averagetotle=%.2f\n",averagetotle);
printf("max=%.2f\n",max);
printf("z=%d\n",z);
return0;
}
2.运行结果窗口
任务四编程穷举法
创建程序文件ex02_04.cpp,使用穷举法求解下面问题:
鸡、兔同笼,共50个头,170只脚。
问鸡、兔各有多少只?
源程序清单:
#include
intmain()
{
intx,y;
for(x=1;x<=50;x++)
for(y=1;y<=50;y++)
{if((x+y==50)&&(2*x+4*y==170))
printf("x=%d,y=%d\n",x,y);
}
return0;
}
实验二模块化设计
实验目的
(1)体验结构化程序设计方法要点,理解模块化设计的思想,划分子模块的原则
(2)掌握is函数的定义和调用
实验内容
任务一 输出m-n之间所有的素数
改写(sample02_07.cpp):
输出m-n之间所有的素数,调用intisprime(intn)函数,每行输出10个素数。
1.源程序清单:
ex02_05.cpp
#include
#include
intisprime(intn);
intmain(void)
{
intcount,m;
count=0;/*count记录素数的个数,用于控制输出格式*/
for(m=2;m<=100;m++){
if(isprime(m))/*如果m是素数*/
{printf("%6d",m);/*输出m*/
count++;}/*累加已经输出的素数个数*/
if(count%10==0)/*如果count是10的倍数,换行*/
printf("\n");
}
printf("\n");
return0;
}
intisprime(intn)
{
inti;
for(i=2;i<=sqrt(n);i++)
if(n%i==0)
return0;
return1;
}
2.运行结果窗口
3.实现算法(自然语言描述)
(1)count用来记下素数的个数初值为0;
(2)m为2到100的整数;
(3)调用isprime函数判断,m如果质数输出m且修正count值;
(4)根据count的数值,按每十个为一行输出2到100的所有素数。
任务二 实现个人所得税的计算。
改写实验一的任务二ex02_02.cpp,使用switch语句实现函数doublecalTaxt(doublenumber)的功能。
1.源程序清单:
ex02_06.cpp
#include
doublecalTaxt(doublenumber);
intmain()
{doublenumber;
//输入一个工资
printf("number:
");
scanf("%lf",&number);
//调用calTax求解应缴个人所得税
doublecalTaxt(doublenumber);
//输出结果
printf("tax=%.2f\n",calTaxt(number));
return0;
}
doublecalTaxt(doublenumber)
{//计算应缴个人所得税
intrate,num;
if(number<=850)
num=1;
else
if(number>850&&number<=1350)
num=2;
else
if(number>1350&&number<=2850)
num=3;
else
if(number>2850&&number<=5850)
num=4;
else
if(number>5850)
num=5;
switch(num)
{
case1:
rate=0;break;
case2:
rate=5;break;
case3:
rate=10;break;
case4:
rate=15;break;
case5:
rate=20;break;
default:
break;
}
returnrate*(number-850)/100;
}
任务三输出100-400之间的水仙花数
1.要求:
创建程序文件ex02_07.cpp
(1)编写子函数判断一个数是否是水仙花数。
(2)编写子函数输出m到n之间的水仙花数。
(3)main函数验证函数的正确性
2.提示:
水仙花数是指每一位的立方和就是数本身,如153=1*1*1+5*5*5+3*3*3
3.代码实现
(1)源程序清单:
创建程序文件ex02_07.cpp
#include
#include
intissxhs(intt);
intshuchu(intm,intn);
intmain()
{
shuchu(100,400);
return0;
}
intissxhs(intt)
{
intsum=0,y;
y=t;
while(t>0)
{
sum=sum+pow(t%10,3);
t=t/10;
}
if(sum==y)
return1;
else
return0;
}
intshuchu(intm,intn)
{
inti;
for(i=m;i<=n;i++)
{
if(issxhs(i))
printf("%d\n",i);
}
return0;
}
(2)运行结果窗口:
4.子函数算法(自然语言描述)
(1)对100到400中的依次取出个位十位百位;
(2)将取出的三个数求立方和;
(3)比较此立方和与原来数字的关系,相等则输出此水仙花数。
实验三综合实验
实验目的
(1)理解变量的作用域:
全局变量和局部变量
(2)理解输出参数的概念和运用场合
(3)理解指针的概念
(4)掌握使用指针变量向主调函数传值的方法。
实验内容
任务一日期换算
1.要求:
创建程序文件ex02_08.cpp
(1)编写子函数判断闰年。
(2)编写子函数求某天是该年的几月几日
(3)编写子函数求某年某月某日是该年的第几天
(4)main函数中设计步骤验证函数的正确性
2.提示
(1)求某年某月某日是该年的第几天
思路:
累计从1月到该日期的前一月的每月天数再加上该日期的天数。
2月份的天数要判断闰年。
累计可使用循环完成也可利用switch语句无break的特性完成。
(2)求某天是该年的几月几日
思路:
循环i从1月开始如果天数days比第i月的天数大则天数days减去第i月的天数否则结束循环。
i+1即为所求月份,days的值为所求日数。
每月的天数仍需区分处理。
3.代码实现
(1)源程序清单:
#include
voidgetdays(intmon,intdat);
voidgetDate(inty,intdays,int*pm,int*pd);
intisLeap(inty);
intmain()
{
intm,d,y,days,year,month,date;
printf("yearanddays:
");
scanf("%d,%d",&y,&days);
printf("yearandmonthanddate");
scanf("%d,%d,%d",&year,&month,&date);
getDate(y,days,&m,&d);
getdays(month,date);
printf("%d-%d-%d\n",y,m,d);
printf("%d\n",days);
return0;
}
voidgetDate(inty,intdays,int*pm,int*pd)
{
intm,d,day,i;
for(i=1;i<=12;i++)
{
switch(i)
{
case1:
day=31;break;
case2:
if(isLeap(y))
day=29;
else
{day=28;}
break;
case3:
day=31;break;
case4:
day=30;break;
case5:
day=31;break;
case6:
day=30;break;
case7:
day=31;break;
case8:
day=31;break;
case9:
day=30;break;
case10:
day=31;break;
case11:
day=30;break;
case12:
day=31;break;
}
if(days>day)
{
days=days-day;
}
else
break;
}
*pm=i;
*pd=days;
}
voidgetdays(intmon,intdat)
{
intyear;
switch(mon-1)
{
case12:
dat=dat+31;
case11:
dat=dat+30;
case10:
dat=dat+31;
case9:
dat=dat+30;
case8:
dat=dat+31;
case7:
dat=dat+31;
case6:
dat=dat+30;
case5:
dat=dat+31;
case4:
dat=dat+30;
case3:
dat=dat+31;
case2:
if(isLeap(year))
dat=dat+29;
else
dat=dat+28;
case1:
dat=dat+31;
}
}
intisLeap(inty)
{
if(y%4==0&&y%100!
=0||y%400==0)
return1;
else
return0;
}
(2)运行结果窗口:
5.测试用例设计
(1)input:
year=2000;days=60;
output:
year:
2010;
month:
3;
date:
1;
(2)input:
2000-2-29;
output:
60;
6.子函数算法(自然语言描述)
(1)给年份和天数求日期
a)将天数与一月比较,若比一月大,减去一月天数再和下个月比较,直到比下个月天数小(注意平年闰年分开判断);
b)此时的月份即所求月份,所剩日子即所求日期。
(2)给年月日求是第几天
a)求出所给月份之前各月的天数和;
b)再将所给日期加进去就可以了。
任务二电费计算
问题描述
电力公司根据如下税率表进行收费,:
头300千瓦时以每千瓦时9美分计,下一个300千瓦时(到600千瓦时)以每千瓦时8美分计,再下一个400千瓦时(到1000千瓦时)以每千瓦时6美分计,超过1000千瓦时的电量以每千瓦时3美分计。
(1)编写一个函数,计算每个客户的总用电费用。
(2)写一个主函数,读取数据文件中的数据,调用计费函数计算每一个客户的总用电费用。
将结果输出到另一个数据文件。
源数据文件由客户编号和用电量两列构成,生成的新数据文件列出三列:
客户编号、用电量和总用电费用。
(3)程序还要计算并输出客户数量、总耗电千瓦时和总的电费。
分析问题
输入:
用电量
输出:
客户数量,总耗电千瓦时和总的电费
问题解决的方法:
设计算法
1.主函数算法
nums=0,qwss=0,results=0;
只读方式打开文档“用电量.txt”;
创建文档“输出.txt”;
如果文件不为空,循环(当文件不为空时):
格式化读取文件“用电量.txt”数据,客户编号num,用电量qws;
格式化写数据“该客户的客户编号、用电量和总用电费用”到文件“用电量2.txt”;
输出客户数量、总耗电千瓦时和总的电费
2.子函数算法
a)qws>1000,此部分电费*3;其余部分电费300*9+300*8+400*6;
b)1000>=qws>600,此部分电费*6,其余部分电费300*9+300*8;
c)600>=qws>300,此部分电费*8,其余部分电费300*9;
d)qws=<300,此部分电费*9;
编码:
ex02_09.cpp
变量及数据类型的确定:
整型(int):
客户电费result每个客户用电量qws,总客户数nums,总用电量qwss,总电费results,客户编号长整型num
FILE结构指针:
*fp1,*fp2;
源程序清单:
#include
intgetshuif(intm);
intmain()
{
intresult,qws,nums=0,qwss=0,results=0;
longnum;
FILE*fp1,*fp2;
if((fp1=fopen("用电量.txt","r"))==NULL)
{
printf("fileopenerror!
\n");
return-1;
}
if((fp2=fopen("用电量2.txt","w"))==NULL)
{
printf("fileopenerror2!
\n");
return-1;
}
while(!
feof(fp1))
{
fscanf(fp1,"%ld%d\n",&num,&qws);
intgetshuif(intm);
result=getshuif(qws);
fprintf(fp2,"%ld%d%d\n",num,qws