C语言程序设计实验 答案.docx
《C语言程序设计实验 答案.docx》由会员分享,可在线阅读,更多相关《C语言程序设计实验 答案.docx(38页珍藏版)》请在冰豆网上搜索。
C语言程序设计实验答案
-前言-
/*非常感谢度娘以及各位网上C语言高手的支持,才能让敝人完成此文档的整理。
本文档集合了本人、度娘、众网友的力量,其中代码的正确率约为90%(不正确的有标注)。
为回报度娘及众网友的帮助,本文档免费下载。
*/
/*配“电子科技大学出版社(周信东主编)”的C语言程序设计实验*/
/*努力吧,骚年以及学妹们!
*/
/*整理edby口玉刀一ofGUET.*/
=====================
实验一C语言程序初步
=====================
1.----------------------------
已知a=5,b=4,c=6,求出s并换行
#include"stdio.h"
main()
{
inta,b,c,s;
a=5;b=4;c=6;
s=(a+b+c)/3;
printf("a=%d,b=%d,c=%d\n,s=%d",a,b,c,s);
}
2.-------------------------------
输入一个数字求他的平方
#include
main()
{
intr,s;
scanf("%d",&r);
s=r*r;
printf("s=%d\n",s);
}
3.--------------------------------
(1)
#include
main()
{
printf("*\n");
printf("***\n");
printf("*\n");
}
(2)
#include
main()
{
intv;
inta,b,c;//a,b,caersides,visvolumeofcube
a=3;b=3;c=5;
v=a*b*c;
printf("v=%d\n",v);
}
=================================
实验二数据类型、运算符和表达式
=================================
1.
(1)--------------------------------------------------
//总觉得打印结果怪怪的,DOYOUTHINKSO?
main()
{
chars1='3',s2='4',s3='5';
intc1=101,c2=102;
printf("3%c\t4%c\t5%c\n",s1,s2,s3);//3%c为输出3和%c
printf("s1=%d\ts2=%d\ts3=%d\n",s1,s2,s3);//注意哦,s1,s2,s3是char!
而%d:
输入输出为整形%ld长整型%hd短整型%hu无符号整形%u%lu%s:
输入输出为字符串%c字符%f:
输入输出为浮点型%lf双精度浮点型
printf("c1=%d\t~%c\n",c1,c1);//换码符'\t',表示水平制表位(horizeontaltab),它的作用是将光标移到最接近8的倍数的位置
printf("c2=%d\t~%c\n",c2,c2);//注意c1,c2的类型
}
(2)
//运行结果为8.300000%是求余数先运算x-y,把结果转换为int型的有利于四则运算
main()
{
floatx=8.3,y=4.2,s;
inta=7;
s=x+a%5*(int)(x-y)%2/3;
printf("s=%f",s);
}
(3)
main()
{
inti,j,p,q;
i=3;j=6;
p=i++;
q=--j;
printf("%d,%d,%d,%d\n",i,j,p,q);
p=i--+3;
q=++j-4;
printf("%d,%d,%d,%d",i,j,p,q);
}
(4)
main()
{
intm=15,n=9,s;
m*=3+2;
printf("%d",m);
n+=n-=n*=n;
printf("%d",n);
printf("%d\n",(s=6*5,m+n+s));
printf("%d\n",s);
}
2.----------------------------------------------------
(1)#include
#defineMM40
main()
{
inta=3,b=6,t;
t=MM/(3+6);
printf("%d%d",t,MM);
}
//原来printf语句打成了print,printf语句中少了一个%d.
(2)
#include
main()
{
intm;
doublex;
scanf("%d",&m);
x=3.14*m*m;
printf("%f",x);
}
//注意分号,%f转义
(3)
main()
{
intm=8,n=5;
printf("%d",m*=m-n);
printf("%d,%d,%d",m=m+1,n-1,m+n);
}
3.----------------------------------------------------
main()
{
inta=9,b=5;
a+=b;
b=a-b;
a-=b;
printf("%d%d",a,b);
}
4.----------------------------------------------------
//4.输入a,b,c三个变量,计算数学公式(a*b)/c
main()
{
inta,b,d;
floats,c;
scanf("%d%d%f",&a,&b,&c);
d=a*b;
s=d/c;
printf("%f",s);
}
=================================
实验三顺序结构和输入输出方法
=================================
1.---------------------------------------------------
(1)main()
{
intm=345,t;
floatn=2.56;
t=2.56*100;
printf("m=%d,n*100=%d\n",m,t);
}
(2)
main()
{
inta,b,c,s,v;
scanf("%d%d%d",&a,&b,&c);
s=a*b;
v=a*b*c;
printf("%d,%d,%d\n",a,b,c);
printf("s=%d,v=%d\n",s,v);
}
2.----------------------------------------------------
(1)
main()
{
intm,n;
floatx=3.5,y=6.2;
scanf("%d,%d",&m,&n);
printf("%6d,%6d",m,n);/*每个数的输出宽度为6,两个数之间逗号建个。
*/
printf("x=%7.2f,y=%7.2f",x,y);/*个数的输出宽度7,小数位2*/
}
(2)
main()
{
inta,b;
longm,n;
floatp,q;
scanf("a=%d,b=%d\n%f,%f",&a,&b,&p,&q);
scanf("%ld",&m);
scanf("%ld",&n);
printf("a=%d,b=%d\nm=%ld,n=%ld\n",a,b,m,n);
printf("p=%f,q=%f\n",p,q);
}
//打印结果有误!
!
3.----------------------------------------------------
main()
{
inti,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",ss);
}
4.----------------------------------------------------
#include
main()
{
charc1,c2;
c1=getchar();
c2=getchar();
putchar(c1);
putchar(c2);
printf("c1=%c,c2=%c",c1,c2);
printf("%d.%d\n",c1,c2);
}
5.---------------------------------------------------
#include
main()
{
inta=790,b,c;
b=790/60;
c=790%60;
printf("%d小时%d分钟",b,c);
}
6.---------------------------------------------------
#include
main()
{
inta,b,c,d;
scanf("%d%d%d",&a,&b,&c);
d=a;
a=c;
c=b;
b=d;
printf("%d,%d,%d\n",a,b,c);
}
7.--------------------------------------------------
#include
main()
{
doublea;
scanf("%lf",&a);
printf("%.2f\n",a);
}
=================================
实验四逻辑运算判断选取控制
=================================
1.---------------------------------------------------
1.a.错误原因分析:
Switch分号错误
switch(a*a+b*b){case3:
case1:
y=a+b;break;case0:
y=b-a;break;}
b.错误原因分析:
switch语句使用错误,后跟括号。
Default放在后
边。
switch(a){case10:
y=a-b;break;case
11:
x=a*b;break;default:
x=a+b;}c.错误原因分析:
没有输出
语句。
switch(a+b){case10:
x=a+b;break;case11:
y=a-
b;break;}
2.----------------------------------------------------
(1)
main()
{
intx,y;
scanf("%d",&x);
if(x>-5&&x<0)y=x;
if(x==0)y=x-1;
if(x>0&&x<10)y=x+1;
printf("%d",y);
}
(2)
main()
{
intx,y;
scanf("%d",&x);
if((x>-5)&&(x<0))y=x;
elseif(x==0)y=x-1;
elseif(x>0&&x<10)y=x+1;
printf("%d",y);
}
3.----------------------------------------------------
main()
{
inta,M;
scanf("%d",&a);
M=a>0?
a:
-a;
printf("abs(a)=%d",M);
}
4.----------------------------------------------------
main()
{
intc,t,m;
printf("inputthenumberofcoatandtrousersyourwantbuy:
\n");
scanf("%d%d",&c,&t);
if(c==t)if(c>=50)m=c*80;elsem=c*90;elseif(c>t)
if(t>=50)m=t*80+(c-t)*60;else90*t+(c-t)*60;
elseif(c>=50)80*t+(t-c)*45;
elsem=c*90+(t-c)*45;
printf("%d",m);
}
5.---------------------------------------------------
#include
int
main(void)
{
inta,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a+b<=c||a+c<=b||b+c<=a)
printf("不构成三角形\n");
elseif(a==b&&b==c)
printf("等边三角形\n");
elseif(a==b||b==c||a==c)
printf("等腰三角形\n");
else
printf("不等边三角形\n");
return0;
}
6.--------------------------------------------------
#include"stdio.h"
main()
{
longa;
scanf("%ld",&a);
if(a%5==0)
if(a%7==0)
printf("yes\n|");
elseprintf("no\n");
}
=================================
实验五循环结构
=================================
1.---------------------------------------------------
#include"stdio.h"
main()
{intn;
while
(1)
{printf("Enteranumber:
");
scanf("%d",&n);
if(n%2==1)
{printf("Isaid");
continue;
}
break;
}
printf("Thanks.Ineededthat!
");
}
2.----------------------------------------------------
#include
main()
{
intc;
while((c=getchar())!
='\n')
{
if((c<='X'&&c>='A'||c<='x'&&c>='a'))
c+=2;
elseif(c=='y'||c=='Y'||c=='z'||c=='Z')
c=c-24;
printf("%c",c);
}
putchar('\n');
}
3.----------------------------------------------------
#include
intmain()
{
intm,k;
for(m=1;m<=4;m++)
{
if(m==1||m==4)
{
for(k=1;k<=6;k++)
printf("*");
printf("\n");
}
else
printf("**\n");
}
return0;
}
4.----------------------------------------------------
#include
main()
{inta,b,c;
printf("*123456789\n");
printf("-----------------------------\n");
for(a=1;a<=9;a++)
{printf("%3d",a);
for(b=1;b<=a;b++)
{c=a*b;
printf("%3d",c);}
printf("\n");}
}
5.--------------------------------------------------
#include
main()
{
doublee=2,w=0.000001,t;
doublen=1,s=1;
t=1/w;
while(s{
s=s*(n+1);
e=e+1/s;
n++;
}
printf("e=%lf,n=%d\n",e,(int)(n-1));
}
6.--------------------------------------------------------
#include
voidmain()
{inti,j,k=0,r,s=0,t=1000,a=0;
for(i=2;i<=t;i++)
{a=0;r=0;
{for(j=1;j
{k=0;if(i%j==0)
{k=j;}r+=k;}
if(r==i)a=r;}
if(a!
=0)
{
if(a==6)
printf("%d",a);
else
printf("+%d",a);
}
s+=a;
}
printf("=%d\n",s);
}
=================================
实验六数组
=================================
1.---------------------------------------------------
#include
main()
{
intgrade[6];
inti,mumber;
for(i=1;i<=5;++i)
grade[i]=0;
printf("enteryournumber\n");
for(i=1;i<=20;++i)
{
scanf("%d",&mumber);
if(!
mumber)break;
++grade[mumber];
}
printf("\n\nresultofsearch\n");
printf("---------------\n");
for(i=1;i<=5;++i)
printf("%4d%d\n",i,grade[i]);
}
2.----------------------------------------------------
#include"stdio.h"
main()
{
intn,k,xx[20];
inti,j,t;
printf("\nPleaseenteranumber");
scanf("%d",&n);
printf("\nPleaseenter%dnumbers:
",n);
for(i=0;iprintf("%\nPleaseenteranothernumber:
");
scanf("%d",&k);
for(i=0;i{
t=xx[0];
for(j=1;jxx[j-1]=xx[j];
xx[n-1]=t;
}
printf("\nAftermoving:
\n");
for(i=0;iprintf("\n");
}
3.----------------------------------------------------
#include
main()
{
inti,a[100]={90,180,270,380,590,530,140,750,380},b[6];
for(i=0;i<6;i++)
b[i]=0;
for(i=0;i<9;i++)
{
switch(a[i]/100)
{
case0:
b[0]++;break;
case1:
b[1]++;break;
case2:
b[2]++;break;
case3:
b[3]++;break;
case4:
b[4]++;break;
default:
b[5]++;break;
}
}
printf("Theresultis:
");
for(i=0;i<6;i++)
printf("%d",b[i]);
printf("\n");
}
4.----------------------------------------------------
#include
main()
{
inti,j,t,p,q,s,n,a[11]={6,3,42,23,35,71,98,67,56,38};
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("%d",a[i]);
}
printf("\ninputnumber:
\n");
scanf("%d",&n);
for(i