在编程的世界里,C语言作为一门基础且强大的编程语言,被广泛应用于系统开发、嵌入式系统、操作系统等领域。为了帮助大家更好地掌握C语言,本文将提供50个经典例题,通过这些例题,你可以一网打尽编程难题,轻松提升编程技能。
1. 数据类型与变量
例题1: 输出以下变量的值,变量类型分别为int、float、double、char。
int a = 10;
float b = 3.14;
double c = 2.71828;
char d = 'A';
解答:
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
double c = 2.71828;
char d = 'A';
printf("int a = %d\n", a);
printf("float b = %f\n", b);
printf("double c = %lf\n", c);
printf("char d = %c\n", d);
return 0;
}
2. 运算符与表达式
例题2: 编写一个程序,计算 (3 + 4) * 5 / 2 - 1 的值。
解答:
#include <stdio.h>
int main() {
int result = (3 + 4) * 5 / 2 - 1;
printf("The result is: %d\n", result);
return 0;
}
3. 控制结构
例题3: 编写一个程序,根据用户输入的年龄,判断其是成年人还是未成年人。
解答:
#include <stdio.h>
int main() {
int age;
printf("Please enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
return 0;
}
4. 循环结构
例题4: 编写一个程序,输出1到100之间所有偶数的和。
解答:
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
printf("The sum of even numbers between 1 and 100 is: %d\n", sum);
return 0;
}
5. 函数
例题5: 编写一个函数,计算两个整数的最大公约数。
解答:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int main() {
int num1, num2, result;
printf("Please enter two integers: ");
scanf("%d %d", &num1, &num2);
result = gcd(num1, num2);
printf("The GCD of %d and %d is: %d\n", num1, num2, result);
return 0;
}
6. 数组
例题6: 编写一个程序,将用户输入的10个整数存储在数组中,并输出这些整数的平均值。
解答:
#include <stdio.h>
int main() {
int numbers[10];
int sum = 0;
float average;
printf("Please enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
average = (float)sum / 10;
printf("The average of the numbers is: %.2f\n", average);
return 0;
}
7. 字符串
例题7: 编写一个程序,比较两个字符串是否相等。
解答:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Please enter two strings:\n");
scanf("%s", str1);
scanf("%s", str2);
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
8. 指针
例题8: 编写一个程序,交换两个整数的值。
解答:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 10, num2 = 20;
printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swap: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
9. 结构体
例题9: 编写一个程序,定义一个学生结构体,并创建一个学生实例,输出其姓名和年龄。
解答:
#include <stdio.h>
typedef struct {
char name[50];
int age;
} Student;
int main() {
Student student;
strcpy(student.name, "John Doe");
student.age = 20;
printf("Name: %s\n", student.name);
printf("Age: %d\n", student.age);
return 0;
}
10. 文件操作
例题10: 编写一个程序,将用户输入的文本保存到一个文件中。
解答:
#include <stdio.h>
int main() {
char filename[100];
char text[1000];
printf("Please enter the filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Please enter the text:\n");
fgets(text, sizeof(text), stdin);
fputs(text, file);
fclose(file);
printf("Text saved to %s\n", filename);
return 0;
}
11. 动态内存分配
例题11: 编写一个程序,动态分配一个整型数组,读取用户输入的10个整数,并输出这些整数的平均值。
解答:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *numbers = (int *)malloc(10 * sizeof(int));
if (numbers == NULL) {
printf("Error allocating memory!\n");
return 1;
}
printf("Please enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += numbers[i];
}
float average = (float)sum / 10;
printf("The average of the numbers is: %.2f\n", average);
free(numbers);
return 0;
}
12. 链表
例题12: 编写一个程序,实现一个单向链表,添加、删除和遍历节点。
解答:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **head, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
void delete(Node **head, int value) {
Node *temp = *head, *prev = NULL;
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found!\n");
return;
}
if (prev == NULL) {
*head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
}
void traverse(Node *head) {
Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
printf("List before deletion: ");
traverse(head);
delete(&head, 20);
printf("List after deletion: ");
traverse(head);
return 0;
}
13. 栈与队列
例题13: 编写一个程序,实现一个栈,添加、删除和遍历元素。
解答:
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
int *array;
int top;
int capacity;
} Stack;
void initStack(Stack *stack, int capacity) {
stack->array = (int *)malloc(capacity * sizeof(int));
stack->top = -1;
stack->capacity = capacity;
}
void push(Stack *stack, int value) {
if (stack->top == stack->capacity - 1) {
printf("Stack is full!\n");
return;
}
stack->array[++stack->top] = value;
}
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack is empty!\n");
return -1;
}
return stack->array[stack->top--];
}
void traverse(Stack *stack) {
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->array[i]);
}
printf("\n");
}
int main() {
Stack stack;
initStack(&stack, 5);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Stack after pushing: ");
traverse(&stack);
int value = pop(&stack);
printf("Popped value: %d\n", value);
printf("Stack after popping: ");
traverse(&stack);
return 0;
}
14. 链表排序
例题14: 编写一个程序,实现一个链表,添加元素,并使用归并排序对链表进行排序。
解答:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert(Node **head, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
Node *merge(Node *a, Node *b) {
Node *result = NULL;
if (a == NULL) {
return b;
}
if (b == NULL) {
return a;
}
if (a->data <= b->data) {
result = a;
result->next = merge(a->next, b);
} else {
result = b;
result->next = merge(a, b->next);
}
return result;
}
void split(Node *source, Node **frontRef, Node **backRef) {
Node *fast;
Node *slow;
slow = source;
fast = source->next;
while (fast != NULL) {
fast = fast->next;
if (fast != NULL) {
slow = slow->next;
fast = fast->next;
}
}
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
void mergeSort(Node **headRef) {
Node *head = *headRef;
Node *a;
Node *b;
if ((head == NULL) || (head->next == NULL)) {
return;
}
split(head, &a, &b);
mergeSort(&a);
mergeSort(&b);
*headRef = merge(a, b);
}
void printList(Node *node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
insert(&head, 10);
insert(&head, 30);
insert(&head, 3);
insert(&head, 4);
insert(&head, 20);
insert(&head, 5);
insert(&head, 1);
printf("Original list: ");
printList(head);
mergeSort(&head);
printf("Sorted list: ");
printList(head);
return 0;
}
15. 树与图
例题15: 编写一个程序,实现一个二叉树,添加、删除和遍历节点。
解答:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
void insert(Node **root, int value) {
if (*root == NULL) {
*root = createNode(value);
return;
}
if (value < (*root)->data) {
insert(&((*root)->left), value);
} else if (value > (*root)->data) {
insert(&((*root)->right), value);
}
}
void delete(Node **root, int value) {
if (*root == NULL) {
return;
}
if (value < (*root)->data) {
delete(&((*root)->left), value);
} else if (value > (*root)->data) {
delete(&((*root)->right), value);
} else {
if ((*root)->left == NULL && (*root)->right == NULL) {
free(*root);
*root = NULL;
} else if ((*root)->left == NULL) {
Node *temp = *root;
*root = (*root)->right;
free(temp);
} else if ((*root)->right == NULL) {
Node *temp = *root;
*root = (*root)->left;
free(temp);
} else {
Node *temp = (*root)->right;
while (temp->left != NULL) {
temp = temp->left;
}
(*root)->data = temp->data;
delete(&((*root)->right), temp->data);
}
}
}
void inorderTraversal(Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
Node *root = NULL;
insert(&root, 10);
insert(&root, 5);
insert(&root, 1);
insert(&root, 7);
insert(&root, 40);
insert(&root, 50);
printf("Inorder traversal: ");
inorderTraversal(root);
delete(&root, 10);
printf("Inorder traversal after deletion: ");
inorderTraversal(root);
return 0;
}
16. 搜索算法
例题16: 编写一个程序,实现一个二叉搜索树,添加、删除和搜索元素。
解答:
“`c
#include
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node *insert(Node *root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
Node *search(Node *root, int value) {
if (root == NULL || root->data == value) {
return root;
}
if (value < root->data) {
return search(root->left, value);
} else {
return search(root->right, value);
}
}
Node *delete(Node *root, int value) {
if (root == NULL) {
return root;
}
if (value < root->data) {
root->left = delete(root->left, value);
} else if (value > root->data) {
root->right = delete(root->right, value);
} else {
if (root->left == NULL) {
Node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node *temp = root->left;
free(root);
return temp;
}
Node *temp = search(root->right, root->data);
root->data = temp->data;
root->right = delete(root->right, root->data);
}
return root;
}
int main() {
Node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 10);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 14);
root = insert(root, 4);
root = insert(root, 7);
root = insert(root, 13);
printf("Inorder traversal: ");
