大学计算机C语言程序设计(程序改错).docx
一、程序改错共137题第1题题号:15【程序改错】功能:根据整型形参m的值,计算如下公式的值。1 112x23x3mxm例如:若m=5,则应输出:0.536389*/#include <stdio. h>double fun(int m)(double y=l.0;int i;/*FOUND*/for (i=2;i<m;i-)/*found*/ y-=l/(i*i);/*found*/return m;)main ()(int n=5;printf(*nthe result is %lfn*, fun(n);答案:1) . for(i=2; i<=m; i+)或 for(i=2;m>=i; i+)或 for(i=2;i<m+l; i+)或 for(i=2;i<l+m;i+)或 for(i=2;m+l>i;i+)或 for(i=2;l+m>i;i+)2) . y-=l.0/(i*i);或 y=y-1.0/(i*i);或 y-=l.0/(float)(i*i);或 y=y-1.0/(float)(i*i);或 y=y-l.0/i/i;或 y=y-l.0/(float) i/(float) i;或 y=y-l/(double)(i*i);或 y-=l/(double)(i*i);3) . return y;或 return (y);或 return(y);第2题题号:362/*【程序改错】功能:删除字符串S中的所有空白字符(包括Tab字符、回车符及换行符)。输入字符串时用#'结束输入。*/#include <string. h>#include <stdio. h>#include <ctype. h>fun ( char *p)(int i, t; char c80;/*found*/for (i =1, t =0; pi; i+)/*found*/if(!isspace(p+i) ct+=pi;/*found*/ct二"o";strcpy (p, c);main()(char c, s80;int i=0;printf("input a string:*);c=getchar();while(c!=,#')(si=c;i+;c=getchar();si=0*;fun (s);puts(s);答案:1) . for (i =0, t =0; pi; i+)2) . if(!isspace(*(p+i) ct+=pi;3) . ct=>0*;第3题题号:251【程序改错】功能:用起泡法对连续输入的十个字符排序后按从小到大的次序输出.#include <stdio. h>#include <string. h>define N 10 sort(char strN)int i, j; char t; for(j=l; j<N; j+)for(i=0;i<N-j;i-)/*found*/ if(stri<stri+l)(t=stri;stri=stri+l;stri+l=t;main()(int i;char strN;for(i=0;i<N;i+)/*FOUND*/ scanf (*%c*, stri);/*found*/ sort(strN);for(i=0;i<N;i+)printfstri);printf("n");答案:1) . for(i=0;i<N-j;i+)2) . if(stri>stri+l)或 if(stri+l<stri)3) . scanf(*%c*,&stri):4) . sort (str);第4题题号:437【程序改错】功能:输入字符序列,字符序列以特殊字符“中'结束。程序从前向后依次读入字符,并翻译出新的字符输出。翻译的规则是:如果当前读入的字符是数字符,如果该字符的面值为n,则它的后一个字符(包括数字符)需重复输出n+1次如果当前读入的字符不是数字符,则输出该字符。输出时,以上述一次翻译为一组,不同组间输出用一个空白字符隔开。翻译直至当前读入的字符为字符,结束翻译和输出。*/#include<stdio. h>void main()(char ch,nch;int count;int k;printf(*Enter string.n");scanf(*%c*,&ch);/*found*/while(ch=,#')(if (ch>=' O'&&ch<='9')(/*found*/count二ch,O'-1;scanf&nch);for(k=0;k<count;k+) printfnch);else/*found*/printf ch);printf(*");scanf &ch);printf (*#n*);)答案:1) . while(ch!=,#')2) . count=ch,O'+1;3) . printf (*%c*, ch);第5题题号:390/*【程序改错】功能:输入一个字符串,过滤此串,滤掉字母字符,并统计新生成串中包含的字符个数。例如:输入的字符串为ab234$df4,则输出为:The new string is 234$4There are 5 char in the new string.。*/#include <stdio. h>#include <conio. h>define N 80int fun(char *ptr)(int i, j;/*found*/for(i=0, j=O;*(ptr+i)!=*0*;i+)/*found*/if (*(ptr+i)>> z*|*(ptr+i)<> a*|*(ptr+i)>* Z*|*(ptr+i)<* A*)(/*FOUND*/(ptr+j)=(ptr+i);j+;)*(ptr+j)='O'return (j);main()char strN;int s;printf(*input a string:*);gets(str);printf(zThe original string is ; puts(str);s=fun(str);printf("The new string is ;puts (str);printf(*There are %d char in the new string. s);答案:1) . for(i=0, j=O;*(ptr+i)!='0' i+) (ptr+iXA* )2) . if(*(ptr+i)>,z,11*(ptr+i)<,a (ptr+i)/ Z*113) .*(ptr+j)=*(ptr+i);第6题题号:396【程序改错】功能:将整型数组中所有小于0的元素放到所有大于0的元素的前面(要求只能扫描数组一次)。#include<stdio. h>define Max 100void fun(int a, int n)(/*FOUND*/int i=5;j=n-l, temp;while(i<j)(whi le (ai<0)i+;while(aj>=0)/*FOUND*/ j+;/*found*/if(i>j)(temp=ai;ai=aj;aj=temp;void main ()(static a=l,-3,-1,3,2,4,-4,5,-5,-2), n=10, i;fun (a, n);for(i=0;i<10;i+)printf (*%d *, ai);答案:1) . int i=0, j=n-l , temp ;2) . j;3) . if(i<j)第7题题号:5/*【程序改错】功能:求如下表达式:111S =1+1+21+2+31+2+3+n#include <stdio. h> main()(int n;double fun();printf("Please input a number:");/*FOUND*/print(*%d*,n);printf (*%10.6fn”, fun(n);)/*FOUND*/fun(int n)int i, j, t;double s;s=0;/*found*/while(i=l;i<=n;i+);(t=0;for(j=l;j<=i;j+)t=t+j;/*found*/=s+l/t;return s;)答案:1) . scanf ("%d",&n);2) . double fun(int n)3) . for(i=l;i<=n;i+)或 for(i=l;n>=i;i+)或 for(i=l;i<n+l;i+)或 for(i=l;n+l>i;i+)4) . s+=l.0/t;或 s= s +1.0/(float) t;或 s= s +1.0/t;或 s+=l.0/(float) t;或 s+=l.0/(double) t;或 s= s +1.0/(double) t;第8题题号:387/*【程序改错】功能:判断字符ch是否与str所指串中的某个字符相同;若相同,什么也不做,若不同,则将其插在串的最后。*/#include <conio. h>include <stdio. h>#include <string. h>/*found*/void fun(char str, char ch )(while (*str &&*str != ch )str+;/*F0UND*/if (*str = ch )str 0= ch;/*FOUND*/ strl='O'main()(char s81, c ;printf('nPlease enter a string:n");gets ( s );printf ('n Please enter the character to search :");c = getchar ();fun(s, c);printf('nThe result is %sn*, s);答案:1) . void fun(char *str, char ch )2) . if (*str ='0')3) . strl='0'第9题题号:401/*【程序改错】功能:编写函数fun计算下列分段函数的值: x*x+x x<0且 x#-3f (x)= x*x+5x0Wx<10且 xW2及 xW3x*x+x-l 其它*/#include <stdio. h>double fun(float x)(/*FOUND*/ double y/*FOUND*/ if (x<0 I x!=-3.0)y=x*x+x;else if (x>=0&& x<10.0&& x!=2.0&& x!=3.0) y=x*x+5*x;elsey=x*x+x-l;/*F0UND*/return x;main()(double f;float x;printf("input x=");scanf("%f",&x);f=fun(x);printf ("x二%f, f (x)=%fn”, x, f);答案:1) . float y;2) . if (x<0&& x!=-3.0)3) . return y;第10题题号:435/*【程序改错】功能:输入 n,计算 s=l+l+2+l+2+3+l+2+3+4+.+1+2+3+4+.+n#include<stdio. h> void main ()/*F0UND*/ int i, s;p, n;printf("Enter n:rT);/*FOUND*/ scanf n);/*FOUND*/ for(s=p=0, i=l;i<n;i+)s+=p+=i;printf(*S=%dn”,s);答案:1) . int i, s, p, n;2) . scanf (*%d*,&n);3) . for(s=p=0, i=l;i<=n;i+)第11题题号:426/*【程序改错】功能:809*?=800*?+9*?+1其中??代表的两位数,8*?的结果为两位数,9*?的结果为3位数。求?代表的两位数,及809*?后的结果。*/#include <stdio. h>output (long b, long i)(printf(*n%ld=800*%ld+9*%ld+ln, b, i, i);printf ("?代表的两位数:%d”, i);printf ("n809*?后的结果:d”, b);getchO ;main ()(/*found*/ long int a;b;i;a=809;/*FOUND*/ for(i=10;i<100, i+)/*FOUND*/b=i/a+l;if(b>=l000&&b<=l0000&&8*i<100&&9* i>=100) output (b, i);答案:1) . long int a , b , i ;2) . for(i=10;i<100;i+)或 for(i=10;i<=99;i+)3) . b= i*a +1;或 b= a*i +1;第12题题号:380/*【程序改错】功能:输入两个双精度数,函数返回它们的平方和的平方根值。例如:输入:22.936和14.121,输出为:y =26.934415o8include <stdio. h>#include <conio. h>#include <math. h>/*FOUND*/ double fun (double *a,*b)(double c;/*FOUND*/ c = sqr(a*a + b*b);/*EOUND*/ return *c;?main ()(double a, b, y;printf (* Enter a, b :;scanf&a,&b );y = fun (&a,&b);printf (y =%f n", y );答案:1) . double fun (double *a, double *b)2) . c = sqrt (*a *a +*b *b);3) . return c;第13题题号:416/*【程序改错】功能:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。#include<stdio. h>main()(int i=5;void palin(int n);printf("40:");palin(i);printf('n");)void pal in(n)int n;(/*found*/ int next;if(n<=l)(/*found*/ next!=getchar();printf Cn0:*);putchar(next);)else(next=getchar ();/*FOUND*/ pal in(n);putchar(next);)答案:1) . char next;2) . next=getchar ();3) . palin(n-l);第14题题号:257【程序改错】功能:编写一个函数,该函数可以统计一个长度为3的字符串在另一个字符串中出现的次数。例如:假定输入的字符串为:asdasasdfgasdaszx67asdmklo,字符串为:asd,则应输出n=4。*/#include<stdio. h>#include<string. h>#include<conio. h>int fun(char *str, char *substr)(/*EOUND*/unsigned int i, n=0/*FOUND*/for(i=0;i<=strlen(str);i+)if(stri=substrO)&&(stri+l=substrl)&&(stri+2=substr2)/*E0UND*/+i;return n;main ()(char str81, substr4;int n;printf ("输入主字符串:”);gets(str);printf (输入子字符串:);gets (substr);puts (str);puts (substr);n=fun(str, substr);printf("n=%dn”, n);答案:1) . unsigned int i,n=0;2) . for(i=0;i<=strlen(str)-3;i+)或 for(i=0;i<strlen(str)-2;i+)3) . n+;或 n+=l;或 n=n+l;或+n;第15题题号:258【程序改错】功能:在键盘上输入一个3行3列矩阵的各个元素的值(值为整数),后输出矩阵第一行与第三行元素之积,并在fun()函数中输出。#include <stdio. h>int fun(int a33)int i, j, sum;/*FOUND*/sum=0;/*FOUND*/for(i=0;i<3;i+)for(j=0;j<3;j+)/*found*/ sum=*aij:return sum;main()(int i, j, s, a33;for(i=0;i<3;i+)(for(j=0;j<3;j+) scanf (*%d*,&aij);s=fun(a);printf (/rSum=%dn*, s);答案:1) . sum=l;2) . for(i=0;i<3;i+=2)或 for(i=0;i<3;i=i+2)或 for(i=0;i<3;i+, i+)3) . sum=sum*aij;或 sum*=aij;第16题题号:419/*【程序改错】功能:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。-*/#include<stdio. h> main()/*found*/long ge, shi, qianjwan, x;scanf &x);/*found*/wan=x%10000;qian=x%10000/1000;shi=x%100/10;ge=x%10;/*F0UND*/if (ge=f an| shi=qian)printf("this number is a huiwen'n");else答案:1) .2) .3) .printf("this number is not a huiwen'n");long ge, shi, qian, wan, x;wan=x/10000;if (ge=wan&&shi=qian)或 if (shi =qian&&ge=wan)第17题题号:442/*一【程序改错】功能:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。#include <stdio. h>#include <conio. h>int length(p)char *p;int n;n=0;/*FOUND*/while(p,0*)n+;P+;)return n;main()(int len;/*FOUND*/char *str20;printf(please input a string:n*);scanf ("%s”, str);/*FOUND*/len=length(str);printf(*the string has %d characters.*, len);答案:1) . while(*p!=0*)或 while(*p)或 while(*p!=O)或 while(*p!=NULL)2) . char str20;3) . len=length(str);第18题题号:29【程序改错】功能:将字符串s中最后一次出现的子字符串tl替换成字符串t2,所形成的新串放在w所指的数组中,在此处,要求tl和t2所指字符串的长度相同。例如:当s所指字符串中的内容为"abcdabfabc”, tl中的内容为ab,t2中的内容为“99时,结果w所指数组中的内容应为"abcdabf99c”。*/#include <conio. h>include <stdio. h>ttinclude <string. h>void fun (char *s, char *tl, char *t2, char *w)(char *p ,*r,*a;strcpy( w, s );/*F0UND*/while ( w )(p = w; r = tl;while (*r )/*FOUND*/if (*r =*p )(r+;p+;elsebreak;/*FOUND*/if (*r ='/O') a = w;w+;r = t2;while (*r )(/*FOUND*/a =*r;a+;r+;)main ()(char s100, tl100, t2100, w100;printf(*nPlease enter string S:"); scanf("%s”, s);printf(*nPlease enter substring tl:"); scanf('%s”, tl); printf(*nPlease enter substring t2:*); scanft2); if ( strlen(tl)=strlen(t2)fun( s, tl, t2, w);printf("nThe result is :%sn*, w);elseprintf(*XnError : strlen(tl)!= strlen(t2)n*);)答案:1) . while (*w)或 while (*w!=0)或 while (*w!八O')或 for (;*w;)或 for (;*w!=0*;)2) .if (*r=*p )3) .if (*r=*0*) a = w;或 if (*r =0) a = w;4) .*a=*r;第19题题号:410【程序改错】功能:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。#include <stdio. h>main ()(char c;int letters=0, space=0, digit=O, others=0;printf(please input some characters、/');/*FOUND*/while(c=getchar ()=,n )(/*FOUND*/if (c>=,a'&&c<=,z &&c>=,A'&&c<=,Z') letters+;/*FOUND*/else if (c=!'') space+;else if (c='O'&&c<二'9')digit+;elseothers+;printf (*all in all :char=%d space=%d digit=%d others=%dnA,, letters, space, digit, others);)答案:1) . while(c=getchar()!=>n )2) . if ( c>=&&c<= z | c>= A*&&c<= Z*)或 if ( c<='z'&&c>='a' I | c>='A'&&c<='Z')或 if ( c <=' z'&& c >二'a'| c >=' A'&& c<=,Z*)3) . else if(c='')第20题题号:20【程序改错】功能:根据以下公式求五值,并作为函数值返回。例如:给指定精度的变量eps输入0.0005时,应当输出Pi=3.140578。n 1121231234=1+ X -+- X - X +- X - X - X -+23353573579*/#include <stdio. h> double fun(double eps)(double s, t;int n=l;s=0.0;t=l;/*F0UND*/while(t<=eps)s+=t;/*found*/ t=n/(2*n+l)*t;n+;/*F0UND*/return s;main ()double x;scanf &x);printf (*neps=%lf, Pi=%lfnn”, x, fun(x);答案:1) . while (t>eps)或 while(eps<t)或 while(t>=eps)或 while(eps<=t)或 while (t>eps)或 while (eps<t)或 while (t>=eps)或 while (eps<=t)2) . t=t*n/(2*n+l);或 t=l.0*n/(2*n+l)*t;或 t=n/(2*n+l.0)*t;或 t=n/(2.0*n+l)*t;或 t=n/(2.0*n+l.0)*t;或 t=l.0*n/(2.0*n+l.0)*t;3) . return 2* s ;或 return (2*s);或 return(2*s);或 return (s*2);或return(s*2);第21题题号:407/*【程序改错】功能:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后除以10的余数代替该位数字。再将新生成数据的第一位和第四位交换,第二位和第三位交换。例如:输入一个四位整数1234,则结果为:9876oinclude <stdio. h>main()(int a, i, aa4, t;printf(输入一个四位整数:”);/*FOUND*/scanf a);aa0=a%10;/*FOUND*/aal=a%100%10;aa2=a%1000/100;aa3=a/1000;/*FOUND*/ for(i=0;i<3;i+)aai+=5;aai%=10;for(i=0;i<=3/2;i+)(t=aai:aai=aa3-i;aa3i=t:)for(i=3;i>=0;i)printfaai);)答案:1) . scanf &a);2) . aal=a%100/10;3) . for(i=0;i<=3;i+)或 for(i=0;i4;i+)第22题题号:28【程序改错】功能:实现两个字符串的连接。例如:输入dfdfqe和12345时,则输出dfdfqel2345.*/include <stdio. h>main ()(char si80, s280;void scat (char si , char s2口);gets(sl);gets(s2);scat (si, s2);puts (si);void scat (char si, char s2口)(int i=0, j=0;/*FOUND*/while(sli=八O') i+;/*FOUND*/while(s2j=O,)(/*FOUND*/s2j=sli;i+;j+;)/*found*/s2j=O'答案:1) . while( sii!='0')或 while(sli)或 while( sii!=0)2) . while( s2j!='0')或 while(s2j)或 while( s2j!=0)3). sli=s2j;4) . sii-O,;或*(sl+i)='0'或 sli=0;第23题题号:381/*【程序改错】功能:求二分之一的圆面积,函数通过形参得到圆的半径,函数返回二分之一的圆而积。例如:输入圆的半径值:19.527输出为:s =598.9500170*/include <stdio. h>#include <conio. h>/*FOUND*/double fun( r)(double s;/*FOUND*/s=1/2*3.14159* r * r;/*FOUND*/return r;main()(float x;printf ("Enter x:");scanf (&x );printf (功能:利用二维数组输出如图所示的图形。*/include <stdio. h>#include <conio. h>/*F0UND*/define N= 7main ()char aN N;int i, j, z;for(i=0;i<N;i+)for(j=0;j<N;j+)/*found*/ai j=;z=0;for (i=0; i<(N+l)/2; i+)功能:求两个形参的乘积和商数,并通过形参返回调用程序。例如:输入:61.82 和 12. 65,输出为:c = 782. 023000d = 4.886957*/#include <stdio. h>#include <conio.h> /*F0UND*/void