2012年9月全国计算机等级考试二级C上机考试题库答案参照.pdf
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
![preview](https://dl-preview.csdnimg.cn/36141862/0001-74dbc4a4e1fb0dc2240a1bc70eb3459a_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
【全国计算机等级考试二级C上机考试题库答案解析】 全国计算机等级考试二级C语言上机部分主要测试考生对C语言编程的基本技能和问题解决能力。以下是对给出的三套试题的部分题目进行详细解答。 1. 程序填空: 题目要求去除一个整数中各位上为偶数的数,保留奇数位。正确代码如下: ```c { int t = *n % 10; // 获取最低位数字 if (t % 2 != 0) // 检查是否为奇数 { *n = x; // 保留此位 } } ``` 2. 程序修改: 题目中fun函数的目标是计算阶乘。错误在于while循环条件和递减操作的位置。修正后的代码: ```c double fun(int n) { double result = 1.0; if (n == 0) { return 1.0; } while (n > 1) { result *= n--; } return result; } ``` 3. 程序设计: 题目要求将数字字符串转换为整数。给出的代码存在一个小错误,即在将字符转换为整数时减去48,这会导致负数处理不正确。修正后的代码: ```c long fun(char *p) { int i, len, t; long x = 0; len = strlen(p); if (p[0] == '-') { t = -1; len--; p++; } else { t = 1; } while (*p) { x = x * 10 + (*p - '0'); // 修正此处,去掉-48 p++; } return x * t; } ``` 第二套题目: 1. 文件读写: 程序中需要创建文件指针并打开文件,然后逐字符读取并显示。正确代码: ```c FILE *fp = fopen("filename", "r"); // 打开文件 while (!feof(fp)) { putchar(ch); ch = fgetc(fp); } fclose(fp); // 关闭文件 ``` 2. 字符串处理: fun函数应将字符串中非数字字符替换掉。错误在于没有结束字符串。修正后的代码: ```c void fun(char *s) { int i, j; for (i = 0, j = 0; s[i] != '\0'; i++) if (s[i] >= '0' && s[i] <= '9') s[j++] = s[i]; s[j] = '\0'; // 添加结束字符 } ``` 3. 二维数组转字符串: fun函数需按列顺序将二维数组元素放入字符串。正确实现如下: ```c void fun(char s[][N], char *b) { int i, j, n = 0; for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { b[n] = s[j][i]; n = i * M + j + 1; } } b[n] = '\0'; // 添加结束字符 } ``` 第三套题目: 1. 文件操作: 函数fun用于更新文件末尾的学生数据。正确实现: ```c fp = fopen(filename, "rb+"); fseek(fp, -sizeof(STU), SEEK_END); // 移动到倒数第一个学生记录 fwrite(&n, sizeof(STU), 1, fp); // 写入新数据 ``` 2. 链表创建: Creatlink函数创建链表,错误在于头节点的初始化和链表插入。修正后的代码: ```c NODE *Creatlink(int n, int m) { NODE *h = NULL, *p, *s; int i; p = (NODE *)malloc(sizeof(NODE)); // 初始化头节点 h = p; p->next = NULL; for (i = 1; i <= n; i++) { s = (NODE *)malloc(sizeof(NODE)); s->data = rand() % m; s->next = p->next; p->next = s; p = p->next; } return h; } ``` 3. 字符串统计单词数: fun函数统计一行字符串中的单词数。正确实现: ```c int fun(char str[]) { int count = 0; int start = 0; for (int i = 0; str[i]; i++) { if (str[i] == ' ' && str[i - 1] != ' ') { // 判断单词边界 count++; } } if (str[strlen(str) - 1] != ' ') { // 检查末尾是否有单词 count++; } return count; } ``` 以上内容详细解释了每道题目要求的功能实现,涵盖了C语言的基本语法、文件操作、链表创建、字符串处理以及数组和指针的应用。这些知识点对于备考全国计算机等级考试二级C语言的考生来说非常重要。
![synapse3](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![asar](https://img-home.csdnimg.cn/images/20210720083646.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 7
- 资源: 14万+
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)