C语言第十章复习题(含答案)
- 格式:pdf
- 大小:131.51 KB
- 文档页数:15
高等教育自学考试C语言程序设计(二)课程代号(05772)同步练习题班级学号姓名二O 一六年三月目录第一章C语言概述第二章基本数据类型第三章控制结构第四章运算符和表达式第五章函数第六章数组第七章字符与字符串第八章变量类别与编译预处第九章指针第十章结构体、共用体和枚举类型第十一章文件第一章C语言概述一、单项选择题1.在计算机上可以直接运行的程序是()。
A.高级语言程序B.汇编语言程序C.机器语言程序D.C语言程序2. 一个C语言程序是由()A.若干函数组成B.若干过程组成C.若干主程序组成D.若干子程序组成3. C语言不具有的特点是()A.具有结构化的控制语句B.数据类型丰富C.语法限制不太严格,程序设计自由度大D.在可移植性上,C语言比其他语言差4. 以下叙述不正确的是()A.一个C语言程序可由一个或多个函数组成B.一个C语言程序必须包含一个主函数C.C语言程序的基本组成单位是函数D.在C语言程序中,注释说明只能位于一条语句的后面5. 以下叙述正确的是()A.C语言比其他语言高级B.C语言可以不用编译就能被计算机识别和执行C.C语言以接近英语国家的自然语言和数学语言作为语言的表达形式D.C语言出现的最晚,所以具有其他语言的一切优点二、填空1. 计算机语言的发展经过了、和等阶段。
2. C语言既适合编写,也适合编写应用软件。
标准答案一、单项选择题CADDC二、填空1. 机器语言,汇编语言,高级语言2. 系统软件第二章基本数据类型一、单项选择题1.正确的C语言用户自定义标识符是()A.printB.floatC.when?D.random%22.属于C语言基本数据类型的是( )A.指针型B.无符号整型C.数组型D.结构型3.C语言的长整型数值在计算机中占用的字节个数是( )A.1B.2C.3D.44.C语言中,″\x3d″在内存中占用的字节数是( )A.1B.2C.4D.55.下列关于C语言的叙述错误的是( )A. 英文字母大小写不加以区分B. 不同类型的变量可以出现在同一个表达式中C. 在赋值表达式中赋值号两边的类型可以不同D. 某些运算符在不同的场合可以有不同的含义6.下列转义字符中错误的是( )A. ′\000′B. ′\14′C. ′\x111′D. ′\2′7.下列标识符中,不是C语言保留字的是( )A. charB. whileC. minD. default8.下列保留字中用于构成循环结构的是()A.ifB.whileC.switchD.default9. 数据-324在二进制文件和文本文件中所占的字节数分别是()A.2,2B.2,4C.4,2D.4,410. 请选出可以作为C语言用户标识符的一组标识符()A.void, define, WORD B.A3_B3, _123, abcC.FOR, -abc, Case D.2a, Do, Sizeof11.下列运算符优先级最高的是()A.关系运算符B.赋值运算符C.算术运算符D.逻辑运算符12. sizeof(float)是()A.一种函数调用B.一个不合法的表示形式C.一个整型表达式D.一个浮点表达式13. 下列叙述不正确的是()A.一个C语言程序可由一个或多个函数组成B.一个C语言程序必须包含一个main函数C.C语言程序的基本组成单位是函数D.在C语言程序中,注释说明只能位于一条语句的后面14. 编译C语言程序时,程序中的注释部分将()A.不参加编译,也不会出现在目标程序中B.参加编译,但不会出现在目标程序中C.不参加编译,但会出现在目标程序中D.参加编译,并会出现在目标程序中15. 下列字符串常量不正确的是()A.'abc' B."12'12" C."0" D." "16. 下列4个选项中,均是合法整型常量的是()A.160 -0xffff 011B.-0xcdf 01ª0xeC.-01 986,012 0668D.-0x48a 2e5 0x17. 以下选项中不属于C语言类型的是()A.signed short int B.unsigned long intC.unsigned int D.long short18. 数值029是一个()A.八进制数B.十六进制数C.十进制数D.非法数19. 在C语言中,要求运算数必须是整型的运算符是()A./ B.++ C.!=D.%20. 当c的值不为0时,以下能将c的值赋给变量a,b 的是()A.c=b=a B.(a=c) || (b=c)C.(a=c) && (b=c) D.a=c=b二、填空1. 表示空类型的保留字是_____________。
谭浩强版C语言的第十章《指针》答案第十章《指针》答案如下inc/testPtr.h#include <string.h>#include <ctype.h>#include <math.h>#include <assert.h>#define SIZE 1024int a2i(char *start, char *end){int size = 0, ret = 0;long base = 0;size = end - start + 1;base = (long)pow(10, size - 1);while(size-- > 0){ret += (*start++ - '0') * base;base /= 10;}return ret;}int extraNum(char *str, int arr[]){int ite = 0, counter = 0;char *start = NULL, *end = NULL;while(*str != '\0'){if(isdigit(*str)){start = end = str;while( isdigit(*end) && *end != '\0'){++end;}arr[ite++] = a2i(start, end-1);str = end;}else{str++;}}return ite;}int sortStr(char *arr[], int size){int ite1 = 0, ite2 = 0, minPos = 0;char *tmp;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ++ite2 ){if( strcmp(arr[ite2], arr[minPos]) < 0 ){minPos = ite2;}}if(minPos != ite1){tmp = arr[minPos];arr[minPos] = arr[ite1];arr[ite1] = tmp;}}return 0;}int sort(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( arr[ite2] < arr[minPos] ){minPos = ite2;}}if(minPos != ite1){tmp = arr[ite1];arr[ite1] = arr[minPos];arr[minPos] = tmp;}}return 0;}int sortPtr(int arr[], int size){int minPos = 0, ite1 = 0, ite2 = 0, tmp = 0;for(ite1 = 0; ite1 < size - 1; ite1++){minPos = ite1;for(ite2 = ite1 + 1; ite2 < size; ite2++){if( *(arr + ite2) < *(arr + minPos) ){minPos = ite2;}}if(minPos != ite1){tmp = *(arr + ite1);*(arr + ite1) = *(arr + minPos);*(arr + minPos) = tmp;}}return 0;}int getMultiArr(int arr[][5], int n){int i = 0, j = 0, min = 0, tmp = 0, size = 5 * n;int *p = NULL, pos[size];/* copy */p = (int *)malloc(size * sizeof(int));assert(p != NULL);memcpy(p, arr, size * sizeof(int));/* sort */for(i = 0; i < size - 1; i++){min = i;for(j = i + 1; j < size; j++){if( *(p + j) < *(p + min)){min = j;}}if(min != i){tmp = *(p + min);*(p + min) = *(p + i);*(p + i) = tmp;}}/* move */for(i = 0; i < n; i++){for(j = 0; j < 5; j++){if( *p == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][0];arr[0][0] = tmp;continue;}if( *(p + 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[0][4];arr[0][4] = tmp;continue;}if( *(p + 2) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][0];arr[n - 1][0] = tmp;continue;}if( *(p + 3) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n - 1][4];arr[n - 1][4] = tmp;continue;}if( *(p + size - 1) == arr[i][j] ){tmp = arr[i][j];arr[i][j] = arr[n/2][2];arr[n/2][2] = tmp;continue;}}}free(p);p = NULL;return 0;}int statStr(char *str){int upper = 0, lower = 0, space = 0, num = 0, other = 0;while(*str != '\0'){if(isdigit(*str)){num++;}else if (isupper(*str)){upper++;}else if (islower(*str)){lower++;}else if (isspace(*str)){space++;}else{other++;}str++;}assert(3 == upper);assert(5 == lower);assert(10 == num);assert(2 == space);assert(6 == other);return 0;}int average(int(*stu)[6], int classNum, int stuNum){int i = 0, ave = 0;for(i = 0; i < stuNum; i++){ave += (*(stu + i))[classNum];}ave /= stuNum;return ave;}int searchStu(int(*stu)[6], int stuNum){int counter = 0, i = 0, j = 1, stuCounter = 0;for (i = 0; i < stuNum; i++){counter =0;for(j = 1; j < 6; j++){if( (*(stu + i))[j] < 60 ){counter++;}}if(counter >= 2){stuCounter++;}}return stuCounter;}int moveInt(int arr[], int n, int m){int i = 0, *p = NULL;p = (int*)malloc(n * sizeof(int));assert(p != NULL);memcpy(p + m, arr, (n - m) * sizeof(int));memcpy(p, arr + n -m , m * sizeof(int));memcpy(arr, p, n * sizeof(int));free(p);p = NULL;return 0;}int myStrcmp(char *p1, char*p2){int ret = 0;while((*p1 != '\0') && (*p2 != '\0') && ( *p1 == *p2 ) ) {p1++;p2++;}if(*p1 == '\0'){ret = -1;}else if (*p2 == '\0'){ret = 1;}else{ret = (*p1 - *p2) > 0 ? 1 : -1;}return ret;}int revArr(int a[], int size){int tmp = 0, *start = NULL, *end = NULL;start = a;end = start + size - 1;size = size / 2;while(size >= 0){tmp = *(start + size);*(start + size) = *(end - size);*(end - size) = tmp;size--;}return 0;}char *getMonth(char *month[], int which) {assert(which <= 12);return ( *(month + which - 1));}int getStr(char *dest, char* src, int m) {int len = 0;len = strlen(src) + 1 - m;src = src + m - 1;memcpy(dest, src, len * sizeof(char));return 0;}int removePer3(int arr[], int size){int i = 0;for(i = 0; i < size; i++){if(((arr[i]) % 3) == 0){arr[i] = 0;}}return 0;}int getMinMax(int a[], int size){int i = 0, min = 0, max = 0, tmp = 0;min = max = 0;for(i = 0; i < size; i++){if (a[i] <= a[min]){min = i;}if(a[i] >= a[max]){max = i;}}tmp = a[0];a[0] = a[min];a[min] = tmp;tmp = a[size - 1];a[size - 1] = a[max];a[max] = tmp;}int test_10_1(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sortPtr(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_1 Passed!");return 0;}int test_10_2(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable"};iRet = sortStr(arr, 3);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_2 Passed!");return 0;}int test_10_3(){int ret = 0, a[10] = {7,2,3,9,1,0,7,6,5,0};ret = getMinMax(a, 10);assert(a[0] == 0);assert(a[9] == 9);printf("\r\nTest_10_3 Passed!");return 0;}int test_10_4(){int ret = 0, arr[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};ret = moveInt(arr, 9, 3);assert(6 == arr[0]);assert(7 == arr[1]);assert(8 == arr[2]);assert(0 == arr[3]);assert(1 == arr[4]);assert(2 == arr[5]);assert(3 == arr[6]);assert(4 == arr[7]);assert(5 == arr[8]);printf("\r\nTest_10_4 Passed!");return 0;}int test_10_5(){int ret = 0, i = 0, a[12] = {1,2,3,4,5,6,7,8,9,10,11,12};ret = removePer3(a, 12);assert(a[2] == 0);assert(a[5] == 0);assert(a[8] == 0);assert(a[11] == 0);printf("\r\nTest_10_5 Passed!");return 0;}int test_10_7(){int ret = 0;char s2[100] = {0}, *s1 = "hello world!";getStr(s2, s1, 7);assert( strcmp(s2, "world!") == 0);printf("\r\nTest_10_7 Passed!");return 0;}int test_10_8(){int ret = 0;char *str = "a123xABC ??#$%^ 302tab5876";ret = statStr(str);if(ret == 0){printf("\r\nTest_10_8 Passed!");}return 0;}int test_10_10(){int i = 0, j = 0, ret = 0;int arr[5][5] = { \{16, 17, 18, 19, 20}, \{11, 12, 13, 14, 15}, \{1, 2, 3, 4, 5}, \{21, 22, 23, 24, 25}, \{6, 7, 8, 9, 10}, \};ret = getMultiArr(arr, 5);assert(1 == arr[0][0] );assert(2 == arr[0][4] );assert(3 == arr[4][0] );assert(4 == arr[4][4] );assert(25 == arr[2][2] );printf("\r\nTest_10_10 Passed!");return 0;}int test_10_11(){int ite = 0, iRet = 0;char *arr[10] = { "In the", \"An aud", \"The ef", \"Proces", \"Sets o", \"Unifie", \"Out-of", \"Practi", \"Ration", \"Explor" \};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An aud") == 0 );assert( strcmp(arr[1], "Explor") == 0);assert( strcmp(arr[2], "In the") == 0);assert( strcmp(arr[3], "Out-of") == 0);assert( strcmp(arr[4], "Practi") == 0);assert( strcmp(arr[5], "Proces") == 0);assert( strcmp(arr[6], "Ration") == 0);assert( strcmp(arr[7], "Sets o") == 0);assert( strcmp(arr[8], "The ef") == 0);assert( strcmp(arr[9], "Unifie") == 0);printf("\r\nTest_10_11 Passed!");return 0;}int test_10_12(){int ite = 0, iRet = 0;char *arr[10] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features", \"Unified Change Management (UCM),", \"Out-of-the-box process that supports best", \"Practices for change management as described in the IBM", \"Rational Unified Process. Project managers can configure", \"Explorer. For more information about Rational ClearCase"};iRet = sortStr(arr, 10);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "Explorer. For more information about Rational ClearCase") == 0);assert( strcmp(arr[2], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[3], "Out-of-the-box process that supports best") == 0);assert( strcmp(arr[4], "Practices for change management as described in the IBM") == 0);assert( strcmp(arr[5], "Process by using one of the following") == 0);assert( strcmp(arr[6], "Rational Unified Process. Project managers can configure") == 0);assert( strcmp(arr[7], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[8], "The efforts of your team can be coordinated into a definable") == 0);assert( strcmp(arr[9], "Unified Change Management (UCM),") == 0);printf("\r\nTest_10_12 Passed!");return 0;}int test_10_14(){int ret = 0, a[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};ret = revArr(a, 11);assert(10 == a[0]);assert(9 == a[1]);assert(8 == a[2]);assert(7 == a[3]);assert(6 == a[4]);assert(5 == a[5]);assert(4 == a[6]);assert(3 == a[7]);assert(2 == a[8]);assert(1 == a[9]);assert(0 == a[10]);printf("\r\nTest_10_14 Passed!");return 0;}int test_10_15(){int ave = 0, ret = 0;int student[4][6] = { \{1, 100, 90, 80, 70, 97}, \{2, 34, 45, 56, 78, 97}, \{3, 76, 34, 68, 84, 12}, \{4, 90, 90, 90, 75, 28} \};ave = average(student, 1, 4);ret = searchStu(student, 4);assert(75 == ave);assert(2 == ret);printf("\r\nTest_10_15 Passed!");return 0;}int test_10_16(){int iRet = 0, ite = 0, arr[SIZE] = {0};char *str = "a123x456 17960? 302tab5876";iRet = extraNum(str, arr);assert(123 == arr[0]);assert(456 == arr[1]);assert(17960 == arr[2]);assert(302 == arr[3]);assert(5876 == arr[4]);printf("\r\nTest_10_16 Passed!");return 0;}int test_10_17(){int ret = 0;char *s1 = "abcd", *s2 = "abCd";ret = myStrcmp(s1, s2);assert(ret == 1);char *s3 = "aBcd", *s4 = "abCd";ret = myStrcmp(s3, s4);assert(ret == -1);char *s5 = "abcde", *s6 = "abcd";ret = myStrcmp(s5, s6);assert(ret == 1);char *s7 = "abcd", *s8 = "abcde";ret = myStrcmp(s7, s8);assert(ret == -1);char *s9 = "abcd", *s10 = "abCde";ret = myStrcmp(s9, s10);assert(ret == 1);printf("\r\nTest_10_17 Passed!");return 0;}int test_10_18(){int which = 0;char *month[12] = { \"January", \"February", \"March", \"April", \"May", \"June", \"July", \"August", \"September", \"October", \"November", \"December" \};which = 11;assert( strcmp("November", getMonth(month, which)) == 0 );which = 7;assert( strcmp("July", getMonth(month, which)) == 0 );printf("\r\nTest_10_18 Passed!");return 0;}int test_10_20(){int ite = 0, iRet = 0;char *arr[5] = { "In the IBM Rational ClearCase environment", \"An auditable history of source files and software builds is maintained in your organization", \"The efforts of your team can be coordinated into a definable", \"Process by using one of the following", \"Sets of Rational ClearCase features" \};iRet = sortStr(arr, 5);assert( strcmp(arr[0], "An auditable history of source files and software builds is maintained in your organization") == 0 );assert( strcmp(arr[1], "In the IBM Rational ClearCase environment") == 0);assert( strcmp(arr[2], "Process by using one of the following") == 0);assert( strcmp(arr[3], "Sets of Rational ClearCase features") == 0);assert( strcmp(arr[4], "The efforts of your team can be coordinated into a definable") == 0);printf("\r\nTest_10_20 Passed!");return 0;}int test_10_21(){int ite = 0, iRet = 0, arr[5] = {121, 234, 456456, 543, 23};iRet = sort(arr, 5);assert (23 == arr[0]);assert (121 == arr[1]);assert (234 == arr[2]);assert (543 == arr[3]);assert (456456 == arr[4]);printf("\r\nTest_10_21 Passed!");return 0;}int testPtr(){int iRet = 0;#if 0#endifiRet += test_10_1();iRet += test_10_2();iRet += test_10_3();iRet += test_10_4();iRet += test_10_5();iRet += test_10_7();iRet += test_10_8();iRet += test_10_10();iRet += test_10_11();iRet += test_10_12();iRet += test_10_14();iRet += test_10_15();iRet += test_10_16();iRet += test_10_17();iRet += test_10_18();iRet += test_10_20();iRet += test_10_21();return iRet;}src/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "../inc/testFile.h" #include "../inc/testBits.h" #include "../inc/testPtr.h"int main(){int iRet = 0;#if 0iRet += testFile();assert(iRet == 0);iRet += testBits();assert(iRet == 0);#endifiRet += testPtr();assert(iRet == 0);return 0;}。
第10章对文件的输入输出(2012年9月真题)(40)有以下程序#include <stdio.h>main(){ FILE *fp;int i,a[6]={1,2,3,4,5,6};fp=fopen("d2.dat","w+");for(i=0;i<6;i++) fprintf(fp,"%d\n",a[i]);rewind(fp);for(i=0;i<6;i++) fscanf(fp,"%d",&a[5-i]);fclose(fp);for(i=0;i<6;i++) printf("%d,",a[i]);}程序运行后的输出结果是A)4,5,6,1,2,3, B)1,2,3,3,2,1,C)1,2,3,4,5,6, D)6,5,4,3,2,1,答案:D(2012年3月真题)40、以下函数不能用于向文件写入数据的是A ftellB fwriteC fputcD fprintf答案:A(2011年9月真题)40.有以下程序#include<stdio.h>main(){FILE *fp; int k,n,i,a[6]={1,2,3,4,5,6};fp=fopen("d2.dat","w");for(i=0;i<6;i++)fprintf(fp,"%d\n",a[i]);fclose(fp); fp=fopen("d2.dat","r");for(i=0;i<3;i++)fscanf(fp,"%d%d",&k,&n);fclose(fp); printf("%d,%d\n",k,n); }程序运行后的输出结果是A.1,2B.3,4C.5,6D.123,456答案:C(2011年3月真题)(40)设fp已定义,执行语句fp=fopen("file","w");后,以下针对文本文件file操作叙述的选项中正确的是A)写操作结束后可以从头开始读 B)只能写不能读C)可以在原有内容后追加写 D)可以随意读和写(2010年3月真题)(40)有以下程序#include#include<stdio.h>main(){ FILE *fp;char str[10];fp=fopen("myfile.dat","w");fputs("abc",fp);fclose(fp);fopen("myfile.data","a+");fprintf(fp,"%d",28);rewind(fp);fscanf(fp,"%s",str); puts(str);fclose(fp); }程序运行后的输出结果是A)abc B) 28c C) abc28 D)因类型不一致而出错答案:B(2009年9月真题)(40)下列关于C语言文件的叙述中正确的是A)文件由一系列数据依次排列组成,只能构成二进制文件B)文件由结构序列组成,可以构成二进制文件或文本文件C)文件由数据序列组成,可以构成二进制文件或文本文件D)文件由字符序列组成,其类型只能是文本文件答案:C(2009年3月真题)40.有以下程序#include <stdio.h>main(){ FILE *f;f=fopen("filea.txt","w"); fprintf(f,"abc"); fclose(f); }若文本文件filea.txt中原有内容为:hello,则运行以上程序后,文件filea.txt的内容为A)helloabc B)abclo C)abc D)abchello答案:C(2008年9月真题)(40)有以下程序#include <stdio.h>main(){ FILE *pf;char *s1="China", *s2="Beijing";pf=fopen("abc.dat","wb+");fwrite(s2,7,1,pf);rewind(pf); /*文件位置指针回到文件开头*/fwrite(s1,5,1,pf);fclose(pf);}以上程序执行后abc.dat文件的内容是A) China B) Chinang C) ChinaBeijing D) Bei jingChina答案:B(2008年4月真题)30、下列叙述中错误的是( )。
第10章字符串三、编程题10.19请编写函数mygets和myputs,其功能与gets和puts相同,函数中用getchar 和putchar读入和输出字符。
#include <stdio.h>voidmygets(char * str){ char c;int i=0;while((c=getchar())!='\n') str[i++]=c;str[i]='\0';}voidmyputs(char * str){ int i=0;while(str[i]!='\0') putchar(str[i++]);putchar('\n');}void main(){ char c[80];mygets(c);myputs(c);}10.20编写函数,判断一个字符串是否是回文。
若是回文函数返回值为1,否则返回值为0。
回文是顺读和倒读都一样的字符串。
#include <stdio.h>#include <string.h>int judge(char * str){ char a[80];inti,len=strlen(str),j=0;for(i=len-1;i>=0;i--)a[j++]=str[i];a[j]='\0';//最后加上字符串结束标志if(strcmp(a,str)==0) return 1;else return 0;}void main(){char c[80];int n;gets(c);n=judge(c);if(n==1) printf("是回文\n");else printf("不是回文\n");}10.21请编写函数,删除字符串中指定位置(下标)上的字符。
删除成功函数返回被删字符,否则返回空值。
#include <stdio.h>#include <string.h>charfundel(char * str, int n){ char c='\0';inti,len=strlen(str);if(n>0 && n<len){ c=str[n];for(i=n;i<len;i++)str[i]=str[i+1];}return c;}void main(){char c[80],ch;int n;gets(c);//输入字符串scanf("%d",&n);//输入要删除字符的下标ch=fundel(c,n);if(ch=='\0') printf("删除失败\n");else printf("删除成功,删除后:%s\n",c); }。
明解C语⾔⼊门篇第⼗章答案练习10-1#include <stdio.h>void adjust_point(int*n) {if (*n > 100)*n = 100;if (*n < 0)*n = 0;}int main() {int x;printf("请输⼊⼀个数:");scanf("%d", &x);adjust_point(&x);printf("修改后的值是%d",x);}练习10-2void decrement_date(int* y, int* m, int* d) {*d -= 1;if (*d == 0) {*m -= 1;if (*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 ||* m == 0) {*d = 31;}if (*m == 2) {*d = 28;if (*y % 4 == 0) {*d = 29;}}else*d = 30;if (*m == 0) {*m = 12;*y -= 1;}}}void increment_date(int* y, int* m, int* d) {*d += 1;if ((*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 || *m == 12) && (*d == 32)) {*d = 1;*m += 1;}if ((*m == 4 || *m == 6 || *m == 9 || *m == 11 ) && (*d == 31)) {*d = 1;*m += 1;}if (*m == 2) {if (*d == 29&& ((*y & 4) != 0)) {*d = 1;}if (*d == 30 && ((*y & 4) == 0)) {*d = 1;*m += 1;}}if (*m == 13) {*y += 1;*m = 1;}}练习10-3#include<stdio.h>void swap(int* px, int*py) {int temp = *px;*px = *py;*py = temp;}void sort3(int*n1, int*n2, int*n3) {if (*n1 > *n2) {swap(n1, n2);}if (*n1 > * n3) {swap(n1, n3);}if (*n2 > * n3) {swap(n2, n3);}}int main(void) {int n1, n2, n3;printf("n1=");scanf("%d", &n1);printf("n2=");scanf("%d", &n2);printf("n3=");scanf("%d", &n3);putchar('\n');sort3(&n1, &n2, &n3);printf("%d,%d,%d", n1, n2, n3); }练习10-4#include <stdio.h>#define number 5void set_idx(int*v, int n) {int i = 0;for (i = 0; i < n; i++) {*(v + i) = i;}}int main() {int i;set_idx(x, number);for (i = 0; i < number; i++) {printf("x[%d]=%d", i,x[i] );putchar('\n');}}练习10-5会报错数组中元素会溢出,因为直接从v【2】开始赋值。
一、C 语言概述练习题选择1.一个C 程序的执行是从。
A)本程序的 main 函数开始,到 main 函数结束B)本程序文件的第一个函数开始,到本程序文件的最后一个函数结束C)本程序文件的第一个函数开始,到本程序 main 函数结束D)本程序的 main 函数开始,到本程序文件的最后一个函数结束2.以下叙述不正确的是。
A) 一个C 源程序必须包含一个main 函数B)一个C 源程序可由一个或多个函数组成C) C 程序的基本组成单位是函数D) 在C 程序中,注释说明只能位于一条语句的后面3.以下叙述正确的是。
A)在对一个 C 程序进行编译的过程中,可发现注释中的拼写错误B)在C 程序中,main 函数必须位于程序的最前面C)C 语言本身没有输入输出语句D) C 程序的每行中只能写一条语句4.一个C 语言程序是由。
A)一个主程序和若干个子程序组成B) 函数组成C) 若干过程组成D) 若干子程序组成5.计算机高级语言程序的运行方法有编译执行和解释执行两种,以下叙述中正确的是。
A) C 语言程序仅可以编译执行B) C 语言程序仅可以解释执行C) C 语言程序既可以编译执行又可以解释执行D) 以上说法都不对6.以下叙述中错误的是。
A) C 语言的可执行程序是由一系列机器指令构成的B)用C 语言编写的源程序不能直接在计算机上运行C)通过编译得到的二进制目标程序需要连接才可以运行D)在没有安装 C 语言集成开发环境的机器上不能运行 C 源程序生成的.exe文件7.以下叙述正确的是。
A) C 语言程序是由过程和函数组成的B) C 语言函数可以嵌套调用,例如:fun(fun(x))C) C 语言函数不可以单独编译 D) C 语言中除了 main 函数,其他函数不可作为单独文件形式存在二、数据类型、运算符与表达式选择.1.若x、i、j、k 都是int 型变量,则计算下面表达式后,x 的值为x=(i=4,j=16,k=32) A) 4 B) 16 C) 32 D) 522.下列四组选项中,均不是C 语言关键字的选项是。
C语言程序设计复习题(含参考答案)C语言程序设计复习题(含参考答案)一、单项选择题(本题共30小题,每小题2分,共60分)在每小题列出的四个备选项中只有一个是符合题目要求的。
1. 以下正确的C语言自定义标识符是______。
A. _1aB. 2a_C. doD. a.122、假设所有变量均为整型,则表达式(x=3,y=4,y++, y-x`)的值是______。
A.7B.1C.6D.23. 设int a, x=2; 执行语句a=x>0?3*x:x=10; 后,变量x的值是_______。
A. 1B. 2C. 6D. 104.设有以下程序段:int x=2,y=2,z=0,a;a=++x||++y&&z++;printf("%d,%d,%d\n",x,y,z);执行后输出的结果是_________。
A. 2, 2, 0B. 3, 3,1C. 3, 2, 0D. 3, 2, 15. 在C语言中,strlen("\\TOP\t\65\"")的值是_______。
A. 5 B.7C. 8D. 126. 设float x,由键盘输入:12.45, 能正确读入数据的输入语句是_________。
A. scanf("%5f",&x);B. scanf("%5d",&x);C. scanf("%f",x);D. scanf("%s",&x);7.若有int a=5;则逗号表达式a=2*6,a*3,a+5的值是_________。
A. 12B. 17C.36 D. 108. C语言程序中,整型常量的书写形式不包括_________。
A. 二进制B. 八进制C. 十进制D. 十六进制9.下面程序的输出结果是_____。
#includemain(){ float d=2.2; int x,y;x=6.2;y=(x+3.8)/5.0;printf("%f",d*y);}A. 4B. 4.4C. 2.2D. 010. 设int x;,则与计算︱x︱等价的表达式是_________。