1、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 ”void main() fl
2、oat a,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 ”void main() float r,h,l,v,pi; pi=3.1415926;scanf( “ %f,%f” ,&r,&h);l=2*pi*r;v=pi*r*r*h;printf(l:%6.2fn,l);printf(v:%6.2fn,v);3、#include “stdio.h ”void main() flo
3、at c,f; scanf(%f,&c); f=5.0/9*c+32;printf(%5.2fn,f);五、 注意事项六、 实验总结实验 2 选择结构程序设计一、 实验目的1、掌握 if 语句和 switch 语句的基本结构。2、掌握 if 语句的正确使用,尤其注意 else 和 else if 语句的使用。3、能利用 if 和 switch 语句编写选择结构程序。二、 实验内容1、有一函数:x (x1)y= 2x-1 (1x10)3x-11 (x10)写一程序,输入 x 值,输出y 值。2、给一个百分制成绩,要求输出等级 A、 B、 C、 D、 E。 90 分以上为 A, 8089 分为 B
4、, 70 79 分为 C, 6069 分为 D, 60 分以下为 E。3、输入 4 个整数,要求按由小到大的顺序输出。三、 实验方式:一人一机四、 实验预做1、#include “ stdio.h ”void main() int x,y;printf( “Input x: ”);scanf( “ %d”,&x);if(x1) y=x;printf( “x=%3d,y=x=%dn” ,x,y);else if(x100|scoreb) t=a; a=b; b=t; if(ac) t=a; a=c; c=t; if(ad) t=a; a=d; d=t; if(bc) t=b; b=c; c=t;
5、 if(bd) t=b; b=d; d=t; if(cd) t=c; c=d; d=t; printf( “The sorted is:n ”);printf( “%d,%d,%d,%dn”,a,b,c,d);五、 注意事项六、 实验总结实验 3 循环结构程序设计一、 实验目的1、掌握 while 、 do-while 、for 循环结构的使用方法以及循环条件的使用。2、能够使用 3 种循环结构设计程序,分析程序并解决实际问题。3、正确编写具有循环结构的 C 语言程序。二、 实验内容1、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。2、求 1!+2! + +20 !。3、打
6、印九九表: 1*1=11*2=2 2*2=41*9=9 2*9=18 3*9=27 8*9=72 9*9=81三、 实验方式:一人一机四、 实验预做1、#include “stdio.h ”void main() char c;int letter=0,space=0,digit=0,other=0;printf(“Input a line:n”);while(c=getchar()!=n ) if(c= a &c= A &c= 0 &c=9)digit+;elseother+;printf( “letter=%d,space=%d,digit=%d,other=%dn ”, letter,s
7、pace,digit,other);2 #include “stdio.h ”void main() float s=0,t=1; int n; for(n=1;n=20;n+) t=t*n; s=s+t;printf(1!+2!+ +20!=%en,s);3 #include “stdio.h ”void main() int i,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、掌握数组
8、的定义、赋值和输入输出的方法。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 ”void main() int i,n=10,a10,avg=0;
9、for(i=0;in;i+)scanf( “ %d” ,&ai);for(i=0;in;i+) if(i%3=0) printf( “n ”);printf( “%d”,ai);for(i=0;in;i+)avg+=ai;avg=avg/n;printf( “average=%fn ” ,avg);2、 #include “stdio.h ”#define N 5void main() int aN,i,temp;printf( “Enter array a:n ”);for(i=0;iN;i+)scanf( “ %d” ,&ai);printf( “array a:n ”);for(i=0;
10、iN;i+)printf( “%4d” ,ai);for(i=0;iN/2;i+) temp=ai; ai=aN-i-1; aN-i-1=temp; printf( “Now,array a:n ”);for(i=0;iN;i+)printf( “%4d” ,ai);printf( “n ”);3、#include “stdio.h ”#define N 10void main() int aN,i,j,r,temp;printf( “Please input %d numbersn ”,N);for(i=0;iN;i+)scanf( “%d”,&ai);for(i=0;iN-1;i+) r=
11、i;for(j=i+1;jN;j+)if(ajar) r=j;if(r!=i) temp=ar; ar=ai; ai=temp; printf( “the array after sort:n for(i=0;iN;i+)” );printf(“%5d”,ai);printf(“n ” );五、 注意事项六、 实验总结实验 2 函数程序设计一、 实验目的1、学习 C语言中函数的定义和调用方法。2、掌握通过参数在函数间传递数据的方法。3、熟悉 Turbo C+ 3.0 环境对函数程序的调试方法。二、 实验内容1、写出下列程序的运行结果。#include “ stdio.h ”int a=5; i
12、nt b=7;void main() int a=4,b=5,c;c=plus(a,b);printf( “a+b=%d” ,c);plus(int x,int y) int z; z=x+y;return (z);2、写出下列程序的运行结果。#include “ stdio.h ”void main() int x; for(x=1;xN)printf( “Error!Please input again:nelseflag=0;”);sort(str);printf( “The sorted is: ” );for(i=0;iN;i+)printf( “%c” ,stri);sort(ch
13、ar strN) int i,j; char t; for(j=1;jN;j+)for(i=0;(istri+1) t=stri; stri=stri+1; stri+1=t; 五、 注意事项六、 实验总结实验 3 指针程序设计一、 实验目的1、通过实验进一步掌握指针的概念,学会声明和使用指针变量。2、正确使用数组的指针和指向数组的指针变量。3、正确使用字符串的指针和指向字符串的指针变量。二、 实验内容1、将字符数组中大写字符改为小写字符,将小写字符改为大写字符。 (用指针完成)2、将一个字符串中的字符反序排列。 (用指针完成)3、编写一个计算字符串长度的函数,模拟 C 的“ string.h
14、 ”函数库内的计算字符串长度的函数 strlen() 。(用指针完成)三、 实验方式:一人一机四、 实验预做1、#include “stdio.h ”void main() char str130 = “ Welcome to C Language ”; char *cp1=str1;while(*cp1) if(*cp1 =A &*cp1= a &*cp1=z)*cp1=*cp1-32;else*cp1=*cp1;cp1+;puts(str1);2、#include “stdio.h ”void main() char ch30,c,*cp1,*cp2; int i,n=0;gets(ch)
15、;cp1=ch; cp2=ch;wh ile(*cp2!= 0 ) n+; cp2+; cp2-;n=n/2;for(i=0;in;i+)c=*cp1; *cp1=*cp2; *cp2=c; cp1+; cp2-;puts(ch);3、#include “stdio.h ”int strlength(char *s);void main() int n1,n2;char *list1= “ See no evil,hear no evil,char *list2= “ blind and deaf? ”n1=strlength(list1);n2=strlength(list2);puts(l
16、ist1);”;printf(“The length of list1=%dn”,n1);puts(list2);printf(“The length of list2=%dn”,n2);int strlength(char *s) int n=0;while(*s!= 0 ) n+; s+; return (n);五、 注意事项六、 实验总结实验六 综合实验一、 实验目的1、掌握 C语言中的变量、数组、函数、指针、结构体等主要知识点。2、掌握 C程序的结构化程序设计方法,能使用 C 语言开发简单的应用程序。3、掌握 C程序的运行、调试方法等。二、 实验内容编写一个学生信息排序程序。要求:1、
17、程序运行时可输入 n 个学生的信息和成绩( n 预先定义)。2、学生信息包括:学号、英文姓名、年龄;学生成绩包括:语文、数学、计算机。3、给出一个排序选择列表,能够按照上述所列信息(学号、姓名、年龄、语文、数学、计算机)中的至少一个字段进行排序,并显示其结果。1、使用函数方法定义各个模块。三、 实验方式:一人一机四、 实验预做 共 10 分#include #include #define N 3struct studentint stunum;char stuname10;int stuage;int chinese;int math;int computer;int sum; 1 分voi
18、d printspace()int i;for(i=0;i40;i+)printf(_);printf(n);void printinformation(struct student stu)int i;printf(stuNum stuName Age Chinese math computer sum sort:n);printspace();for(i=0;iN;i+) printf(%10d%10s%4d%10d%10d%10d%10d%10d,stui.stunum,stui.stuname,stui.stuage,stui.chinese,stui.math,puter,stui.
19、sum,i+1);printf(n); 1 printspace();void readinformation(struct student stu)int i,m;m=N;printf(input %d student information:n,m);for(i=0;iN;i+) printf(input the %d student stunum:,i+1); scanf(%d,&stui.stunum); printf(input the %d student stuname:,i+1); scanf(%s,stui.stuname);printf(input the %d stude
20、nt stuage:,i+1); scanf(%d,&stui.stuage);printf(input the %d student chinese score:,i+1); scanf(%d,&stui.chinese);printf(input the %d student math score:,i+1); scanf(%d,&stui.math);printf(input the %d student computer score:,i+1); scanf(%d,&puter);stui.sum=stui.chinese+stui.math+puter;1void sortbystu
21、num(struct student stu)1int i,j;struct student t;for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(stuj.stunumstuj+1.stunum) t=stuj;stuj=stuj+1;stuj+1=t; 1void sortbystuname(struct student stu)int i,j;struct student t;for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(strcmp(stuj.stuname,stuj+1.stuname)0) t=stuj;stuj=stuj+1;s
22、tuj+1=t;1void sortbysum(struct student stu)int i,j;struct student t;for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(stuj.sumstuj+1.sum) t=stuj;stuj=stuj+1;stuj+1=t;void sortbystuage(struct student stu)int i,j;struct student t;for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(stuj.stuagestuj+1.stuage) t=stuj;stuj=stuj+1;stuj+1=t;void sortbychinese(struct student stu)int i,j;struct student t;for(i=0;iN-1;i+)if(stuj.chinesestuj+1.chinese) t=stuj;stuj=stuj+1;stuj+1=t;void sortbymath(struct student stu)int i,j;struct student t;for(i=0;iN-1;i+)for(j=0;jN-i-1;j+)if(stuj.mathstuj+1.math) t=stuj;stuj=stuj+1;stuj+1=t;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1