引言
C语言作为一门历史悠久且广泛应用于系统级编程和嵌入式开发的编程语言,其精髓在于其简洁、高效和灵活性。为了帮助读者深入理解和掌握C语言的精髓,本文将提供200道实战练习题,涵盖C语言的基础语法、数据结构、算法等方面,旨在帮助读者通过解决实际问题来提升编程能力。
第一章 C语言基础语法
1.1 数据类型和变量
题目1:声明并初始化一个整型变量,并打印其值。
#include <stdio.h>
int main() {
int num = 10;
printf("The value of num is: %d\n", num);
return 0;
}
题目2:计算一个整数的阶乘。
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
1.2 运算符和表达式
题目3:编写一个程序,交换两个变量的值。
#include <stdio.h>
int main() {
int a = 5, b = 10;
int temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
return 0;
}
题目4:编写一个程序,计算表达式的值。
#include <stdio.h>
int main() {
int x = 3, y = 4;
int result = (x + y) * (x - y);
printf("The result of expression is: %d\n", result);
return 0;
}
第二章 数据结构
2.1 数组
题目5:编写一个程序,将一个整型数组的元素逆序。
#include <stdio.h>
void reverseArray(int arr[], int size) {
int temp;
for (int i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, size);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2.2 链表
题目6:实现一个单链表,并在其中插入和删除节点。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertNode(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
if (temp != NULL && temp->data == key) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
int main() {
struct Node* head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
insertNode(&head, 3);
deleteNode(&head, 2);
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
return 0;
}
第三章 算法
3.1 排序算法
题目7:实现冒泡排序算法,对一个整型数组进行排序。
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
3.2 搜索算法
题目8:实现二分搜索算法,在一个有序数组中查找一个元素。
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) printf("Element is not present in array");
else printf("Element is present at index %d", result);
return 0;
}
结论
通过以上200道实战练习题,读者可以全面深入地掌握C语言的精髓。通过不断练习和挑战,相信每一位编程者都能在C语言的道路上取得长足的进步。
