大学计算机C语言程序设计(程序设计).docx
一、程序设计共113题第1题题号:319/*【程序设计】功能:判断m是否为素数。*/#include "stdio. h" void wwjt ();int fun(int m)/*Program*/* End */ main ()int m, k=0;for(m=100;m<200;m+) if(fun(m)printf("%4d”,m);k+;if(k%5=0) printf("n");)printf("k=%dn”,k); wwjt();void wwjt ()FILE *IN,*OUT; int ilN, iOUT, i;IN=fopen (*in. dat","r");if(IN=NULL)(printf("Please Verify The Currernt Dir. It May Be Changed");OUT=fopen("out. dat","w");if(OUT=NULL)(printf(*Please Verify The Current Dir. It May Be Changed*);for(i=0;i<10;i+)fscanf(IN,"%d”,&iIN);iOUT=fun(iIN);fprintf (OUT,*%dn*, iOUT);fclose(IN);fclose(OUT);答案:int i, k=l;if(m<=l) k=0;for(i=2;i<m;i+)if(m%i=0) k=0;return k;第2题题号:351【程序设计】功能:请编一个函数 void fun(int ttMN, int ppN).tt指向一个M行N列的二维数组,求出二维数组每列中最大元素,并依次放入PP所指一维数组中。二维数组中的数已在主函数中赋予。*/#include<conio. h>#include<stdio. h>ttdefine M 3ttdefine N 4void wwjt();void fun(int ttMN, int ppN)/*Pr()gram*/* End */)mainOint tMN=22,45,56,30,19,33,45,38),20,22,66,40;int pN, i, j, k;printf C*The original data is:n");for(i=0;i<M;i+)(for(j=0;j<N;j+)printf (*%6d*, tij);printf("n");fun(t, p);printf(*nThe result is:n");for(k=0;k<N;k+) printf(*%4d*, pk);printf("n");wwjt ();void wwjt ()(int i, j, array34,p4;FILE *rf,*wf ;rf = fopen("in. dat","r");wf = fopen(*out. dat*,'w");for (i=0; i <3; i+)for (j=0; j <4; j+)fscanf (rf,"%d”,&arrayij);fun (array, p);for (j=0; j <4; j+)fprintf (wf,"%7d”, pj); fprintf (wf,"n");)fclose(rf);fclose(wf);答案:-int i, j;for(j=0;j<N;j+)(ppj=ttoj;for(i=l;i<M;i+)if(ttij>ppj)ppj=ttij;第3题题号:375/*【程序设计】功能:从键盘上输入任意实数,求出其所对应的函数值。z=e 的 x 次幕(x>10)z=log(x+3)(x>-3)z=sin(x)/(cos (x)+4)*/include <math. h>#include<stdio. h>void wjt ();double y (float x)(/*Prograiii*/* End */main ()float x;scanf (*%f *,&x);printf("y=%fn", y(x);wwjt ();void wwjt ()FILE *IN,*0UT;int n;float i;double o;IN=fopen(*in. dat*,"r");if(IN=NULL)(printf(*Read FILE Error*);)OUT=fopen(*out. dat","w");if(OUT=NULL)(printf("Write FILE Error*);for(n=0;n<10;n+)fscanf (IN,;o=y(i);fprintf (OUT,o);)fclose(IN);fclose(OUT);答案:-double z;if(x>10) z=exp(x);else if(x>-3) z=log(x+3);else z=sin(x)/(cos(x)+4);return (z);第4题题号:334/*【程序设计】功能:求给定正整数n以内的素数之积。(n<28)*/#include <stdio. h>#include"conio. h"void wjt ();long fun(int n)(/*Prograni*/* End */ mainO(int m;printf(*Enter m:");scanf (*%d*,&m);printf(*nThe result is %ldn*, fun(m); wwjt ();void wwjt ()(FILE *IN,*OUT;int c ;int t;long o;IN=fopen(*in. dat*,"r"); if(IN=NULL)printf(*Read FILE Error*);)OUT=fopen("out. dat","w");if(OUT=NULL)(printf("Write FILE Error*);)for(c=l;c<=5;c+)(fscanf (IN,"%d",&t);o=fun(t);fprintf(OUT,o);)fclose(IN);fclose(OUT);)答案:-long i, k;long s=l;for(i=2;i<=n;i+)for(k=2;k<i;k+)if (i%k=0) break;if(k=i)s=s*i;return s;第5题题号:333【程序设计】功能:计算并输出给定整数的所有因子之积(包括自身)。规定这个整数的值不大于50o*/#include <stdio. h>#include*conio.h" void wwjt ();long int fun(int n)/*Program*/main ()int m;printf("Enter m:");scanf (*%d*,&m);printf(*nThe result is %ldn*» fun(m);wwjt ();)void wwjt ()FILE *IN,*OUT;int s ;int t;long int o;IN二fopen("in. dat*,"r");if(IN=NULL)(printf(*Read FILE Error*);OUT二fopen ("out. dat","w");if(OUT=NULL)(printfCWrite FILE Error*);)for(s=l;s<=5;s+)(fscanf (IN,"%d",&t);o=fun(t);fprintf (OUT,"%ldn”, o);fclose(IN);fclose(OUT);答案:-long s=l, i;for(i=2;i<=n;i+) if(n%i=0)s=s*i;return s;第6题题号:50/*【程序设计】功能:求出菲波那契数列的前一项与后一项之比的极限的近似值。例如:当误差为0.0001时,函数值为0.618056。*/#include <stdio. h>#include "math, h”void wwjt ();float fun()/*Prograiii*/* End */)main()(printfCy=%fn*,fun();wwjt ();void wwjt ()(FILE *IN,*OUT;float fOUT;IN=fopen(*19. IN*,*r*);if(IN=NULL)(printf(*Please Verify The Currernt Dir. it May Be Changed");OUT=fopen("19. out","w");if(OUT=NULL)(printf("Please Verify The Current Dir. it May Be Changed");fOUT=fun();fprintf(OUT,"%fn,fOUT);fclose(IN);fclose(OUT);float fl=l,f2=l,f3;float rl=2,r2;dor2=rl;rl=fl/f2;f3=fl+f2;fl=f2;f2=f3;while(fabs(rl-r2)>le-4);return rl;第7题题号:394【程序设计】功能:产生20个30,120上的随机整数放入二维数组 a54中,求其中的最小值。-*/include *stdlib. h#include <stdio. h> void wwjt ();int amin(int a54)/*Prograni*/* End */ main()int a54, i, j;for(i=0;i<5;i+)for(j=0;j<4;j+)aij=random(120-30+1)+30;for(i=0;i<5;i+)(for (j=0; j<4; j+)printf ("%5d", aij);printfCn*);)printf("min=%dn”, amin(a);wwjt ();)void wwjt ()(FILE *IN,*OUT;int m, n;int i54;int o;IN=fopen(*in. dat","r");if(IN=NULL)(printfCRead FILE Error*);0UT=fopen(*out. dat","w");if(OUT二二NULL)printf(*Write FILE Error*);)for(m=0;m<5;m+)for(n=0;n<4;n+)(fscanf (IN,*%d*,&i mn);o=amin(i);fprintf(OUT,*%dn*, o);fclose(IN);fclose(OUT);答案:int i, j, s;s=a00;for(i=0;i<5;i+)for(j=0;j<4;j+)if (s>aij)s=aij;return(s);第8题题号:337/*【程序设计】功能:将两个两位数的正整数a、b合并形成一个整数放在 c中。合并的方式是:将a数的十位和个位数依次放在c数的百位和个位上,b数的十位和个位数依次放在c数的十位和千位上。例如:当a=45, b=12o调用该函数后,c=2415o*/#include <stdio. h>void wjt ();void fun(int af int b, long *c)/*Program*/main ()(int a, b; long c;printf("input a, b:');scanf ("%d%d”,&a,&b);fun (a, b,&c);printf(*The result is:%ldn*, c);wwjt ();void wwjt ()(FILE *rf,*wf ;int i, a, b ; long c ;rf = fopen("in. dat","r");wf = fopen ("out. dat","w");for(i =0; i <10; i+)(fscanf (rf,"%d,%d",&a,&b);fun (a, b,&c);fprintf (wf,*a=%d, b=%d, c=%ldn*, a, b, c)fclose(rf);fclose(wf);答案:*c=a/l0*100+a%10+b/l0*10+b%10*1000;第9题题号:388/*【程序设计】功能:编写函数判断一个整数能否同时被3和5整除,若能则返回值为1,否则为0。调用该函数求出15300之间能同时被3和5整除的数的个数。*/#include <stdio. h>void wwjt ();int sum(int n)(/*Prograni*/* End */)main ()(int i,s=0;for(i=15;i<=300;i+)if(sum(i)=l) s=s+l;printf (*s=%dn*, s);wwjt ();)void wwjt ()(FILE *IN,*OUT;int n;int i12;int o;IN=fopen("in. dat","r");if(IN=NULL)(printfCRead FILE Error");)0UT=fopen(*out. dat","w");if(OUT=NULL)(printf(*Write FILE Error*);for(n=0;n<5;n+)fscanf(IN,"%d”,&in);for(n=0;n<5;n+)o = sum(in);fprintf(OUT,"%dn", o);)fclose(IN);fclose(OUT);答案一if(n%3=0&&n%5=0) return(l);return(0);第10题题号:16/*【程序设计】功能:编写函数fun求1!+2!+3!+n!的和,在main函数中由键盘输入n值,并输出运算结果。请编写fun 函数。例如:若n值为5,则结果为153。/#include<stdio. h> void wwjt ();long int fun(int n)/*Program*/* End */ main ()int n;long int result;scanf &n);result=fun(n);printf(*%ldn*, result);wwjt ();)void wwjt()(FILE *IN,*OUT;int i;int ilN;long int iOUT;IN=fopen(*in. dat*,'r");if(IN=NULL)(printf(*Please Verify The Currernt Dir. It May Be Changed*);)OUT=fopen(*out. dat","w");if(OUT=NULL)(printf(*Please Verify The Current Dir. It May Be Changed*);)for(i=0;i<10;i+)fscanf (IN,*%d",&iIN);iOUT=fun(iIN);fprintf(OUT,"%ldn”, iOUT);fclose(IN);fclose(OUT);答案:int i;float f=l, s=0;for(i=l;i<=n;i+)f=f*i;s=s+f;return s;第11题题号:329【程序设计】功能:对某一正数的值保留2位小数,并对第三位进行四舍五入。I#include <stdio. h>#include"conio. h"void wjt ();double fun(float h)(/*Prograni*/* End */ mainO(float m;printf(*Enter m:");scanf (*%f*,&m);printf(*nThe result is %8.2fn*, fun(m); wwjt ();void wwjt ()(FILE *IN,*OUT;int s ;float t;double o;IN=fopen(*in. dat","r");if(IN=NULL)printf(*Read FILE Error*);)OUT=fopen("out. dat","w");if(OUT=NULL)(printf("Write FILE Error*);)for(s=l;s<=5;s+)(fscanf (IN,"%f",&t);o=fun(t);fprintf (OUT,"%8.2fn", o);fclose(IN);fclose(OUT);)答案:-int i;i=(int)(h*1000)%10;if(i>=5)return(int)(h*100+l)/100.0;elsereturn(int)(h*100)/100.0;第12题题号:410【程序设计】功能:编写函数fun将个数组中的值按逆序存放,并在 mainO函数中输出。例如:原来存顺序为8,6,5,4,1。要求改为:1,4,5,6,8。*/#include <stdio. h>#define N 5void wwjt ();int fun(int arr, int n)/*Program*/main ()int aN=8,6,5,4,1, i;for(i=0;i<N;i+)printf ("%4d", ai);printf("n");fun (a, N);for(i=0;i<N;i+)printf ("%4d", ai);wwjtO ;)void wwjt()(FILE *IN,*OUT;int n;int i10;IN=fopen("in. dat","r"); if(IN=NULL)printfCRead FILE Error");0UT=fopen ("out. dat","w");if(OUT=NULL)(printf("Write FILE Error*):for(n=0;n<10;n+)fscanf (IN,"%d",&i n);fun(i,10);for(n=0;n<10;n+)fprintf(OUT,"%dn", in);fclose(IN);fclose(OUT);答案:int i, t;for(i=0;i<n/2;i+) t=arri;arri=arrn-l-i; arrn-l-i=t;第13题题号:327/*【程序设计】功能:能计算从1开始到n的自然数的和,n由键盘输入,并在mainO函数中输出。*/#include <stdio. h>void wwjt ();int fun(int n)/*Prograiii*/* End */ mainO(int m;printf("Enter m:"); scanf (*%d*,&m);printf(*nThe result is %dn*, fun(m); wwjt ();void wwjt ()(FILE *IN,*OUT;int t;int o;int c;IN=fopen ("in. dat","r");if(IN=NULL)printf(*Read FILE Error*);0UT=fopen ("out. dat","w");if(OUT=NULL)(printf("Write FILE Error*):for(c=l;c<=5:c+)fscanf(IN,"%d",&t);o=fun(t);fprintf (OUT,"%dn”, o);)fclose(IN);fclose(OUT):答案:-int sum, i;sum =0;for(i=l;i=n;i+)sum=sum+i;return (sum):第14题题号:507/*【程序设计】功能:编写函数fun求一个字符串的长度,在main函数中输入字符串,并输出其长度。*/include <stdio. h>void wwjt ();int fun(char *pl)(/*Program*/* End */ main ()(char *p, a20;int len;p=a;printf(*please input a string:n*);gets(p);len=fun(p);printf("The string* s length is:%dn*, len); wwjt ();void wwjt ()(FILE *IN,*0UT;char *pIN, sin20;int iOUT, i;pIN=sin;IN=fopen("3. IN","1);if(IN=NULL)printf(*Please Verify The Currernt Dir. it May Be Changed");0UT=fopen("3. out","w");if(OUT=NULL)(printf(*Please Verify The Current Dir. it May Be Changed*);)for(i=0;i<10;i+)(fscanf (IN,"%s”, pIN);iOUT=fun (pIN);fprintf (OUT,"%dn", iOUT);)fclose(IN);fclose(OUT);)答案:char *p;int n=0;p=pl:while(*p+)n+;return n;第15题题号:44/*【程序设计】功能:求出二维数组周边元素之和,作为函数值返回。二维数组的值在主函数中赋予。*/ttdefine M 4define N 5#include <stdio. h>void wwjt ();int fun(int aMN)/*Prograiri*/mainO(int aMN=1,3,5,7,9,2,4,6,8,10,2,3,4,5,6,4,5,6,7,8;int y;y=fun(a);printf("s=%dn”, y);wwjt();)void wwjt ()(FILE *IN,*OUT;int iINMN, iOUT;int i, j, k;IN=fopen (*14. IN*,*r*);if(IN=NULL)printf(*Please Verify The Currernt Dir. it May Be Changed*);OUT二fopen("14. out","w");if(OUT=NULL)printf(*Please Verify The Current Dir. it May Be Changed*);for(k=0;k<10;k+) for(i=0;i<M;i+)for(j=0;j<N;j+)fscanf (IN,*%d*,&iINij);iOUT=fun(iIN);fprintf(OUT,*%dn*, iOUT);)fclose(IN);fclose(OUT);答案:int s=0;int i, j;for(i=0;i<M;i+) s=s+ai0+aiN-l; for(j=l;j<N-l;j+)s=s+a0J+aM-lj; return s;第16题题号:364/*【程序设计】功能:求个四位数的各位数字的立方和。*/#include <stdio. h> void wwjt ();int fun(int n)(/*Prograiii*/* End */ main()(int k;k=fun(1234);printf("k=%dn", k); wwjt ();)void wwjt ()FILE *IN,*OUT;int ilN, iOUT, i;IN=fopen("in. dat","r"); if(IN=NULL)Changed");Changed*);printf("Please Verify The Currernt Dir. it May Be OUT=fopen("out. dat","w"):if(OUT=NULL)(printf("Please Verify The Current Dir. it May Be for(i=0;i<5;i+)fscanf(IN,"%d”,&iIN);iOUT=fun(iIN);fprintf (OUT,*%dn*, iOUT);fclose(IN);fclose(OUT);答案:int d, s=0;while (n>0)d=n%10;s+=d*d*d;n/=10;)return s;第17题题号:339/木【程序设计】功能:判断整数X是否是同构数。若是同构数,函数返回1;否则返回0。x的值由主函数从键盘读入,要求不大于1000说明:所谓“同构数”是指这样的数,这个数出现在它的平方数的右边。例如:输入整数5,5的平方数是25,5是25中右侧的数,以5是同构数。*/#include <stdio. h> void wwjt ();int fun(int x)/*Prograni*/* End */)main ()int x, y;printf(*nPlease enter a integer numbers:*);scanf &x);if(x>100)(printf(*data error!n);exit (0);)y=fun(x);if (y)printf(*%d YESn", x);elseprintf(*%d N0n”,x);wwjt ();void wwjt ()(FILE *IN,*OUT;int n, t;int o;IN=fopen(*in. dat","r");if(IN=NULL)(printf("Read FILE Error*);0UT=fopen(*out. dat","w");if(OUT二二NULL)printf(*Write FILE Error*);for(t =0; t <5; t+)(fscanf(IN,*%d*,&n);o=fun(n);fprintf (OUT,"%dn", o )fclose(IN);fclose(OUT);)答案:int k;k=x*x;if(k%10=x)|(k%100=x) return 1;elsereturn 0;第18题题号:331【程序设计】功能:找出一个大于给定整数且紧随这个整数的素数,并作为函数值返回。*/include <stdio. h>#include*conio. h"void wwjt ();int fun(int n)/*Prograrn*/main ()(int m;printf(*Enter m:");scanf("%d",&m);printf("nThe result is %dn*, fun(m); wwjt ();)void wwjt ()FILE *IN,*OUT;int s ;int t;int o;IN=fopen(*in. dat","r");if(IN=NULL)(printf("Read FILE Error");OUT二fopen("out. dat","w");if(OUT=NULL)printfCWrite FILE Error");for(s=l;s<=5;s+)(fscanf(IN,"%d",&t);o=fun(t);fprintf (OUT,"%dn”, o);fclose(IN);fclose(OUT);)答案:int i, k;for(i=n+l;i+)for(k=2;k<i;k+)if(i%k=O)break;if(k=i) return (i);第19题题号:317卜*【程序设计】功能:求出NXM整型数组的最大元素及其所在的行坐标及列坐标(如果最大元素不唯一,选择位置在最前面的一个)。例如:输入的数组为:12341561218910112求出的最大数为18,行坐标为2,列坐标为1。*/ttdefine N 4#define M 3include <stdio. h>void wwjt ();int Row, Col;int fun(int arrayNM)/*Program*/* End */main()int aNM, i, j, max;printf("input a array:");for(i=O;i<N;i+)for(j=0;j<M;j+)scanf (*%d*,&aij);for(i=0;i<N;i+)(for(j=0;j<M;j+)printf (*%d*, aij);printf(*n*);max=fun(a);printf ("max=%d, row=%d, col=%d”, max, Row, Col);wwjt 0; void wwjt()(FILE *IN,*OUT;int iINNM, iOUT, i, j;IN=fopen(*in. dat","r");if(IN=NULL)(printf("Please Verify The Currernt Dir. it May Be Changed");OUT二fopen("out. dat","w");if(OUT=NULL)(printf(*Please Verify The Current Dir. it May Be Changed*);for(i=0;i<N;i+)for(j=0;j<M;j+)fscanf (IN,*%d*,&iINij);iOUT=fun