C语言程序设计实验报告——张蒙蒙

重庆邮电大学移通学院

C语言程序设计实验报告

学 生: 张蒙蒙

学 号: 0214110122

班 级: 一班

专 业: 数字媒体

重庆邮电大学移通学院

1

重庆邮电大学移通学院

目 录

第一章 基础掌握............................................................................................................ 3

1.1 实验目的 ................................................................................... 错误!未定义书签。

1.2 实验要求 ................................................................................................................. 3

1.3 实验基本内容.......................................................................................................... 3

第二章 数据类型............................................................................................................ 4

2.1 实验目的 ................................................................................................................. 4

2.2 实验要求 ................................................................................................................. 4

2.3 实验基本内容........................................................................................................... 4

第三章 选择结构程序设计 ............................................................................................... 5

3.1 实验目的 ................................................................................................................ 5

3.2 实验要求 ................................................................................................................. 5

3.3 实验基本内容.......................................................................................................... 5

第四章 循环程序设计 .................................................................................................... 7

4.1 实验目的 ................................................................................................................. 7

4.2 实验要求 ................................................................................................................. 7

4.3 实验基本内容.......................................................................................................... 5

第五章 程序设计............................................................................................................ 7

5.1 实验目的 ................................................................................................................. 7

5.2 实验要求 ................................................................................................................. 7

5.3实验基础内容 .................................................................................................... 8

2

重庆邮电大学移通学院

第一章 基础掌握

1.1 实验目的

掌握C程序设计编程环境Visual C++,掌握运行一个C程序的基本步骤,包括编辑、编译、链接和运行。

1.2 实验要求

在报告中记录建立、保存C程序实习过程。

1.3 实验基本内容

编写程序,在屏幕上显示一个短句“Hello World”

(1) 程序:

1) 编译如下程序:

#include<stdio.h>

Void main()

{

printf (“Hello World!\n”);

}

2)链接。执行Build-命令。

3)运行。执行Build Execute 命令。显示结果。

4)关闭程序工作区。

(2) 运行结果:

输出结果:Hello World!

总结:

掌握C语言程序设计的基本框架,能够编写简单的程序。

3

重庆邮电大学移通学院

第二章 数据类型

2.1 实验目的

(1) 掌握C语言的运算符和表达式的正确使用以及C语言的几种基本数据类型和基本输入输出函数的使用方法。

(2)通过编程进一步理解和掌握运算符的确切含义和功能。

2.2 实验要求

(1) 在报告中记录建立、保存C程序实习过程。 (2) 完成典型题目,分析遇到的困难和实验中的收获。

2.3 实验基本内容

输入程序,观察输出结果 1>./*test2.1c*/

1)编译如下程序: #include <stdio.h> void main() {

char ch; int k;

ch='a',k='b';

printf("%d,%x,%c",ch,ch,ch,ch,k); printf("k=%%d\n",k) }

2)链接。执行Build-命令。

3)运行。执行Build Execute 命令。显示结果。

4)关闭程序工作区。

2>./*test2.2c*/

1)编译如下程序: #include <stdio.h> void main() {

float a1,a2; double b1,b2; a1=1234.12; a2=0.000001; b1=1234.12;

b2=0.000001;

printf("%f,%l f\n",a1+a2,b1+b2);

}

2)链接。执行Build-命令。

3)运行。执行Build Execute 命令。显示结果。

4)关闭程序工作区。

实验结果:

97.61,141,ak=xd

实验结果:

1234.120089,1234.120001

总结:

学会C语言的运算符和表达式的正确使用

4

重庆邮电大学移通学院

第三章 选择结构程序设计

3.1 实验目的

学会改正程序中的错误

3.2 实验要求

(1) 在报告中记录建立、保存C程序实习过程。

(2) 完成典型题目,分析遇到的困难和实验中的收获。

3.3 实验基本内容

输入实验程序,程序有错,改正程序中的错误

1)输入如下程序:

#include<stdio.h>

#include<math.h>

void main()

{

double a,b,c,d;

printf("输入一元二次方程a=,b=,c=\n");

scanf("a=%l f,b=%l f",&a,&b,&c); if(a==0) { if(b=0) } { } else Print f("x=%0.2f\n",-c/b); if(c==0) Print f("0==0参数对方程无意义!”); else Print f("c!=0方程不成立"); else if(d>=0) { printf("x1=%0.2f\n",(-b+sq rt(d))/(2*a)); printf("x1=%0.2f\n",(-b-sq rt(d))/(2*a)); } else {

5

重庆邮电大学移通学院

printf("x1=%0.2f+%0.2fi\n",-b/(2*a),s q rt(-d)/(2*a)); printf("x1=%0.2f-%0.2fi\n",-b/(2*a),s q rt(-d)/(2*a));

}

}

2)链接。执行Build-命令。

3)运行。执行Build Execute 命令。显示结果。

4)关闭程序工作区。

实验结果:

运行不了程序 ,做以下修改:

#include<stdio.h>

#include<math.h>

void main()

{

//

//

double a,b,c,d; printf("输入一元二次方程a=,b=,c=\n"); scanf("a=%l f,b=%l f",&a,&b,&c); c=5; if(a==0) //{ /*if(b=0) { } else printf("x=%0.2f\n",-c/b); if(c==0) printf("0==0参数对方程无意义!”); else printf("c!=0方程不成立"); }*/ else if(d>=0) { } else { } printf("x1=%0.2f\n",(-b+sq rt(d))/(2*a)); printf("x1=%0.2f\n",(-b-sq rt(d))/(2*a)); printf("x1=%0.2f+%0.2fi\n",-b/(2*a),sq rt(-d)/(2*a)); printf("x1=%0.2f-%0.2fi\n",-b/(2*a),sq rt(-d)/(2*a));

}

这样就可以输出程序了。

总结: 学会使用逻辑运算符和逻辑表达式,掌握简单的查错方法,并改错。

6

重庆邮电大学移通学院

第四章 循环程序设计

4.1 实验目的

熟练使用while\do…while语句实现循环程序设计。

4.2 实验要求

(1) 在报告中记录建立、保存C程序实习过程。

(2) 完成典型题目,分析遇到的困难和实验中的收获。

4.3 实验基本内容

1) 编译如下程序:

#include<stdio.h>

void main()

{

int m,n,j,k; printf("input m n\n"); while(scan f ("%d",&m,&n),m<0&&n<0); j=m; while(j/n!=0) j=j+m; k=(m*n)/j; printf("最小公倍数是%d\n最大公约数是%d\n",j,k); }

2)链接。执行build-命令。

3)运行。执行build execute 命令。显示结果。

4)关闭程序工作区。

实验结果:

Input m n

5 2

然后运算出来结果:

最大公倍数是5

最大公约数是0

总结:

学使用for,while,do...while语句实现循环程序,

7

重庆邮电大学移通学院

第五章 程序设计

5.1 实验目的

检验最近学习成果,进一步巩固C语言的学习

5.2 实验要求

1)输入任意四位数,写出个,十,百,千位上的数 2)连接任意两个字符串

5.3 实验基本内容

1)编译如下程序: #include<stdio.h> void main() {

int a,b,c,d,x; scanf("%d",&x); if(999<x<10000) {

a=x/1000;

b=x/100-a*10;

c=x/10-a*100-b*10; d=x-a*1000-b*100-c*10; printf("%d\n",x);

1)编译如下程序: #include<stdio.h> main() {

char s1[10],s2[5]; int i,j;

scanf("%s",s1); scanf("%s",s2); i=j=0;

while(s1[i]!='\0') i++;

printf("a=%d,b=%d,c=%d,d=%d\n",a,

b,c,d); }

}

while((s1[i++]=s2[j++])!='\0'); printf("string no.1. %s\n",s1); }

2)链接。执行build-命令。

3)运行。执行build execute 命令。显示结果。

4)关闭程序工作区。

2)链接。执行build-命令。

3)运行。执行build execute 命令。显示结果。

4)关闭程序工作区。

实验结果: abcd

adsg

string no.1.abcdadsf

实验结果:

2678 2678

a=2,b=6,c=7,d=8

总结:通过对程序的设计提高编程能力

8

 

第二篇:C语言实验编程答案

实验三 顺序结构和输入/输出方法 3-1-(1)

#include "stdio.h" main()

{int m=345; float n=2.56;

printf("m=%d,n*100=%f\n",m,n*100); }

3-1-(2)

#include "stdio.h" main()

{double a,b,c,s,v;

scanf("%lf%lf%lf",&a,&b,&c); s=a*b; v=a*b*c;

printf("%f,%f,%f\n",a,b,c); printf("s=%f,v=%f\n",s,v); }

3-2-(1)

#include "stdio.h" main() {int m,n;

float x=3.5,y=6.2;

scanf("%d,%d",&m,&n); }

3-2-(2)

#include "stdio.h" main() {int a,b; long m,n; float p,q;

printf("a=%d,b=%d\n m=%ld,n=%ld\n",a,b,m,n);

printf("p=%f,q=%f\n",p,q); }

3-3

#include "stdio.h" main()

{int i,j,k,ss,m=1,n=1; scanf("%d,%d",&i,&j); m+=i++; n*=--j;

ss=(k=m+2,n-5,k+n); printf("%d\n",ss); } 3-4

#include "stdio.h" main()

{char c1,c2; c1=getchar(); c2=getchar(); putchar(c1); putchar(c2); printf("\n");

printf("%c,%c\n",c1,c2); } 3-5

#include "stdio.h" main()

{int a=790,m,n; m=a/60; n=a%60;

printf("The time is %dh %dmin\n",m,n); } 3-6

#include "stdio.h" main()

{float a,b,c,t;

scanf("%f%f%f",&a,&b,&c); t=c; c=b; b=a; a=t;

printf("a=%f,b=%f,c=%f\n",a,b,c); }

1

3-7

#include "stdio.h" main()

{double a,m,n; scanf("%lf",&a); m=a*1000; n=(int)m%10; if (n>=5) m+=1; m=m/1000;

printf("m=%0.2f\n",m); }

实验四 逻辑运算和判断选取控制 4-1

#include "stdio.h" main() {int a,b;

scanf("%d%d",&a,&b); switch (a*a+b*b) {case 3:

case 1: printf("y=%d\n",a+b);break; case 0: printf("y=%d\n",b-a);break; }

switch(a)

{default: printf("x=%d\n",a+b);break; case 10: printf("y=%d\n",a-b);break; case 11: printf("x=%d\n",a*b);break; }

switch(a+b)

{case 10: printf("x=%d\n",a+b);break; case 11: printf("y=%d\n",a-b);break; } }

4-2-(1)

#include "stdio.h" main() { int x,y;

scanf("%d",&x); printf("%d",y); }

4-2-(2)

#include "stdio.h" main() {int x,y;

scanf("%d",&x);

printf("%d",y); } 4-3

#include "stdlib.h" #include "stdio.h" main() {int a,M;

scanf("%d",&a); printf("abs(a)=%d\n",M); } 4-4

#include "stdio.h" main() { int c,t,m;

printf("input the number of coat and trousers your want buy:\n");

scanf("%d%d",&c,&t); if (c>=50) m=c*80; if (t>=50) m=t*80+(c-t)*60; else m=c*90+(t-c)*45; printf("m=%d\n",m); }

2

4-5

#include "stdio.h" main()

{float x,y,z;

scanf("%f%f%f",&x,&y,&z);

if(x+y<=z||x+z<=y||y+z<=x) printf("data error! in put again:\n");

else if (x==y&&y==z) printf("这是等边三角5-3

#include "stdio.h" main() {int i,j;

for(i=1;i<=4;i++) {if (i==1||i==4)

{for (j=1;j<=6;j++) {printf(" *");} 形\n");

else if (x==y||y==z||z==x) printf("这是等腰三角形\n");

else printf("这是不等边三角形\n");

} 4-6

#include "stdio.h" main() { int a;

scanf("%d",&a);

if (a%5==0&&a%7==0) printf("yes\n"); else printf("no\n"); }

实验五 循环结构 5-2

#include "stdio.h" main() { int c;

while ((c=getchar())!='\n')

{ c+=2;

else if (c=='y'||c=='Y'||c=='z'||c=='Z') }

putchar('\n'); }

}

else for (j=1;j<=6;j++)

{if (j==1||j==6) {printf(" *");} else printf(" "); }

printf("\n"); } } 5-4

#include "stdio.h" main() { int i,j;

for (i=0;i<10;i++) {if (i==0)

{printf("*");}

else printf("%4d",i); }

printf("\n");

printf("------------------------------------\n"); for (j=1;j<=9;j++) {printf("%d",j);

for (i=1;i<=j;i++) {printf("%4d",i*j);} printf("\n"); } }

3

5-5

#include "stdio.h" main()

{double e=1.0,a=1.0; int k=1,n=1; do { a/=k; e+=a; k++; n++;

}while (a>=1.e-5);

printf("e=%f,n=%d\n",e,n); } 5-6

#include "stdio.h" main()

{ int i,j,k=0,sum=0,n=1; for(i=1;i<=1000;i++) {for(j=1;j<i;j++) {if(i%j==0) k+=j; }

if (i==k) { if(n==1) { printf("%d",i); n=2; } else printf("+%d",i); sum+=i; } k=0; }

printf("=%d\n",sum); }

实验六 数 组 6-1

#include "stdio.h" main()

{ int dengji[6]; int i,number;

for (i=1;i<=5;++i) dengji[i]=0; printf("Enter your number\n"); for (i=1;i<=20;++i)

{scanf ("%d",&number); if (!number) break; ++dengji[number]; }

printf("\nResalt of dengji\n"); printf("-------------------\n"); for(i=1;i<=5;++i)

printf("%4d %d\n",i,dengji[i]); } 6-2

#include "conio.h" #include "stdio.h" main()

{ int n,k,xx[20]; int i,j,t;

printf("\nPlease enter a number:"); scanf("%d",&n);

printf("\nPlease enter %d number:",n); for (i=0;i<n;i++) scanf("%d",&xx[i]); printf("\nPlease enter another number:"); scanf("%d",&k); for (i=0;i<k;i++) }

printf("\nAfter moving:\n");

printf("\n"); }

4

6-3

#include "conio.h" #include "stdio.h" main() { int i,a[100]={90,180,270,380,590,530,140,750,380};int for(i=0,j=0;i<10;i++) {if(k>a[i]) j++;} for( i=10;i>j;i--) a[i]=a[i-1]; a[j]=k;

for(i=0;i<11;i++) printf("%5d",a[i]); printf("\n"); b[6];

for (i=0;i<6;i++) {b[i]=0;} for (i=0;i<9;i++) {switch (a[i]/100)

{case 0: ++b[0];break; case 1: ++b[1];break; case 2: ++b[2];break; case 3: ++b[3];break; case 4: ++b[4];break; default : ++b[5]; } }

printf("The result is:");

for (i=0;i<6;i++) printf("%3d",b[i]); printf("\n"); } 6-4

#include "stdio.h" main()

{ int i,j,m,k,end=0; int a[11];

for (i=0;i<10;i++) scanf("%d",&a[i]);

for (j=1;j<=9&&!end;j++) {end=1;

for (i=0;i<10-j;i++) if (a[i]>a[i+1]) {m=a[i]; a[i]=a[i+1]; a[i+1]=m; end=0; } }

scanf ("%d",&k);

} 6-5

#include "stdio.h" main()

{int i,j,k,n=0; char s[100]; char str[10]; gets(str); gets(s);

for (i=0;str[i];i++) {if(str[i]==s[0])

{for(j=i+1,k=1;s[k];j++,k++) {if (str[j]==s[k]) continue; else break; }

if(!s[k]) n++; } }

printf("%s can be find %2d\n",s,n); }

实验七 字符处理 7-2

#include "stdio.h" main()

{ char s1[80],s2[80]; int i,k;

printf("Input string s1 and s2 :\n"); gets(s1); gets(s2); i=0;

while(s1[i]!='\0') i++; k=0;

while(s2[k]!='\0') { s1[i]=s2[k];

5

i++; k++; } s1[i]='\0'; printf("s1+s2=%s\n",s1); } 7-3 #include "stdio.h" main() { int ch=0,sh=0,other=0; char c; while((c=getchar())!='!') {if('a'<=c&&c<='z') ch++; else if('0'<=c&&c<='9') sh++; else other++; } printf("ch=%d-sh=%d other=%d\n",ch,sh,other); } 7-4 #include "stdio.h" main() {int i; char s[]="This is a program"; for(i=0;i<30;i++) if(s[i]=='a') break; printf("The first (a) at%2d\n",i); } 7-5 #include "stdio.h" main() {char s; while ((s=getchar())!='@') {if('A'<=s&&s<='Z') s+=32; if('a'<=s&&s<='w') s+=3; else if (s=='x'||s=='y'||s=='z') s-=23; putchar(s); } putchar('\n'); }

7-6 #include "stdio.h" main() {int i,j; char s1[100]; char s2[100]; printf("Enter the string s1 and s2:\n"); gets(s1); gets(s2); for(i=0;i<100;i++) {if(s1[i]==s2[i]) continue; j=s1[i]-s2[i]; break; } printf("The first different cher is %c and %c The minus is %d\n",s1[i],s2[i],j); }

实验八 函 数 8-1 #include "stdio.h" max(int a,int b) {int c; c=a>b? a:b; return(c); } main() { extern int a,b; printf("%d\n",max(a,b)); } int a=3,b=5; 8-2 #include "stdio.h" int prime(int n) { int i; for(i=2;i<n;i++) if(n%i==0) return (0); if(i>n/2) return (1); } main()

6

{int a,b;

scanf("%d",&a); b=prime(a);

if (b==0) printf("%d is not a shushu\n",a); if (b==1) printf("%d is a shushu\n",a); } 8-3

#include "stdio.h" int jisheng (int a) {int b=1,i;

for(i=1;i<=a;i++) b*=i; return(b); }

main() {long m,n; int s;

scanf("%d%d",&m,&n);

if(m>n) printf("The munber is wrong, please put again\n");

else s=jisheng(n)/(jisheng(n-m)*jisheng(m)); printf("%d\n",s); }

实验九 指 针 9-1

#include "stdio.h" main() { int j,k;

int a[12],*p; p=&a[0];

for(k=0;k<12;k++) scanf("%d",p++); p=a;

for(j=0;j<12;j++) { printf("%5d",*p++); if((j+1)%4==0) printf("\n"); } } 9-3

#include "stdio.h" main() { int a[10];

7

int *p,i; p=a;

for(i=0;i<10;i++) { *p=i; p++; }

for(i=0;i<10;i++,p++) printf("%2d",*p); printf("\n"); } 9-4

#include "stdio.h" int table[10];

void lookup(int*t,int*a,int n) { int k; *a=t[0];

for(k=0;k<n;k++) }

main()

{ int k,min,*p=&min; for(k=0;k<10;k++) scanf("%d",table+k); printf("min=%d\n",min); } 9-5

#include "stdio.h" main()

{ int a[5][5]={0},*p[5],i,j;

for(i=0;i<5;i++) )=1; }

for(i=0;i<5;i++) {for(j=0;j<5;j++)

printf("%2d",p[i][j]); } }

相关推荐