1. 打印“Hello, World!”
C语言编程的第一步,当然是从打印“Hello, World!”开始。这个简单的程序能够让你了解C语言的基本语法。
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
2. 变量和数据类型
了解C语言中的变量和数据类型是学习编程的基础。
#include <stdio.h>
int main() {
int age = 25;
float salary = 5000.50;
char grade = 'A';
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}
3. 运算符
C语言中的运算符包括算术运算符、关系运算符和逻辑运算符。
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
4. 控制语句
使用if-else语句进行条件判断。
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("Number is positive\n");
} else {
printf("Number is not positive\n");
}
return 0;
}
5. 循环语句
for循环和while循环是C语言中常用的循环结构。
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
6. 数组
数组是C语言中用于存储多个相同类型数据的一种数据结构。
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Number at index %d: %d\n", i, numbers[i]);
}
return 0;
}
7. 函数
函数是C语言中用于组织代码和重用代码的一种方式。
#include <stdio.h>
void printMessage() {
printf("Hello from a function!\n");
}
int main() {
printMessage();
return 0;
}
8. 指针
指针是C语言中用于存储变量地址的一种数据类型。
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void *)&a);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Value pointed by ptr: %d\n", *ptr);
return 0;
}
9. 结构体
结构体是C语言中用于组织不同类型数据的一种数据结构。
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee emp;
strcpy(emp.name, "John Doe");
emp.age = 30;
emp.salary = 5000.50;
printf("Name: %s\n", emp.name);
printf("Age: %d\n", emp.age);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
10. 链表
链表是C语言中用于存储一系列数据元素的一种数据结构。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(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 printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
printList(head);
return 0;
}
11. 字符串处理
C语言中处理字符串的方法。
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100] = "World";
char result[200];
strcpy(result, str1);
strcat(result, str2);
printf("Concatenated String: %s\n", result);
printf("Length of str1: %lu\n", strlen(str1));
printf("Length of str2: %lu\n", strlen(str2));
return 0;
}
12. 文件操作
C语言中处理文件的方法。
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
char buffer[100];
while (fgets(buffer, 100, file)) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
13. 动态内存分配
C语言中动态分配内存的方法。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
14. 指针与数组
指针与数组的关系。
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
15. 指针与函数
指针在函数中的应用。
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
16. 结构体与指针
结构体与指针的关系。
#include <stdio.h>
struct Employee {
char name[50];
int age;
float salary;
};
void printEmployee(struct Employee *emp) {
printf("Name: %s\n", emp->name);
printf("Age: %d\n", emp->age);
printf("Salary: %.2f\n", emp->salary);
}
int main() {
struct Employee emp;
strcpy(emp.name, "John Doe");
emp.age = 30;
emp.salary = 5000.50;
printEmployee(&emp);
return 0;
}
17. 动态分配二维数组
动态分配二维数组的方法。
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 3, cols = 4;
int **arr = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
arr[i] = (int*)malloc(cols * sizeof(int));
for (int j = 0; j < cols; j++) {
arr[i][j] = i * cols + j;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
18. 深拷贝与浅拷贝
深拷贝与浅拷贝的区别。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void copyList(struct Node* src, struct Node** dest) {
struct Node* temp = src;
struct Node* head = NULL;
while (temp != NULL) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = temp->data;
new_node->next = head;
head = new_node;
temp = temp->next;
}
*dest = head;
}
int main() {
struct Node* src = NULL;
struct Node* dest = NULL;
src = (struct Node*)malloc(sizeof(struct Node));
src->data = 10;
src->next = (struct Node*)malloc(sizeof(struct Node));
src->next->data = 20;
src->next->next = NULL;
copyList(src, &dest);
while (dest != NULL) {
printf("%d ", dest->data);
dest = dest->next;
}
return 0;
}
19. 链表反转
链表反转的方法。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void reverseList(struct Node** head_ref) {
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int main() {
struct Node* head = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = (struct Node*)malloc(sizeof(struct Node));
head->next->data = 2;
head->next->next = (struct Node*)malloc(sizeof(struct Node));
head->next->next->data = 3;
head->next->next->next = NULL;
printf("Original List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
reverseList(&head);
printf("Reversed List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
return 0;
}
20. 字符串搜索算法
字符串搜索算法的实现。
#include <stdio.h>
#include <string.h>
void searchSubstring(char *str, char *sub) {
int index = 0;
while (str[index] != '\0') {
int j = 0;
while (sub[j] != '\0' && str[index + j] == sub[j]) {
j++;
}
if (sub[j] == '\0') {
printf("Substring found at index %d\n", index);
return;
}
index++;
}
printf("Substring not found\n");
}
int main() {
char str[100] = "Hello, World!";
char sub[10] = "World";
searchSubstring(str, sub);
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[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
22. 链表合并
链表合并的方法。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* sortedMerge(struct Node* a, struct Node* b) {
struct Node* result = NULL;
if (a == NULL) {
return b;
} else if (b == NULL) {
return a;
}
if (a->data <= b->data) {
result = a;
result->next = sortedMerge(a->next, b);
} else {
result = b;
result->next = sortedMerge(a, b->next);
}
return result;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node* a = NULL;
struct Node* b = NULL;
a = (struct Node*)malloc(sizeof(struct Node));
a->data = 1;
a->next = (struct Node*)malloc(sizeof(struct Node));
a->next->data = 3;
a->next->next = NULL;
b = (struct Node*)malloc(sizeof(struct Node));
b->data = 2;
b->next = (struct Node*)malloc(sizeof(struct Node));
b->next->data = 4;
b->next->next = NULL;
printf("List A: ");
printList(a);
printf("List B: ");
printList(b);
struct Node* result = sortedMerge(a, b);
printf("Merged List: ");
printList(result);
return 0;
}
23. 字符串反转
字符串反转的方法。
#include <stdio.h>
#include <string.h>
void reverseString(char* str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[100] = "Hello, World!";
printf("Original String: %s\n", str);
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
24. 字符串匹配算法
字符串匹配算法的实现。
#include <stdio.h>
#include <string.h>
void stringMatch(char* str, char* sub) {
int i, j;
int m = strlen(sub);
int n = strlen(str);
for (i = 0; i <= n - m; i++) {
j = 0;
while (j < m && sub[j] == str[i + j]) {
j++;
}
if (j == m) {
printf("Substring found at index %d\n", i);
return;
}
}
printf("Substring not found\n");
}
int main() {
char str[100] = "Hello, World!";
char sub[10] = "World";
stringMatch(str, sub);
return 0;
}
25. 矩阵乘法
矩阵乘法的实现。
”`c
#include
void multiplyMatrices(int a[][3], int b[][3], int result[][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void printMatrix(int matrix[][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[3][3];
