实战案例一:打印Hello World
在C语言编程中,第一个学习项目通常是打印“Hello World”到控制台。这是一个简单的程序,用于展示C语言的基本语法和编译过程。
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
实战案例二:变量和基本数据类型
了解C语言中的变量和数据类型是学习编程的基础。在这个案例中,我们将创建几个变量并打印它们的值。
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
char c = 'A';
printf("整型变量a的值是:%d\n", a);
printf("浮点型变量b的值是:%f\n", b);
printf("字符型变量c的值是:%c\n", c);
return 0;
}
实战案例三:运算符和表达式
C语言中的运算符和表达式是进行复杂计算的关键。在这个案例中,我们将学习如何使用算术运算符和逻辑运算符。
#include <stdio.h>
int main() {
int x = 5, y = 3;
printf("x + y = %d\n", x + y);
printf("x - y = %d\n", x - y);
printf("x * y = %d\n", x * y);
printf("x / y = %d\n", x / y);
printf("x % y = %d\n", x % y);
printf("!(x > y) = %d\n", !(x > y));
return 0;
}
实战案例四:控制结构
控制结构是编程中的核心,它们允许程序根据条件执行不同的代码块。在这个案例中,我们将学习如何使用if语句和for循环。
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i % 2 == 0) {
printf("偶数:%d\n", i);
} else {
printf("奇数:%d\n", i);
}
}
return 0;
}
实战案例五:函数
函数是C语言中的代码块,可以重复使用以提高代码的可读性和可维护性。在这个案例中,我们将创建一个简单的函数来计算两个数的和。
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int x = 10, y = 20;
printf("x + y = %d\n", sum(x, y));
return 0;
}
实战案例六:指针
指针是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("交换前:x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("交换后:x = %d, y = %d\n", x, y);
return 0;
}
实战案例七:结构体
结构体是C语言中用于组织相关数据的复合数据类型。在这个案例中,我们将创建一个结构体来表示一个学生,并打印其信息。
#include <stdio.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
int main() {
Student stu1 = {"Alice", 20, 90.5};
printf("学生姓名:%s\n", stu1.name);
printf("学生年龄:%d\n", stu1.age);
printf("学生成绩:%f\n", stu1.score);
return 0;
}
实战案例八:文件操作
文件操作是C语言中的一个重要方面,它允许我们读取和写入文件。在这个案例中,我们将学习如何使用fopen、fprintf和fclose函数来创建和写入一个文本文件。
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "w");
if (fp == NULL) {
printf("文件打开失败\n");
return 1;
}
fprintf(fp, "这是一个示例文件。\n");
fclose(fp);
return 0;
}
实战案例九:动态内存分配
动态内存分配是C语言中的一个高级特性,它允许我们在运行时分配和释放内存。在这个案例中,我们将使用malloc和free函数来动态分配和释放内存。
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int *)malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("内存分配失败\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;
}
实战案例十:字符串处理
字符串处理是C语言中的一个重要方面,它允许我们操作文本数据。在这个案例中,我们将学习如何使用strlen、strcpy和strcat函数来处理字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[100];
printf("str1的长度:%lu\n", strlen(str1));
strcpy(str3, str1);
strcat(str3, str2);
printf("str3:%s\n", str3);
return 0;
}
实战案例十一:结构体数组
结构体数组是C语言中用于存储多个结构体实例的数组。在这个案例中,我们将创建一个结构体数组来存储学生的信息,并打印它们。
#include <stdio.h>
typedef struct {
char name[50];
int age;
float score;
} Student;
int main() {
Student stu1 = {"Alice", 20, 90.5};
Student stu2 = {"Bob", 22, 85.0};
Student stu3 = {"Charlie", 21, 92.0};
Student students[3] = {stu1, stu2, stu3};
for (int i = 0; i < 3; i++) {
printf("学生姓名:%s\n", students[i].name);
printf("学生年龄:%d\n", students[i].age);
printf("学生成绩:%f\n", students[i].score);
}
return 0;
}
实战案例十二:链表
链表是C语言中用于动态存储数据的一种数据结构。在这个案例中,我们将创建一个简单的单向链表来存储学生信息,并打印它们。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insert(&head, 3);
insert(&head, 1);
insert(&head, 4);
insert(&head, 1);
insert(&head, 5);
printList(head);
return 0;
}
实战案例十三:递归
递归是C语言中的一种编程技巧,它允许函数调用自身。在这个案例中,我们将使用递归函数来计算阶乘。
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("5的阶乘:%d\n", factorial(num));
return 0;
}
实战案例十四:排序算法
排序算法是C语言中用于对数据进行排序的方法。在这个案例中,我们将实现一个简单的冒泡排序算法来对整数数组进行排序。
#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[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("排序后的数组:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
实战案例十五:查找算法
查找算法是C语言中用于在数据集合中查找特定元素的方法。在这个案例中,我们将实现一个简单的线性查找算法来在整数数组中查找元素。
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = linearSearch(arr, n, x);
if (result == -1)
printf("元素不在数组中\n");
else
printf("元素在索引:%d\n", result);
return 0;
}
实战案例十六:字符串匹配算法
字符串匹配算法是C语言中用于在字符串中查找子字符串的方法。在这个案例中,我们将实现一个简单的字符串匹配算法(如KMP算法)来查找子字符串。
#include <stdio.h>
void computeLPSArray(char* pat, int M, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
void KMPSearch(char* pat, char* txt) {
int M = strlen(pat);
int N = strlen(txt);
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
printf("在索引 %d 到 %d 找到模式\n", i - j, i);
j = lps[j - 1];
} else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
int main() {
char txt[] = "ABABDABACDABABCABAB";
char pat[] = "ABABCABAB";
KMPSearch(pat, txt);
return 0;
}
实战案例十七:动态规划
动态规划是C语言中用于解决优化问题的一种方法。在这个案例中,我们将使用动态规划来计算斐波那契数列。
#include <stdio.h>
int fib(int n) {
if (n <= 1)
return n;
int dp[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}
int main() {
int n = 9;
printf("斐波那契数列的第 %d 个数是:%d\n", n, fib(n));
return 0;
}
实战案例十八:贪心算法
贪心算法是C语言中用于解决优化问题的一种方法。在这个案例中,我们将使用贪心算法来求解背包问题。
#include <stdio.h>
int maxProfit(int prices[], int n) {
int maxDiff = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i - 1])
maxDiff = maxDiff + prices[i] - prices[i - 1];
}
return maxDiff;
}
int main() {
int prices[] = {10, 7, 5, 8, 11, 9};
int n = sizeof(prices) / sizeof(prices[0]);
printf("最大利润:%d\n", maxProfit(prices, n));
return 0;
}
实战案例十九:回溯算法
回溯算法是C语言中用于解决组合问题和排列问题的一种方法。在这个案例中,我们将使用回溯算法来求解八皇后问题。
#include <stdio.h>
int isSafe(int board[], int row, int col, int n) {
for (int i = 0; i < col; i++) {
if (board[i] == row || abs(board[i] - row) == abs(i - col))
return 0;
}
return 1;
}
void solveNQueensUtil(int board[], int col, int n) {
if (col >= n) {
for (int i = 0; i < n; i++)
printf("%d ", board[i]);
printf("\n");
return;
}
for (int i = 0; i < n; i++) {
if (isSafe(board, i, col, n)) {
board[col] = i;
solveNQueensUtil(board, col + 1, n);
}
}
}
void solveNQueens(int n) {
int board[n];
for (int i = 0; i < n; i++)
board[i] = -1;
solveNQueensUtil(board, 0, n);
}
int main() {
int n = 8;
solveNQueens(n);
return 0;
}
实战案例二十:树和图
树和图是C语言中用于表示数据结构的一种方法。在这个案例中,我们将创建一个简单的二叉树来存储整数,并遍历它。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node* newNode(int data) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void inorderTraversal(Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
printf("中序遍历:\n");
inorderTraversal(root);
printf("\n");
return 0;
}
实战案例二十一:排序算法(快速排序)
快速排序是C语言中用于对数据进行排序的一种高效算法。在这个案例中,我们将实现快速排序算法来对整数数组进行排序。
#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("排序后的数组:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
实战案例二十二:排序算法(归并排序)
归并排序是C语言中用于对数据进行排序的一种高效算法。在这个案例中,我们将实现归并排序算法来对整数数组进行排序。
”`c
#include
void merge(int arr[], int l, int m, int r) {
int i, j, k;
