C语言入门(9)--完结篇

这篇,咱们结合几个案例,综合运用一下C语言的部分知识。

废话不多说,直接上代码!

题目1:字符串处理与动态内存分配

场景:编写一个程序,实现一个简单的文本编辑器,用户可以输入一段文本,程序将文本复制到另一个字符串中,并在末尾添加一个新字符串,然后输出最终结果。

要求

  1. 使用malloc动态分配内存以存储用户输入的文本。
  2. 使用字符串处理函数strcpystrcat来操作字符串。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    
    
    char *input, *result, *appendText = " - Appended Text";
    printf("Enter a string: ");
    getline(input, 100);
    size_t len = strlen(input) + strlen(appendText) + 1;
    result = (char *)malloc(len);
    if (result == NULL) {
    
    
        perror("malloc failed");
        exit(EXIT_FAILURE);
    }
    strcpy(result, input);
    strcat(result, appendText);
    printf("Resulting string: %s\n", result);
    free(result);
    return 0;
}

题目2:结构体与文件操作

场景:在一个学校中,需要将学生的信息存储到文件中,并能够从文件中读取。

要求

  1. 定义一个结构体来存储学生的信息。
  2. 实现文件的写入和读取操作。
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    
    
    char name[50];
    int age;
    float scores[3];
} Student;

void saveStudent(const Student stu, const char *filename);
Student loadStudent(const char *filename);

int main() {
    
    
    Student stu = {
    
    "John Doe", 20, {
    
    85, 90, 75}};
    const char *filename = "student.dat";
    saveStudent(stu, filename);
    Student loadedStu = loadStudent(filename);
    printf("Loaded Student: %s, Age: %d, Scores: %.2f, %.2f, %.2f\n", loadedStu.name, loadedStu.age, loadedStu.scores[0], loadedStu.scores[1], loadedStu.scores[2]);
    return 0;
}

void saveStudent(const Student stu, const char *filename) {
    
    
    FILE *file = fopen(filename, "w");
    if (file == NULL) {
    
    
        perror("File open failed");
        exit(EXIT_FAILURE);
    }
    fprintf(file, "%s %d", stu.name, stu.age);
    for (int i = 0; i < 3; i++) {
    
    
        fprintf(file, " %f", stu.scores[i]);
    }
    fclose(file);
}

Student loadStudent(const char *filename) {
    
    
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
    
    
        perror("File open failed");
        exit(EXIT_FAILURE);
    }
    Student stu;
    fscanf(file, "%s %d", stu.name, &stu.age);
    for (int i = 0; i < 3; i++) {
    
    
        fscanf(file, "%f", &stu.scores[i]);
    }
    fclose(file);
    return stu;
}

题目3:指针与数组

场景:在一个图书馆中,需要通过书架上书籍的索引来查找特定书籍。

要求

  1. 使用指针来遍历数组。
  2. 实现一个函数,通过索引找到并返回书籍的名称。
#include <stdio.h>

#define MAX_BOOKS 100

int main() {
    
    
    char *books[MAX_BOOKS] = {
    
    "Book1", "Book2", "Book3", "Book4"};
    int index;
    printf("Enter the index of the book to find: ");
    scanf("%d", &index);
    if (index >= 0 && index < MAX_BOOKS) {
    
    
        printf("Book at index %d is: %s\n", index, books[index]);
    } else {
    
    
        printf("Invalid index.\n");
    }
    return 0;
}

题目4:递归与数学计算

场景:计算斐波那契数列的第n项值。

要求

  1. 使用递归函数来计算斐波那契数列。
#include <stdio.h>

int fibonacci(int n) {
    
    
    if (n <= 1) {
    
    
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    
    
    int n;
    printf("Enter the term number for Fibonacci sequence: ");
    scanf("%d", &n);
    printf("Fibonacci of %d is %d\n", n, fibonacci(n));
    return 0;
}

题目5:排序算法与数组操作

场景:在一个学校中,需要对学生的成绩进行排序,以便颁发奖学金。

要求

  1. 实现一个排序算法,对学生的成绩进行降序排序。
  2. 使用数组来存储学生的成绩。
#include <stdio.h>

void sortDescending(int arr[], int size) {
    
    
    for (int i = 0; i < size - 1; i++) {
    
    
        for (int j = 0; j < size - 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 scores[] = {
    
    85, 90, 78, 92, 76};
    int size = sizeof(scores) / sizeof(scores[0]);
    sortDescending(scores, size);
    printf("Sorted scores in descending order: ");
    for (int i = 0; i < size; i++) {
    
    
        printf("%d ", scores[i]);
    }
    return 0;
}

这些题目覆盖了C语言中的多个实际应用场景,适合用来练习和提高C语言编程能力。

当然,这几个题目全当抛砖引玉,还有很多更好的题目,等待大家主动的发掘。

C语言的相关分享,咱们到这告一段落。

第一次写博客,选择C语言开始,也发现很多在语言表达方面的不足,特别感谢各位看官的耐心包容和支持,感激不尽!

期待继续和你分享其他语言的知识,更多编程的心得,生产力工具的发掘,还有很多暂时还没想到的故事。

感谢你的阅读

咱们下一篇见!

猜你喜欢

转载自blog.csdn.net/Chen7Chan/article/details/143352004