二级C语言

第1121题

函数fun的功能是:根据所给的年、月、日,计算出该日是这一年的第几天,并作为函数值返回。其中函数isleap用来判别某一年是否为闰年。

例如,若输入:200851,则程序输出:2008年5月1日是该年的第122天。

请在程序的下划线处填入正确的内容

#include <stdio.h>
#include <stdlib.h>
int isleap(int year)
{
	int leap;
	leap= (year%4==0 && year%100!=0 || year%400==0);
	/**********found**********/
	return __(1)__;
}
int fun(int year, int month, int day) 
{
	int table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int days=0,i;
	for(i=1; i<month; i++)
	{
		days=days + table[i];
	}
	/**********found**********/
	days=days+__(2)__;
	if(isleap(year) && month>2)
	{
		/**********found**********/
		days=days+__(3)__;
	}
	return days;
}
main()
{  
	int year, month, day,days;
	printf("请输入年、月、日:");
	scanf("%d%d%d", &year, &month, &day);
	days = fun(year, month, day);
	printf("%d年%d月%d日是该年的第%d天\n", year, month, day, days);
	system("pause");
}


第1122题

函数fun的功能是:在有n名学生,2门课成绩的结构体数组std中,计算出第1门课程的平均分,作为函数值返回。

例如,主函数中给出了4名学生的数据,则程序运行的结果为:第1门课程的平均分是:76.125000。

请改正程序中的错误,使它能得出正确的结果。

#include <stdio.h>
#include <stdlib.h>
typedef struct
{ 
	char num[8];
	double score[2];
} STU;
double fun(STU std[],int n)
{
	int i;
	/**********found**********/
	double sum;
	for(i=0; i<n; i++)
	{
		/**********found**********/
		sum += std[i].score[1]; 
	}
	return sum/n;
}
main()
{
	STU std[]={"N1001",76.5,82.0 ,"N1002",66.5,73.0,
					  "N1005",80.5,66.0,"N1006",81.0,56.0};
	printf("第1门课程的平均分是:%lf\n", fun(std,4));
	system("pause");
}


第1123题

请编写函数proc,其功能是:判断形参n中的正整数是几位数(输入数据的位数不超过4位),并将结果通过函数值返回。

例如:若输入的数据为123,则输出结果为:输入的数字是3位。

#include <stdio.h>
#include <stdlib.h>
void NONO();
int proc(int n)
{
     int t=0;
     if(①)
     {
          ②;
     }
     else if(③)
     {
          ④;
     }
     else if(⑤)
     {
          ⑥;
     }
     else
     {
           ⑦;
     }
     return t;
}
main()
{
	int n, place;
	do
	{
		printf("请输入一个4位以内的正整数:");
		scanf("%d", &n);
	}
	while (n<0 || n>9999);
	place=proc(n);
	printf("输入的数字是%d位\n", place);
	NONO();
	system("pause");
}
void NONO()
{/* 本函数用于打开文件,输入数据,
 调用函数,输出数据,关闭文件。*/
	FILE *rf,*wf;
	int i, n, place;
	rf=fopen("in.dat","r");
	wf=fopen("out.dat","w");
	for( i=0; i<8; i++ )
	{
		fscanf(rf, "%d ", &n);
		place=proc(n);
		fprintf(wf, "%d\n", place);
	}
	fclose(rf);
	fclose(wf);
}
第1124题

给定程序中,函数fun的功能是:不断从终端读入整数,由变量a统计大于0的个数,用变量c来统计小于0的个数,当输入O时结束输入,并通过形参pa和pb把统计的数据传回主函数进行输出。

#include <stdio.h>
#include <stdlib.h>
void fun(int *px,int *py)
{
	/**********found**********/
	int __(1)__;
	scanf("%d",&k);
	/**********found**********/
	while __(2)__
	{
		if(k>0)
		{
			a++;
		}
		if(k<0)
		{
			c++;
		}
		/**********found**********/
		__(3)__;
	}
	*px=a;
	*py=c;
}
main()
{
	int x, y;
	fun(&x,&y);
	printf("x=%d y=%d\n", x, y);
	system("pause");
}
第1125题

将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun的作用是:累加链表结点数据域中的数据作为函数值返回。

请改正程序中的错误,使它能得出正确的结果。

#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
	int data;
	struct list *next;
} LIST;
int fun(LIST *h)
{
	LIST *p;
	int t=0;
	p=h;
	/**********found**********/
	while(*p)
	{
		/**********found**********/
		t=t+p.data;
		p=(*p).next;
	}
	return  t;
}
main()
{
	LIST a,b,c,*h; 
	a.data=34;
	b.data=51;
	c.data=87;
	c.next='\0';
	h=&a;
	a.next=&b;
	b.next=&c;
	printf("总和 = %d\n",fun(h));
	system("pause");
}


第1126题

请编写函数fun,其功能是分别统计形参t所指二维数组中字母A和C的个数。

#include <stdio.h>
#include <stdlib.h>
#define M 14
void fun(char (*t)[M],int *a,int *c)
{
      int i,j;
      *a=0;*c=0;
      for(①)
      {
         for(②)
         {
             if(③)
             {
                  ④;
             }
             else if(⑤)
             {
                   ⑥;
             }
         }
      }
}
void get(char (*s)[M])
{
	int i, j;
	for(i=0; i<M; i++)
	{
		for(j=0; j<M; j++)
		{
			s[i][j]=65+rand()%12;
			printf("%c", s[i][j]);
		}
		printf("\n");
	}
}
main()
{ 
	void NONO();
	char a[M][M];
	int x,y;
	get(a);
	fun(a, &x, &y);
	printf("A的个数为%d  C的个数为 %d\n", x, y);
	NONO();
}
void NONO()
{/* 本函数用于打开文件,输入数据,调用函数,
 输出数据,关闭文件。 */
	FILE *rf, *wf;
	int i, j, x, y;
	char a[M][M];
	rf=fopen("in.dat","r");
	wf=fopen("out.dat","w");
	for( i=0; i<M; i++ )
	{
		for( j=0; j<M; j++)
		{
			fscanf(rf,"%c", &a[i][j]);
		}
	}
	fun (a, &x, &y);
	fprintf(wf, "A=%d\n", x);
	fprintf(wf, "C=%d\n", y);
	fclose(rf);
	fclose(wf);
	system("pause");
}


第1127题

给定程序中,函数fun的功能是建立一个NxN的矩阵。矩阵元素的构成规律是:最外层元素的值全部为1;从外向内第2层元素的值全部为2;第3层元素的值全部为3,.…依次类推。

例如,若N=5,生成的矩阵为:

11111

12221

12321

12221

11111

#include <stdio.h>
#include <stdlib.h>
#define N 5
/**********found**********/
void fun(int (*a)__(1)__)
{
	int i, j, k, m;
	if(N%2==0)
	{
		m=N/2 ;
	}
	else
	{
		m=N/2+1;
	}
	for(i=0; i<m; i++) 
	{
		/**********found**********/
		for(j=__(2)_ ; j<N-i; j++)
		{
			a[i][j]=a[N-i-1][j]=i+1;
		}
		for(k=i+1; k<N-i; k++)
		{
			/**********found**********/
			a[k][i]=a[k][N-i-1]=__(3)__;
		}
	}
}
main()
{
	int x[N][N]={0},i,j;
	fun(x);
	printf("\nThe result is:\n");
	for(i=0; i<N; i++)
	{
		for(j=0; j<N; j++) 
		{
			printf("%3d",x[i][j]);
		}
		printf("\n");
	}
	system("pause");
}


第1128题

函数fun的功能是:将十进制正整数m转换成k(2sks9)进制数,并按高位到低位顺序输出。

例如,若输入8和2,则应输出1000(即+进制数8转换成二进制表示是1000)。

请改正程序中的错误,使它能得出正确的结果。

#include <stdio.h>
#include <stdlib.h>
void fun(int m, int k);
{
	int aa[20], i;
	for(i = 0; m; i++)
	{
		/**********found**********/
		aa[i] = m/k;
		m /= k;
	}
	for(; i; i--)
	{
		/**********found**********/
		printf("%d",aa[i]);
	}
}
main()
{
	int b, n;
	printf("\nPlease enter a number and a base:\n");
	scanf("%d %d", &n, &b);
	fun(n, b);
	printf("\n");
	system("pause");
}
第1129题

编写一个函数fun,从num个字符串中找出最长的一个字符串,并通过形参指针max传回该串地址。(注意:主函数中用****作为结束输入的标志。)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun(char(*a)[81], int num, char **max)
{
int i, maxlen; 
maxlen=strlen(a[0]); 
for(①)
{
if(②)
{
③
*max=a[i];④
}
}
main()
{
	char ss[10][81], *ps;
	void NONO();
	int n, i=0;
	printf("输入若干个字符串:");
	gets(ss[i]);
	puts(ss[i]);
	while(!strcmp(ss[i],"****")==0)
	{
		i++;
		gets(ss[i]);
		puts(ss[i]);
	}
	n=i;
	fun(ss, n, &ps);
	printf("\nmax=%s\n", ps);
	NONO();
}
void NONO ()
{/* 请在此函数内打开文件,输入测试数据,
 调用函数,输出数据,关闭文件。 */
	char ss[20][81], *ps;
	int n, i=0;
	FILE *rf, *wf;
	rf=fopen("in.dat","r");
	wf=fopen("out.dat","w");
	fgets(ss[i], 81, rf);
	while(!strncmp(ss[i],"****",4)==0)
	{
		i++;
		fgets(ss[i], 81, rf);
	}
	n=i;
	fun(ss, n, &ps);
	fprintf(wf, "%s", ps);
	fclose(rf);
	fclose(wf);
	system("pause");
}


第1130题

给定程序中,函数fun的功能是:判定形参a所指的NxN(规定N为奇数)的矩阵是否是"幻方",若是,函数返回值为1;不是,函数返回值为0。“幻方"的判定条件是:矩阵每行、每列、主对角线及反对角线上元素之和都相等。

例如,以下3x3的矩阵就是一个"幻方":

4 9 2

3 5 7

8 1 6

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

#include <stdio.h>
#include <stdlib.h>
#define N 3
int fun(int (*a)[N])
{
	int i,j,m1,m2,row,colum;
	m1=m2=0;
	for(i=0; i<N; i++)
	{
		j=N-i-1; 
		m1+=a[i][i]; 
		m2+=a[i][j];
	}
	if(m1!=m2)
	{
		return 0;
	}
	for(i=0; i<N; i++) 
	{
		/**********found**********/
		row=colum=__(1)__;
		for(j=0; j<N; j++)
		{
			row+=a[i][j]; 
			colum+=a[j][i];
		}
		/**********found**********/
		if((row!=colum) __(2)__ (row!=m1))
		{
			return 0;
		}
	}
	/**********found**********/
	return __(3)__;
}
main()
{
	int x[N][N], i, j;
	printf("输入一个3×3的矩阵:\n");
	for(i=0; i<N; i++)
	{
		for(j=0; j<N; j++) 
		{
			scanf("%d", &x[i][j]);
		}
	}
	if(fun(x)) 
	{
		printf("这个矩阵是幻方\n");
	}
	else
	{
		printf("这个矩阵不是幻方\n");
	}
	system("pause");
}


第1131题

给定程序fun函数的功能是:根据整型形参m,计算如下公式的值:

Snipaste_2021-04-22_00-56-28.png

例如,若主函数中输入5,则应输出-0.283333

请改正程序中的错误,使它能得出正确的结果。

不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
#include <stdlib.h>
double fun(int m)
{
	double t=1.0;
	int i;
	for(i=2; i<=m; i++)
	{
		/**********found**********/
		t = 1.0-1/i;
	}
	/**********found**********/
	______;
}
main()
{
	int m ;
	printf("\nPlease enter 1 integer numbers:\n");
	scanf("%d", &m);
	printf("\n\nThe result is %lf\n", fun(m));
	system("pause");
}


第1132题

请编写一个函数fun,函数的功能是删除字符串中的所有空格。

例如,主函数中输入"asd af aa 267",则输出为"asdafaaz67"

请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void fun(char *str)
{
    int i=0;
    char *p=str;
    while(①)
    {
      if(②)
      {
        ③
      }
      ④
    }
    ⑤
}
main()
{
	char str[81];
	void NONO ();
	printf("Input a string:");
	gets(str);
	puts(str);
	fun(str);
	printf("*** str:%s\n",str);
	NONO();
}
void NONO ()
{/* 请在此函数内打开文件,输入调试数据,
 调用 fun 函数,输出数据,关闭文件。 */
	char str[81];
	int n=0;
	FILE *rf, *wf;
	rf=fopen("in.dat","r");
	wf=fopen("out.dat","w");
	while(n<8) 
	{
		fgets(str, 80, rf);
		fun(str);
		fprintf(wf, "%s", str);
		n++;
	}
	fclose(rf);
	fclose(wf);
	system("pause");
}


第1133题

给定程序中,函数fun的功能是根据形参i的值返回某个函数的值。当调用正确时,程序输出:

x1=5.000000,x2=3.000000,x1*x1 +x1*x2=40.000000

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

#include <stdio.h>
#include <stdlib.h>
double f1(double x)
{
	return x*x;
}
double f2(double x, double y)
{
	return x*y;
}
/**********found**********/
__(1)__ fun(int i, double x, double y)
{
	if(i==1)	
	{
		/**********found**********/
		return __(2)__(x);
	}
	else 
	{
		/**********found**********/
		return __(3)__(x,y);
	}
}
main()
{
	double x1=5, x2=3, r;
	r = fun(1, x1, x2);
	r += fun(2, x1, x2);
	printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);
	system("pause");
}


第1134题

给定程序函数fun的功能是:比较两个字符串,将长的那个字符串的首地址作为函数值返回。

请改正程序中的错误,使它能得出正确的结果。

不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include <conio.h>
#include <stdio.h>
/**********found**********/
char fun(char *s, char *t)
{
	int s1=0, t1=0;
	char *ss, *tt;
	ss=s;
	tt=t;	
	while(*ss)
	{
		s1++;
		/**********found**********/
		(*ss)++;
	}
	while(*tt)
	{
		t1++;
		tt++;
	}
	if (t1>s1)
	{
		return t;
	}
	else 
	{
		return s;
	}
}
main()
{
	char a[80],b[80];
	printf("\nEnter a string:");
	gets(a);
	printf("\nEnter a string again:");
	gets(b);
	printf("\n\nThe longer is:\n\n%s\n",fun(a,b));
	system("pause");
}


第1135题

请编写函数fun,函数的功能是:判断字符串是否为回文?若是,函数返回1,主函数中输出:YES;否则返回0,主函数中输出NO,回文是指顺读和倒读都一样的字符串。

例如,字符串LEVEL是回文,而字符串123312就不是回文。

请勿改动主函数main和其它函数中的任何内容,仅在函数fun中填入你编写的若干语句。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 80
int fun(char *str)
{
    int i,n=0,fg=1;
    char *p=str;
    while(①)
    {
         ②
         ③
    }
    for(i=0;i<n/2;i++)
    {
         if(④)
        {
            ⑤
            ⑥
        }
    
    }
    return fg;

}
main()
{
char s[N];
void NONO();
printf("Enter a string:"); 
gets(s);
printf("\n");
puts(s);
if(fun(s)) 
{
printf("YES\n");
}
else 
{
printf("NO\n");
}
NONO();
system("pause");
}
void NONO()
{/* 请在此函数内打开文件,输入测试数据,
 调用函数,输出数据,关闭文件。 */
FILE *rf, *wf;
int i; 
char s[N];
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0; i<8; i++)
{
fscanf(rf, "%s", s);
if(fun(s)) 
{
fprintf(wf, "%s YES\n", s);
}
else 
{
fprintf(wf, "%s NO\n", s);
}
}
fclose(rf); 
fclose(wf);
}
第1136题

用筛选法可得到2-n(n<10000)之间的所有素数,方法是:首先从素数2开始,将所有2的倍数的数从数表中删去(把数表中相应位置的值置成0);接着从数表中找下一个非0数,并从数表中删去该数的所有倍数;依此类推,直到所找的下一个数等于n为止。这样会得到一个序列:

2,3,5,7,11,13,17,19,23,....

函数fun用筛选法找出所有小于等于n的素数,并统计素数的个数作为函数值返回。

请在程序的下划线处填入正确的内容,使程序得出正确的结果。

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

#include <stdio.h>
#include <stdlib.h>
int fun(int n)
{
	int a[10000], i, j, count=0;
	for (i=2; i<=n; i++)
	{
		a[i] = i;
	}
	i = 2;
	while (i<n)
	{
		/**********found**********/
		for (j=a[i]*2; j<=n; j+=__(1)__)
		{
			a[j] = 0;
		}
		i++;
		/**********found**********/
		while (__(2)__==0)
		{
			i++;
		}
	}
	printf("\n 2 到 %d 的素数有:\n", n);
	for (i=2; i<=n; i++)
	{
		/**********found**********/
		if (a[i]!=__(3)__)
		{
			count++; 
			printf(count%15?"%5d":"\n%5d",a[i]);
		}
	}
	return count;
}
main()
{
	int n=30, r;
	r = fun(n);
	printf("\n素数的个数为:%d\n", r);
	system("pause");
}


第1137题

函数fun的功能是:为一个偶数寻找两个素数,这两个素数之和等于该偶数,并将这两个素数通过形参指针传回主函数。

请改正程序中的错误,使它能得出正确的结果。

不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include <stdio.h>
#include <math.h>
void fun(int a,int *b,int *c)
{
	int i, j, d, y;
	for(i=3; i<=a/2; i=i+2)
	{
		/**************found**************/
		Y=1;
		for(j=2; j<=sqrt((double)i); j++)
		{
			if(i%j==0) 
			{
				y=0;
			}
		}
		if(y==1)
		{
			/**************found**************/
			d==a-i;
			for(j=2; j<=sqrt((double)d); j++)
			{
				if(d%j==0) y=0;
			}
			if(y==1)
			{ 
				*b=i; *c=d;
			}
		}
	}
}
main()
{
	int a,b,c;
	do
	{
		printf("\nInput a:"); 
		scanf("%d",&a);
	}
	while(a%2);
	fun(a,&b,&c);
	printf("\n\n%d = %d + %d\n",a,b,c);
	system("pause");
}


第1138题

请编写函数fun,它的功能是:计算并输出n(包括n)以内能被5或9整除的所有自然数的倒数之和。

例如,在主函数中从键盘给n输入20后,输出为:s=0.583333.

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

#include <stdio.h>
#include <stdlib.h>
double fun(int n)
{
    int i;
    double sun=0.0;
    for(i=1;i<n;i++)
    {
        if(①)
        {
             ②
        }
    }
    return sum;
}
main()
{
	void NONO();
	int n; 
	double s;
	printf("\nInput n: "); 
	scanf("%d",&n);
	s=fun(n);
	printf("\ns=%f\n",s);
	NONO();
	system("pause");
}
void NONO()
{/* 请在此函数内打开文件,输入测试数据,
 调用函数,输出数据,关闭文件。 */
	FILE *rf, *wf; 
	int n, i; 
	double s;
	rf=fopen("in.dat","r");
	wf=fopen("out.dat","w");
	for(i=0; i<8; i++) 
	{
		fscanf(rf, "%d", &n);
		s=fun(n);
		fprintf(wf, "%lf\n", s);
	}
	fclose(rf); 
	fclose(wf);
}


第1139题

函数fun的功能是:从三个形参a,b,c中找出中间的那个数,作为函数值返

回。例如,当a=3, b=5, c=4时,中数为4。

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。

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

int fun(int a, int b, int c)
{
    int t; t = (a>b) ? (b>c? b :(a>c?c:___1___)) : ((a>c)?___2___ : ((b>c)?c:___3___)); 
    return t;
}
第1140题

给定程序中函数fun的功能是:首先将大写字母转换为对应小写字母;若小写字母为a~u,则将其转换为其后的第5个字母;若小写字母为v~z,使其值减21,转换后的小写字母作为函数值返回例如,若形参是字母A,则转换为小写字母;若形参是字母W,则转换为小写字母b。

请改正程序中的错误,使它能得出正确的结果。

不要改动main函数,不得增行或删行,也不得更改程序的结构!

#include<stdio.h>
#include<stype.h>
char fun(char c)
{
    /********found********/
    if (c>= 'A' && c<='Z')
        c = c-32;
/********found********/
    if (c>='a' && c<='u')
        c = c-5;
    else if (c>='v' && c<='z')
        c = c-21;
    return c;
}
main ( )
{
    char cl, c2;
    printf("\nEnter a letter(A-Z):");
    cl = get char( );
    if (isupper(cl))
    {
        c2 = fun(cl);
        printf("\n k nThe letter %c change to %c\n",cl,c2);
    }
    else
    {
        printf("\nEnter (A-Z) !\n");
    }
}