C语言作为一门历史悠久且广泛应用的编程语言,以其简洁、高效、可移植性强等特点,深受开发者喜爱。掌握C语言,不仅能够帮助我们理解计算机的工作原理,还能为后续学习其他编程语言打下坚实的基础。下面,我将通过50个经典案例,带你轻松实战C语言编程,解锁编程奥秘。
1. 计算阶乘
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factorial of %d = %ld", num, factorial(num));
return 0;
}
2. 斐波那契数列
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
if (n < 1)
return;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
printf("Fibonacci Series: %d %d", a, b);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
fibonacci(n);
return 0;
}
3. 水仙花数
#include <stdio.h>
int isArmstrong(int num) {
int sum = 0, temp, remainder;
temp = num;
while (temp != 0) {
remainder = temp % 10;
sum += remainder * remainder * remainder;
temp /= 10;
}
return sum == num;
}
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (isArmstrong(num))
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
4. 汉诺塔问题
#include <stdio.h>
void moveTower(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
moveTower(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
moveTower(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
moveTower(n, 'A', 'C', 'B');
return 0;
}
5. 字符串反转
#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 - 1 - i];
str[len - 1 - i] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed string: %s", str);
return 0;
}
6. 密码破解
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isPasswordCorrect(char *password, char *guess) {
return strcmp(password, guess) == 0;
}
int main() {
char password[10], guess[10];
printf("Enter the password: ");
scanf("%s", password);
printf("Enter your guess: ");
scanf("%s", guess);
if (isPasswordCorrect(password, guess))
printf("Correct! You've cracked the password.\n");
else
printf("Incorrect! Try again.\n");
return 0;
}
7. 素数检测
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
8. 斐波那契数列(递归)
#include <stdio.h>
long fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
printf("%ld ", fibonacci(i));
return 0;
}
9. 冒泡排序
#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 n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
10. 选择排序
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
11. 插入排序
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
12. 快速排序
#include <stdio.h>
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++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
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 n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
13. 归并排序
#include <stdio.h>
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 n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
14. 二分查找
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main() {
int n, x;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to be searched: ");
scanf("%d", &x);
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", result);
return 0;
}
15. 合并两个有序数组
#include <stdio.h>
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int arr3[]) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
arr3[k++] = arr1[i++];
} else {
arr3[k++] = arr2[j++];
}
}
while (i < n1) {
arr3[k++] = arr1[i++];
}
while (j < n2) {
arr3[k++] = arr2[j++];
}
}
int main() {
int n1, n2;
printf("Enter the size of first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter the elements of first array: ");
for (int i = 0; i < n1; i++)
scanf("%d", &arr1[i]);
printf("Enter the size of second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter the elements of second array: ");
for (int i = 0; i < n2; i++)
scanf("%d", &arr2[i]);
int arr3[n1 + n2];
mergeArrays(arr1, n1, arr2, n2, arr3);
printf("Merged array: ");
for (int i = 0; i < n1 + n2; i++)
printf("%d ", arr3[i]);
return 0;
}
16. 字符串查找
#include <stdio.h>
#include <string.h>
int findSubstring(char str1[], char str2[]) {
int n1 = strlen(str1);
int n2 = strlen(str2);
for (int i = 0; i <= n1 - n2; i++) {
int j;
for (j = 0; j < n2; j++)
if (str1[i + j] != str2[j])
break;
if (j == n2)
return i;
}
return -1;
}
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int result = findSubstring(str1, str2);
if (result == -1)
printf("Second string is not a substring of the first string.");
else
printf("Second string is found at index %d in the first string.", result);
return 0;
}
17. 字符串替换
#include <stdio.h>
#include <string.h>
void replaceSubstring(char str[], char from[], char to[]) {
int i = 0, j = 0, k = 0;
int len = strlen(str);
while (i < len) {
if (str[i] == from[j]) {
j++;
if (j == strlen(from)) {
str[i - j + k] = to[0];
k++;
i++;
j = 0;
}
} else {
str[i - j + k] = str[i];
i++;
k++;
}
}
str[k] = '\0';
}
int main() {
char str[100], from[100], to[100];
printf("Enter the string: ");
scanf("%s", str);
printf("Enter the substring to be replaced: ");
scanf("%s", from);
printf("Enter the substring to replace with: ");
scanf("%s", to);
replaceSubstring(str, from, to);
printf("Modified string: %s", str);
return 0;
}
18. 字符串反转
#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 - 1 - i];
str[len - 1 - i] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed string: %s", str);
return 0;
}
19. 密码破解
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int isPasswordCorrect(char *password, char *guess) {
return strcmp(password, guess) == 0;
}
int main() {
char password[10], guess[10];
printf("Enter the password: ");
scanf("%s", password);
printf("Enter your guess: ");
scanf("%s", guess);
if (isPasswordCorrect(password, guess))
printf("Correct! You've cracked the password.\n");
else
printf("Incorrect! Try again.\n");
return 0;
}
20. 素数检测
”`c
#include
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0
