C语言 实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言实验报告答案”。
C语言程序设计(B)
(2010-2011-2)
实验报告
教学班级: 学号: 姓名: 课程教师: 实验辅导教师:
江西理工大学
自由编辑的程序
一、实验前的源程序:
//任意整数的叠加 #include void main(){ int i,j,sum=0;printf(“please input a int numbern”);scanf(“%d”,&j)for(i=0;i
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名10.cpp:7: parse error before
`for' [Error] D:Program FilesC-Free 4temp未命名10.cpp:7: parse error before `)' 构建中止 未命名10: 2 个错误, 0 个警告
实验后的源程序:
//任意整数的叠加 #include void main(){
int i,j,sum=0;printf(“please input a int numbern”);scanf(“%d”,&j);for(i=0;i
二、实验前的源程序:
/*小写字母转大写字母*/ #include void main(){ char c1,c2;c1='w';
江西理工大学
} c2='s';c1=c1-32;c2=c2-32;printf(“%c,%cn”,c1,c);
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名11.cpp:9: `c' undeclared(first
use this function)[Error] D:Program FilesC-Free 4temp未命名11.cpp:9:(Each undeclared
identifier is reported only once [Error] D:Program FilesC-Free 4temp未命名11.cpp:9: for each function it
appears in.)构建中止 未命名11: 3 个错误, 0 个警告
实验后的源程序:
/*小写字母转大写字母*/ #include void main(){ char c1,c2;c1='w';c2='s';c1=c1-32;c2=c2-32;printf(“%c,%cn”,c1,c2);}
三、实验前的源程序:
/*查看某一年是否为闰年*/ #include void main(){ int year,i;scanf(“%d”,&year);if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
i=1;
else
江西理工大学
i=0;
}
else
i=1;
} else
i=0;if(i)
printf(“%d 是闰年n”,year);else
printf(“%d 不是闰年n”,year);}
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名14.cpp:15: parse error before
`else' [Error] D:Program FilesC-Free 4temp未命名14.cpp:25: parse error at end
of input 构建中止 未命名14: 2 个错误, 0 个警告
实验后的源程序:
/*查看某一年是否为闰年*/ #include void main(){ int year,i;scanf(“%d”,&year);if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
i=1;
else
i=0;
}
else
i=1;
} else
i=0;if(i)
江西理工大学
printf(“%d 是闰年n”,year);else
printf(“%d 不是闰年n”,year);}
数据的输入和输出
四、程序改错题
改错前的源程序;#include void main(){ float x,y,z;scanf(“%5.2f,%5.2f”,&x,&y);z=x+y;printf(“z=%5.2f”,&z);} 修改后的源程序:
#include void main(){ float x,y,z;scanf(“%f%f”,&x,&y);z=x+y;printf(“z=%5.2f”,z);}
改错前的源程序;#include void main(){ int x=7654123;x*=10;printf(“x=%7d”,x);} 修改后的源程序:
江西理工大学
#include void main(){
long x=7654123;x*=10;printf(“x=%7d”,x);}
改错前的源程序:
#include void main(){ float c1=67;char c2;c2=c1+5;printf(“c1=%c,c2=%cn”,c1,c2);printf(“c1=%d,c2=%d”,&c1,&c2);} 修改后的源程序:
#include void main(){ int c1=67;char c2;c2=c1+5;printf(“c1=%c,c2=%cn”,c1,c2);printf(“c1=%d,c2=%d”,c1,c2);}
五、程序编写题:已知char ch’b’;int i=3 ,j=5;float x=22.354,y=435.6789;根据下面的输出结果编写程序。ch =’b’,ASCII=98 i=3□□□□□□j=5 x=22.35□□□y=435.68 实验前的源程序:
#include void main()
江西理工大学{ char ch='b';int i=3,j=5;float x=22.354,y=435.6789;printf(“ch='%c',ASCII=%dn”,ch,ch);printf(“i=%d
j=%dn”,i,j);printf(“x=%.2f
y=%.2fn”,x,y);} 实验错误报告:无 实验后的源程序:
#include void main(){ char ch='b';int i=3,j=5;float x=22.354,y=435.6789;printf(“ch='%c',ASCII=%dn”,ch,ch);printf(“i=%d
j=%dn”,i,j);printf(“x=%.2f
y=%.2fn”,x,y);}
六、从键盘输入一行字符,统计其中小写字母、大写字母和其它字符的个数:
实验前的源程序:
#include “stdio.h” void main(){ printf(“请任意输入一串字符:n”);
char ch,sum1=0,sum2=0,other=0;
ch=getchar();
while(c!='n')
{
if(c>='A'&&c
else if(c>='a'&&c
else other++;
c=getchar();
} printf(“大写字母的个数:%dn”,sum1);printf(“小写字母的个数:%dn”,sum2);
江西理工大学printf(“其他字符母个数:%dn”,other);}
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名7.cpp:7: `c' undeclared(first
use this function)[Error] D:Program FilesC-Free 4temp未命名7.cpp:7:(Each undeclared
identifier is reported only once [Error] D:Program FilesC-Free 4temp未命名7.cpp:7: for each function it
appears in.)构建中止 未命名7: 3 个错误, 0 个警告
实验后的源程序:
#include “stdio.h” void main(){ printf(“请任意输入一串字符:n”);
char ch,sum1=0,sum2=0,other=0;
ch=getchar();
while(ch!='n')
{
if(ch>='A'&&ch
else if(ch>='a'&&ch
else other++;
ch=getchar();
} printf(“大写字母的个数:%dn”,sum1);printf(“小写字母的个数:%dn”,sum2);printf(“其他字符母个数:%dn”,other);}
七、使用以下公式求∏的近似值,要求精确到最后一项的绝对值小于10e-4
∏/4=1-1/3+1/5-1/7+……
实验前的源程序:
#include “stdio.h” #include “math.h” main(){
江西理工大学
} float sum=0;int i,j;for(i=1;;i++){ j=2*i-1;if(1.0/j>0.0001){ sum+=pow(-1,i+1)*(1.o/j);continue;break;} printf(“∏=%fn”,sum*4.0);
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名9.cpp:13: nondigits in number
and not hexadecimal [Error] D:Program FilesC-Free 4temp未命名9.cpp:19: parse error at end
of input 构建中止 未命名9: 2 个错误, 0 个警告
实验后的源程序:
#include “stdio.h” #include “math.h” main(){ float sum=0;int i,j;for(i=1;;i++){
j=2*i-1;if(1.0/j>0.0001){ sum+=pow(-1,i+1)*(1.0/j);continue;} break;} printf(“∏=%fn”,sum*4.0);}
八、用选择法对10个整数排序: 实验前的源程序:
江西理工大学#include main(){ printf(“请输入一串乱序的10个整数:n”);int a[10];int i,j,k;for(i=0;i
scanf(“%d”,a[i]);} printf(“n”);for(i=0;i
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
k=a[j+1];} printf(“这10个整数从小到大排列为:”);for(j=0;j
printf(“%d ”,a[j]);} printf(“n”);printf(“这10个整数从大到小排列为:”);for(j=9;j>=0;j--){
printf(“%d ”,a[j]);}
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名1.cpp:33: parse error at end of
input 构建中止 未命名1: 1 个错误, 0 个警告
实验后的源程序:
//用选择法对10个整数排序
#include void main(){ printf(“请输入一串乱序的10个整数:n”);int a[10];
江西理工大学 int i,j,k;for(i=0;ia[j+1]){
k=a[j];
a[j]=a[j+1];
k=a[j+1];} } printf(“这10个整数从小到大排列为:”);for(j=0;j=0;j--){ printf(“%d ”,a[j]);} }
九、求一个3*3的整数矩阵对角线元素之积:
实验前的源程序:
#include void main(){ int a[3][3];int i,j,ji=1;printf(“请输入一个3*3的矩阵:n”);for(i=0;i
scanf(“%d”,&a[i][j])} for(i=0;i
江西理工大学 {
for(j=0;j
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
}
printf(“n”);
for(i=0;i
{
for(j=0;j
if(i=j)
ji*=a[i][j];
printf(“主对角线的积为:%dn”,ji);
} }
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名4.cpp:11: parse error before
`}' 构建中止 未命名4: 1 个错误, 0 个警告
实验后的源程序:
#include void main(){ int a[3][3];int i,j,ji=1;printf(“请输入一个3*3的矩阵:n”);for(i=0;i
scanf(“%d”,&a[i][j]);} for(i=0;i
for(j=0;j
{
printf(“%d ”,a[i][j]);
}
printf(“n”);
}
江西理工大学
printf(“n”);
for(i=0;i
{
for(j=0;j
if(i=j)
ji*=a[i][j];
printf(“主对角线的积为:%dn”,ji);
} }
十、将一个数组中的值按你需从新存放。例如,原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。
实验前的源程序:
#include void main(){ int a[10],i,j,t;printf(“请问你要输个几个数字: ”);scanf(“%d”,&j);printf(“请输入大小顺序的%d个数字: ”,j);for(i=0;i
scanf(“%d”,&a[i]);}
for(i=0;i
t=a[i];
a[i]=a[j-i-1];
t=a[j-i-1];} printf(“该数组逆序排列为:”);
for(i=0;i
printf(“%d ”,a[i]);printf(“n”);}
实验错误报告:
[Error] D:Program FilesC-Free 4temp未命名3.cpp:25: parse error at end
of input 构建中止 未命名3: 1 个错误, 0 个警告
江西理工大学实验后的源程序:
#include void main(){ int a[10],i,j,t;printf(“请问你要输个几个数字: ”);scanf(“%d”,&j);printf(“请输入大小顺序的%d个数字: ”,j);for(i=0;i
scanf(“%d”,&a[i]);}
for(i=0;i
t=a[i];
a[i]=a[j-i-1];
a[j-i-1]=t;} printf(“该数组逆序排列为:”);
for(i=0;i
printf(“%d ”,a[i]);} printf(“n”);}
江西理工大学
实验报告Ⅰ指导教师曾宪华实验时间: 2010 年10月日 学院通信与信息工程专业通信类班级0101105学号2011210150姓名陈环宇 实验室s313实验目的及要求:(1)掌握选择结构程序设计,学会......
实验报告一(注意:红字不用抄写,其它必须抄写。实验纸不够用写反面。)一、实验内容if语句、ifelse语句、ifelse if语句的练习二、重点难点分析题目,正确应用相应的选择语句三、实......
《C语言程序设计》实验报告实验名称 ____ C程序的运行环境和运行C程序的方法_学期日期同组人李江涛指导老师_杨雪松___________成绩___________ -------------------------......
南昌大学实验报告学生姓名: 学 号: 专业班级:√ 综合 □ 设计 □ 创新 实验日期: 实验成绩: 实验类型:□ 验证 □一.实验名称实验3 控制语句二.实验目的1.熟练掌握if 、if…else、i......
郑州轻工业学院 实 践 报 告实现内容:OJ1123最佳校友(数组)、OJ1158又是升序(指针)、OJ1180成绩统计(结构)、OJ1203做幻方(文件)学号:541507020140 学生姓名:王红旭专业班级:电子信息科......