实例1:C语言中的变量声明与初始化
在C语言中,正确声明和初始化变量是编程的基础。以下是一个简单的变量声明和初始化的例子:
#include <stdio.h>
int main() {
int a = 10; // 声明并初始化整型变量a
float b = 3.14f; // 声明并初始化浮点型变量b
char c = 'A'; // 声明并初始化字符型变量c
return 0;
}
实例2:C语言中的基本输入输出
输入输出是C语言编程中不可或缺的一部分。以下是一个简单的输入输出示例:
#include <stdio.h>
int main() {
int num;
printf("请输入一个整数:");
scanf("%d", &num);
printf("你输入的整数是:%d\n", num);
return 0;
}
实例3:C语言中的循环结构
循环结构是C语言中用于重复执行代码块的重要工具。以下是一个使用for循环的例子:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
实例4:C语言中的条件语句
条件语句用于根据条件执行不同的代码块。以下是一个使用if语句的例子:
#include <stdio.h>
int main() {
int num = 5;
if (num > 0) {
printf("num是一个正数\n");
} else {
printf("num不是一个正数\n");
}
return 0;
}
实例5:C语言中的数组操作
数组是C语言中用于存储一系列相同类型数据的一种数据结构。以下是一个使用数组的例子:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("数组的第一个元素是:%d\n", arr[0]);
return 0;
}
实例6:C语言中的指针操作
指针是C语言中用于存储变量地址的数据类型。以下是一个使用指针的例子:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("num的值是:%d\n", *ptr);
return 0;
}
实例7:C语言中的函数定义与调用
函数是C语言中用于封装代码块的一种方式。以下是一个函数定义和调用的例子:
#include <stdio.h>
// 函数声明
void printMessage();
int main() {
// 函数调用
printMessage();
return 0;
}
// 函数定义
void printMessage() {
printf("这是一个函数。\n");
}
实例8:C语言中的结构体
结构体是C语言中用于组合不同类型数据的一种数据类型。以下是一个结构体的例子:
#include <stdio.h>
// 结构体定义
struct Person {
char name[50];
int age;
};
int main() {
struct Person p;
strcpy(p.name, "张三");
p.age = 25;
printf("姓名:%s,年龄:%d\n", p.name, p.age);
return 0;
}
实例9:C语言中的位操作
位操作是C语言中用于操作二进制位的一种操作。以下是一个位操作的例子:
#include <stdio.h>
int main() {
int num1 = 5; // 二进制:101
int num2 = 3; // 二进制:011
printf("num1 & num2:%d\n", num1 & num2); // 与操作:001
printf("num1 | num2:%d\n", num1 | num2); // 或操作:111
printf("num1 ^ num2:%d\n", num1 ^ num2); // 异或操作:110
return 0;
}
实例10:C语言中的文件操作
文件操作是C语言中用于处理文件的一种方式。以下是一个文件操作的例子:
#include <stdio.h>
int main() {
FILE *fp;
char filename[] = "example.txt";
char buffer[100];
// 打开文件
fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件:%s\n", filename);
return -1;
}
// 读取文件内容
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
// 关闭文件
fclose(fp);
return 0;
}
实例11:C语言中的动态内存分配
动态内存分配是C语言中用于在运行时分配内存的一种方式。以下是一个动态内存分配的例子:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
// 动态分配内存
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("内存分配失败\n");
return -1;
}
// 使用分配的内存
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
// 释放内存
free(arr);
return 0;
}
实例12:C语言中的字符串操作
字符串操作是C语言中用于处理字符串的一种方式。以下是一个字符串操作的例子:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
char buffer[100];
// 字符串连接
strcpy(buffer, str1);
strcat(buffer, str2);
printf("连接后的字符串:%s\n", buffer);
// 字符串比较
if (strcmp(str1, str2) == 0) {
printf("str1和str2相等\n");
} else {
printf("str1和str2不相等\n");
}
return 0;
}
实例13:C语言中的预处理指令
预处理指令是C语言中用于在编译前处理源代码的一种方式。以下是一个预处理指令的例子:
#include <stdio.h>
#define PI 3.14
int main() {
printf("PI的值是:%f\n", PI);
return 0;
}
实例14:C语言中的宏定义
宏定义是C语言中用于创建简写名称的一种方式。以下是一个宏定义的例子:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int x = 10;
int y = 20;
printf("max(%d, %d):%d\n", x, y, MAX(x, y));
return 0;
}
实例15:C语言中的结构体指针
结构体指针是C语言中用于指向结构体变量的一种方式。以下是一个结构体指针的例子:
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1, *ptr;
strcpy(p1.name, "张三");
p1.age = 25;
ptr = &p1;
printf("姓名:%s,年龄:%d\n", (*ptr).name, (*ptr).age);
printf("姓名:%s,年龄:%d\n", ptr->name, ptr->age);
return 0;
}
实例16:C语言中的函数指针
函数指针是C语言中用于指向函数的一种方式。以下是一个函数指针的例子:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*ptr)(int, int) = add;
printf("add(2, 3):%d\n", ptr(2, 3));
return 0;
}
实例17:C语言中的递归函数
递归函数是C语言中用于在函数内部调用自身的一种方式。以下是一个递归函数的例子:
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num = 5;
printf("5的阶乘是:%d\n", factorial(num));
return 0;
}
实例18:C语言中的队列实现
队列是一种先进先出(FIFO)的数据结构。以下是一个队列实现的例子:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
void initQueue(Queue *q) {
q->front = 0;
q->rear = 0;
}
int isEmpty(Queue *q) {
return q->front == q->rear;
}
int isFull(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
void enqueue(Queue *q, int value) {
if (isFull(q)) {
printf("队列已满\n");
return;
}
q->data[q->rear] = value;
q->rear = (q->rear + 1) % MAX_SIZE;
}
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("队列已空\n");
return -1;
}
int value = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return value;
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
printf("队列的第一个元素是:%d\n", dequeue(&q));
printf("队列的第一个元素是:%d\n", dequeue(&q));
printf("队列的第一个元素是:%d\n", dequeue(&q));
return 0;
}
实例19:C语言中的栈实现
栈是一种后进先出(LIFO)的数据结构。以下是一个栈实现的例子:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
void initStack(Stack *s) {
s->top = -1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
int isFull(Stack *s) {
return s->top == MAX_SIZE - 1;
}
void push(Stack *s, int value) {
if (isFull(s)) {
printf("栈已满\n");
return;
}
s->data[++s->top] = value;
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf("栈已空\n");
return -1;
}
return s->data[s->top--];
}
int main() {
Stack s;
initStack(&s);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("栈的第一个元素是:%d\n", pop(&s));
printf("栈的第一个元素是:%d\n", pop(&s));
printf("栈的第一个元素是:%d\n", pop(&s));
return 0;
}
实例20:C语言中的排序算法
排序算法是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;
}
实例21:C语言中的查找算法
查找算法是C语言中用于在数据中查找特定元素的一种方式。以下是一个二分查找算法的例子:
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int x) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
left = mid + 1;
} else {
right = mid - 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("元素不在数组中\n");
} else {
printf("元素在数组中的位置是:%d\n", result);
}
return 0;
}
实例22:C语言中的链表操作
链表是C语言中用于存储一系列数据的一种数据结构。以下是一个单向链表操作的例子:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
// 创建新节点
Node *createNode(int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// 向链表末尾添加节点
void appendNode(Node **head, int value) {
Node *newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
return;
}
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 ", current->data);
current = current->next;
}
printf("\n");
}
// 删除链表中的节点
void deleteNode(Node **head, int value) {
Node *current = *head;
Node *previous = NULL;
while (current != NULL && current->data != value) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("元素不在链表中\n");
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
int main() {
Node *head = NULL;
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
appendNode(&head, 4);
appendNode(&head, 5);
printList(head);
deleteNode(&head, 3);
printList(head);
return 0;
}
实例23:C语言中的递归链表操作
递归链表操作是C语言中利用递归方法对链表进行操作的一种方式。以下是一个递归查找链表中节点值的例子:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
// 创建新节点
Node *createNode(int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// 递归查找链表中的节点值
int findNode(Node *head, int value) {
if (head == NULL) {
return -1;
} else if (head->data == value) {
return 1;
} else {
return findNode(head->next, value);
}
}
int main() {
Node *head = NULL;
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
appendNode(&head, 4);
appendNode(&head, 5);
int value = 3;
int result = findNode(head, value);
if (result == -1) {
printf("元素不在链表中\n");
} else {
printf("元素在链表中的位置是:%d\n", value);
}
return 0;
}
实例24:C语言中的字符串匹配算法
字符串匹配算法是C语言中用于在字符串中查找子字符串的一种方式。以下是一个KMP算法的例子:
”`c
#include
// 计算部分匹配表 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++;
}
}
}
}
// KMP算法 void KMPSearch(char *txt, char *pat) {
int M = strlen(pat);
int N = strlen(txt);
// 创建部分匹配表
int lps[M];
computeL
