小题
对于函数的形参可以在函数内部定义同名变量,且调用时该变量被优先调用。
错
报错:Redefinition of 'x'clang(redefinition)。
一个结构体变量可以包含自身类型的成员。
错
报错:Field has incomplete type 'struct node'clang(field_incomplete_or_sizeless),当然包含自身类型的指针是支持的,参见链表。
以下哪个typedef定义正确地声明了一个包含3个元素的函数指针数组类型,其中每个函数指针指向返回float、参数为int的函数?
A. typedef float (*func_ptr)(int)[3];
B. typedef float (func_array[3])(int);
C. typedef float (*func_array[3])(int);
D. typedef float (*)[3] func_ptr(int);
答案:C
选项是胡诌的
以下选项等价“真”的是
A."\0" B.!3.14 C.0.0 D.3^3
答案:A
A."\0":字符串等价于指针,指针显然非0,为真。
B.!3.14:3.14非零为真,取反后为假。
C.0.0:隐式转换为0,即为假。
D.3^3:相同的值异或必定的到0,为假。
大题
程序填空1
读取文件中的所有数据并冒泡排序,并输出。然后输出数据总个数,原文件中原格式的所有数据。
以下为示意代码
c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1000
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
FILE *file;
int data[MAX_SIZE];
int count = 0;
file = fopen("test.txt", "r");
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
while (fscanf(file, "%d", &data[count]) != EOF && count < MAX_SIZE) {
count++;
}
fclose(file);
printf("数据总个数: %d\n", count);
// 显示读取到的数据
for (int i = 0; i < count; i++) {
printf("%d ", data[i]);
}
bubbleSort(data, count);
// 输出排序后的数据
for (int i = 0; i < count; i++) {
printf("%d ", data[i]);
}
return 0;
}
程序填空2
对于给定链表,删去所有值为给定值的节点,然后翻转链表。
以下为示意代码
c
struct Node* deleteNodes(struct Node* head, int B) {
while (head != NULL && head->data == B) {
struct Node* temp = head;
head = head->next;
free(temp);
}
if (head == NULL) {
return NULL;
}
struct Node* current = head;
struct Node* prev = NULL;
while (current != NULL) {
if (current->data == B) {
if (prev != NULL) {
prev->next = current->next;
}
struct Node* temp = current;
current = current->next;
free(temp);
} else {
prev = current;
current = current->next;
}
}
return head;
}
struct Node* reverseList(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next; // 保存下一个节点
current->next = prev; // 反转指针
prev = current; // 移动prev到当前节点
current = next; // 移动current到下一个节点
}
return prev;
}
