1、国家二级(C 语言)机试-试卷 24-1 及答案解析(总分:12.00,做题时间:90 分钟)一、程序填空题(总题数:2,分数:4.00)1.程序填空题()(分数:2.00)_2.给定程序中,函数 fun 的功能是:将形参指针所指结构体数组中的三个元素按 num 成员进行升序排列。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANKlC 中。 不得增行或删行,也不得更改程序的结构! #include typedef struct int num; char name10; PERSON; /*found*/ void fun(P
2、ERSON 【1】) /*found*/ 【2】temp; if(std0.numstd1.num) temp=std0; std0=std1; std1=temp; if(std0.numstd2.num) temp=std0; std0=std2; std2=temp; if(std1.numstd2.num) temp=std1; std1=std2; std2=temp; main() PERSON std= 5,“Zhanghu“,2,“WangLi“,6,“LinMin“ ; int i; /*found*/ fun(【3】); printf(“/nThe result is :/
3、n“); for(i=0; i_二、程序修改题(总题数:2,分数:4.00)3.程序修改题()(分数:2.00)_4.给定程序 MODllC 中函数 fun 的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序; 再把第四个字符插入到前三个字符中。待排序的字符串已在主函数中赋予。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动 mmn 函数,不得增行或删行,也不得更改程序的结构。 #include #include #define N 80 void ins
4、ert(char *aa) int i,j,n; char ch; /*found*/ n=strlem(aa); for(i=1; i=0) void NONO (); printf(“/nPlease enter a string:“); gets(a); printf(“/nPlease enter a char:“); ch = getchar(); printf(“/nThe number of the char is: %d/n“, fun(a, ch); NONO (); void NONO () /* 本函数用于打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。*
5、/ int i ; FILE *rf, *wf ; char aM, bM, ch ; rf = fopen(“in.dat“,“r“); wf = fopen(“out.dat“,“w“); for(i = 0 ; i std1.num) temp=std0; std0=std1; std1=temp; if(std0.numstd2.num) temp=std0; std0=std2; std2=temp; if(std1.numstd2.num) temp=std1; std1=std2; std2=temp; main() PERSON std= 5,“Zhanghu“,2,“Wang
6、Li“,6,“LinMin“ ; int i; /*found*/ fun(【3】); printf(“/nThe result is :/n“); for(i=0; i_正确答案:(正确答案:*std PERSON std)解析:解析:第一空:fun 函数的功能是将形参指针所指结构体数组中的三个元素进行排序,可知 fun 函数的参数是指针,由“if(std0.numstd1.num)”可知形参名为 std,故第一空处应为“void fun(PERSON *std)”。 第二空:temp 变量用来进行交换时临时保存变量值,由“temp=std0;”可知temp 是 PERSON 结构体变量,故
7、第二空处的 temp 结构体定义应为“PERSON temp;”。 第三空:这里是调用 fun 函数,fun 函数的参数是结构体变量指针,std 是结构体数组,数组名相当于数组的首地址,故第三空处应为“fun(std);”。二、程序修改题(总题数:2,分数:4.00)3.程序修改题()(分数:2.00)_解析:4.给定程序 MODllC 中函数 fun 的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序; 再把第四个字符插入到前三个字符中。待排序的字符串已在主函数中
8、赋予。 请改正程序中的错误,使它能得出正确结果。 注意:不要改动 mmn 函数,不得增行或删行,也不得更改程序的结构。 #include #include #define N 80 void insert(char *aa) int i,j,n; char ch; /*found*/ n=strlem(aa); for(i=1; i=0) ch=aai;)解析:解析:(1)strlen 是求字符串长度的函数,函数调用的形式是:strlen(字符串地址),故第一标识下应改成“n=strlen( aa );”。 (2)程序调试时会提示“c=aai;”中的 c 未定义,insert 函数中定义的是变
9、量 ch 作为存放 aa 字符串中字符的临时变量,然后对其与其他字符进行比较。所以将“c=aai;”改为“ch=aai;”。三、程序设计题(总题数:2,分数:4.00)5.程序设计题()(分数:2.00)_解析:6.请编写函数 fun,它的功能是:求出 ss 所指字符串中指定字符的个数,并返回此值。 例如,若输入字符串:123412132,输入字符为:1,则输出:3。 注意:部分源程序在文件 PROGIC 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 #include #include #define M 81 int fun(ch
10、ar *ss, char c) main() char aM, ch; void NONO (); printf(“/nPlease enter a string:“); gets(a); printf(“/nPlease enter a char:“); ch = getchar(); printf(“/nThe number of the char is: %d/n“, fun(a, ch); NONO (); void NONO () /* 本函数用于打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。*/ int i ; FILE *rf, *wf ; char aM, b
11、M, ch ; rf = fopen(“in.dat“,“r“); wf = fopen(“out.dat“,“w“); for(i = 0 ; i 10 ; i+) fscanf(rf, “%s“, a); fscanf(rf, “%s“, b); ch = *b ; fprintf(wf, “%c=%d/n“, ch, fun(a, ch); fclose(rf); fclose(wf); (分数:2.00)_正确答案:(正确答案: int cnt=0; char *p=ss; while(*p) if(*p=c)cnt+; p+; return cnt; )解析:解析:如果不是串尾就进入 while 循环,在循环过程中利用 if 条件表达式判断当前字符是否为指定的字符,如果符合条件,那么对统计计数变量进行加 1 操作。