在备战考研的过程中,C语言程序设计是计算机科学与技术专业的重要科目之一。为了帮助广大考生在复习过程中更好地掌握C语言程序设计,下面将详细介绍500道经典习题,这些习题涵盖了C语言程序设计的各个知识点,旨在助你一臂之力。
一、C语言基础知识
1. 数据类型与变量
题目:请编写一个C语言程序,定义一个整型变量,初始化为100,然后输出该变量的值。
- 代码:
#include <stdio.h> int main() { int num = 100; printf("The value of num is: %d\n", num); return 0; }
2. 运算符与表达式
题目:编写一个C语言程序,计算两个整数的和、差、积、商。
- 代码:
#include <stdio.h> int main() { int a = 10, b = 5; printf("Sum: %d\n", a + b); printf("Difference: %d\n", a - b); printf("Product: %d\n", a * b); printf("Quotient: %d\n", a / b); return 0; }
二、控制结构
1. 选择结构
题目:编写一个C语言程序,根据用户输入的年龄判断是儿童、青少年还是成人。
- 代码:
#include <stdio.h> int main() { int age; printf("Please enter your age: "); scanf("%d", &age); if (age < 18) { printf("You are a child.\n"); } else if (age >= 18 && age <= 35) { printf("You are a youth.\n"); } else { printf("You are an adult.\n"); } return 0; }
2. 循环结构
题目:编写一个C语言程序,计算1到100之间所有整数的和。
- 代码:
#include <stdio.h> int main() { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } printf("The sum of 1 to 100 is: %d\n", sum); return 0; }
三、函数与递归
1. 函数定义与调用
题目:编写一个C语言程序,定义一个函数计算两个整数的最大公约数,并在主函数中调用该函数。
- 代码:
#include <stdio.h> int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int main() { int num1 = 12, num2 = 18; printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2)); return 0; }
2. 递归函数
题目:编写一个C语言程序,使用递归函数计算斐波那契数列的第n项。
- 代码:
#include <stdio.h> int fibonacci(int n) { if (n <= 1) { return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } int main() { int n = 10; printf("The %dth Fibonacci number is: %d\n", n, fibonacci(n)); return 0; }
四、指针与数组
1. 指针基本操作
题目:编写一个C语言程序,使用指针交换两个整数的值。
- 代码:
#include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; printf("Before swap: x = %d, y = %d\n", x, y); swap(&x, &y); printf("After swap: x = %d, y = %d\n", x, y); return 0; }
2. 二维数组
题目:编写一个C语言程序,计算一个3x3矩阵的主对角线元素之和。
- 代码:
#include <stdio.h> int main() { int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int sum = 0; for (int i = 0; i < 3; i++) { sum += matrix[i][i]; } printf("The sum of the main diagonal elements is: %d\n", sum); return 0; }
五、结构体与文件操作
1. 结构体定义与使用
题目:编写一个C语言程序,定义一个结构体表示学生信息,并创建一个包含3个学生的数组。
- 代码:
#include <stdio.h> struct Student { char name[50]; int age; float score; }; int main() { struct Student students[3] = { {"Alice", 20, 85.5}, {"Bob", 22, 90.0}, {"Charlie", 21, 78.5} }; for (int i = 0; i < 3; i++) { printf("Name: %s, Age: %d, Score: %.1f\n", students[i].name, students[i].age, students[i].score); } return 0; }
2. 文件操作
题目:编写一个C语言程序,创建一个文本文件并写入一些内容。
- 代码:
#include <stdio.h> int main() { FILE *fp = fopen("example.txt", "w"); if (fp == NULL) { printf("Error opening file.\n"); return 1; } fprintf(fp, "This is a sample text.\n"); fprintf(fp, "It is written using fprintf.\n"); fclose(fp); return 0; }
六、综合练习
1. 字符串处理
题目:编写一个C语言程序,实现字符串的拷贝、连接和比较功能。
- 代码:
#include <stdio.h> #include <string.h> void copyString(char *src, char *dest) { while (*src) { *dest++ = *src++; } *dest = '\0'; } void concatenateStrings(char *src, char *dest) { while (*src) { *dest++ = *src++; } *dest = '\0'; src = dest - 1; while (*src) { *dest++ = *src++; } *dest = '\0'; } int compareStrings(char *str1, char *str2) { while (*str1 && *str2 && *str1 == *str2) { str1++; str2++; } return *str1 - *str2; } int main() { char src[100] = "Hello"; char dest[100]; copyString(src, dest); printf("Copied string: %s\n", dest); concatenateStrings(src, dest); printf("Concatenated string: %s\n", dest); int result = compareStrings(src, "Hello World"); if (result == 0) { printf("The strings are equal.\n"); } else { printf("The strings are not equal.\n"); } return 0; }
2. 数据结构
题目:编写一个C语言程序,实现链表的基本操作,如创建、插入、删除和遍历。
- 代码:
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; struct Node* createNode(int data) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } void insertNode(struct Node **head, int data) { struct Node *newNode = createNode(data); newNode->next = *head; *head = newNode; } void deleteNode(struct Node **head, int data) { struct Node *temp = *head, *prev = NULL; if (temp != NULL && temp->data == data) { *head = temp->next; free(temp); return; } while (temp != NULL && temp->data != data) { prev = temp; temp = temp->next; } if (temp == NULL) { return; } prev->next = temp->next; free(temp); } void traverseList(struct Node *head) { struct Node *temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { struct Node *head = NULL; insertNode(&head, 10); insertNode(&head, 20); insertNode(&head, 30); insertNode(&head, 40); printf("Original list: "); traverseList(head); deleteNode(&head, 20); printf("List after deleting 20: "); traverseList(head); return 0; }
七、总结
通过以上500道经典习题,相信广大考生在复习C语言程序设计的过程中能够得到很好的提升。在备考过程中,建议考生不仅要注重理论知识的掌握,还要多动手实践,通过做题来巩固所学知识。最后,祝大家考研顺利,取得理想的成绩!
