C语言程序设计教程实验.docx
《C语言程序设计教程实验.docx》由会员分享,可在线阅读,更多相关《C语言程序设计教程实验.docx(27页珍藏版)》请在冰豆网上搜索。
C语言程序设计教程实验
实验一C语言程序设计入门篇
实验1简单的C程序设计
一、实验目的
1、熟悉C语言上机环境及C语言的上机操作过程。
2、了解如何编辑、编译、连接和运行一个C程序。
3、掌握C语言中的数据类型。
4、掌握C语言中基本输入/输出函数的调用方法。
二、实验内容
1、输入三角形三边长,求三角形面积。
2、已知圆半径、圆柱高,求圆周长、圆柱体积。
3、输入一个摄氏温度,要求输出华氏温度。
公式为f=5/9*c+32。
三、实验方式:
一人一机
四、实验预做
1、#include“stdio.h”
#include“math.h”
voidmain()
{floata,b,c,s,area;
scanf(“%f,%f,%f”,&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“area=%f”,area);
}
2、#include“stdio.h”
voidmain()
{floatr,h,l,v,pi;pi=3.1415926;
scanf(“%f,%f”,&r,&h);
l=2*pi*r;
v=pi*r*r*h;
printf("l:
%6.2f\n",l);
printf("v:
%6.2f\n",v);
}
3、#include“stdio.h”
voidmain()
{floatc,f;scanf("%f",&c);f=5.0/9*c+32;
printf("%5.2f\n",f);
}
五、注意事项
六、实验总结
实验2选择结构程序设计
一、实验目的
1、掌握if语句和switch语句的基本结构。
2、掌握if语句的正确使用,尤其注意else和elseif语句的使用。
3、能利用if和switch语句编写选择结构程序。
二、实验内容
1、有一函数:
x(x<1)
y=2x-1(1
≤x<10)
3x-11(x
≥10)
写一程序,输入x值,输出
y值。
2、给一个百分制成绩,要求输出等级‘A’、‘B’、‘C’、‘D’、‘E’。
90分以上为‘A’,80—89分为‘B’,70—79分为‘C’,60—69分为‘D’,60分以下为‘E’。
3、输入4个整数,要求按由小到大的顺序输出。
三、实验方式:
一人一机
四、实验预做
1、#include“stdio.h”
voidmain()
{intx,y;
printf(“Inputx:
”);
scanf(“%d”,&x);
if(x<1)
{y=x;
printf(“x=%3d,y=x=%d\n”,x,y);
}
elseif(x<10)
{y=2*x-1;
printf(“x=%3d,y=2*x-1=%d\n”,x,y);
else
{y=3*x-11;
printf(“x=%3d,y=3*x-11=%d\n”,x,y);
}
}
2、#include“stdio.h”
voidmain()
{floatscore;
chargrade;
printf("Pleaseinputthestudent
’sscore:
");
scanf("%f",&score);
while(score>100||score<0)
{
printf("\nError!
Pleaseinputagain.");
}
scanf("%f",&score);
switch((int)(score/10))
{
case10:
case9:
grade=
‘A';break;
case8:
grade=
‘B';break;
case7:
grade=
‘C';break;
case6:
grade=
‘D';break;
case5:
case4:
case3:
case2:
case1:
case0:
grade=
‘E';
}
printf("Thescoreis%5.1f,thegradeis%c.\n",score,grade);
}
3、#include“stdio.h”
voidmain()
{inta,b,c,d,t;
printf(“Pleaseinput4integers:
”);
scanf(“%d,%d,%d,%d”,&a,&b,&c,&d);
printf(“\na=%d,b=%d,c=%d,d=%d\n”,a,b,c,d);
if(a>b){t=a;a=b;b=t;}
if(a>c){t=a;a=c;c=t;}
if(a>d){t=a;a=d;d=t;}
if(b>c){t=b;b=c;c=t;}
if(b>d){t=b;b=d;d=t;}
if(c>d){t=c;c=d;d=t;}
printf(“Thesortedis:
\n”);
printf(“%d,%d,%d,%d\n”,a,b,c,d);
五、注意事项
六、实验总结
实验3循环结构程序设计
一、实验目的
1、掌握while、do-while、for循环结构的使用方法以及循环条件的使用。
2、能够使用3种循环结构设计程序,分析程序并解决实际问题。
3、正确编写具有循环结构的C语言程序。
二、实验内容
1、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
2、求1!
+2!
++20!
。
3、打印九九表:
1*1=1
1*2=22*2=4
1*9=92*9=183*9=278*9=729*9=81
三、实验方式:
一人一机
四、实验预做
1、#include“stdio.h”
voidmain()
{charc;
intletter=0,space=0,digit=0,other=0;
printf(
“Inputaline:
\n
”);
while((c=getchar())!
=
‘\n’)
{if(c>=‘a’&&c<=‘z’||c>=‘A’&&c<=‘Z’)
letter++;
elseif(c==‘’)
space++;
elseif(c>=‘0’&&c<=‘9’)
digit++;
else
other++;
}
printf(“letter=%d,space=%d,digit=%d,other=%d\n”,letter,space,digit,other);
}
2#include“stdio.h”
voidmain()
{floats=0,t=1;intn;for(n=1;n<=20;n++)
{t=t*n;s=s+t;
}
printf("1!
+2!
++20!
=%e\n",s);
}
3#include“stdio.h”
voidmain()
{inti,j;for(i=1;i<=9;i++){for(j=1;j<=i;j++)
printf(“%3d*%d=%2d”,i,j,i*j);
printf(“\n”);
}
}
五、注意事项
六、实验总结
实验二C语言程序设计提高篇
实验1数组程序设计
一、实验目的
1、掌握数组的定义、赋值和输入输出的方法。
2、学习用数组实现相关的算法。
3、掌握在字符串中删除和插入字符的方法。
4、掌握C语言中字符数组的字符串处理函数的使用。
二、实验内容
1、输入10个整数,按每行3个数输出这些整数,最后输出
2、将一个数组中的值按逆序重新存放。
例如,原来顺序为
10个整数的平均值。
8,6,5,4,1。
要求改为
1,4,5,6,8。
3、在键盘上输入N个整数,利用选择排序法使该数组中的数按照从大到小的次序(升序)排列。
三、实验方式:
一人一机
四、实验预做
1、#include“stdio.h”
voidmain()
{inti,n=10,a[10],avg=0;for(i=0;iscanf(“%d”,&a[i]);
for(i=0;i{if(i%3==0)printf(“\n”);
printf(“%d”,a[i]);
}
for(i=0;iavg+=a[i];
avg=avg/n;
printf(“average=%f\n”,avg);
}
2、#include“stdio.h”
#defineN5
voidmain()
{inta[N],i,temp;
printf(“Enterarraya:
\n”);
for(i=0;iscanf(“%d”,&a[i]);
printf(“arraya:
\n”);
for(i=0;iprintf(“%4d”,a[i]);
for(i=0;i{temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}printf(“Now,arraya:
\n”);
for(i=0;iprintf(“%4d”,a[i]);
printf(“\n”);
}
3、#include“stdio.h”
#defineN10
voidmain()
{inta[N],i,j,r,temp;
printf(“Pleaseinput%dnumbers\n”,N);
for(i=0;iscanf(“%d”,&a[i]);
for(i=0;i{r=i;
for(j=i+1;jif(a[j]if(r!
=i)
{temp=a[r];a[r]=a[i];a[i]=temp;}
}
printf(“thearrayaftersort:
\nfor(i=0;i”);
printf(
“%5d”,a[i]);
printf(
“\n”);
}
五、注意事项
六、实验总结
实验2函数程序设计
一、实验目的
1、学习C语言中函数的定义和调用方法。
2、掌握通过参数在函数间传递数据的方法。
3、熟悉TurboC++3.0环境对函数程序的调试方法。
二、实验内容
1、写出下列程序的运行结果。
#include“stdio.h”
inta=5;intb=7;
voidmain()
{inta=4,b=5,c;
c=plus(a,b);
printf(“a+b=%d”,c);
}
plus(intx,inty)
{intz;z=x+y;
return(z);
}
2、写出下列程序的运行结果。
#include“stdio.h”
voidmain()
{intx;for(x=1;x<=3;++x)
f(x);
}
f(inty)
{staticinti=5;intj=3;j++;
printf(“%d+%d+%d=%d\n”,i,j,y,i+j+y);i*=2;
}
3、写一个函数,用“起泡法”对输入的10个字符按由小到大顺序排序。
三、实验方式:
一人一机
四、实验预做
1、a+b=9
2、5+4+1=10
10+4+2=16
20+4+3=27
3、#include“stdio.h”
#include“string.h”
#defineN11
charstr[N];
voidmain()
{inti,flag;for(flag=1;flag==1;)
{printf(“Pleaseinputastring:
\n”);
scanf(“%s”,str);
if(strlen(str)>N)
printf(“Error!
Pleaseinputagain:
\n
else
flag=0;
”);
}
sort(str);
printf(“Thesortedis:
”);
for(i=0;iprintf(“%c”,str[i]);
}
sort(charstr[N])
{inti,j;chart;for(j=1;jfor(i=0;(i=‘\0’);i++)
if(str[i]>str[i+1])
{t=str[i];str[i]=str[i+1];str[i+1]=t;}
}
五、注意事项
六、实验总结
实验3指针程序设计
一、实验目的
1、通过实验进一步掌握指针的概念,学会声明和使用指针变量。
2、正确使用数组的指针和指向数组的指针变量。
3、正确使用字符串的指针和指向字符串的指针变量。
二、实验内容
1、将字符数组中大写字符改为小写字符,将小写字符改为大写字符。
(用指针完成)
2、将一个字符串中的字符反序排列。
(用指针完成)
3、编写一个计算字符串长度的函数,模拟C的“string.h”函数库内的计算字符串长度的函数strlen()。
(用指针完成)
三、实验方式:
一人一机
四、实验预做
1、#include“stdio.h”
voidmain()
{charstr1[30]=“WelcometoCLanguage”;char*cp1=str1;
while(*cp1)
{if(*cp1>=‘A’&&*cp1<=‘Z’)
*cp1=*cp1+32;
elseif(*cp1>=‘a’&&*cp1<=‘z’)
*cp1=*cp1-32;
else
*cp1=*cp1;
cp1++;
}
puts(str1);
}
2、#include“stdio.h”
voidmain()
{charch[30],c,*cp1,*cp2;inti,n=0;
gets(ch);
cp1=ch;cp2=ch;
while(*cp2!
=‘\0’)
{n++;cp2++;}cp2--;
n=n/2;
for(i=0;i{c=*cp1;*cp1=*cp2;*cp2=c;cp1++;cp2--;
}
puts(ch);
}
3、#include“stdio.h”
intstrlength(char*s);
voidmain()
{intn1,n2;
char*list1=“Seenoevil,hearnoevil,
char*list2=“blindanddeaf?
”
n1=strlength(list1);
n2=strlength(list2);
puts(list1);
”;
printf(
“Thelengthoflist1=%d\n
”,n1);
puts(list2);
printf(
“Thelengthoflist2=%d\n
”,n2);
}
intstrlength(char*s)
{intn=0;
while(*s!
=‘\0’)
{n++;s++;}return(n);
}
五、注意事项
六、实验总结
实验六综合实验
一、实验目的
1、掌握C语言中的变量、数组、函数、指针、结构体等主要知识点。
2、掌握C程序的结构化程序设计方法,能使用C语言开发简单的应用程序。
3、掌握C程序的运行、调试方法等。
二、实验内容
编写一个学生信息排序程序。
要求:
1、程序运行时可输入n个学生的信息和成绩(n预先定义)。
2、学生信息包括:
学号、英文姓名、年龄;学生成绩包括:
语文、数学、计算机。
3、给出一个排序选择列表,能够按照上述所列信息(学号、姓名、年龄、语文、数学、
计算机)中的至少一个字段进行排序,并显示其结果。
1、使用函数方法定义各个模块。
三、实验方式:
一人一机
四、实验预做共10分
#include
#include
#defineN3
structstudent
{
intstunum;
charstuname[10];
intstuage;
intchinese;
intmath;
intcomputer;
intsum;
};1分
voidprintspace()
{
inti;
for(i=0;i<40;i++)
printf("_");
printf("\n");
}
voidprintinformation(structstudentstu[])
{
inti;
printf("stuNumstuNameAgeChinesemathcomputersumsort:
\n");
printspace();
for(i=0;i{printf("%10d%10s%4d%10d%10d%10d%10d%10d",stu[i].stunum,
stu[i].stuname,stu[i].stuage,stu[i].chinese,stu[i].math,
stu[i].computer,stu[i].sum,i+1);
printf("\n");
}1printspace();
}
voidreadinformation(structstudentstu[])
{
inti,m;
m=N;
printf("input%dstudentinformation:
\n",m);
for(i=0;i{printf("inputthe%dstudentstunum:
",i+1);scanf("%d",&stu[i].stunum);printf("inputthe%dstudentstuname:
",i+1);scanf("%s",stu[i].stuname);
printf("inputthe%dstudentstuage:
",i+1);scanf("%d",&stu[i].stuage);
printf("inputthe%dstudentchinesescore:
",i+1);scanf("%d",&stu[i].chinese);
printf("inputthe%dstudentmathscore:
",i+1);scanf("%d",&stu[i].math);
printf("inputthe%dstudentcomputerscore:
",i+1);scanf("%d",&stu[i].computer);
stu[i].sum=stu[i].chinese+stu[i].math+stu[i].computer;
}1
}
voidsortbystunum(structstudentstu[])
{
1
inti,j;
structstudentt;
for(i=0;ifor(j=0;jif(stu[j].stunum{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;1
}
}
voidsortbystuname(structstudentstu[])
{
inti,j;
structstudentt;
for(i=0;ifor(j=0;jif(strcmp(stu[j].stuname,stu[j+1].stuname)<0)
{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
1
}
}
voidsortbysum(structstudentstu[])
{
inti,j;
structstudentt;
for(i=0;ifor(j=0;jif(stu[j].sum{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
voidsortbystuage(structstudentstu[])
{
inti,j;
structstudentt;
for(i=0;ifor(j=0;jif(stu[j].stuage{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
voidsortbychinese(structstudentstu[])
{
inti,j;
structstudentt;
for(i=0;i
if(stu[j].chinese{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
voidsortbymath(structstudentstu[])
{
inti,j;
structstudentt;
for(i=0;ifor(j=0;jif(stu[j].math{t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}