特级茶叶 2023-04-03 18:51 采纳率: 78.6%
浏览 50
已结题

嵌入式设计linux

请用C语言编写主程序程序,在虚i拟机调试,题目如下图,请按题目要求编写,并给出程序

img

img

img

  • 写回答

4条回答 默认 最新

  • 社区专家-Monster-XH 2023-04-03 18:56
    关注

    基于Monster 组和GPT的调写:
    1.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        // 打开File.txt文件进行读取
        FILE *fp;
        fp = fopen("File.txt", "r");
        if (fp == NULL) {
            printf("Failed to open file.\n");
            return -1;
        }
    
        // 统计各个字符类型的数量
        int upper_count = 0;
        int lower_count = 0;
        int digit_count = 0;
        char c;
        while ((c = fgetc(fp)) != EOF) {
            if (c >= 'A' && c <= 'Z') {
                upper_count++;
            } else if (c >= 'a' && c <= 'z') {
                lower_count++;
            } else if (c >= '0' && c <= '9') {
                digit_count++;
            }
        }
    
        // 关闭File.txt文件
        fclose(fp);
    
        // 将读取的内容和统计数据写入新文件
        char student_info[50] = "5dLe63 Zhang San.txt";
        fp = fopen(student_info, "w");
        if (fp == NULL) {
            printf("Failed to create new file.\n");
            return -1;
        }
    
        // 打开File.txt文件进行读取
        fp = fopen("File.txt", "r");
        if (fp == NULL) {
            printf("Failed to open file.\n");
            return -1;
        }
    
        // 将File.txt文件的内容写入新文件
        while ((c = fgetc(fp)) != EOF) {
            fputc(c, fp);
        }
    
        // 将统计数据写入新文件
        fprintf(fp, "\ncapital letter:%d small letter:%d\n", upper_count, lower_count);
        fprintf(fp, "number letter:%d\n", digit_count);
    
        // 关闭File.txt和新文件
        fclose(fp);
    
        return 0;
    }
    
    
    

    2.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_PRODUCTS 100 // 定义最大商品数量
    
    typedef struct Product {
        char name[100];
        double price;
        int quantity;
    } Product;
    
    void print_product(Product p) {
        printf("%s\t%.2lf\t%d\n", p.name, p.price, p.quantity);
    }
    
    void read_products(Product products[], int *count) {
        FILE *fp;
        fp = fopen("a.txt", "r"); // 打开文件
    
        if (fp == NULL) {
            printf("Failed to open file\n");
            exit(1);
        }
    
        char line[100];
        char *token;
    
        while (fgets(line, sizeof(line), fp) != NULL) {
            Product p;
            token = strtok(line, ",");
            strcpy(p.name, token);
            token = strtok(NULL, ",");
            p.price = atof(token);
            token = strtok(NULL, ",");
            p.quantity = atoi(token);
            products[*count] = p;
            (*count)++;
        }
    
        fclose(fp);
    }
    
    double calculate_total_price(Product products[], int count) {
        double total = 0;
        for (int i = 0; i < count; i++) {
            total += products[i].price * products[i].quantity;
        }
        return total;
    }
    
    void write_products(Product products[], int count) {
        FILE *fp;
        fp = fopen("a.txt", "a"); // 打开文件,添加内容
    
        if (fp == NULL) {
            printf("Failed to open file\n");
            exit(1);
        }
    
        for (int i = 0; i < count; i++) {
            fprintf(fp, "%s,%.2lf,%d\n", products[i].name, products[i].price, products[i].quantity);
        }
    
        fclose(fp);
    }
    
    void add_product(Product products[], int *count) {
        Product p;
        printf("Enter product name: ");
        scanf("%s", p.name);
        printf("Enter product price: ");
        scanf("%lf", &p.price);
        printf("Enter product quantity: ");
        scanf("%d", &p.quantity);
        products[*count] = p;
        (*count)++;
    }
    
    int main() {
        Product products[MAX_PRODUCTS];
        int count = 0;
    
        read_products(products, &count);
    
        double total_price = calculate_total_price(products, count);
        printf("Total price: %.2lf\n", total_price);
    
        int choice;
        printf("1. Add product\n");
        printf("2. Exit\n");
        scanf("%d", &choice);
    
        if (choice == 1) {
            add_product(products, &count);
            write_products(products, count);
            total_price = calculate_total_price(products, count);
            printf("Total price: %.2lf\n", total_price);
        }
    
        return 0;
    }
    
    
    

    3.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Employee {
        int id;
        char name[20];
        int age;
        float salary;
    };
    
    void input_employee_data(struct Employee *employee) {
        printf("请输入员工号码:");
        scanf("%d", &employee->id);
        printf("请输入员工姓名:");
        scanf("%s", employee->name);
        printf("请输入员工年龄:");
        scanf("%d", &employee->age);
        printf("请输入员工工资:");
        scanf("%f", &employee->salary);
    }
    
    void save_employee_data(FILE *file, struct Employee *employee) {
        fprintf(file, "%d %s %d %.2f\n", employee->id, employee->name, employee->age, employee->salary);
    }
    
    void print_employee_data(struct Employee *employee) {
        printf("%d %s %d %.2f\n", employee->id, employee->name, employee->age, employee->salary);
    }
    
    int main() {
        FILE *file = fopen("f1.dat", "a+");
        if (file == NULL) {
            printf("打开文件失败");
            return 1;
        }
    
        struct Employee employee;
        char choice = 'y';
        while (choice == 'y') {
            input_employee_data(&employee);
            save_employee_data(file, &employee);
    
            printf("是否继续输入员工信息(y/n)?");
            scanf(" %c", &choice);
        }
    
        rewind(file);
    
        printf("所有员工信息如下:\n");
        while (fscanf(file, "%d %s %d %f", &employee.id, employee.name, &employee.age, &employee.salary) != EOF) {
            print_employee_data(&employee);
        }
    
        fclose(file);
    
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 创建了问题 4月3日

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择