郑州大学c语言实验报告册答案(全部)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验一
1.
#include
void main()
{
int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
printf("sum=%d\n",a+b+c);
}
2.
#include
void main()
{
int a,b,he,cha,ji,shang;
scanf("%d,%d",&a,&b);
he=a+b;
cha=a-b;
ji=a*b;
shang=a/b;
printf("he=%d\n",he);
printf("cha=%d\n",cha);
printf("ji=%d\n",ji);
printf("shang=%d\n",shang);
}
3.
#include
void main()
{
int a,b,c,d,e,sum;
scanf("%d,%d,%d,%d,%d",&a,&b,&c,&d,&e);
sum=a*a+b*b+c*c+d*d+e*e;
printf("sum=%d\n",sum);
}
4.
#include
void main()
{
int a,b,c,d,e,sum;
scanf("%d,%d,%d,%d,%d",&a,&b,&c,&d,&e);
sum=a*a+b*b+c*c+d*d+e*e;
printf("sum=%d\n",sum);
}
5.
#include
#define PI 3.14159
void main()
{
float r,l,s;
printf("请输入圆半径(r):");
scanf("%f",&r);
l=2*PI*r;
s=r*r*PI;
printf("\n圆周长 l=%6.2f\n",l);
printf("圆面积 s=%6.2f\n",s);
}
6.
#include
void main()
{
printf("\"what a beautiful campus!\"\n");
printf("\"I wish you every success!\"\n");
}
实验二
1.
#include
void main()
{
int a,b,c;float x;
scanf("a=%d,b=%d,c=%d,x=%f",&a,&b,&c,&x);
printf("%f",a*x*x*x+b*x*x+c);
}
2。
#include
void main()
{
int a,b;
scanf("a=%d,b=%d",&a,&b);
printf("ji=%d,shang=%d,yushu=%d",a*b,a/b,a%b);
}
3.
#include "stdio.h"
void main()
{
float C,F;
scanf("%f",&F);
C=5.0/9*(F-32);
printf("%f",C);
}
4.
#include
#define PI 3.14159
void main()
{
float r,l,s;
printf("请输入圆半径(r):");
scanf("%f",&r);
l=2*PI*r;
s=r*r*PI;
printf("\n圆周长 l=%6.2f\n",l);
printf("圆面积 s=%6.2f\n",s);
}
5。
#include
void main()
{
float c=3.123456789;
printf("%f",c);
}
实验三
1.
#include
void main()
{
int a=3,b=4,c=5;
float x=1.414,y=1.732,z=2.712;
printf("a=%-7d b=%-7d c=%-7d\nx=%-7.3f y=%-7.3f z=%-7.3f",a,b,c,x,y,z);
}
2.
#include
void main()
{
int a,b,c;
float x,y,z;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%f",&x);
scanf("%f",&y);
scanf("%f",&z);
printf("a=%d,b=%d,c=%d\nx=%.3f,y=%.3f,z=%.3f,\n",a,b,c,x,y,z);
}
3,
#include
void main()
{
int a,b,c,x1,x2;
scanf("%d,%d,%d",&a,&b,&c);
x1=a+b+c;
x2=a-b-c;
printf("x1=a+b+c=%d+%d+%d=%d\n",a,b,c,x1);
printf("x2=a-b-c=%d-%d-%d=%d\n",a,b,c,x2);
}
4.
#include
void main()
{
char c1,c2,c3,c4;
scanf("%c%c%c%c",&c1,&c2,&c3,&c4);
printf("%d,%d,%d,%d",c1,c2,c3,c4);
}
5.
#include
void m
ain()
{
int d1=60,d2=61,d3=62,d4=63;
printf("%c %c %c %c",d1+5,d2+5,d3+5,d4+5);
}
6.
#include
void main()
{
int m=12345678,n=3456789;
printf("m=%d n=%d",m,n);
}
7.
#include
void main()
{
int a=20,b=64,c=127;
printf("a=%d,%o,%x\n",a,a,a);
printf("b=%d,%o,%x\n",b,b,b);
printf("c=%d,%o,%x\n",c,c,c);
}
8.
#include
void main()
{
float x,y,z;
x=1.414;
y=1.732;
z=2.712;
printf("%6.2f,%6.2f,%6.2f",x,y,z);
}
9.
#include"stdio.h"
void main()
{
float a;
a=1.0/3;
printf("%0.6f%%\n",a);
}
10.
#include
void main()
{
int a,b,
a=getchar(b=getchar()c=getchar();
a=a+32;
b=b+32;
c=c+32;
putchar(a);
putchar(b);
putchar(c);
}
实验四
1.
#include
void main ()
{
int a,b,c,max;
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(a
max=b;
if(b
max=c;
printf("%d",max);
}
2.
#include
void main()
{
int x,y;
scanf("%d",&x);
if(x<1)
y=x;
else
if(x<10)
y=2*x-1;
else
y=3*x-11;
printf("%d\n",y);
}
3.
#include
void main()
{
int grade;
printf("Please enter the grade");
scanf("%d",&grade);
if(grade<=100 && grade>=90)
printf("A");
else if(grade<=89 && grade>=80)
printf("B");
else if(grade<=79 && grade>=70)
printf("C");
else if(grade<=69 && grade>=60)
printf("D");
else if(grade<60)
printf("E");
}
4.
#include
void main()
{
int a,b,c,d,e,f,g,h;
scanf("%d",&f);
a=f/10000;
b=f/1000%10;
c=f/100%10;
d=f/10%10;
e=f%10;
if(f/10>0)
{
if(f/100>0)
{
if(f/1000>0)
{
if(f/10000>0)
g=5;
else g=4;
}
else g=3;
}
else g=2;
}
else g=1;
printf("g=%d\n",g);
printf("a=%d\nb=%d\nc=%d\nd=%d\ne=%d\n",a,b,c,d,e);
printf("h=%d%d%d%d%d\n",e,d,c,b,a);
}
5.
#include "stdio.h"
#include "math.h"
void main()
{ float a,b,c,p,q,t,x1,x2;
printf("input a,b,c: "); scanf("%f,%f,%f",&a,&b,&c);
if (a==0.) printf("a==0!!! return.");
else
{ p=-b/(2.*a); t=b*b-4*a*c; q=sqrt(fabs(t))/(2.*a);
if (t==0.) printf("\n x1=x2=%-f",p);
else if (t>0.) printf("\n x1=%-f,x2=%-f\n",p+q,p-q);
else printf("\n x1=%-f + %f i, x2=%-f -%f i\n", p, q, p, q);
}
}
实验五
1.
#include
void main()
{int i,sum;
i=1,sum=0;
while(i<=100)
{sum=sum+i;
i++;
}
printf("%d\n",sum);
}
2.
#include
void main()
{int i,j,sum;
i=1,j=1,sum=0;
for(i=1;i<=10;i++)
{
j=j*i;
sum=sum+j;
printf("%d\n",sum);
}
printf("%d\n",sum);
}
3.
#include
#include
void main()
{
int s;
float n,t,p;
t=1;p=0;n=1.0;s=1;
do
{
p=p+t;n=n*n+1;
s=-s;t=s/n;
}
while (fabs(t)>1e-5);
printf("p=%10.6f\n",p);
}
4.#include
void main()
{char c;
int letters=0,digit=0,other=0;
while((c=getchar())!='\n')
{if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c>='0'&&c<='9')
digit++;
else other++;}
printf("%d\n%d\n%d\n",letters,digit,other);
}
5,
#include "stdio.h"
void main()
{ int i;
long int f1, f2;
f1=f2=1;
for (i=1; i<=20; i++)
{
printf("%12ld %12ld",f1,f2);
f1=f1+f2;
f2=f2+f1;
}
}
实验六
1.
#include
void main()
{
int sum=0,mul=1;
int i;
for(i=1;i<=10;i++)
{
mul*=i;
sum+=mul;
}
printf(" 1!+2!+3!....10!=%d\n",sum);
}
2.
#include
void main()
{
int i,j,k;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
k=j*i;
printf("%2d*%2d=%2d",i,j,k);
}
printf("\n");
}
}
3.
#include
void main()
{
int i,j,k;
for(i=0;i<=9;i++)
{
for(j=0;j<=8-i;j++)
printf(" ");
for(k=0;k<=2*i;k++)
printf("*");
printf("\n");
}
for(i=0;i<=8;i++)
{
for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<=16-2*i;k++)
printf("*");
printf("\n");
}
}
4.
#include
void main()
{int i,j,k;
for(i=0;i<=20;i++)
for(j=0;j<=33;j++)
{k=100-i-j;
if(5*i+3*j+k/3==100&&k%3==0&&(i+j+k==100))
printf("%d,%d,%d\n",i,j,k);}
}
5.
#include
void main ()
{int sum=0,s=0,i;
scanf ("%d",&i);
while (i<=5)
{s=s+i;
sum=sum+s;
i++;
}
printf ("sum=%d\n",sum);
}
实验七
1.
#include
#include
#include
void main()
{
int i,a[15];
for(i=0;i<15;i++)
a[i]=rand()%100;
for(i=0;i<15;i++)
{printf("%d ",a[i]);
if((i+1)%5==0)
printf("\n");
}
}
2.
#include
void main ()
{int a[10],i,max,min,sum=0,s=0;
float t;
for (i=0;i<10;i++)
scanf ("%d",&a[i]);
printf ("\n");
max=a[0],min=a[1];
for (i=0;i<10;i++)
if (a[i]>max)
max=a[i];
for (i=0;i<10;i++)
if (a[i]
min=a[i];
for (i=0;i<10;i++)
{s=s+a[i];
t=s/10;}
for (i=0;i<10;i++)
if (a[i]>t)
sum=sum+1;
printf ("%d,%d,%d\n",max,min,sum);
}
3.
#include"stdio.h"
void main()
{
int i;
int f[20]={0,1,1};
for(i=0;i<=19;i++)
{
f[i+3]=f[i+2]+f[i+1]+f[i];
printf("%d,",f[i]);
}
}
4.
#include
void main()
{ int a[5]={67,89,76,98,66};
int b[5],i,j;
for (i=0;i<=4;i++)
{
j=4-i;
b[j]=a[i];}
for (j=0;j<=4;j++)
printf("%d",b[j]);
}
5.
#include
void main()
{int i,j,max,a[10],t;
printf ("Please enter data:");
for (i=0;i<=9;i++)
scanf ("%d",&a[i]);
for (i=0;i<=9;i++)
{max=i;
for (j=i+1;j<=9;j++)
if (a[max]
t=a[i];a[i]=a[max];a[max]=t;}
printf ("The result is :"
); printf("sum=%ld,average=%f\n",sum,sum/(N*1.0)); anhua);
for (i=0;i<=9;i++)
printf ("%d",a[i]);
printf ("\n");
}
实验八
1.
#include"stdio.h"
void main()
{
int a[9],f[3][3];
int i,j,t;
for(i=0;i<9;i++)
scanf("%d",&a[i]);
i=0;
for(t=0;t<3;t++)
{for(j=0;j<3;j++)
{
f[t][j]=a[i];
i++;
printf("%d",f[t][j]);
}
printf("\n");
}
}
2.
#include
#define m 3
#define n 4
void main()
{ long a
?[n+1];
int i,j;
for (i=0;i
{ a[i][n]=0;
for(j=0;j
{ a[i][j]=rand();
a[i][n]=a[i][n]+a[i][j];
printf("%8ld",a[i][j]);
}
printf(" %8ld \n",a[i][n]);}
}
3.
#include
#define m 3
#define n 4
main()
{ long a
?[n];
int i,j,row=0,col=0;
for (i=0;i
{ for (j=0; j
{a[i][j]=rand();
printf("%8ld",a[i][j]);
if (a[row][col]
{ row=i;col=j; }
}
printf("\n");
}
printf("\n max=%ld row=%d col=%d \n",a[row][col],row+1,col+1);
}
4.
void main()
{ float a[100][2],temp,temp1; int i,j,p,n;
printf("\n input number of student n=");
scanf("%d",&n);
for (i=0;i
{ printf("\n input num,score of %dth student:",i+1);
scanf("%f,%f",&temp1,&temp);
a[i][0]=temp1;a[i][1]=temp;
}
for (i=0; i
{ p=i;
for (j=i+1; j
if (p!=i)
{ temp=a[p][1];a[p][1]=a[i][1];a[i][1]=temp;
temp=a[p][0];a[p][0]=a[i][0];a[i][0]=temp; }
}
printf("\n input admittable number:");scanf("%d",&p);
printf("\n output name list: \n");
for (i=0; i
printf("%10.0f %10.1f\n",a[i][0],a[i][1]);
}
5.
#include
void main()
{
int a[4][4];
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
a[i][j]=1;
else a[i][j]=0;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
实验九
1
#include "stdio.h"
long fun(int K)
{
int fact=1,i;
for(i=1;i<=K;i++)
fact*=i;
return fact;
}
void main()
{
int m,n;
long k1,k2,k3;
printf("input m,n:");
scanf("%d,%d",&m,&n);
k1=fun(m);
k2=fun(n);
k3=fun(n-m);
printf("fact=%f\n",k2/(k1*k3*1.0));
}
2
#include
long IS_sum(int n)
{
int i;
long int f1, f2,f3,sum;
f1=f2=1;
sum=0;
for (i=1; i<=n; i++)
{
f3=f1+f2;
sum=sum+f3;
printf("%12ld %12ld",f1,f2);
f1=f1+f2;
f2=f2+f1;
}
return(sum);
}
void main()
{
int N;
long sum;
printf("Input n:");
scanf("%d",&N);
sum=IS_sum(N);
printf("\n");
}
3
#include "stdio.h"
#include "math.h"
double fun(double x)
{
double f=0.0;
f=sin(2*x)+x;
return f;
}
double integral(double a,double b,double(* fun)(double),int N)
{
const int n=N;
int i=0;
double sumf=0.0,xi=0.0,xi1=0.0,fv=0.0,h=0.0;
h=(b-a)/n;
xi=a;
for(i=0;i
xi1=xi+h;
fv=((* fun)(xi)+(* fun)(xi1))*0.5*h;
sumf=sumf+fv;
xi=xi1;
}
return sumf;
}
void main()
{
double t;
int k;
printf("Inpute k:");
scanf("%d",&k);
t=integral(0,3.14159,fun,k);
printf("Result=%10.3f\n", t);
}
4
#include "stdio.h"
#include "math.h"
psushu(m)
int m;
{
int i=2,t;
for(;i<=m;i++)
if(m%i==0&&i
else t=0;
return m;
}
main()
{
int a,s;
printf("enter sushu is \n");
scanf("%d",&a);
s=psushu(a);
if(s==1) printf("a is sushu\n");
else printf("s is not sushu\n");
}
6
#include
isprime(int n)
{
for (int i = 3; i < n / 2; i++)
if (!(n%i))
return 0;
return 1;
}
void main()
{
int sum = 2;
int a,b;
scanf("%d,%d",&a,&b);
for (int i = a; i < b; i +=2)
if (isprime(i))
sum += i;
printf("sum = %d\n", sum);
}
实验十
1
打开"开始---程序----附件----记事本"
输入:
1001 zhangsan 78 90 89
1002 lisi 99 88 77
1003 wangwu 89 76 55
1004 zhaoliu 53 55 44
存在e盘根目录下,文件名为123.txt
2
#include
#include
#define N 5
main( )
{
FILE *fp;
int num,sc1,sc2,sc3,score,i;
char name[10];
if((fp=fopen("c:\\123.txt","r"))==NULL)
{ printf("Error");
exit(0);
}
for (i=1;i<=N;i++)
{ fscanf(fp,"%d%s%d%d%d\n",&num,name,&sc1,&sc2,&sc3);
score=sc1+sc2+sc3;
printf("%6d%10s%6d%6d%6d%6d\n",num,name,sc1,sc2,sc3,score/3);
}
fclose(fp);
}
3
#include
#include
#define N 5
struct student
{
int num;
char name[10];
int score;
char dianhua[10];
};
main( )
{
FILE *fp;
int i;
struct student stu[N];
clrscr();
if((fp=fopen("d:\\xuesheng.txt","w"))==NULL)
{
printf("Error");
exit(0);
}
for (i=0;i
scanf("%d%s%d%s",&stu[i].num,stu[i].name,&stu[i].score,stu[i].dianhua);
fprintf(fp,"%6d%10s%6d%10s\n",stu[i].num,stu[i].name,stu[i].score,stu[i].dianhua);
}
fclose(fp);
}
4
#include
#include
#define N 5
struct student
{
int num;
char name[10];
int score;
char dianhua[10];
};
main( )
{
FILE *fp;
int i,xuehao;
struct student stu[N];
clrscr();
if((fp=fopen("c:\\xuesheng.txt","r"))==NULL)
{ printf("Error");
exit(0);
}
scanf("%d",&xuehao);
for (i=0;i
if(stu[i].num==xuehao)
printf("%6d%10s%6d%10s\n",stu[i].num,stu[i].name,stu[i].score,stu[i].di
}
fclose(fp);
}
5
#include
#include
void main( )
{
FILE *fp1,*fp2;
char ch;
if(( fp1=fopen("c:\\file4.c", "r"))==NULL)
{ printf("Error");
exit(0);
}
if(( fp2=fopen("c:\\file5.c", "w"))==NULL)
{ printf("Error");
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
fputc(ch,fp2);
ch=fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
}