内容正文:
7.1与7.2节:模块化程序设计和C语言程序的组成、库函数
一、单项选择题
1.以下关于模块化程序设计的说法,正确的是( )
A. 模块化程序设计只能用于大型程序开发
B. 模块化设计会增加程序的调试难度
C. 模块化设计将程序分解为独立功能模块,便于复用
D. 模块不能被多个程序调用
2.C 语言程序的基本组成单位是()
A. 语句 B. 函数 C. 变量 D. 表达式
3.一个 C 语言程序的执行入口是()
A. 任意函数 B. main () 函数 C. printf () 函数 D. 自定义函数
4.以下关于 C 语言函数的说法,错误的是()
A. 函数之间相互独立 B. 函数可以嵌套定义
C. 函数可以被多次调用 D. main () 函数不可被其他函数调用
5.调用数学库函数时,需要包含的头文件是()
A. stdio.h B. ctype.h C. math.h D. string.h
6.调用输入 / 输出库函数(如 printf)时,需要包含的头文件是()
A. stdio.h B. math.h C. ctype.h D. string.h
7.以下关于#include命令的说法,正确的是()
A. #include是 C 语言语句,需以分号结尾
B. #include <头文件>和#include "头文件"功能完全相同
C. #include命令必须以#开头
D. 调用库函数时可以不写#include命令
8.模块化程序设计的核心思想是()
A. 自底向上、逐步求精 B. 自顶向下、逐步细化和模块化
C. 一次性编写完整程序 D. 不使用任何子模块
9.一个 C 语言程序中,主函数 main 的数量是()
A. 0 个 B. 1 个 C. 2 个 D. 任意多个
10.调用字符串处理库函数时,需要包含的头文件是()
A. stdio.h B. math.h C. ctype.h D. string.h
二、填空题。
1.C语言程序的基本模块是___________。
2.#include <stdio.h>中,< >表示从______目录查找头文件。
3.如果一个程序需要用到函数sqrt(求平方根)和pow(求幂),则应该在程序开头包含头文件___________。
4.一个C语言程序有且仅有一个________函数。
5.在C语言中,函数之间是________的关系,即在一个函数内部不能再定义另一个函数。
6.使用#include命令包含头文件时,如果使用双引号(如#include "myhead.h"),则编译器会首先在________目录中查找该头文件。
7.在C语言中,程序的执行总是从________函数开始,调用其他函数后最终回到该函数结束。
8. 调用函数strlen, strcpy时,需要包含的头文件是___________。
9.在C语言中,函数体必须用一对________括起来。
10.在C语言中,________函数不能被任何其他函数调用。
三、阅读程序题。
1.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'b';
int res = isupper(ch);
printf("%d", res != 0);
}
2.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'm';
char res = toupper(ch);
printf("%c", res);
}
3.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'K';
char res = tolower(ch);
printf("%c", res);
}
4.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = ' ';
int res = isspace(ch);
printf("%d", res != 0);
}
5.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double a = 16.0;
double res = sqrt(a);
printf("%.0f", res);
}
6.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
int b = -9;
int res = abs(b);
printf("%d", res);
}
7.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double x = 2.0, y = 3.0;
double res = pow(x, y);
printf("%.0f", res);
}
8.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double n = 3.2;
double res = ceil(n);
printf("%.0f", res);
}
9.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double a = -7.5;
double res = fabs(a);
printf("%.1f", res);
}
10.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = '5';
int res = isdigit(ch);
printf("%d", res != 0);
}
四、程序填空题(根据题意,将空缺的语句补上)
1.补全代码,对 9.2 向下取整。
#include <stdio.h>
#include <math.h>
main() {
double m = 9.2;
double res = _____________;
printf("%.0f", res);}
2.补全代码,计算以 10 为底 1000 的对数。
#include <stdio.h>
#include <math.h>
int main() {
double res = _____________;
printf("%.0f", res);}
3.补全代码,判断字符'8'是否为数字字符
#include <stdio.h>
#include <ctype.h>
main() {
char ch = '8';
int flag = ______(ch);
printf("%d", flag != 0);
}
4.补全代码,判断字符's'是否为字母。
#include <stdio.h>
#include <ctype.h>
main() {
char c = 's';
int f = __________(c);
printf("%d", _______________);}
5.补全代码,判断字符是否为大写字母。
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'G';
int res = ______(ch);
printf("%d", _____________);
}
三、编程题。
1. 编写程序,定义两个字符串 apple 和 apply,使用字符串比较函数进行比较:
若相等输出字符串相同,不相等输出字符串不同。
2.编写程序,输入一个负数小数,使用浮点数绝对值函数求出绝对值,
再使用幂函数计算该绝对值的 2 次方,最终输出结果。
3.编写程序,定义字符串 Hello2026,取出首个字符,判断是否为字母,若是字母转为大写,最后输出转换后的字符与原字符串长度。
4.定义字符串 Code2026Test;
要求①输出字符串总长度;②提取字符串第 1 个字符,判断是否为小写字母;③若是小写则转大写,否则保持原字符输出。
5.定义字符串 java666 与 c999;
要求:①比较两个字符串字典序;②统计各自长度;③将短的字符串拼接到长的字符串后方并输出;④全程使用标准字符串库函数实现。
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$
7.1与7.2节:模块化程序设计和C语言程序的组成、库函数
一、单项选择题
1.以下关于模块化程序设计的说法,正确的是( )
A. 模块化程序设计只能用于大型程序开发
B. 模块化设计会增加程序的调试难度
C. 模块化设计将程序分解为独立功能模块,便于复用
D. 模块不能被多个程序调用
【答案】C
【解析】模块化设计将程序分解为独立功能模块,可被多次调用,便于复用、阅读和调试,适用于各类程序开发,故 C 正确。
2.C 语言程序的基本组成单位是( )
A. 语句 B. 函数 C. 变量 D. 表达式
【答案】B
【解析】C 语言程序由函数组成,函数是程序的基本模块,故 B 正确。
3.一个 C 语言程序的执行入口是( )
A. 任意函数 B. main () 函数 C. printf () 函数 D. 自定义函数
【答案】B
【解析】C 语言程序由函数组成,函数是程序的基本模块,故 B 正确。
4.以下关于 C 语言函数的说法,错误的是( )
A. 函数之间相互独立 B. 函数可以嵌套定义
C. 函数可以被多次调用 D. main () 函数不可被其他函数调用
【答案】B
【解析】C 语言中函数不能嵌套定义,只能嵌套调用,故 B 错误。
5.调用数学库函数时,需要包含的头文件是( )
A. stdio.h B. ctype.h C. math.h D. string.h
【答案】C
【解析】数学库函数的头文件是math.h,故 C 正确。
6.调用输入 / 输出库函数(如 printf)时,需要包含的头文件是( )
A. stdio.h B. math.h C. ctype.h D. string.h
【答案】A
【解析】printf等输入 / 输出函数的头文件是stdio.h,故 A 正确。
7.以下关于#include命令的说法,正确的是( )
A. #include是 C 语言语句,需以分号结尾
B. #include <头文件>和#include "头文件"功能完全相同
C. #include命令必须以#开头
D. 调用库函数时可以不写#include命令
【答案】C
【解析】include是预处理命令,不是语句,无需分号;尖括号和双引号的查找路径不同;调用库函数必须包含对应头文件,故 C 正确。
8.模块化程序设计的核心思想是( )
A. 自底向上、逐步求精 B. 自顶向下、逐步细化和模块化
C. 一次性编写完整程序 D. 不使用任何子模块
【答案】B
【解析】结构化程序设计的核心是自顶向下、逐步细化和模块化,故 B 正确。
9.一个 C 语言程序中,主函数 main 的数量是( )
A. 0 个 B. 1 个 C. 2 个 D. 任意多个
【答案】B
【解析】C 语言程序有且仅有一个main()函数,故 B 正确。
10.调用字符串处理库函数时,需要包含的头文件是( )
A. stdio.h B. math.h C. ctype.h D. string.h
【答案】D
【解析】字符串处理库函数的头文件是string.h,故 D 正确。
二、填空题。
1.C语言程序的基本模块是___________。
【答案】函数
【解析】函数是C语言程序的基本组成单位。
2.#include <stdio.h>中,< >表示从______目录查找头文件。
【答案】系统
【解析】< >从系统库目录查找。
3.如果一个程序需要用到函数sqrt(求平方根)和pow(求幂),则应该在程序开头包含头文件___________。
【答案】#include <math.h>
【解析】 数学计算相关的库函数,如sqrt, pow, sin, cos等,其声明位于math.h头文件中。
4.一个C语言程序有且仅有一个________函数。
【答案】main
【解析】main函数是C语言程序的入口和出口,是程序执行的起点和终点,每个程序必须有且只有一个main函数。
5.在C语言中,函数之间是________的关系,即在一个函数内部不能再定义另一个函数。
【答案】平行 或 相互独立
【解析】 C语言规定,所有函数(包括主函数main)在定义时都是平行、相互独立的,不允许在一个函数内部定义另一个函数,即不允许函数的“嵌套定义”。
6.使用#include命令包含头文件时,如果使用双引号(如#include "myhead.h"),则编译器会首先在________目录中查找该头文件。
【答案】当前 或 用户当前 或 源文件所在
【解析】用双引号包含头文件,编译器首先在源文件所在的当前目录(用户目录)中查找,找不到再去系统标准目录查找。
7.在C语言中,程序的执行总是从________函数开始,调用其他函数后最终回到该函数结束。
【答案】main
【解析】这是C语言程序执行的固定流程。
8. 调用函数strlen, strcpy时,需要包含的头文件是___________。
【答案】<string.h>
【解析】字符串操作相关函数的声明在string.h中。
9.在C语言中,函数体必须用一对________括起来。
【答案】{} 或 花括号
【解析】函数体是函数实现的部分,必须用花括号{}括起来。
10.在C语言中,________函数不能被任何其他函数调用。
【答案】main
【解析】main函数是程序执行的起点,由系统调用,用户自定义的函数不能调用main函数。
三、阅读程序题。
1.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'b';
int res = isupper(ch);
printf("%d", res != 0);
}
【答案】0
2.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'm';
char res = toupper(ch);
printf("%c", res);
}
【答案】M
3.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = 'K';
char res = tolower(ch);
printf("%c", res);
}
【答案】k
4.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = ' ';
int res = isspace(ch);
printf("%d", res != 0);
}
【答案】1
5.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double a = 16.0;
double res = sqrt(a);
printf("%.0f", res);
}
【答案】4
6.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
int b = -9;
int res = abs(b);
printf("%d", res);
}
【答案】9
7.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double x = 2.0, y = 3.0;
double res = pow(x, y);
printf("%.0f", res);
}
【答案】8
8.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double n = 3.2;
double res = ceil(n);
printf("%.0f", res);
}
【答案】4
9.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <math.h>
main() {
double a = -7.5;
double res = fabs(a);
printf("%.1f", res);
}
【答案】7.5
10.写出下列程序段的输出结果:________________。
#include <stdio.h>
#include <ctype.h>
main() {
char ch = '5';
int res = isdigit(ch);
printf("%d", res != 0);
}
【答案】1
四、程序填空题(根据题意,将空缺的语句补上)
1.补全代码,对 9.2 向下取整。
#include <stdio.h>
#include <math.h>
main() {
double m = 9.2;
double res = _____________;
printf("%.0f", res);}
【答案】floor(m)
2.补全代码,计算以 10 为底 1000 的对数。
#include <stdio.h>
#include <math.h>
int main() {
double res = _____________;
printf("%.0f", res);}
【答案】log10(1000.0)
3.补全代码,判断字符'8'是否为数字字符
#include <stdio.h>
#include <ctype.h>
main() {
char ch = '8';
int flag = ______(ch);
printf("%d", flag != 0);
}
【答案】isdigit
4.补全代码,判断字符's'是否为字母。
#include <stdio.h>
#include <ctype.h>
main() {
char c = 's';
int f = __________(c);
printf("%d", _______________);}
【答案】isalpha f != 0
5.补全代码,判断字符是否为大写字母。
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'G';
int res = ______(ch);
printf("%d", _____________);
}
【答案】isupper res != 0
三、编程题。
1. 编写程序,定义两个字符串 apple 和 apply,使用字符串比较函数进行比较:
若相等输出字符串相同,不相等输出字符串不同。
【参考代码】
#include <stdio.h>
#include <string.h>
main()
{ char s1[] = "apple";
char s2[] = "apply";
int res;
res = strcmp(s1, s2);
if(res == 0)
printf("字符串相同
");
else
printf("字符串不同
");
}
2.编写程序,输入一个负数小数,使用浮点数绝对值函数求出绝对值,
再使用幂函数计算该绝对值的 2 次方,最终输出结果。
【参考代码】
#include <stdio.h>
#include <math.h>
main()
{ double x, abs_x, result;
scanf("%lf", &x);
abs_x = fabs(x);
result = pow(abs_x, 2.0);
printf("运算结果:%.2f
", result);
}
3.编写程序,定义字符串 Hello2026,取出首个字符,判断是否为字母,若是字母转为大写,最后输出转换后的字符与原字符串长度。
【参考代码】
#include <stdio.h>
#include <ctype.h>
#include <string.h>
main()
{ char str[] = "Hello2026";
char ch = str[0];
int length;
if(isalpha(ch)) ch = toupper(ch);
length = strlen(str);
printf("转换后字符:%c
", ch);
printf("原字符串长度:%d
", length);
}
4.定义字符串 Code2026Test;
要求①输出字符串总长度;②提取字符串第 1 个字符,判断是否为小写字母;③若是小写则转大写,否则保持原字符输出。
【参考代码】
#include <stdio.h>
#include <ctype.h>
#include <string.h>
main()
{ char str[] = "Code2026Test";
char first;
int len;
len = strlen(str);
first = str[0];
printf("字符串总长:%d
", len);
if(islower(first))
first = toupper(first);
printf("首字符处理后:%c
", first);
}
5.定义字符串 java666 与 c999;
要求:①比较两个字符串字典序;②统计各自长度;③将短的字符串拼接到长的字符串后方并输出;④全程使用标准字符串库函数实现。
【参考代码】
#include <stdio.h>
#include <string.h>
main()
{ char s1[40] = "java666";
char s2[] = "c999";
int len1, len2, cmp;
len1 = strlen(s1);
len2 = strlen(s2);
cmp = strcmp(s1, s2);
printf("s1长度:%d,s2长度:%d
", len1, len2);
if(cmp > 0)
printf("字典序:s1更大
");
else if(cmp < 0)
printf("字典序:s2更大
");
if(len1 > len2)
{ strcat(s1, s2);
printf("拼接结果:%s
", s1); }
else
{ strcat(s2, s1);
printf("拼接结果:%s
", s2); }
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$