案例一:简单的“Hello World”程序
在这个案例中,我们将创建一个最简单的C语言程序,它会输出“Hello World”到控制台。
#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}
这是一个基本的C语言程序,它使用了printf函数来输出文本。
案例二:变量和数据类型
了解变量和数据类型是学习C语言的基础。在这个案例中,我们将声明几个变量,并展示如何使用它们。
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9f;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %.2f meters\n", height);
printf("Grade: %c\n", grade);
return 0;
}
案例三:控制流语句
在这个案例中,我们将使用if语句来根据条件执行不同的代码块。
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
案例四:循环语句
循环语句允许我们重复执行代码块。在这个案例中,我们将使用for循环打印1到10的数字。
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
案例五:函数定义和调用
函数是C语言的核心概念之一。在这个案例中,我们将定义一个简单的函数来计算两个数的和。
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int result = sum(5, 7);
printf("The sum is: %d\n", result);
return 0;
}
案例六:指针
指针是C语言中的一个高级特性,允许我们直接操作内存地址。在这个案例中,我们将创建一个指向整数的指针,并修改它的值。
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Original value of a: %d\n", a);
printf("Address of a: %p\n", (void *)&a);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Value of *ptr: %d\n", *ptr);
*ptr = 20;
printf("New value of a: %d\n", a);
return 0;
}
案例七:结构体
结构体允许我们创建复杂的数据类型。在这个案例中,我们将定义一个包含姓名和年龄的结构体。
#include <stdio.h>
typedef struct {
char name[50];
int age;
} Person;
int main() {
Person person;
strcpy(person.name, "John Doe");
person.age = 30;
printf("Name: %s\n", person.name);
printf("Age: %d\n", person.age);
return 0;
}
案例八:数组
数组是C语言中用于存储相同类型数据的集合。在这个案例中,我们将创建一个整数数组,并遍历它。
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Number %d: %d\n", i + 1, numbers[i]);
}
return 0;
}
案例九:字符串操作
C语言提供了几种函数来操作字符串。在这个案例中,我们将使用strcpy和strlen函数。
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[100] = "World";
strcpy(str1, str2);
printf("str1: %s\n", str1);
printf("Length of str1: %ld\n", strlen(str1));
return 0;
}
案例十:文件操作
文件操作是C语言中的一个重要部分。在这个案例中,我们将创建一个文件,并写入一些文本。
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
fprintf(file, "This is a test file.\n");
fclose(file);
return 0;
}
案例十一:动态内存分配
动态内存分配允许我们在运行时分配和释放内存。在这个案例中,我们将使用malloc和free函数。
#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 * 2;
}
for (int i = 0; i < 5; i++) {
printf("Value at index %d: %d\n", i, ptr[i]);
}
free(ptr);
return 0;
}
案例十二:结构体和动态内存分配
在这个案例中,我们将创建一个结构体数组,并使用动态内存分配来存储它。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
char name[50];
} Student;
int main() {
int num_students = 3;
Student *students = (Student *)malloc(num_students * sizeof(Student));
for (int i = 0; i < num_students; i++) {
students[i].id = i + 1;
sprintf(students[i].name, "Student %d", i + 1);
}
for (int i = 0; i < num_students; i++) {
printf("ID: %d, Name: %s\n", students[i].id, students[i].name);
}
free(students);
return 0;
}
案例十三:指针和字符串函数
在这个案例中,我们将使用指针和字符串函数来复制字符串。
#include <stdio.h>
#include <string.h>
int main() {
char src[100] = "Source string";
char dest[100];
strcpy(dest, src);
printf("Source: %s\n", src);
printf("Destination: %s\n", dest);
return 0;
}
案例十四:函数指针
函数指针允许我们指向函数。在这个案例中,我们将定义一个函数指针,并使用它来调用函数。
#include <stdio.h>
void printMessage(const char *message) {
printf("Message: %s\n", message);
}
int main() {
void (*funcPtr)(const char *) = printMessage;
funcPtr("Hello from function pointer!");
return 0;
}
案例十五:递归函数
递归函数是一种函数,它在其定义中调用自身。在这个案例中,我们将创建一个递归函数来计算阶乘。
#include <stdio.h>
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
案例十六:结构体数组排序
在这个案例中,我们将使用结构体数组,并使用冒泡排序算法对其进行排序。
#include <stdio.h>
typedef struct {
int id;
char name[50];
} Student;
void bubbleSort(Student *students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].id > students[j + 1].id) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
int main() {
Student students[3] = {
{1, "Alice"},
{3, "Bob"},
{2, "Charlie"}
};
int n = sizeof(students) / sizeof(students[0]);
bubbleSort(students, n);
for (int i = 0; i < n; i++) {
printf("ID: %d, Name: %s\n", students[i].id, students[i].name);
}
return 0;
}
案例十七:指针数组和多维数组
在这个案例中,我们将创建一个指针数组和多维数组,并展示如何访问它们的元素。
#include <stdio.h>
int main() {
int array[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*ptr)[3] = array;
printf("Element at [0][0]: %d\n", (*ptr)[0]);
printf("Element at [1][2]: %d\n", (*ptr)[1][2]);
return 0;
}
案例十八:链表
链表是一种常见的数据结构,它由一系列节点组成。在这个案例中,我们将创建一个单向链表,并添加一些节点。
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insertAtHead(Node **head, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
Node *head = NULL;
insertAtHead(&head, 3);
insertAtHead(&head, 2);
insertAtHead(&head, 1);
printList(head);
return 0;
}
案例十九:队列
队列是一种先进先出(FIFO)的数据结构。在这个案例中,我们将创建一个队列,并添加和删除元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int front;
int rear;
int size;
int *array;
} Queue;
void initializeQueue(Queue *q, int capacity) {
q->front = q->size = 0;
q->rear = capacity - 1;
q->array = (int *)malloc(capacity * sizeof(int));
}
void enqueue(Queue *q, int value) {
if (q->size == q->capacity) {
printf("Queue is full.\n");
return;
}
q->rear = (q->rear + 1) % q->capacity;
q->array[q->rear] = value;
q->size++;
}
int dequeue(Queue *q) {
if (q->size == 0) {
printf("Queue is empty.\n");
return -1;
}
int value = q->array[q->front];
q->front = (q->front + 1) % q->capacity;
q->size--;
return value;
}
int main() {
Queue q;
initializeQueue(&q, 5);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
return 0;
}
案例二十:栈
栈是一种后进先出(LIFO)的数据结构。在这个案例中,我们将创建一个栈,并添加和删除元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int top;
unsigned capacity;
int *array;
} Stack;
void initializeStack(Stack *s, unsigned capacity) {
s->capacity = capacity;
s->top = -1;
s->array = (int *)malloc(s->capacity * sizeof(int));
}
int isFull(Stack *s) {
return s->top == s->capacity - 1;
}
int isEmpty(Stack *s) {
return s->top == -1;
}
void push(Stack *s, int item) {
if (isFull(s)) {
printf("Stack is full.\n");
return;
}
s->array[++s->top] = item;
}
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty.\n");
return -1;
}
return s->array[s->top--];
}
int main() {
Stack s;
initializeStack(&s, 5);
push(&s, 1);
push(&s, 2);
push(&s, 3);
printf("Popped: %d\n", pop(&s));
printf("Popped: %d\n", pop(&s));
return 0;
}
案例二十一:散列表
散列表是一种基于键值对的数据结构。在这个案例中,我们将创建一个简单的散列表,并添加一些键值对。
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
typedef struct Node {
int key;
int value;
struct Node *next;
} Node;
unsigned int hash(int key) {
return key % TABLE_SIZE;
}
Node *createNode(int key, int value) {
Node *node = (Node *)malloc(sizeof(Node));
node->key = key;
node->value = value;
node->next = NULL;
return node;
}
void insert(Node **table, int key, int value) {
unsigned int index = hash(key);
Node *node = createNode(key, value);
if (table[index] == NULL) {
table[index] = node;
} else {
Node *current = table[index];
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
}
int main() {
Node *table[TABLE_SIZE] = {NULL};
insert(table, 1, 100);
insert(table, 2, 200);
insert(table, 3, 300);
// Accessing values from the hash table
printf("Value for key 2: %d\n", table[1]->value);
printf("Value for key 3: %d\n", table[2]->value);
return 0;
}
案例二十二:排序算法——快速排序
快速排序是一种高效的排序算法,它使用分而治之的策略。在这个案例中,我们将实现快速排序算法。
#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;
}
案例二十三:排序算法——归并排序
归并排序是一种稳定的排序算法,它将数组分为更小的部分,然后合并它们。在这个案例中,我们将实现归并排序算法。
”`c
#include
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6
