C/C++公共基础一卷考核题及答案
一、选择题
题目答案写在记事本中,以.txt的形式提交。
1.全局变量储存在以下哪个内存区内?( )
**A.静态存储区 ** B.栈区 C.堆区 D.代码区
2.指针类型的全局变量如果没有进行初始化,那么程序会讲该变量自动初始化为?()
A.0 B.随机数 C.'0' D.NULL
3.在C/C++中,0b开头的数表示的是指值的16进制()
A.对 B.错
4.在C/C++中,0x开头的数表示的是指值的2进制()
A.对 B.错
5.如果定义了一个数组arr[4],那么arr代表的意思是()
A.单纯只是数组的名字 B.数组所有元素的值
C.数组0号元素的值 D.数组0号元素的地址(指针)
6.结构体struct变量所占的内存大小为( )
A.4字节 B.所占内存最大的成员变量的大小
C.根据不同的字长,拥有不同的大小 D.所有成员变量所占内存大小之和
7.联合体union变量所占的内存大小为( )
A.4字节 B.所占内存最大的成员变量的大小
C.根据不同的字长,拥有不同的大小 D.所有成员变量所占内存大小之和
8.一般,头文件的扩展名是( )
A.c B.cpp **C.h ** D.sln
9.一般,C/C++的头文件的作用是( )
A.声明函数与变量 B.进行业务代码的编写
C.进行预处理代码的编写 D.多此一举,没有啥实质作用
10.以下说法正确的是(多选)( )
**A. ****C++****兼容C语言,且可以在C++工程中使用C语言 **
B.C语言不兼容C++,不可以在C语言工程中使用C++
C.C语言兼容C++,可以在C语言工程中使用C++
D.C语言不兼容C++,但可以在C语言工程中使用C++
11.分析下面代码中g_spi1_handler变量的类型( )
SPI_HandleTypeDef g_spi1_handler; /* SPI1句柄 */
/**
* @brief SPI通信初始化代码
* @note 主机模式,8位数据,禁止硬件片选
* @param 无
* @retval 无
*/
void spi1_init(void)
{
SPI1_SPI_CLK_ENABLE(); /* SPI1时钟使能 */
g_spi1_handler.Instance = SPI1_SPI; /* SPI1 */
g_spi1_handler.Init.Mode = SPI_MODE_MASTER; /* 设置SPI工作模式,设置为主模式 */
g_spi1_handler.Init.Direction = SPI_DIRECTION_2LINES; /* 设置SPI单向或者双向的数据模式:SPI设置为双线模式 */
g_spi1_handler.Init.DataSize = SPI_DATASIZE_8BIT; /* 设置SPI的数据大小:SPI发送接收8位帧结构 */
g_spi1_handler.Init.CLKPolarity = SPI_POLARITY_HIGH; /* 串行同步时钟的空闲状态为高电平 */
g_spi1_handler.Init.CLKPhase = SPI_PHASE_2EDGE; /* 串行同步时钟的第二个跳变沿(上升或下降)数据被采样 */
g_spi1_handler.Init.NSS = SPI_NSS_SOFT; /* NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 */
g_spi1_handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; /* 定义波特率预分频的值:波特率预分频值为256 */
g_spi1_handler.Init.FirstBit = SPI_FIRSTBIT_MSB; /* 指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 */
g_spi1_handler.Init.TIMode = SPI_TIMODE_DISABLE; /* 关闭TI模式 */
g_spi1_handler.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; /* 关闭硬件CRC校验 */
g_spi1_handler.Init.CRCPolynomial = 7; /* CRC值计算的多项式 */
HAL_SPI_Init(&g_spi1_handler); /* 初始化 */
__HAL_SPI_ENABLE(&g_spi1_handler); /* 使能SPI1 */
spi1_read_write_byte(0XFF); /* 启动传输, 实际上就是产生8个时钟脉冲, 达到清空DR的作用, 非必需 */
}A.常量 B.结构体 C.指针变量 D.浮点型
二、程序题
提交源文件.cpp,源文件以题号命名。
第15题提交压缩包,压缩包内要有main.cpp和库的文件,压缩包以题号命名。
11.编写一个函数,使用 while 循环计算从 1 到给定正整数 n 的所有整数的和。在主函数中,接受用户输入的一个正数 n,然后调用函数计算和,并输出结果。

#include <iostream>
int calculateSum(int n);
int main() {
int n;
std::cout << "请输入n: ";
std::cin >> n;
int sum = calculateSum(n);
std::cout << "从1一直加到" << n << "等于: " << sum << std::endl;
return 0;
}
int calculateSum(int n) {
int sum = 0;
int i = 1;
while (i <= n) {
sum += i;
i++;
}
return sum;
}12.请使用联合体(共同体)来完成输入一个字母,并打印他的ascii码。 (特殊要求:该联合体变量必须定义为局部变量)

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
union Convert
{
int asc;
char letter;
};
typedef Convert con;
int main()
{
con con__; //局部变量
con__.asc = 0; //有的编译器需要初始化后三个字节。
cin >> con__.letter;
cout << con__.asc << endl;
return 0;
}13.创建一个名为“Student”的结构体,其中包含三个成员变量:name(字符串类型)、age(整型)和score(浮点型)。定义一个函数,该函数返回值为void,函数的作用是给Student类型的结构体中的所有成员变量进行赋值。请创建一个Student结构体,并使用函数给该结构体进行赋值,最后打印所有成员变量。(特殊要求:使用typedef对结构体类型进行重命名)

#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
typedef struct
{
std::string name;
int age;
float score;
}Student;
void struct_init(Student* ps);
int main()
{
Student stu;
struct_init(&stu);
cout << stu.name << "的年龄是" << stu.age << ",分数是:" << stu.score << endl;
}
void struct_init(Student * ps)
{
ps->name = "李华";
ps->age = 18;
ps->score = 99.5;
}14.冒泡排序:编写一个函数,作用是使用冒泡排序算法对给定的整数数组进行升序排序。在主函数中,创建一个含有5个元素的整数数组并初始化,然后调用该函数对数组进行排序,并在主函数中输出排序后的结果。

#include <iostream>
void bubbleSort(int* arr, int size);
int main() {
int arr[] = { 5, 2, 8, 3, 1 };
int size = sizeof(arr) / sizeof(arr[0]);
std::cout << "原始数组: ";
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
bubbleSort(arr, size);
std::cout << "\n排列后的数组: ";
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
return 0;
}
void bubbleSort(int* arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}15.编写自己的库并调用
主函数写在main.cpp文件中,然后新建1个C++的库,库名叫my_lib。
my_lib库用来定义一个命名空间叫four_oper,该命名空间里有一个my_multi函数,该函数返回值为int,入口参数是两个int类型的变量,作用是把两个入口参数进行相乘并return。该命名空间中还有两个整形变量a和b(必须要定义,初始化无所谓)。
请在主函数里给four_oper命名空间里的a和b赋值(随便赋值),并调用my_multi函数将a和b两值相乘的结果算出来,然后打印出来。

main.cpp
#include <iostream>
#include "my_lib.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
cout << "请输入a的值:";
cin >> four_oper::a;
cout << "请输入b的值:";
cin >> four_oper::b;
auto c = four_oper::my_multi(four_oper::a, four_oper::b);
cout << "答案是:" << c << endl;
return 0;
}my_lib.cpp
#include "my_lib.h"
int four_oper::a = 5;
int four_oper::b = 5;
int four_oper::my_multi(int a, int b)
{
return (a * b);
}my_lib.h
#ifndef __MY_LIB_H_
#define __MY_LIB_H_
namespace four_oper
{
extern int a,b;
int my_multi(int a, int b);
}
#endif16.编写一个名为 calculate 的函数,该函数接受两个单精度浮点数为参数,并返回它们相乘的结果。然后,重载该函数,使其能够接受三个整形参数,其中第三个参数为默认值为0的整数。当使用两个参数调用该函数时,函数应该返回这两个整数的和;当使用三个参数调用该函数时,函数应该返回前两个整数的和加上第三个整数。在主函数中,请定义5个变量,(整形x,y,z,浮点型p和q,随便赋值一个数),分别使用 calculate 函数计算并打印出①p和q相乘后的值,②x和y相加后的值,③x,y和z相加后的值。

#include <iostream>
int calculate(float a, float b);
int calculate(int a, int b, int c = 0);
int main() {
int x = 5, y = 10, z = 20;
float p = 4.0,q = 5.0;
std::cout << "Multi of p and q: " << calculate(p, q) << std::endl;
std::cout << "Sum of x, y: " << calculate(x, y) << std::endl;
std::cout << "Sum of x, y, and z: " << calculate(x, y, z) << std::endl;
return 0;
}
int calculate(float a, float b) {
return (a * b);
}
int calculate(int a, int b, int c) {
return (a + b + c);
}

