计算机二级c语言上机100题 联系客服

发布时间 : 星期五 文章计算机二级c语言上机100题更新完毕开始阅读c0c450b9c77da26925c5b00c

素中,右上三角元素置为0。例如,若N=3,有下列矩阵: 1 2 3 4 5 6

7 8 9计算结果为: 1 0 0 6 5 0

10 14 9

#define N 4 void fun(int (*t)[N]) { int i, j; for(i=1; i

/**********found**********/ t[i][j] =t[i][j]+t[j][i];

/**********found**********/ t[j][i]=0;}}} 50.2:改错

Fun:计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值。其中x和y的值不等,z和y的值不等。如当x的值为9、y的值为11、z的值为15时,函数值为-3.50。 #include #include

/************found************/ #define FU(m,n) ((m)/(n)) float fun(float a,float b,float c) { float value;

value=FU(a+b,a-b)+FU(c+b,c-b); /************found************/

return (value);} 50.3:设计

规定输入的字符串中只包含字母和*号,函数fun的功能是:将字符串中的前导*号全部删除,中间和尾部的*号不删除。如当字符串中的内容为:*******A*BC*DEF*G****,删除后,字符串中的内容应当是:A*BC*DEF*G****,在编写函数时,不得使用C语言提供的字符串函数。 void fun( char *a ) {int j=0;

char *p = a ; while(*p == '*') p++ ; while(*p) { a[j++] = *p ; p++;}

a[j]=0 ;} 51.1:填空

Fun:计算出形参s所指字符串中包含的单词个数,作为函数值返回,为便于统计,规定各单词之间用空格隔开。如形参s所指的字符串为:This is a C language program,函数的返回值为6 。

int fun(char *s)

{ int n=0, flag=0;

while(*s!='\\0')

{ if(*s!=' ' && flag==0) { n++ ; flag=1;} if (*s==' ') flag= 0 ; s++ ;} return n;}

51.2:改错

Fun:从n(形参)个学生的成绩中统计出低于平均分的学生人数,此人数由函数值返回,平均分存放在形参aver所指的存储单元中。如输入8名学生的成绩:80.5 60 72 90.5 98 51.5 88 64 ,则低于平均分的学生人数为:4(平均分为:75.5625)。 #define N 20

int fun ( float *s, int n, float *aver ) { float ave, t = 0.0 ; int count = 0, k, i ; for ( k = 0 ; k < n ; k++ )

/**************found**************/ t += s[k] ; ave = t / n ;

for ( i = 0 ; i < n ; i++ ) if ( s[ i ] < ave ) count++ ;

/**************found**************/ *aver = ave ;

return count ;} 51.3:设计

Fun:功能是求出数组的最大元素在数组中的下标并存放在k所指的存储单元中。如输入如下整数:876 675 896 101 301 401 980 431 451 777 ,则输出结果为6,980 int fun(int *s, int t, int *k) {int i, pos = 0, max = *s ; for(i = 1 ; i < t ; i++) { if(max < *(s + i)) { max = *(s + i) ; pos = i ;}} *k = pos ;}

52.1:填空

Fun:将N×N矩阵中元素的值按列右移一个位置,右边被移出矩阵的元素绕回左边。如N=3,有下列矩阵: 计算结果为:

1 2 3 3 1 2 4 5 6 6 4 5 7 8 9 9 7 8 #define N 4

void fun(int (*t)[N]) { int i, j, x;

/**********found**********/ for(i=0; i

{

/**********found**********/ x=t[i][N-1] ;

for(j=N-1; j>=1; j--) t[i][j]=t[i][j-1]; /**********found**********/ t[i][0]=x;}} 52.2:改错

Fun:计算并输出下列级数的前N项之和SN,直到SN?1大于

q

21{ double f, t; int n; f = 1.0 + x;

/**********found**********/ t = x; n = 1; do {

n++;

/**********found**********/ t *= (-1.0)*x/n; f += t; }

/**********found**********/ while (fabs(t) >= 1e-6);

return f;} 53.2:改错

Fun:求整数x的y次方的低3位值。如整数5的6次方为15625,此值的低3位值为625。 long fun(int x,int y,long *p ) { int i; long t=1;

/**************found**************/ for(i=1; i<=y; i++) t=t*x; *p=t;

/**************found**************/ t=t00;

return t;} 53.3:设计

Fun:计算并输出当x<0.97时下列多项式的值,直到Sn?Sn?1?0.000001为止。

Sn?1?0.5x?0.5(0.5?1)2!x?2为止,q

?32?43???的值通过形参传入。

N?1NSN?,如q的值为50.0,则函数

值为:49.394948。 double fun( double q ) { int n; double s,t; n = 2; s = 2.0;

while (s<=q) {t=s;

/************found************/ s=s+(n+1.)/n; n++;}

printf(\

/************found************/ return t;}

52.3:设计

Fun:求Fibonacci数列中大于t的最小的一个数,结果由函数返回。其中Fibonacci数列F(n)定义为:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2),如当t=1000时,函数值为1597。 #include int fun( int t) { int f0 = 0, f1 = 1, f ; do { f = f0 + f1 ; f0 = f1 ; f1 = f ; } while(f < t) ; return f ;} 53.1:填空 Fun:

f(x)?1?x?x20.5(0.5?1)(0.5?2)3!x???30如在主函数中从键盘给x输入0.21后,输出为:s=1.100000.

#include double fun(double x) {int n=1; /* 循环计数*/

double sn=1; /* 累计数*/

double xn=1,xn1=0; /*x的n值,以及x的n-1值;*/

n?2 while(fabs(xn-xn1)>=0.000001)/*绝对值是否满足条件*/ {xn=xn*x*(0.5-n+1)/n; /*表达式分解以后

n?12!?x3!?x4!???(?1)x3n?(n?1)!?(?1)xnxn=(xn-1)*x*(0.5-n+1)/n*/ 41 n+=1;

n! sn+=xn; /*sn累加上xn*/} return(sn);}

54.1:填空

Fun:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 #include

直到

xnn!?10。若x=2.5,函数值为1.917975.

?6#include double fun(double x)

#define N 8 typedef struct list { int data;

struct list *next; } SLIST;

SLIST *creatlist(int *); void outlist(SLIST *); int fun( SLIST *h)

{ SLIST *p; int s=0; p=h->next; while(p)

{

/**********found**********/ s+= p->data;

/**********found**********/

p=p->next; }

return s;} 54.2:改错

Fun:将s所指字符串中出现的与t1所指字符串相同的子串全部替换成t2所指字符串,所形成的新串放在w所指的数组中。要求t1和t2所指字符串的长度相同。如当s所指字符串中内容为 :“abcdabfab”t1所指子串中的内容为:“ab”,t2所指子串中的内容为:“99”时,结果在w所指的数组中的内容应为:“99cd99f99”。 #include

int fun (char *s, char *t1, char *t2 , char *w) {int i; char *p , *r, *a; strcpy( w, s ); while ( *w )

{ p = w; r = t1;

/************found************/ while ( *r )

if ( *r == *p ) { r++; p++; } else break; if ( *r == '\\0' )

{ a = w; r = t2; while ( *r ){

/************found************/ *a = *r; a++; r++;}

w += strlen(t2) ;} else w++;}} 54.3:设计

Fun:将s所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指数组中。如当s所指字符串中的内容为“ABCDEFGHIJK”,在t所指数组中的内容应是:“BDFHJ”。

#include

void fun(char *s, char t[]) {int i, j = 0 ;

for(i = 1 ; i < strlen(s); i+=2) t[j++] = s[i] ;

t[j] = 0 ;} 55.1:填空

Fun:判断形参s所指字符串是否是“回文”(Palindrome),若是,函数返回值为1;不是,函数返回值为0。“回文”是正读和反读都一样的字符串(不区分大小写字母)。如LEVEL和Level是“回文”,而LEVLEV不是“回文”。 #include #include int fun(char *s) { char *lp,*rp;

/**********found**********/ lp= s ;

rp=s+strlen(s)-1;

while((toupper(*lp)==toupper(*rp)) && (lp

/**********found**********/ if(lp

55.2:改错

Fun:求出以下分数序列的前

n项之和。

238131,2,53,5,8,?1和值通过函数返回23,1main函数。如n=5,则应输出:8.391667。 double fun (int n ) { int a = 2, b = 1, c, k ;

double s=0.0 ; for ( k = 1; k <= n; k++ )

{ s = s + 1.0 * a / b ;

/**************found**************/ c = a; a += b; b = c;} return(s) ;}

55.3:设计

Fun:将大于形参m且紧靠m的k个素数存入xx所指的数组中。如输入17,5则应输出:19,23,29,31,37。 void fun(int m, int k, int xx[]) {

/* 以下代码仅供参考 */ int i, j=1, t=m+1;

while(j<=k) {

/* 以下完成判断素数,并存放到数组xx中 */ for(i = 2 ; i < t; i++) if(t % i==0) break; if(i==t) { xx[j-1] = i; j++; } t++;}

}

}

56.1:填空

从键盘输入若干行文本(每行不超过80个字符),写到文件myfile4.txt中,用-1作为字符串输入结束标志。然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和WriteText实现。 #include #include void WriteText(FILE *); void ReadText(FILE *); main()

{ FILE *fp;

if((fp=fopen(\ { printf(\ } WriteText(fp); fclose(fp);

if((fp=fopen(\

{ printf(\ }

ReadText(fp);

fclose(fp); }

/**********found**********/ void WriteText(FILE *fw)

{ char str[81];

printf(\ gets(str);

while(strcmp(str,\/**********found**********/ fputs(str,fw); fputs(\ gets(str); } }

void ReadText(FILE *fr) { char str[81];

printf(\ fgets(str,81,fr); while( !feof(fr) ) {

/**********found**********/ printf(\

fgets(str,81,fr); }} 56.2:改错

Fun:从低位开始取出长整型变量s中奇数位上的数,依次构成一个新数放在t中,高位仍在高位,低位仍在低位。如当s中的数为:7654321时,t中的数为:7531。 void fun (long s, long *t) { long sl=10; *t = s % 10; while ( s > 0)

{ s = s/100;

*t = s * sl + *t;

/************found************/ sl = sl*10;}}

56.3:设计

Fun:将两个两位数的正整数a、b合并形成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的个位和百位上,b数的十位和个位数依次放在c数的千位和十位上。如当a=45,b=12时,调用函数后,c=1524。 void fun(int a, int b, long *c)

{ *c=(b/10)*1000+(a)*100+(b)*10+(a/10); }

57.1:填空

Fun:把形参a所指数组中的最小值放在元素a[0]中,接着把形参a所指数组中的最大值放在a[1]元素中;再把a所指数组元素中的次小值放在a[2]中,把a所指数组元素中的次大值放在a[3]中;其余以此类推。若a所指数组中的数据最初排列为:9、1、4、2、3、6、5、8、7;则按规则移动后,数据排列为1、9、2、8、3、7、4、6、5。形参n中存放a所指数组中数据的个数。Fun函数中的max存放当前所找的最大值,px存放当前所找的最大值的下标。

#define N 9 void fun(int a[], int n) { int i,j, max, min, px, pn, t; for (i=0; i

/**********found**********/ max = min = a[i]; px = pn = i;

for (j=i+1; j

{ max = a[j]; px = j; } /**********found**********/ if (min>a[j])

{ min = a[j]; pn = j; } }

if (pn != i)

{ t = a[i]; a[i] = min; a[pn] = t; if (px == i) px =pn; }

if (px != i+1)

{ t = a[i+1]; a[i+1] = max; a[px] = t; }

}} 57.2:改错

Fun:用递归算法计算斐波拉契数列中第n项的值。从第一项起,斐波拉契数列为:1、1、2、3、5、8、13、21、?若给n输入7,该项的斐波拉契数值为:13。 long fun(int g)