C语言程序设计实验报告
1 实验目的
第一个实验:
1熟练掌握顺序结构程序设计;
2学会正确使用逻辑运算符及逻辑表达式;
3熟练掌握if、if…else、if…else if语句,掌握if语句中的嵌套关系和匹配原则,利用if语句实现分支选择结构;
4熟练掌握switch语句格式及使用方法,利用switch语句实现分支选择结构
5掌握简单、常用的算法,并在编程过程中体验各种算法的编程技巧,进一步学习调试程序,掌握检查语法错误和逻辑错误的的方法。
第二个实验:
1熟练掌握while语句、do-while语句和for语句格式及使用方法,掌握三种循环控制语句的循环过程以及循环结构的嵌套,利用三种循环语句实现循环结构;
2掌握简单、常用的算法,并在编程过程中体验各种算法的编程技巧;
3进一步学习调试程序,掌握语法错误和逻辑错误的检查方法。
2 实验内容
第一个实验:
输入一个整数m,将其数值按照大于0,小于等于0两个类别分类显示。
要求:
1该程序利用if-else语句实现;
2将变量m定义为整型;
3通过scanf函数输入变量m的值;
4输入整数m前,利用puts()或printf()给出提示信息;
5输出结果时有必要的说明,例如:输入358时,显示 358 is greater than 0,输入-23时,显示 -23 is less than or equal to 0;
6运行程序,分别输入123,0,-22,并检查输出信息的的正确性。
第二个实验:
输入一串字符(以回车键结束),统计其中数字、大写字母、小写字母、以及其他字符的个数。
要求:
1通过键盘输入字符串;
2在输入字符串之前给出相应提示;
3按照数字、大写字母、小写字母和其他字符数的顺序输出结果。
3 算法描述流程图
4 源程序
第一个实验:
#include<stdio.h>
main()
{
int m;
scanf("%d",m);
printf("enter a character:");
{
if(m>0)
printf("%d is greater than 0",m);
else
printf("%d is less than or equal to 0",m);
}
}
第二个实验:
#include<string.h>
#include<stdio.h>
main()
{
char c;
int w=0,x=0,y=0,z=0;
printf("enter characters:");
while((c=getchar())!='\n')
{
if((c>='0'&&c<='9'))
w++;
else if(c>='A'&&c<='Z')
x++;
else if(c>='a'&&c<='z')
y++;
else
z++;
}
printf("w=%d,x=%d,y=%d,z=%d",w,x,y,z);
}
5 测试数据
第一个实验:
测试数据为:123,0,-22
第二个实验:
所输入的字符串为:
6 运行结果
7 出现问题及解决方法
1第一个实验中,对于“printf("%d is greater than 0",m);”的输入存在一些问题;通过查课本,找到类似输出形式的写法;
2第二个实验中需要用到函数string,通过百度查阅弄懂这个函数的使用方法,还是对这个用法不太了解。
8 实验心得
1对if,if-else,if-else if语句的使用方法及具体含义有了更深次的了解,对编写条件控制结构的程序有了一定的认识和思路;
2对于do-while,while了解不太深刻,需要进一步学习和上机实验的练习进行巩固和熟练;
3对于调试程序中出现的error和warning的解决方法有了更深的认识。
第二篇:C语言 实验报告
C语言程序设计(B)
(2010-2011-2)
实验报告
教学班级: 学号: 姓名:
课程教师: 实验辅导教师:
江西理工大学 1
自由编辑的程序
一、实验前的源程序:
//任意整数的叠加
#include<stdio.h>
void main()
{
int i,j,sum=0;
printf("please input a int number\n");
scanf("%d",&j)
for(i=0;i<=j;i++)
sum=sum+i;
printf("此数的叠加=%d\n",sum);
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名10.cpp:7: parse error before
`for'
[Error] D:\Program Files\C-Free 4\temp\未命名10.cpp:7: parse error before `)'
构建中止 未命名10: 2 个错误, 0 个警告
实验后的源程序:
//任意整数的叠加
#include<stdio.h>
void main()
{
int i,j,sum=0;
printf("please input a int number\n");
scanf("%d",&j);
for(i=0;i<=j;i++)
sum=sum+i;
printf("此数的叠加=%d\n",sum);
}
二、实验前的源程序:
/*小写字母转大写字母*/
#include<stdio.h>
void main()
{
char c1,c2;
c1='w';
江西理工大学 2
}
c2='s'; c1=c1-32; c2=c2-32; printf("%c,%c\n",c1,c);
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名11.cpp:9: `c' undeclared (first
use this function)
[Error] D:\Program Files\C-Free 4\temp\未命名11.cpp:9: (Each undeclared
identifier is reported only once
[Error] D:\Program Files\C-Free 4\temp\未命名11.cpp:9: for each function it
appears in.)
构建中止 未命名11: 3 个错误, 0 个警告
实验后的源程序:
/*小写字母转大写字母*/
#include<stdio.h>
void main()
{
char c1,c2;
c1='w';
c2='s';
c1=c1-32;
c2=c2-32;
printf("%c,%c\n",c1,c2);
}
三、实验前的源程序:
/*查看某一年是否为闰年*/
#include <stdio.h>
void main()
{
int year,i;
scanf("%d",&year);
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
i=1;
else
江西理工大学 3
i=0;
}
else
i=1;
}
else
i=0;
if(i)
printf("%d 是闰年\n",year);
else
printf("%d 不是闰年\n",year);
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名14.cpp:15: parse error before
`else'
[Error] D:\Program Files\C-Free 4\temp\未命名14.cpp:25: parse error at end
of input
构建中止 未命名14: 2 个错误, 0 个警告
实验后的源程序:
/*查看某一年是否为闰年*/
#include <stdio.h>
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)
江西理工大学 4
printf("%d 是闰年\n",year); else
printf("%d 不是闰年\n",year); }
数据的输入和输出
四、程序改错题
改错前的源程序;
#include <stdio.h>
void main()
{
float x,y,z;
scanf("%5.2f,%5.2f",&x,&y); z=x+y;
printf("z=%5.2f",&z); }
修改后的源程序:
#include <stdio.h>
void main()
{
float x,y,z;
scanf("%f%f",&x,&y); z=x+y;
printf("z=%5.2f",z);
}
改错前的源程序;
#include<stdio.h>
void main()
{
int x=7654123;
x*=10;
printf("x=%7d",x);
}
修改后的源程序:
江西理工大学 5
#include<stdio.h>
void main()
{
long x=7654123;
x*=10;
printf("x=%7d",x);
}
改错前的源程序:
#include <stdio.h>
void main()
{
float c1=67;
char c2;
c2=c1+5;
printf("c1=%c,c2=%c\n",c1,c2);
printf("c1=%d,c2=%d",&c1,&c2);
}
修改后的源程序:
#include <stdio.h>
void main()
{
int c1=67;
char c2;
c2=c1+5;
printf("c1=%c,c2=%c\n",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 <stdio.h>
void main()
江西理工大学 6
{
char ch='b';
int i=3,j=5;
float x=22.354,y=435.6789;
printf("ch='%c',ASCII=%d\n",ch,ch);
printf("i=%d j=%d\n",i,j);
printf("x=%.2f y=%.2f\n",x,y);
}
实验错误报告:无
实验后的源程序:
#include <stdio.h>
void main()
{
char ch='b';
int i=3,j=5;
float x=22.354,y=435.6789;
printf("ch=\'%c\',ASCII=%d\n",ch,ch);
printf("i=%d j=%d\n",i,j);
printf("x=%.2f y=%.2f\n",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<='Z') sum1++;
else if(c>='a'&&c<='z') sum2++;
else other++;
c=getchar();
}
printf("大写字母的个数:%d\n",sum1);
printf("小写字母的个数:%d\n",sum2);
江西理工大学 7
printf("其他字符母个数:%d\n",other);
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名7.cpp:7: `c' undeclared (first
use this function)
[Error] D:\Program Files\C-Free 4\temp\未命名7.cpp:7: (Each undeclared
identifier is reported only once
[Error] D:\Program Files\C-Free 4\temp\未命名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<='Z') sum1++;
else if(ch>='a'&&ch<='z') sum2++;
else other++;
ch=getchar();
}
printf("大写字母的个数:%d\n",sum1);
printf("小写字母的个数:%d\n",sum2);
printf("其他字符母个数:%d\n",other);
}
七、使用以下公式求∏的近似值,要求精确到最后一项的绝
对值小于10e-4 ∏/4=1-1/3+1/5-1/7+……
实验前的源程序:
#include "stdio.h"
#include "math.h"
main()
{
江西理工大学 8
} 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("∏=%f\n",sum*4.0);
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名9.cpp:13: nondigits in number
and not hexadecimal
[Error] D:\Program Files\C-Free 4\temp\未命名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("∏=%f\n",sum*4.0);
}
八、用选择法对10个整数排序:
实验前的源程序:
江西理工大学 9
#include <stdio.h>
main()
{
printf ("请输入一串乱序的10个整数:\n");
int a[10];
int i,j,k;
for(i=0;i<10;i++)
{
scanf("%d",a[i]);
}
printf("\n");
for(i=0;i<10;i++)
for(j=0;j<10-j;j++)
{
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
k=a[j+1];
}
printf("这10个整数从小到大排列为:");
for(j=0;j<10;j++)
{
printf("%d ",a[j]);
}
printf("\n");
printf("这10个整数从大到小排列为:");
for(j=9;j>=0;j--)
{
printf("%d ",a[j]);
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名1.cpp:33: parse error at end of
input
构建中止 未命名1: 1 个错误, 0 个警告
实验后的源程序:
//用选择法对10个整数排序
#include <stdio.h>
void main()
{
printf ("请输入一串乱序的10个整数:\n");
int a[10];
江西理工大学 10
int i,j,k;
for(i=0;i<10;i++)
{
scanf("%d",a[i]);
}
printf("\n");
for(i=0;i<10;i++)
for(j=0;j<10-j;j++)
{
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
k=a[j+1];
}
}
printf("这10个整数从小到大排列为:");
for(j=0;j<10;j++)
{
printf("%d ",a[j]);
}
printf("\n");
printf("这10个整数从大到小排列为:");
for(j=9;j>=0;j--)
{
printf("%d ",a[j]);
}
}
九、求一个3*3的整数矩阵对角线元素之积: 实验前的源程序:
#include <stdio.h>
void main()
{
int a[3][3];
int i,j,ji=1;
printf("请输入一个3*3的矩阵:\n");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j])
}
for(i=0;i<3;i++)
江西理工大学 11
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(i=j)
ji*=a[i][j];
printf("主对角线的积为:%d\n",ji);
}
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名4.cpp:11: parse error before
`}'
构建中止 未命名4: 1 个错误, 0 个警告
实验后的源程序:
#include <stdio.h>
void main()
{
int a[3][3];
int i,j,ji=1;
printf("请输入一个3*3的矩阵:\n");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
江西理工大学 12
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(i=j)
ji*=a[i][j];
printf("主对角线的积为:%d\n",ji);
}
}
十、 将一个数组中的值按你需从新存放。例如,原来顺序为8,
6,5,4,1。要求改为1,4,5,6,8。
实验前的源程序:
#include <stdio.h>
void main()
{
int a[10],i,j,t;
printf("请问你要输个几个数字: ");
scanf("%d",&j);
printf("请输入大小顺序的%d个数字: ",j);
for(i=0;i<j;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<j/2;i++)
{
t=a[i];
a[i]=a[j-i-1];
t=a[j-i-1];
}
printf("该数组逆序排列为:");
for(i=0;i<j;i++)
{
printf("%d ",a[i]);
printf("\n");
}
实验错误报告:
[Error] D:\Program Files\C-Free 4\temp\未命名3.cpp:25: parse error at end
of input
构建中止 未命名3: 1 个错误, 0 个警告
江西理工大学 13
实验后的源程序:
#include <stdio.h>
void main()
{
int a[10],i,j,t;
printf("请问你要输个几个数字: "); scanf("%d",&j);
printf("请输入大小顺序的%d个数字: ",j); for(i=0;i<j;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<j/2;i++)
{
t=a[i];
a[i]=a[j-i-1];
a[j-i-1]=t;
}
printf("该数组逆序排列为:"); for(i=0;i<j;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
江西理工大学 14