一、程序填空题
在此程序中,通过定义学生结构体变量,存储学生的学号、姓名和3门课的成绩。函数fun的功能是:将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。
例如,若传给形参a的数据中学号、姓名和三门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后的数据应为:10002、”LiSi”、96、81、89。
#include <stdio.h>
#include <string.h>
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
__1__ fun(struct student a)
{ int i;
a.sno = 10002;
/**********found**********/
strcpy(__2__, "LiSi");
/**********found**********/
for (i=0; i<3; i++) __3__+= 1;
return a;
}
void main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);
printf("\n");
}
答案:(1) struct student (2) a.name (3) a.score[i]
二、程序修改题
在此程序中,假定整数数列中的数不重复,并存放数组中。下列给定程序中函数fun的功能是:删除数列中值为x的元素。变量n中存放数列中元素的个数。
#include <stdio.h>
#define N 20
int fun(int *a,int n,int x)
{ int p=0,i;
a[n]=x;
while( x!=a[p] )
p=p+1;
/**********found**********/
if(P==n) return -1;
else
{ for(i=p;i<n-1;i++)
/**********found**********/
a[i+1]=a[i];
return n-1;
}
}
void main()
{ int w[N]={-3,0,1,5,7,99,10,15,30,90},x,n,i;
n=10;
printf("The original data :\n");
for(i=0;i<n;i++) printf("%5d",w[i]);
printf("\nInput x (to delete): "); scanf("%d",&x);
printf("Delete : %d\n",x);
n=fun(w,n,x);
if ( n==-1 ) printf("***Not be found!***\n\n");
else
{ printf("The data after deleted:\n");
for(i=0;i<n;i++) printf("%5d",w[i]);printf("\n\n");
}
}
答案:(1) if(p==n) return -1; (2) a[i]=a[i+1];
三、程序设计题
在此程序中,编写函数fun,其功能是:将两个两位数的正整数a、b合并成一个整数放在c中。合并的方式是:将a数的十位和个位数依次放在c数的个位和百位上,b数的十位和个位数依次放在c数的千位和十位上。
例如,当a=15,b=12时,调用该函数后,c=1524。
#include <stdio.h>
void fun(int a, int b, long *c)
{
}
void main()
{ int a,b; long c;
void NONO ( );
printf("Input a, b:");
scanf("%d %d", &a, &b);
fun(a, b, &c);
printf("The result is: %ld\n", c);
NONO();
}
void NONO ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
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=%ld\n", a, b, c) ;
}
fclose(rf) ;
fclose(wf) ;
}
答案:
*c=a/10+(b%10)*10+(a%10)*100+(b/10)*1000;