【计算机类职业资格】二级C语言-187 (1)及答案解析.doc

上传人:outsidejudge265 文档编号:1324971 上传时间:2019-10-17 格式:DOC 页数:8 大小:35KB
下载 相关 举报
【计算机类职业资格】二级C语言-187 (1)及答案解析.doc_第1页
第1页 / 共8页
【计算机类职业资格】二级C语言-187 (1)及答案解析.doc_第2页
第2页 / 共8页
【计算机类职业资格】二级C语言-187 (1)及答案解析.doc_第3页
第3页 / 共8页
【计算机类职业资格】二级C语言-187 (1)及答案解析.doc_第4页
第4页 / 共8页
【计算机类职业资格】二级C语言-187 (1)及答案解析.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、二级 C 语言-187 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数的功能是按条件删除一个字符串指定字符一半的数目,具体要求如下:如果该字符串所包含的指定字符的个数是奇数,则不删除,如果其数目是偶数,则删除原串后半部分的指定字符。其中,str 指向原字符串,删除后的字符串存放在 b 所指的数组中,c 中存放指定的字符。例如:当 str 输入“abcabcabcab”,c=b 时,b 的输出为“abcabcaca”;如果 str 的输入为“abcabcabca”,则 b 的输出为“abcabcabc

2、a”。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #includeconio.h #define M 80 void proc(char str, char b, char c) int i=0, j=0; int n=0; int m=0; while(stri!=“/0“) if(stri=c) n+; i+; 1; if(n%2) while(strj!=“/0“) bj=strj; j+; bj=“/0“; el

3、se while(stri!=“/0“) bj+=stri; if(stri=c) m+; if(mn/2) i+; 3; void main() char strM, bM; char c; system(“CLS“); printf(“Enter the string: /n“); gets(str); printf(“Enter the character of the string deleted: “); scanf(“%c“, proc(str, b, c); printf(“The new string is: %s/n“, b); (分数:30.00)二、程序改错题(总题数:1

4、,分数:40.00)2.下列给定的程序中,proc()函数的功能是:将 str 所指字符串中每个单词的最后一个字母改成大写(这里的“单词”是指有空格隔开的字符串)。 例如,若输入:How do you do,则输出:HoW dOyoU dO。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includestring.h #includeconio.h #includectype.h #includestdio.h void proc(char*str) int k=0; fo

5、r(; *str; str+) if(k) /*found* if(str=“) k=0; /*found* *str=toupper(*(str-1); else k=1; void main() char chrstr64; int d; system(“CLS“); printf(“/nPlease enter an English sentence within 63 letters: “); gets(chrstr); d=strlen(chrstr); chrstrd=“; chrstrd+1=0; printf(“/nBofore changing: /n%s“, chrstr)

6、; proc(chrstr); printf(“/nAfter changing: /n%s“, chrstr); (分数:40.00)_三、程序设计题(总题数:1,分数:30.00)3.请编写函数 proc(),函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。 例如,若二维数组中的值为: 0 1 2 3 4 5 9 7 4 5 4 3 8 3 6 3 5 6 8 7 则函数值为 59。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdli

7、b.h #includeconio.h #includestdio.h #define M 4 #define N 5 int proc(int aMN) void main() int aaMN=0, 1, 2, 3, 4, 5, 9, 7, 4, 5, 4, 3, 8, 3, 6, 3, 5, 6, 8, 7; int i, j, y; system(“CLS“); printf(“The original data is: /n“); for(i=0; iM; i+) for(j=0; jN; j+) printf(“%6d“, aaij); printf(“n“); y=proc(aa

8、); printf(“/nThe sun: %d/n“, y); printf(“n“); (分数:30.00)_二级 C 语言-187 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数的功能是按条件删除一个字符串指定字符一半的数目,具体要求如下:如果该字符串所包含的指定字符的个数是奇数,则不删除,如果其数目是偶数,则删除原串后半部分的指定字符。其中,str 指向原字符串,删除后的字符串存放在 b 所指的数组中,c 中存放指定的字符。例如:当 str 输入“abcabcabcab”,c=b 时,b 的输

9、出为“abcabcaca”;如果 str 的输入为“abcabcabca”,则 b 的输出为“abcabcabca”。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #includeconio.h #define M 80 void proc(char str, char b, char c) int i=0, j=0; int n=0; int m=0; while(stri!=“/0“) if(stri=c) n+; i

10、+; 1; if(n%2) while(strj!=“/0“) bj=strj; j+; bj=“/0“; else while(stri!=“/0“) bj+=stri; if(stri=c) m+; if(mn/2) i+; 3; void main() char strM, bM; char c; system(“CLS“); printf(“Enter the string: /n“); gets(str); printf(“Enter the character of the string deleted: “); scanf(“%c“, proc(str, b, c); print

11、f(“The new string is: %s/n“, b); (分数:30.00)解析:i=0 j- bj=“/0“解析 由函数 proc()可知,变量 i 为字符串元素的下标。每次重新开始遍历字符串时,下标要从 0 开始,因此,第一处填“i=0”;变量 n 中存放指定字符的个数,当 n 为偶数时,删除位置大于 n/2 且是指定字符的元素,因此,第二处填“j-”;删除后的字符串放在 b 所指的数组中,最后要添加结束符,因此,第三处填“bj=“/0“”。二、程序改错题(总题数:1,分数:40.00)2.下列给定的程序中,proc()函数的功能是:将 str 所指字符串中每个单词的最后一个字母

12、改成大写(这里的“单词”是指有空格隔开的字符串)。 例如,若输入:How do you do,则输出:HoW dOyoU dO。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includestring.h #includeconio.h #includectype.h #includestdio.h void proc(char*str) int k=0; for(; *str; str+) if(k) /*found* if(str=“) k=0; /*found* *s

13、tr=toupper(*(str-1); else k=1; void main() char chrstr64; int d; system(“CLS“); printf(“/nPlease enter an English sentence within 63 letters: “); gets(chrstr); d=strlen(chrstr); chrstrd=“; chrstrd+1=0; printf(“/nBofore changing: /n%s“, chrstr); proc(chrstr); printf(“/nAfter changing: /n%s“, chrstr);

14、 (分数:40.00)_正确答案:()解析:(1)错误:if(str=“) 正确:if(*str=“) (2)错误:*str=toupper(*(str-1); 正确:*(str-1)=toupper(*(str-1); 解析 判断一个单词结束的标志是其下一个字符为空格。变量str 是指针变量,其指向字符的地址,因此,“if(str=“)”应改为“if(*str=“)”;题目中要求将每一个单词的最后一字符转化为大写,而此时指针变量 p 指向的是单词后的空格,因此,“*str=toupper(*(str-1);”应改为“*(str-1)=toupper(*(str-1);”。三、程序设计题(总题

15、数:1,分数:30.00)3.请编写函数 proc(),函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。 例如,若二维数组中的值为: 0 1 2 3 4 5 9 7 4 5 4 3 8 3 6 3 5 6 8 7 则函数值为 59。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdlib.h #includeconio.h #includestdio.h #define M 4 #define N 5 int proc(int aMN) v

16、oid main() int aaMN=0, 1, 2, 3, 4, 5, 9, 7, 4, 5, 4, 3, 8, 3, 6, 3, 5, 6, 8, 7; int i, j, y; system(“CLS“); printf(“The original data is: /n“); for(i=0; iM; i+) for(j=0; jN; j+) printf(“%6d“, aaij); printf(“n“); y=proc(aa); printf(“/nThe sun: %d/n“, y); printf(“n“); (分数:30.00)_正确答案:()解析:int proc(int aMN) int i, j, sum=0; for(i=0; iM; i+)/i 是行下标变量 for(j=0; jN; j+)/j 是列下标变量 if(i=0|i=M-1|j=0|j=N-1) /if 是判断是否为周边元素,即行或列下标为 0,或为 N-1 sum=sum+aij; /对周边元素求和 return sum; /最后把所得到的和返回到主函数中 解析 要求二维数组周边元素之和,首先应该判断数组中的每一个元素是否是周边元素。找出所有的周边元素后求和,返回给主函数。周边元素的下标特点为行或列的下标为 0 或 N-1。根据此特点,找出所有的周边元素然后求和。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1