书通学习方法网 -包含小学、初中、中考、高中、高考等各种学习方法。
书通网logo
当前位置: 书通网 > 学习方法 > c语言程序设计实验报告(C语言程序设计实验答案)

c语言程序设计实验报告(C语言程序设计实验答案)

时间:2022-12-26 分类:学习方法 来源:书通网

实验一

}

ng>熟悉Turbo C的编程环境

#include

void main

一:目的要求

(

1.熟悉Turbo C的编程环境;

)

{

int a, b, sum;

a = 123;

b = 456;

sum = a + b;

printf

2.知道程序编辑、链(接、执行的基本步骤;

3.学习如何查错并修改“sum isd\n”, sum);

}

调试通过后,自己修改程序,使程序出错,并读懂错误提示,进行修改。

实验二 数据输入输出格式的程序设计

一.目的要求

1.进一步熟悉调试程序的方法;

2.熟练掌握格式输入与格式输出函数的使用。

3.掌握字符与ASCⅡ值之间的转换方法。

编辑

添加图片注释,不超过 140 字(可选)

4.上机前预习编写好程序;

?

编辑

添加图片注释,不超过 140 字(可选)

?

编辑

添加图片注释,不超过 140 字(可选)

利用接口库函数dos.h的调用

1.类型定义结构类型struct date.d;

2.getdate%d);

3.printf 程序;

4.上机前预((&习编写好程序。

二ddd”,d.da_year, d.da_mon, d.da_day)。

五、程序提示:

%/%/%“:实验内容与步骤

#include

#include

main

调试(以下两个程序

{

char c1;

struct date d;

clrscrong>/* The first C Program*/

#include

getdate>vd);

···.

···

···

getchoid main()

{

}

解答:

g> printf(“Hello,World#include

#include

#include

main()

{

char c1;

struct dad);

printfte d;

clrscr(()&;

getdate("\t\ddd",d.da_year, d.da_mon, d.da_day);

printf%/%/%t("\n* * * * * * * * * * * * *\n");

printf("* Menu *\n");

printf("* 1.Input *\n");

printf("* 2.Output *\n");

printf("* 3.End *\n");

printf("* * * * * * * * * * * * *\n");

printf(" Enter Number=> \n");

printf(" Ch=----------> ");

gotoxy(20,8);

c1=getch();

gotoxy(20,8);

putchar(c1);

gotoxy(9,9);

putchar(c1);

gotoxy(20,9);

printf(d",c1);

}

实验三 选择结构程序设计

一:目的要求

1.熟悉用关系运行与逻辑运行符的应用;

2.掌握if语句与switch语句的使用;

3.上机前按实验要求预习编写出完整的程序,才允许上机。

二:实验内容与步骤

1. 在书店买书 ,以100本为限。如果买 1 本不打折扣 ;买2本打折1; 买3本折扣为 15,买3本以上折扣为 20。设书本数为 x,单价为 20.00元。

请使用else if多分支结构或if嵌套结构设计实现该算法的C程序。

2. 从键盘上输入一个百分制成绩score,按下列原则输出其"等!级:score≥90,等级为A;80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;score<60,等级为E。

请使用switch 语句实现上述功能。

(提示:将成绩整除10",把score转变为处于1~!10之间的整数,从而转化成switch语句中的case标号)

三:选做实验

实验指导书56页第二章选择结构的任意实验

解答:

一、

% % %0%"#include

#include

main()

{

int n;

float x,y;

clrscr();

printf("Pln);

x=20.00;

y=0;

if ease Enter The Number:");

goto(xy(25&,1);

scanfn<0)

printf(d",%"("Sorry,you put the wrong number);

else if (n==0)

printf("Please bu);

else if y at least 1 book(n==1)

{ y=x;

printf("------You should pay.2f",y*n);

}

else if %:(n==2)

{ y=x*0.9;

printf("------You should pay.2f",y*n);

}

else if %:(n==3)

{ y=x*0.85;

printf("------You should pay.2f",y*n);

}

else if %:("n<=100)

!

{ y=x*0.8;

printf("------You should pay.2f",y*n);

}

else if %:(n>100)

printf("Sorry,you can buy less then 100 books.");

}

二、

#include

#include

main()

{

int t;

float n;

clrscr();

printf("Pln);

t=n/10;

if ease Enter The Score:");

goto(xy(24&,1);

scanfn<0)

printf(f",%");

else if ("Please Write The Right Score(n>100)

printf("Is He A God?");

else

switch(t)

{

case 10:printf("A+");break;

case 9:printf("A");break;

case 8:printf("B");break;

case 7:printf("C");break;

case 6:printf("D");break;

default:printf("E");break;

}

}

实验四 循环控制

一:目的要求

1.熟悉用while语句,do-while 语句和for语句实现循环的方法;

2.掌握在程序设计中用循环方法实现各种算法;

3.掌握计算程序运行所占机时的计算方法;

4.上机前按实验要求预习编写出完整的程序,才允许上机。

二:实验内容与步骤

目标:100匹马驮100担货;

假设:大马一匹驮3担,中马一匹驮2担,小马两匹驮一担;

组合方法1:大马、中马、小马每种不能少于一匹;

组合方法2:对马匹种类无限制,可以缺少一种或缺二种

问题: 1. 采用组合方法1,用while求解,输出所有组合法,输出组合的总数?

2.采用2种组合,用do-while求解,输出所有组合法,输出组合的总数?

3.采用2种组合,用三重或二重for循环求解,输出所有组合法,输出组合的总数?

4.除打印结果和多少种组合法外,还要分别打印三种算法所费机时。

提示:计算一种算法所占机时的程序提示:

#include

#include

#include

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount;

clrscr();

start=clock(); /* start = time();*/

big=1; middle=1; small=2;

ncount=0;

printf("This a while program\n");

while (big<=33)

{

.

}

end=clock(); /* end = time();*/

printf("The num of method1 is:d\n",ncount);

printf% ("and the time is: (end,start));

/*printf f(“”The difference is 5.1f second\n”, difftime%:(end,start)/18.2);*/

……}

解答:

一、

#include

#include

#include

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=1; middle=1; small=2,i=0,n=0;

ncount=0;

printf("This a while program\n");

while (big<=33)

{

for(small=2;small<=100;small=small+2)

for(m i==100)

{

printfiddle=1;middle<=100;middle++)

{

&(

n=big*3+middle*2+small/2;

i=big+middle&+small;

if (n="Bigd,Middled,Smalld,nd,id\n",big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf%=%=%=%=%==100 ("nd ",n);

printf%=("bigd smalld middled ",big,small,middle);

printf%=%=%=("\n");*/

}

end=clock();

printf("The num of method1 is:d\n",ncount);

printf% ("and the time is:5.1f time\n",difftime% (end,start));

}

二、

#include

#include

#include

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf("This a while program\n");

do

{

for(small=0;small<=100;small=small+2)

for(m i==100)

{

printfiddle=0;middle<=100;middle++)

{

&(

n=big*3+middle*2+small/2;

i=big+middle&+small;

if (n="Bigd,Middled,Smalld,nd,id\n",big,middle,small,n,i);

ncount++;

}

}

big++;

/*

printf%=%=%=%=%==100 ("nd ",n);

printf%=("bigd smalld middled ",big,small,middle);

printf%=%=%=("\n");*/

}

while(big<=34);

end=clock();

printf("The num of method1 is:d\n",ncount);

printf% ("and the time is:5.1f time\n",difftime% (end,start));

}

三、

#include

#include

#include

main()

{

clock_t start,end; /* time_t start,end;*/

int i,big,middle,small,ncount,n;

clrscr();

start=clock(); /* start = time();*/

big=0; middle=0; small=0,i=0,n=0;

ncount=0;

printf("This a while program\n");

for(big=0;big<=34;big++)

{

for(small=0;small<=100;small=small+2)

for(m i==100)

{

printfiddle=0;middle<=100;middle++)

{

&(

n=big*3+middle*2+small/2;

i=big+middle&+small;

if (n="Bigd,Middled,Smalld,nd,id\n",big,middle,small,n,i);

ncount++;

}

}

/*

printf%=%=%=%=%==100 ("nd ",n);

printf%=("bigd smalld middled ",big,small,middle);

printf%=%=%=("\n");*/

}

end=clock();

printf("The num of method1 is:d\n",ncount);

printf% ("and the time is:5.1f time\n",difftime% (end,start));

}

实验五 函数

一、 目的要求

1. 掌握函数的定义和调用方法;

2. 掌握函数实参与行参的对应关系的,以及“值传递”的方式;

3. 掌握求最大公约数和最小公倍数的方法;

4. 按实验内容要求完成全程程序设计后才允许上机。

二、 实验内容与步骤

1. 设计一个函数f,求二个数的最大公约数int f(int x,int y);

2. 设计一个函数g,求二个数的最小公倍数int g(int x,int y);

3. 从键盘输入三个浮点数,求三个数中最大数和最小数的差值。

三、 函数的定义要求

在main( )中实现下列操作

1. a=inNumber( );

2. b= inNumber( );

3. c=f(a,b);

4. d=g(a,b);

5. 输出a,b,c,d。

解答:

一、

#include

#include

void main()

{

int a,b,c,d;

clrscr();

a=inNumber();

b=inNumber();

c=f(a,b);

d=g(a,b);

printf("ad,bd,cd,dd\n",a,b,c,d);

}

int f%=%=%=%=(int x,int y)

{

int i,t,a;

float p1,p2;

if (x>=y)

t=y;

else

t=x;

for (i p2==0)

{

a=i;

}

}

return a;

}

int g=1;i<=t;i++)

{&

p1=i;int x,int y)

{

int i,t,p1,p2,a;

if

p2=(i;

&

if %x>=y)

t=x;

else

t=y;

for y% p2==0)

{

a=i;break;

!"

}

}

return a;

}

int inNumberx(p1==0 (i=t;i<=(x*y);i++)

{

&(

p1=x;

{

int a;

a=0;

while p>

p2=y;(

&

if %ix);

if %a==0)

{&

scanx>0)

a=1;

else

{

printffi(p1==0 (d);

a=0;

}

};

return x;

}

二、

",%"("Wrong#include

void main()

{

float a3],x,y;

int i,j;

clrscr();

for p>

for ((i=0;i&<3;i++)

scanfi=0;i<3;i++)

for (f",%"(j=0;j<2;j++)

if (aj]>aj+1])

{

x=aj];

aj]=aj+1];

aj+1]=x;

}

y=a2]-a0];

printf("\ny=Max{ai]}-Min{ai]}:\n\.3f\n\t%tf)",y,y);

}

实验六 数组

一、 目的要求

1. 掌握数组的定义、赋值和输入输出的方法;

2. 掌握清屏函数clrscr%(()的调用方法;

3. 掌握产生随机数函数randomize()的初始化及调用方法;

4. 上机前按实验要求预习,完成全部程序设计后才允许上机。

二、 实验内容与步骤

已知二维数组a5]5],完成下列要求

(1) 输入数据

ai]j]=random(100); /*产生100以内随机数*/

(2) 显示数组各元素,要求整齐排列;

(3) 将第1与第5行对调后,再显示之;

(4) 求出每行元素的最大值,并指出其行号和列号。

三、 输入随机数的要求

#include

#define RMAX 5

#define cMAX 5

#define nMAX 100

main()

{ . /*变量初始化说明*/

.

clrscr(); /*调清屏函数清屏*/

randomize(); /*在初始化后调用产生随=机'数n函'数)

{

if*/

.

.' Y<'p||temp==>'ayi'])

{

prun=run;

j=j]=random(nMAX);

.

.

.

(

}

);

if

解答(:

#incj===0')y<'/)

{

printfp>

{

(

printfludeYou can only put Y

#define( RMAX 5

(

#define jd%=cMAX 5

#define nMAX 100

(

main()

n)\nPlease put again

{

(

int a5Y/N):]5],i,j,temp1]5],t,max,x,y;/*定义变量为整型(我的程序里面有些定义的变量没用,懒得删除了)*/

clrscr();/*清屏*/

randomize();/*在初始化后调用产生随机数函数*/

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

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

ai]j]=random(nMAX);/*给数组ai]!j]赋随机值*/

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

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

printf(d\t",ai]j]);/*输出数组ai]j]*/

printf%"("\n");/*在输出完一行后自动换行(不理解的话可以暂时删掉这句话,然后运行程序看结果)*/

}

for(j=0;j<5;j++)/*这个循环是用于换行的*/

{

!

temp0]j]=a0]j];/*将数组a的第一行的每个值都赋予temp(这里的temp是一个1*5的数组)*/

a0]j]="a4!]j];/*将数组a的第一行的每个值替换成第五行的值*/

a4]j]=temp0]j];/*将数组a的第五行的每个值替换成temp的值*/

}

"!"

printf("\n");/*输出一个回车*/

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

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

printf(d\t"",ai]j]);/*将换行后的新数组a输出出来*/

printf%"("\n");/*在输出完每一行后自动换行*/

}

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

{

max=ai]0];/*=定'义N一'个||temp最大值变量max,令其暂时等于第i行的第一个数*/

for (j=0;j<5;j++)/*让max和该行的所以数比较,如果有某个数大于max,max变为该数的值*/

if (ai]j]>max)

{

max=ai]j];

}

for (j=0;j<5;j++)/*令max和第i行的每个数进行比较,输出所有等于max的数及该数的坐标*/

if(maxtemp==ai]j])

print=&'fn('")/*如果temp不等于n或N时,进行循环*/

{

if\ndd]d",i,j,max);

}Wrong

}'&Y '<||temp==p'>y '实)/*当temp为y或Y时,进行下列循环,用于进行题目要求的操作*/

{

clrscr验七 Ca,=j')Y;'

}

(

else

printfp>

{

(

printfn Not);

fflush Find The xCh指针

一、 目的((要求

1. 掌握指stdin);

temp=getch针的定义和使用指针变量;

2. 学会使用字(符串的指针和指向数组的指针变量;

3. 学会使用指向=)';M<'/)

printfp>

}

(

}

\nGender:

}

int run 函数的指针变量;

4. 按实验(内容要求完成全程程序设计后才允许上机。

二、 实验内char xCh,char str100],int i)

{

!

int j;

char *p;

clrscr容与步骤

设计一个(函数,它有三个形参

(1) 被);

printf"查找的!字符串str;

(2) 待=('查y找'的)/*当输入的temp不为Y、y、N、n时*/

{

printf字符xCh;

(3) 在(字符串str中xCh出现的位置i=0,1,…

它的返回值是在stYou can only put Yr中xCh 出现的次数(若str中无xCh,则返回值=0)

三、 上机要求(

1、 键入待查的字符xN) or yCh;

2、 键"入被查!的字"符串str;

3、 调用该!函数;

n)\nPlease put again

4、 打印它的返回值和出现的位置;

(

5、 允许重复执行,每次以清屏开始%=%]%Y/N):a(用循环语句控制重复执行)。

四、 提示

xCh在str出现位置应设计为一整型指针,以便记下0~N个位置(整数)。

#include

void mai"n()=<'/Np'>||temp

{

char temp;

int run(),j;

int (*prun)();

temp='Y';

while(txCh=e\njd",j);

}

!

printf%=mp("\nParden>Y/N:");

fflush("stdin);

temp=ge"tch!();

}

if(&temp=='N'||temp=='n')

breakWrong;

);

else

t=1;

}

if

if(&(tempxCh=");

= ' si].sex==x'Cmh'=||si].sex==g'eMt'c)

m++;

printfh();

pristr(0];

"

i=0;

j=0;

whilentf(c\nstr=",xCh);

ge&ts(%"(s!tr);

id%=

p=*p)

{

if (*p==xCh)

{

j++;

printf("xCh" d\t",i);

}

temp

p=p+1;&

i++;

}

&Wrong

return j=;'

}

Mian%:()版:

#include

!

void main()

{

);

fflush"

int i,(j;

char xCstdin);

temp=getchh,str100],*p,temp;

temp='Y';/*(给temp赋初值Y,防止第一个while循环无法运行*/

while(temp));

}

}

}

/*fflush;/*在屏幕输出"提示xCh!=*/

xCh=get(ch();/*从屏幕读取一个字符赋给xCh(getch()和getchar()的区别:前者只要输入一个字符就结束输入过程,后者需要按回车或空格后才结束输入过程)*/

pristr0]stdin)*/

实验八 结构体与共用体

一、 目的要求

1、 掌握结构体类型变量与数组的定义和使用;

2、 学会使用指针变量和结构体指针数组;

3、 按实验内容要求完成全程程序设计后才允\nName:许上机。

二、 实验内容与步骤

1. 设计一个结构

&

struct student {

long no\nAge:; /*学号*/

char name10=]';w '/*姓名*/

char sex; /*性别*/

int age; /*年龄*/

float score; /*平均成绩*/

}

2. 完成下列任务:

(1) 输入实际学生人数n ;/*将指针地址指向str这个字符串的首字符位置*/

i=0;

(

j=0;

2

(2) 输入每个学生的信息,组成结构数组,并输出;

(3) 统计男、女生人数并输出;

(4) 计算全班平均成绩并输出;

(5) 将低于全班平均成绩的学生信息按行输出

三、 上机要求

1. 可划分为若干个函数,或写成一个main!"

whilentf(c\nstr=",xCh);/*在屏幕(xCh=后面输出刚才输入的xCh的值,并提示用户输入str(因为我们用getch%"(),输入完字符后会自动结束xCh的输入进入下一指令的执行(在本题中,下一指令是:printf(c\nstr=",xCh);),而不在屏幕输出刚才输入的字符)【不理解的话把该句改成{printf%"("\nstr=");}看看输出结果就知道了】*/

ge&ts((str);/*输入str*/

);

2. 要求输出格式有提示及相应数据。

#include

struct stu!dent

{

long no; /*学号*/

char name10]; /*姓名*/

char sex; /*性别

p=*p)/*当p所指向(的字符不为空字符时,进行判断循环*/

{

gender)*/

int age; /*年龄*/

float score; /*平均成绩*/

}s3];

void main

if (*p==(xCh)/*当p所指向的字符为所需寻找的xCh时,进行以下操作*/

{

)

{

int i,nYou can only put 10 characters,t,m,na;

float av,sum;

);

else

na=1;

}

printf);

scanf

flosi](.sexat t(emp;

cl&rscr

i++;/(*i!自加1,用于累计str中xCh的数目*/

&printf);' W<'p>m=0;

sum=0;

printf("xCh d\t",j);/*输出xCh在str中出现的位置*/

}

(

p=p+1;/*指针地址移向str的下个字符*/

j++;/*j用于记录此时p的位置,在str中第一个字符时=0,第二个时=1,以此类推*/

}

if %:(i==0)/*当str中没有xCh这个字符时,i=0*/

{

printf("Can Not Find Th,i);/*在屏幕中提示无法找到str中的xCh,并输出i=0*/

}

else

{

printfe xCh("\nid",i);/*\n为换行*/

}

!

printf%=("\nParden>Y/N:");

fflush(stdin);/*清空计算机缓存*/

temp=""getch();/*从屏幕中读取一个字符赋给temp*/

}

if(temp=='!N'||temp=='n')/*当temp为N或n时*/

break;/*跳出n);

printf循环*/

if(tem("The& number of the studens:");

scanfp("\n");

ford",%"(i=0;i

{

si].no);

printf

printf(("\n&No.");

scanf(l"\nName:");

na=0;

whiledsi].name);

if",%"(na==0)

{<(/p>&

scastrlennf(s",%"(si].name)>10)

printf\nGender:("Wrong(W/M)");

t=0;

while(si].sex);

ift==0d%si].age);

printf)

{<(/p>(&&temp);

si].score=temp;

}

for

sca(si]."\nScore:");

scani=0;i

{

sum=sum+si].score;

printff",sexnf(s",%"(f",%"("\nNold\nNames\nGenderc\nAged\nScore.2f",si].no,si].name,si].sex,si].age,si].score);

}

av=sum/n;

printf%:%:%:%:%.("\nThe number of girl(s)d\nThe number of boy%:(s)d\n",n-m,m);

printf%:("\nThe average of the score is.2f",av);

for%:(i=0;i

{

if (si].score<=av)

printf("\nNold; Names; Genderc; Aged; Score.2f",si].no,si].name,si].sex,si].age,si].score);

}

}

%:%:%:%:%.

小编推荐:

推荐阅读