1、实验三:顺序结构程序设计1编写一个程序,输入一个字符,输出它的前后邻居#include void main() char c1;printf(“Input a character: ”);c1=getchar();putchar(c1-1);putchar(t);putchar(c1+1);putchar(n); 2编写一个程序,求三角形的面积。其中,a、b、c 为三角形的三条边,s=(a+b+c)/2,面积为 area=#include #include void main() float a,b,c,s,area;printf(“Input a,b,c: ”);scanf(“%f,%f,%
2、f”,s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c);printf(“area=%fn”,area); 3编写一个程序,求摄氏温度。公式:c=5*(f-32)/9#include void main() float f,c;printf(“Input f: ”);scanf(“%f”,c=5*(f-32)/9;printf(“c=%fn”,c);4设计一个程序,输入正五边形的边长的长度 a,按公式计算该五边形外接圆的半径。#include #include void main() float r,a;printf(“Input a: ”);scanf(“%f
3、”,r=a*sqrt(10+2*sqrt(5)/5)/2;printf(“r=%0.2fn”,r); 5设计程序,输入空间中两点的坐标 x1,y1,z1和 x2,y2,z2计算两点之间的距离。#include #include void main() float x1,x2,y1,y2,z1,z2,d;printf(“Input x1,y1,z1,x2,y2,z2: ”);scanf(“%f,%f,%f,%f,%f,%f”,d=sqrt(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1);printf(“d=%fn”,z); 6编写一个 C程序,输入一
4、个 3位整数,分别输出百位数、十位数和个位数。#include void main() int x, a, b, c;printf(“Input a integer: ”);scanf(“%3d”,a=x/100;b=x/10%10;c=x%10;printf(“a=%d, b=%d, c=%dn”,a,b,c);5210aR)()(csbas实验四 选择结构程序设计1编写程序输入四个整数,要求输出最小的一个。#include void main() int a,b,c,d,min;printf(“Input a,b,c,d: ”);scanf(“%d,%d,%d,%d”,min=a;if(m
5、inb) min=b;if(minc) min=c;if(mind) min=d;printf(“min=%dn”,min); 2编写程序判断体重。体指数(t)=体重(w)/身高 2(h)其中,t=27 时为肥胖。#include void main() float t,w,h;printf(“Input w,h: ”);scanf(“%f,%f”,t=w/(h*h);if(tvoid main() float dan_jia,zong_e,ze_kou;int n;printf(“Input n: ”);scanf(“%d”,if(nvoid main() float dan_jia,zon
6、g_e,ze_kou;int n,m;printf(“Input n: ”);scanf(“%d”,m=(n-1)/5;4有一人过独木桥,到桥中间时看见前面有狼,后面有虎,底下是深深的河水。编程写出此人的最后结局。#include void main() char choice;printf(“Input your choice(a or b or c)”);scanf(“%c”,if(choice=a) printf(“bei lang chi len”);else if(choice=b) printf(“bei hu chi len”);else if(choice=c) printf(
7、“shuai si len”);else printf(“Input error!n”); 5从键盘输入 3个数,判断能否构成三角形,若能构成三角形,计算以这 3个数为边长的三角形面积;否则,输出相应的提示信息。#include #include void main() float a,b,c,s,area;printf(“Input a,b,c: ”);scanf(“%f,%f,%f”,if(a+bc area=sqrt(s*(s-a)*(s-b)*(s-c);printf(“area=%fn”,area);else printf(“bu neng gou cheng san jiao xi
8、nn”);6编写一个程序,输入某学生成绩,若成绩在 85分以上,则输出“Very Good”,若成绩在 60分到 85分之间,则输出“Good” ,若成绩低于 60分,则输出“Bad” 。#include void main() float grade;printf(“Input a studentgrade: ”);scanf(“%f”,if(grade100) printf(“Input error!n”);else if(gradevoid main() int year,month,days;printf(“Input year and month: ”);scanf(“%d,%d”,
9、if(year%4=0 else days=28;switch(month) case 1:case 3:case 5: case 7: case 8: case 10:case 12: days=31;break;case 4:case 6:case 9:case 11:days=30;printf(“days=%dn”,days); 实验五 循环结构程序设计1请分别用 while语句和 do-while语句编程求 的值。#include void main() float sum=0;int k=1;while(kvoid main() float sum=0;int k=1;do sum
10、=sum+k;k+; while(kvoid main() int n,i;float sum=0;printf(“Input n: ”);scanf(“%d”,i=1;while(ivoid main() int n,i;float sum=0;printf(“Input n: ”);scanf(“%d”,i=1;do sum=sum+i*i/(i+1.0);i+; while(ivoid main() char ch;While(ch=getchar()!=n)if(!(ch= | ch=0while(sumvoid main() float m,n,sum=0;m=1;n=2;while
11、(m/n=0.000001) sum=sum+m/n;m+=2;n*=2;printf(“sum=%fn”,sum); 6编程找出 1000以内的完数。所谓完数是它等于它的因子之和。如:6=1+2+3。#include void main() int m,s,i;for(m=2;m#define N 100void main() int sushuN=0,k,n;for(j=1;j#include #define N 100void main() float gradeN,aver,stdev,max,min;int m,k;printf(“Input m:”);scanf(“%d”,for(
12、k=0;kgradek) min=gradek;aver=aver/m;stdev=0;for(k=0;k#include #define N 20void main() int aN,i,j,p,temp;randomize();for(i=0;iaj) p=j;if(p!=i) temp=ap;ap=ai;ai=temp;printf(“After sorted: ”);for(i=0;i#include #define N 20void main() int aN,i,j,p,temp,x,f;randomize();for(i=0;iaj) p=j;if(p!=i) temp=ap;a
13、p=ai;ai=temp;for(i=0;i#include #define N 20void main() int aN,i,j,p,temp;int x,top,mid,bot,f;randomize();for(i=0;iaj) p=j;if(p!=i) temp=ap;ap=ai;ai=temp;for(i=0;i#include #define N 20void main() int aN,i,j,p,temp;int x;randomize();for(i=0;iaj) p=j;if(p!=i) temp=ap;ap=ai;ai=temp;for(i=0;i#include #de
14、fine N 20void main() int aN+1,i,j,p,temp;int x,f;randomize();for(i=0;iaj) p=j;if(p!=i) temp=ap;ap=ai;ai=temp;for(i=0;ii;f-)af=af-1;ai=x;for(i=0;i#include #define N 5void main() int aNN=0,i,j,k,p,q;randomize();for(i=0;iakp) q=k;if(i=q)printf(“a%d%d=%dn”,i,q,aiq); 2使用二维数组,打印杨辉三角形前 10行。#include #define
15、 N 10void main() int yhNN=0,i,j;for(i=0;i#include #include #define N 5void main() int aNN,i,j,x,y,t;randomize();clrscr();for(i=0;iaij) x=i;y=j;t=a00;a00=axy;axy=t;x=0;y=1;for(i=0;iaij) x=i;y=j;t=a04;a04=axy;axy=t;x=0;y=1;for(i=0;iaij) x=i;y=j;t=a40;a40=axy;axy=t;x=0;y=1;for(i=0;iaij) x=i;y=j;t=a44;a
16、44=axy;axy=t;printf(“n“);for(i=0;i#define N 10void main() int aNN,i,j;randomize();for(i=0;ivoid main() char s130,s230;int k;printf(“Input a string: ”);gets(s2);for(k=0;s2k!=0;k+)s1k=s2k;puts(s1); 2、有一篇文章,共有 3行文字,每行有 80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。#include voi main() char str381;int i,j,up
17、per=0,low=0,digit=0,space=0,other=0;printf(“Input three strings: ”);for(i=0;i=aint k;for(k=0;k0) strcpy(min,strk);printf(“The max is: %sn”,max);printf(“The min is: %sn”,min);4、有 n个字符串,要求按字母顺序从小到大对其排序。#include #include #define N 30#define M 20void main() char strMN,s30;int j,k,p;for(j=0;j0) p=k;if(p!
18、=j) strcpy(s,strj);strcpy(strj,strp);strcpy(strp,s);for(k=0;kint panduan(int n) int i,f=1;if(n=1 | n=0) f=0;for(i=2;i#define N 100void main() float gradeN;int k,total,n=0;int tongji(int a,int n);k=0;printf(“Input the No.%ds grade: ”,k+1);scanf(“%f”,while(gradek!=-1) printf(“Input the No.%ds grade: ”
19、,k+1);scanf(“%f”,k+;n+;total=tongji(grade,n);printf(“total=%dn”,total);int tongji(int x,int n) float aver=0;int m,k=0;for(m=0;maver) k+;return k;3、P103 第三题的第三小题。#include int gys(int m,int n) int r;if(mfloat pnx(int n,float x) if(n=0) return 1.0;else if(n=1) return x;else return (2*n-1)*pnx(n-1,x)-(n
20、-1)*pnx(n-2,x)/n);void main() float x,n,pn;printf(“Input x,n: ”);scanf(“%f,%d”,pn=pnx(n,x);printf(“%fn”,pn);2、运行汉诺塔程序void move(char x, char y) printf(“%c%cn”, x, y); void Hanoi(int n, char one, char two, char three) if(n=1) move(one, three);else Hanoi(n-1, one, three, two);move(one, three);Hanoi(n-1
21、, two, one, three); main( ) int m;printf(“input the number of diskes:”);)1(/)1()12(0) 2nxpnxpxpn scanf(“%d”, printf(“The step to moving %3d diskes:n”,m);Hanoi(m, A, B, C); 测试用例:4,10。 3、写一个函数,使输入的一个字符串反序存放。#include #include void main() char s150;void fun(char s);printf(“Input a string: ”);gets(s1);fu
22、n(s1);puts(s1);void fun(char s) int i,j;char ch;for(i=0,j=strlen(s)-1;i#define N 50int baoshu(int x,int n) int i,j,k;i=k=j=0;while(j#define MIN(a,b) (a)void swap(int *a,int *b) int x; x=*a;*a=*b;*b=x;void main() int x,y;printf(“Input x,y: ”);scanf(“%d,%d”,swap(printf(“x=%d, y=%dn”,x,y);2编写程序,用指针来实现数
23、组的输入和输出。数组定义如下:a10=1,2,3,4,5,6,7,8,9,0#include void main() int a10,*p=a,i;for(i=0;i#define N 10void input(int a,int n) int i;for(i=0;iai) min=i;if(amax#define N 50void mov(int a,int n,int m) int i,j,t;for(j=1;j=0;i-)ai+1=ai;a0=t; void main() int xN,n,m,i;printf(“Input n,m(nm): ”);scanf(“%d,%d”,for(i
24、=0;i#define N 5float fun1(float grade5) float aver,sum=0;int i;for(i=0;i=2) sum=0;for(k=0;k=90 | f=1) printf(“%st“,numi);for(k=0;k#include #include void add(void); /*向数据库中添加记录 */void query(void); /*按指定的序号查找相应的记录*/void listall(void); /*列出数据库中的全部记录*/void list(int n); /*列出给定序号的记录 */ struct employee int
25、 no; /*序号 */char name15; /*姓名*/char phone12; /*电话号码*/int age; ; /*年龄 */int index;struct employee record20;main() char ch;system(“cls”);while(1) printf(“n1.Enter new employeen”);printf(“2.Query employeen”);printf(“3.List alln”);printf(“4.Quitn”);printf(“Select:”);ch=getchar();switch(ch) case 1: add();break;case 2: query();break;case 3: listall();break;case 4: exit(0);default:break;