1. 打印Hello World
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
解释:这是C语言编程中最基础的程序,用于输出”Hello, World!“字符串到控制台。
2. 数据类型与变量声明
#include <stdio.h>
int main() {
int num = 10; // 整型变量
float fnum = 3.14f; // 浮点型变量
char ch = 'A'; // 字符型变量
return 0;
}
解释:声明不同类型的数据变量,并赋初值。
3. 运算符与表达式
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b; // 加法
int sub = a - b; // 减法
int mul = a * b; // 乘法
int div = a / b; // 除法
int mod = a % b; // 取模
return 0;
}
解释:使用基本的算术运算符进行计算。
4. if语句
#include <stdio.h>
int main() {
int age = 20;
if (age > 18) {
printf("成年了\n");
}
return 0;
}
解释:使用if语句进行条件判断,并输出结果。
5. switch语句
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("数字为1\n");
break;
case 2:
printf("数字为2\n");
break;
default:
printf("其他数字\n");
}
return 0;
}
解释:使用switch语句进行多分支选择。
6. 循环结构for
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("数字:%d\n", i);
}
return 0;
}
解释:使用for循环结构输出1到5的数字。
7. 循环结构while
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("数字:%d\n", i);
i++;
}
return 0;
}
解释:使用while循环结构输出1到5的数字。
8. 循环结构do-while
#include <stdio.h>
int main() {
int i = 1;
do {
printf("数字:%d\n", i);
i++;
} while (i <= 5);
return 0;
}
解释:使用do-while循环结构输出1到5的数字。
9. 数组操作
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("数组元素:%d\n", arr[i]);
}
return 0;
}
解释:使用数组存储和输出一系列数字。
10. 字符串操作
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "Hello";
char str2[10] = "World";
strcat(str1, str2); // 连接字符串
printf("合并后的字符串:%s\n", str1);
return 0;
}
解释:使用字符串函数进行字符串操作。
11. 指针操作
#include <stdio.h>
int main() {
int num = 10;
int *p = # // 指针指向变量地址
printf("变量地址:%p\n", (void*)p);
printf("变量值:%d\n", *p);
return 0;
}
解释:使用指针获取变量地址和值。
12. 函数定义与调用
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5);
printf("结果:%d\n", result);
return 0;
}
解释:定义并调用一个函数,实现两个整数的加法运算。
13. 结构体定义与使用
#include <stdio.h>
typedef struct {
int id;
char name[20];
} Person;
int main() {
Person person1 = {1, "张三"};
printf("姓名:%s\n", person1.name);
return 0;
}
解释:定义结构体并创建实例,存储个人信息。
14. 链表操作
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void appendNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("节点值:%d\n", current->data);
current = current->next;
}
}
int main() {
Node* head = NULL;
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
printList(head);
return 0;
}
解释:定义链表节点结构体,创建链表,并输出链表中的数据。
15. 字符串函数strncpy
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "Hello";
char str2[10] = "World";
strncpy(str1, str2, 3); // 复制前3个字符
printf("str1:%s\n", str1);
return 0;
}
解释:使用strncpy函数复制字符串的一部分。
16. 内存分配与释放
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*)malloc(sizeof(int));
*ptr = 10;
printf("指针指向的值:%d\n", *ptr);
free(ptr); // 释放内存
return 0;
}
解释:使用malloc函数分配内存,使用free函数释放内存。
17. 文件操作
#include <stdio.h>
int main() {
FILE* fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("打开文件失败\n");
return -1;
}
fprintf(fp, "Hello, World!\n");
fclose(fp);
return 0;
}
解释:使用fopen函数打开文件,使用fprintf函数写入数据,使用fclose函数关闭文件。
18. 排序算法——冒泡排序
#include <stdio.h>
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() {
int arr[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
for (int i = 0; i < n; i++) {
printf("排序后的数组:%d\n", arr[i]);
}
return 0;
}
解释:使用冒泡排序算法对数组进行排序。
19. 排序算法——选择排序
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
for (int i = 0; i < n; i++) {
printf("排序后的数组:%d\n", arr[i]);
}
return 0;
}
解释:使用选择排序算法对数组进行排序。
20. 排序算法——插入排序
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for (int i = 0; i < n; i++) {
printf("排序后的数组:%d\n", arr[i]);
}
return 0;
}
解释:使用插入排序算法对数组进行排序。
21. 排序算法——快速排序
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {5, 2, 8, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("排序后的数组:%d\n", arr[i]);
}
return 0;
}
解释:使用快速排序算法对数组进行排序。
22. 链表操作——删除节点
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void appendNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void deleteNode(Node** head, int data) {
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 printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("节点值:%d\n", current->data);
current = current->next;
}
}
int main() {
Node* head = NULL;
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
printList(head);
deleteNode(&head, 2);
printList(head);
return 0;
}
解释:在链表中创建节点、添加节点、删除节点和输出链表。
23. 链表操作——反转链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void appendNode(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void reverseList(Node** head) {
Node *prev = NULL, *current = *head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("节点值:%d\n", current->data);
current = current->next;
}
}
int main() {
Node* head = NULL;
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
printList(head);
reverseList(&head);
printList(head);
return 0;
}
解释:在链表中创建节点、添加节点、反转链表和输出链表。
24. 动态规划——斐波那契数列
#include <stdio.h>
int fib(int n) {
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int main() {
int n = 10;
printf("斐波那契数列的第%d项:%d\n", n, fib(n));
return 0;
}
解释:使用递归方法计算斐波那契数列的第n项。
25. 动态规划——最长公共子序列
#include <stdio.h>
int lcs(int X[], int Y[], int m, int n) {
int L[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
int main() {
int X[] = {1, 2, 3, 4};
int Y[] = {2, 3, 4, 5};
int m = sizeof(X) / sizeof(X[0]);
int n = sizeof(Y) / sizeof(Y[0]);
printf("最长公共子序列的长度:%d\n", lcs(X, Y, m, n));
return 0;
}
解释:使用动态规划求解最长公共子序列问题。
26. 深度优先搜索(DFS)
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void DFS(Node* root) {
if (root == NULL)
return;
printf("%d ", root->data);
DFS(root->left);
DFS(root->right);
}
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
printf("深度优先搜索:");
DFS(root);
return 0;
}
解释:使用递归方法进行深度优先搜索遍历二叉树。
27. 广度优先搜索(BFS)
”`c
#include
#define MAX_SIZE 100
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
typedef struct Queue {
int items[MAX_SIZE];
int front;
int rear;
} Queue;
void initQueue(Q
