特级茶叶 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日

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备