上一篇 下一篇 分享链接 返回 返回顶部

C语言进阶必备:100例经典程序精解与实战

发布人:不二云 发布时间:7 天前 阅读量:23
# C语言经典程序100例

C语言作为一门操作底层硬件、追求效率的经典编程语言,无论是在学术研究、工业开发,还是编程学习入门阶段,都占据着非常重要的位置。本文整理了100个C语言的经典案例,适合不同水平的学习者参考与练习。

## 一、基础算法

1. **求和计算**
   输入两个整数并返回它们的和,这是最基础的例子,目的是熟悉输入输出和变量操作。

```c
#include 

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("Sum: %d\n", a + b);
    return 0;
}
  1. 判断奇偶数 输入一个整数,判断是否为偶数。这个例子帮助理解条件判断语句if-else的用法。
#include 

int main() {
    int num;
    scanf("%d", &num);
    if (num % 2 == 0) {
        printf("偶数\n");
    } else {
        printf("奇数\n");
    }
    return 0;
}

二、数组与循环

  1. 数组求最大值 输入一组数,输出数组中的最大值,常用来练习数组遍历和循环。
#include 

int main() {
    int array[10];
    int max, i;
    for (i = 0; i < 10; i++) {
        scanf("%d", &array[i]);
    }
    max = array[0];
    for (i = 1; i < 10; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }
    printf("Max value: %d\n", max);
    return 0;
}
  1. 冒泡排序 实现冒泡排序算法,是理解基础排序的一个关键案例,也便于掌握嵌套循环。
#include 

void bubbleSort(int arr[], int n) {
    int i, j, temp;
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int array[5];
    int i;
    for (i = 0; i < 5; i++) {
        scanf("%d", &array[i]);
    }
    bubbleSort(array, 5);
    for (i = 0; i < 5; i++) {
        printf("%d ", array[i]);
    }
    return 0;
}

三、函数与指针

  1. 交换两个变量的值 使用函数和指针实现两数的交换,直观了解指针的作用。
#include 

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x, y;
    scanf("%d %d", &x, &y);
    swap(&x, &y);
    printf("Swapped: %d %d\n", x, y);
    return 0;
}
  1. 阶乘计算 利用递归函数计算阶乘,帮助学生理解递归的概念与使用。
#include 

int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int num;
    scanf("%d", &num);
    printf("Factorial: %d\n", factorial(num));
    return 0;
}

四、文件操作与结构体

  1. 读写文件 基本文件写入和读取操作,是进行文件处理的基础。
#include 

int main() {
    FILE *file;
    char str[100];
    file = fopen("example.txt", "w");
    fprintf(file, "Hello, World!\n");
    fclose(file);
    file = fopen("example.txt", "r");
    fscanf(file, "%s", str);
    printf("%s\n", str);
    fclose(file);
    return 0;
}
  1. 结构体使用 定义并使用结构体来存储和处理数据,适合含有多个属性的对象表达。
#include 

struct Person {
    char name[50];
    int age;
};

int main() {
    struct Person p1 = {"Alice", 25};
    printf("Name: %s, Age: %d\n", p1.name, p1.age);
    return 0;
}

五、综合能力提升

  1. 判断素数 判断一个大于1的自然数是否是素数,算法中常包含循环和一些数学原理。
#include 
#include 

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 number;
    scanf("%d", &number);
    if (isPrime(number)) {
        printf("Prime\n");
    } else {
        printf("Not Prime\n");
    }
    return 0;
}
  1. 链表的基本操作 使用链表数据结构,创建、插入和删除节点,是深入数据结构的入门案例。
#include 
#include 

struct Node {
    int data;
    struct Node* next;
};

void append(struct Node** head, int newData) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    struct Node* last = *head;
    newNode->data = newData;
    newNode->next = NULL;
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = newNode;
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main() {
    struct Node* head = NULL;
    append(&head, 10);
    append(&head, 20);
    printList(head);
    return 0;
}

结语

上述只是C语言经典100例的精选部分,这些案例涵盖了基础语法、逻辑控制、函数指针、结构体、文件操作和链表等知识点。坚持练习和深入理解每一个案例,提升算法设计与编程能力,在学习C语言的道路上定会受益匪浅。

c语言经典程序100例
目录结构
全文
linux运维工具推荐

Linux工具推荐:

支持一键换源/安装宝塔/1p/系统优化等,运维好帮手!Github开源工具,欢迎star~

https://cb2.cn/helpcontent/230.html

(开源地址:https://github.com/JiaP/cb2cn

---------------------------------------

邀请好友注册购买可获得高额佣金!

点击立即开通推介计划!

不二云计算不二云 B站视频创作奖励计划

查看详情 关闭
linux运维工具推荐