知ing

最新计算机二级C语言上机试题汇编100套

NCRE研究组 编 / 高等教育出版社

゛Zirro、Y 上传

查看本书

19: 

程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。 

例如:a所指变量s中的学号、姓名、和三门课的成绩依次是:10001" ZhangSan "958088,修改后输出t中的数据应为:10002"LiSi "968189。 

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

注意:源程序存放在考生文件夹下的BLANK1.C中。 

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

给定源程序: 

#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; 

strcpy(a->name, "LiSi"); 

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

for (i=0; i<3; i++) __2__ += 1; 

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

return __3__ ; 

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"); 

解题思路: 

本题是利用形参对结构体变量中的值进行修改并通过地址把函数值返回。 

第一处:必须定义结构指针返回类型,所以应填:struct student *。 

第二处:分别对成绩增加1分,所以应填:a->score[i]。 

第三处:返回结构指针a,所以应填:a。 

*************************************************** 

给定程序MODI1.C中函数fun的功能是:从N个字符串中找出最长的那个串,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。 

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

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

给定源程序: 

#include <stdio.h> 

#include <string.h> 

#define N 5 

#define M 81 

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

fun(char (*sq)[M]) 

{ int i; char *sp; 

sp=sq[0]; 

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

if(strlen( sp)<strlen(sq[i])) 

sp=sq[i] ; 

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

return sq; 

main() 

{ char str[N][M], *longest; int i; 

printf("Enter %d lines :\n",N); 

for(i=0; i<N; i++) gets(str[i]); 

printf("\nThe N string :\n",N); 

for(i=0; i<N; i++) puts(str[i]); 

longest=fun(str); 

printf("\nThe longest string :\n"); puts(longest); 

解题思路: 

第一处要求返回字符串的首地址,所以应改为:char *fun(char (*sq)[M])。 

第二处返回一个由变量sp控制的字符串指针,所以应改为:return sp;。 

*************************************************** 

函数fun的功能是:将ab中的两个两位正整数合并形成一个新的整数放在中。合并的方式是:a中的十位和个位数依次放在变量c的百位和个位上,b中的十位和个位数依次放在变量c的十位和千位上。 

例如,当a45b=12。调用该函数后,c=2415。 

注意部分源程序存在文件PROG1.C中。数据文件IN.DAT中的数据不得修改。 

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

给定源程序: 

#include <stdio.h> 

void fun(int a, int b, long *c) 

main() 

{ int a,b; long c; 

printf("Input a, b:"); 

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

fun(a, b, &c); 

printf("The result is: %ld\n", c); 

NONO(); 

 

解题思路: 

本题是给出两个两位数的正整数分别取出各位上的数字,再按条件组成一个新数。 

a十位数字的方法:a/10 

a个位数字的方法:a%10 

参考答案: 

void fun(int a, int b, long *c) 

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

※※※※※※※※※※※※※※※※※※※※※※※※※ 


查看更多