当前位置:文档之家› 大连理工大学C语言机房模拟试题之程序改错题(全)

大连理工大学C语言机房模拟试题之程序改错题(全)

大连理工大学C语言机房模拟试题之程序改错题(全)
大连理工大学C语言机房模拟试题之程序改错题(全)

程序改错

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:求1到20的阶乘的和。

------------------------------------------------------*/

#include "stdio.h"

void fun()

{

int n,j;

float s=0.0,t=1.0;

for(n=1;n<=20;n++)

{

/**********ERROR**********/

s=1; // t=1;

for(j=1;j<=n;j++)

/**********ERROR**********/

t=t*n; // t=t*j;

/**********ERROR**********/

s+t=s; // s=s+t; s+=t;

}

/**********ERROR**********/

printf("jiecheng=%d\n",s); //printf("jiecheng=%f\n",s);

}

main()

{

fun();

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:编写一个函数,该函数可以统计一个长度为3的字符串在另一个字符串中出现的次数。

例如:假定输入的字符串为:asdasasdfgasdaszx67asdmklo,字符串为:asd,则应输出n=4。

------------------------------------------------------*/

#include "stdio.h"

#include "string.h"

#include "conio.h"

int fun(char *str,char *substr)

/**********ERROR**********/

int i,n=0 // int i,n=0;

/**********ERROR**********/

for(i=0;i<=strlen(str);i++) // for(i=0;i<=strlen(str)-3;i++)

//for(i=0;i

if((str[i]==substr[0])&&(str[i+1]==substr[1])&&(str[i+2]==substr[2])) /**********ERROR**********/

++i; // n++; n+=1; n=n+1; ++n;

return n;

}

main()

{

char str[81],substr[4];

int n;

printf("输入主字符串: ");

gets(str);

printf("输入子字符串: ");

gets(substr);

puts(str);

puts(substr);

n=fun(str,substr);

printf("n=%d\n",n);

}

/*------------------------------------------------------

【程序改错】1246

--------------------------------------------------------

功能:有一数组内放10个整数,要求找出最小数和它的下标,然后

把它和数组中最前面的元素即第一个数对换位置.

------------------------------------------------------*/

#include "stdio.h"

main( )

{

int i,a[10],min,k=0;

printf("\n please input array 10 elements\n");

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

/***********ERROR***********/

scanf("%d", a[i]); //scanf("%d",&a[i]);

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

printf("%d",a[i]);

min=a[0];

/***********ERROR***********/

for(i=3;i<10;i++) //for(i=1;i<10;i++) for(i=0;i<10;i++)

// for(i=1;i<=9;i++) for(i=0;i<=9;i++)

/***********ERROR***********/

if(a[i]>min) // if(a[i]

{

min=a[i];

k=i;

}

/***********ERROR***********/

a[k]=a[i]; // a[k]=a[0];

a[0]=min;

printf("\n after eschange:\n");

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

printf("%d",a[i]);

printf("\nk=%d\nmin=%d\n",k,min);

}

/*------------------------------------------------------

【程序改错】1906

--------------------------------------------------------

功能:以下程序把两个数按由大到小的顺序输出来.

------------------------------------------------------*/

#include "stdio.h"

/***********ERROR***********/

void swap( int *p1,*p2) //swap( int *p1,int *p2) void swap( int *p1,int *p2) {

int p;

p=*p1;

*p1=*p2;

*p2=p;

}

main( )

{

int a,b, *p,*q;

printf("input a,b:");

/***********ERROR***********/

scanf("%d%d",a,b); //scanf("%d%d",&a,&b);

p=&a;

q=&b;

if(a

swap(p,q);

printf("a=%d,b=%d\n",a,b);

/***********ERROR***********/

printf("max=%d,min=%d\n",p,q); //printf("max=%d,min=%d\n",*p,*q);

// printf("max=%d,min=%d\n",a,b); }

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:输出Fabonacci数列的前20项,要求变量类型定义成浮点型,输出时只输出整数部分,输出项数不得多于或少于20。

------------------------------------------------------*/

#include "stdio.h"

void fun()

{

int i;

float f1=1,f2=1,f3;

/**********ERROR**********/

printf("%8d",f1); //printf("%8.0f%8.0f",f1, f2);

/**********ERROR**********/

for(i=1;i<=20;i++) //for(i=3;i<=20;i++)

{

f3=f1+f2;

/**********ERROR**********/

f2=f1; //f1=f2;

/**********ERROR**********/

f3=f2; //f2=f3;

printf("%8.0f",f1); //printf("%8.0f",f3);

}

printf("\n");

}

main()

{

fun();

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:求两数平方根之和,作为函数值返回。

例如:输入12和20,输出结果是:y = 7.936238。

------------------------------------------------------*/

#include "stdio.h"

#include "conio.h"

#include "math.h"

/**********ERROR**********/

double fun (double *a, *b) //double fun (double *a, double *b) {

double c;

/**********ERROR**********/

c = sqr(a)+sqr(b) ; //c = sqrt(*a)+sqrt(*b);

/**********ERROR**********/

return a; //return c;

}

main ( )

{

double a, b, y;

printf ( "Enter a & b : "); scanf ("%lf%lf", &a, &b );

y = fun (&a, &b); printf ("y = %f \n", y );

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:求广义菲玻那契级数的第n项。广义菲玻那契级数的前n 项为: 1,1,1,3,5,9,17,31,……

从第4项开始,每一项都是前3项之和。

项值通过函数值返回 main ( )函数。

例如:若 n = 15,则应输出:The value is: 2209。

------------------------------------------------------*/

#include "conio.h"

#include "stdio.h"

long fun ( int n )

{

long a=1, b=1, c=1, d=1, k;

/**********ERROR**********/

for (k=4; k

{

d = a+b+c;

/**********ERROR**********/

a=b //a=b;

b=c;

c=d;

}

/**********ERROR**********/

return k; //return d;

}

main( )

{

int n = 15;

printf( "The value is: %ld\n", fun ( n ) );

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:判断字符ch是否与str所指串中的某个字符相同;若相同,什么也不做,若不同,则将其插在串的最后。

------------------------------------------------------*/

#include "conio.h"

#include "stdio.h"

#include "string.h"

/**********ERROR**********/

void fun(char str, char ch ) // char *str

{

while ( *str && *str != ch )

str++;

/**********ERROR**********/

if ( *str!='\0' ) // if ( *str=='\0' )

{

str [ 0 ] = ch;

/**********ERROR**********/

str[1] = '0'; //’\0’

}

}

main( )

{

char s[81], 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 %s\n", s);

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:求1到10的阶乘的和。

------------------------------------------------------*/

#include "stdio.h"

main()

{

int i;

float s=0;

float fac(int n);

/**********ERROR**********/

for(i=1;i<10;i++) //for(i=1;i<=10;i++)

/**********ERROR**********/

s=fac(i); // s=s+fac(i); //s+=fac(i);

printf("%f\n",s);

}

float fac(int n)

{

/**********ERROR**********/

int y=1; // float y=1.0; //float y=1;

int i;

for(i=1;i<=n;i++)

y=y*i;

/**********ERROR**********/

return; //return y;

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:实现交换两个整数的值。

例如:给a和b分别输入3和6 ,输出为a=6 b=3

------------------------------------------------------*/

#include "stdio.h"

/**********ERROR**********/

void fun (int a, b) //void fun (int *a,int *b)

{

int t;

/**********ERROR**********/

t=a; // t=*a;

/**********ERROR**********/

a=b; //*a=*b;

/**********ERROR**********/

b=t; //*b=t;

}

{

int a,b;

printf("enter a,b:");scanf("%d%d",&a,&b);

fun(&a,&b);

printf("a=%d b=%d\n",a,b);

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

------------------------------------------------------*/

#include "stdio.h"

main()

{

int i,j,k;

/**********ERROR**********/

printf("\n") //printf("\n");

/**********ERROR**********/

for(i=1;i<=5;i++) //for(i=1;i<5;i++)

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

for (k=1;k<5;k++)

{

/**********ERROR**********/

if (i!=k||i!=j||j!=k) // if (i!=k&&i!=j&&j!=k)

printf("%d,%d,%d\n",i,j,k);

}

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:以下程序把两个数按由大到小的顺序输出来.

------------------------------------------------------*/

#include "stdio.h"

/***********ERROR***********/

void swap( int *p1,*p2) //swap( int *p1,int *p2)

{

int p;

p=*p1;

*p1=*p2;

}

main( )

{

int a,b, *p,*q;

printf("input a,b:");

/***********ERROR***********/

scanf("%d%d",a,b); // scanf("%d%d",&a,&b);

p=&a;

q=&b;

if(a

swap(p,q);

printf("a=%d,b=%d\n",a,b);

/***********ERROR***********/

printf("max=%d,min=%d\n",p,q); // printf("max=%d,min=%d\n",*p,*q); }

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉

一半,又多吃了一个。以后每天早上都吃了前一天剩下的一

半零一个。到第10天早上想再吃时,见只剩下一个桃子了。

求第一天共摘了多少。

------------------------------------------------------*/

#include "stdio.h"

main()

{

int day,x1,x2;

day=9;

/**********ERROR**********/

x2==1; // x2=1;

while(day>0)

{

/**********ERROR**********/

x1=(x2+1)/2; // x1=(x2+1)*2;

x2=x1;

/**********ERROR**********/

day++; //day--; day-=1; day=day-1;

}

printf("the total is %d\n",x1);

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:将s所指字符串的反序和正序进行连接形成一个新串放在

t所指的数组中。

例如:当s所指的字符串的内容为"ABCD"时,t所指数组中

的内容为"DCBAABCD"。

------------------------------------------------------*/

#include "conio.h"

#include "stdio.h"

#include "string.h"

/**********ERROR**********/

void fun (char s, char t) // void fun (char s[],char t[]) void fun (char *s,char *t)

// void fun (char *s,char t[]) void fun (char s[],char *t)

{

int i, d;

/**********ERROR**********/

d = len(s); // d = strlen(s);

/**********ERROR**********/

for (i = 1; ii; i++)

// for (i = 0; i<=d-1; i++) for (i = 0; d-1>=i; i++)

t[i] = s[d - 1 - i ];

for (i = 0; i

t[ d + i ] = s[i];

/**********ERROR**********/

t[2*d] = '/0'; // t[2*d] = '\0'; t[2*d] = NULL; t[2*d] = 0;

// t[i+d] = '\0'; t[i+d] = NULL; t[i+d] = 0;

}

main()

{

char s[100], t[100];

printf("\nPlease enter string S:");

scanf("%s", s);

fun(s, t);

printf("\nThe result is: %s\n", t);

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:一个5位数,判断它是不是回文数。即12321是回文数,个

位与万位相同,十位与千位相同。

------------------------------------------------------*/

#include "stdio.h"

main( )

{

/**********ERROR**********/

long ge,shi,qian;wan,x; //long ge,shi,qian,wan,x;

scanf("%ld",&x);

/**********ERROR**********/

wan=x%10000; //wan=x/10000;

qian=x%10000/1000;

shi=x%100/10;

ge=x%10;

/**********ERROR**********/

if (ge==wan||shi==qian) //if(ge==wan&&shi==qian) if(shi==qian&&ge==wan) printf("this number is a huiwen\n");

else

printf("this number is not a huiwen\n");

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:用下面的和式求圆周率的近似值。直到最后一项的绝对值

小于等于0.0001。

π/4= 1- 1/3 + 1/5 - 1/7 ....

------------------------------------------------------*/

#include "stdio.h"

/**********ERROR**********/

#include "math" //#include "math.h" #include

void fun()

{

int i=1;

/**********ERROR**********/

int s=0,t=1,p=1; //float s=0,t=1,p=1; double s=0,t=1,p=1;

/**********ERROR**********/

while(fabs(t)<=1e-4) // while(fabs(t)>1e-4)

{

s=s+t;

p=-p;

i=i+2;

t=p/i;

}

/**********ERROR**********/

printf("pi=%d\n",s*4); // printf("pi=%f\n",s*4); printf("pi=%lf\n",s*4); }

main()

{

fun();

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:实现两个字符串的连接。

例如:输入dfdfqe和12345时,则输出dfdfqe12345.

------------------------------------------------------*/

#include"stdio.h"

main()

{

char s1[80],s2[80];

void scat(char s1[],char s2[]);

gets(s1);

gets(s2);

scat(s1,s2);

puts(s1);

}

void scat (char s1[],char s2[])

{

int i=0,j=0;

/**********ERROR**********/

while(s1[i]= ='\0') i++; //while(s1[i]!='\0')i++; while(s1[i])i++;

//while(s1[i]!=NULL)i++; while(s1[i]!=0)i++;

/**********ERROR**********/

while(s2[j]= ='\0') //while(s2[j]!='\0') while(s2[j])

{

/**********ERROR**********/

s2[j]=s1[i]; //s1[i]=s2[j];

i++;

j++;

}

/**********ERROR**********/

s2[j]='\0'; //s1[i]='\0'; *(s1+i)='\0';

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:实现3行3列矩阵的转置,即行列互换。

------------------------------------------------------*/

#include "stdio.h"

void fun(int a[3][3],int n)

{

int i,j,t;

for(i=0;i

for(j=0;j

/**********ERROR**********/

scanf("%d",a[i][j]); //scanf("%d",&a[i][j]);

for(i=0;i

{

for(j=0;j

printf("%4d",a[i][j]);

printf("\n");

}

for(i=0;i

/**********ERROR**********/

for(j=0;j

{

/**********ERROR**********/

a[i][j]=t; //t=a[i][j];

a[i][j]=a[j][i];

/**********ERROR**********/

t=a[j][i]; //a[j][i]=t;

}

for(i=0;i

{

for(j=0;j

printf("%4d",a[i][j]);

printf("\n");

}

}

main()

{

int b[3][3];

fun(b,3);

}

/*------------------------------------------------------ 【程序改错】

--------------------------------------------------------

功能:从键盘输入10个字符,统计其中数字字符的个数。

------------------------------------------------------*/

#include

int main( )

{

int i,sum=0;

char c;

for(i=11;i>=2;i--)

{

/**********ERROR**********/

c=gets( ); //c=getchar();

if(c< '0' ||c> '9')

/**********ERROR**********/

break; //continue;

++sum;

}

printf("sum=%d",sum);

return 0;

}

/*--------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:依次取出字符串中所有数字字符, 形成新的字符串, 并取代原字符串。例如:输入ab12c3d,则输出123。

注意:不得增行或删行,也不得更改程序的结构!

------------------------------------------------------*/

#include "stdio.h"

/**********ERROR**********/

void fun(char s) //void fun(char *s) //void fun(char s[]) {

int i,j;

for(i=0,j=0; s[i]!='\0'; i++)

if(s[i]>='0'&&s[i]<='9')

/**********ERROR**********/

s[j]=s[i]; // s[j++]=s[i];

/**********ERROR**********/

s[j]="\0"; //s[j]=’\0’;

}

main()

{

char item[80];

printf("\nEnter a string: ");

gets(item);

printf("\n\nThe string is: \"%s\"\n",item);

fun(item);

printf("\n\nThe string of changing is: \"%s\"\n",item);

}

/*------------------------------------------------------

【程序改错】

--------------------------------------------------------

功能:读入一个整数m( 5≤m≤20 ),函数rnd获得m个随机整数,

函数sortpb将这m个随机整数从小到大排序。

例如:若输入整数7,则应输出:3 10 17 28 32 36 47。

------------------------------------------------------*/

#include "conio.h"

#include "stdio.h"

sortpb ( int n, int a[] )

{

/**********ERROR**********/

int i, j, p; // int i, j, p, t;

for ( j = 0; j < n-1 ; j++ )

{

p = j;

for ( i = j + 1; i < n ; i ++ )

/**********ERROR**********/

if ( a[p] > a[j] ) p = i; // if ( a[p] > a[i] ) p = i; /**********ERROR**********/

if ( p == j ) // if ( p != j )

{

t = a[j];

a[j] = a[p];

a[p] = t;

}

}

}

double rnd ( )

{

static t = 29, c = 217, m = 1024, r = 0;

r =( r*t + c )%m; return( ( double )r/m );

}

getarr( int n, int *x )

{

int i;

for( i = 1; i <= n; i++, x++ ) *x = ( int )( 50*rnd() );

}

putarr( int n, int *z )

{

int i;

for( i = 1; i <= n; i++, z++ )

{

printf( "%4d", *z );

if ( !( i%10 ) ) printf( "\n" );

}

printf("\n");

}

main()

{

int aa[20], n;

printf( "\nPlease enter an integer number between 5 and 20: " ); scanf( "%d", &n );

getarr( n, aa );

printf( "\n\nBefore sorting %d numbers:\n", n ); putarr( n, aa ); sortpb( n, aa );

printf( "\nAfter sorting %d numbers:\n", n ); putarr( n, aa );

相关主题
文本预览
相关文档 最新文档