aaaaaaaa

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Device {
    char name[50];
    char model[20];
    float price;
    int quantity;
    struct Device* next;
};

struct Device* head = NULL;

// 保存数据到文件中
void saveDataToFile() {
    FILE* fp = fopen("devices.txt", "w");
    struct Device* cur = head;
    while (cur != NULL) {
        fprintf(fp, "%s,%s,%.2f,%d\n", cur->name, cur->model, cur->price, cur->quantity);
        cur = cur->next;
    }
    fclose(fp);
}

// 从文件中读取数据
void loadDataFromFile() {
    head = NULL;
    FILE* fp = fopen("devices.txt", "r");
    if (fp == NULL) {
        return;
    }
    char line[1024];
    while (fgets(line, sizeof(line), fp)) {
        struct Device* device = (struct Device*)malloc(sizeof(struct Device));
        sscanf(line, "%[^,],%[^,],%f,%d\n", device->name, device->model, &device->price, &device->quantity);
        device->next = head;
        head = device;
    }
    fclose(fp);
}

// 录入数据
void addDevice() {
    struct Device* device = (struct Device*)malloc(sizeof(struct Device));
    printf("Enter device name: ");
    scanf("%s", device->name);
    printf("Enter device model: ");
    scanf("%s", device->model);
    printf("Enter device price: ");
    scanf("%f", &device->price);
    printf("Enter device quantity: ");
    scanf("%d", &device->quantity);
    device->next = head;
    head = device;
    printf("Device added successfully!\n");
}

// 显示所有设备
void showAllDevices() {
    struct Device* cur = head;
    printf("%-50s%-20s%10s%10s\n", "Device Name", "Model", "Price", "Quantity");
    while (cur != NULL) {
        printf("%-50s%-20s%10.2f%10d\n", cur->name, cur->model, cur->price, cur->quantity);
        cur = cur->next;
    }
}

// 修改设备
void updateDevice() {
    char name[50], model[20];
    printf("Enter device name to update: ");
    scanf("%s", name);
    printf("Enter device model to update: ");
    scanf("%s", model);
    struct Device* cur = head;
    while (cur != NULL) {
        if (strcmp(cur->name, name) == 0 && strcmp(cur->model, model) == 0) {
            printf("Enter new device name: ");
            scanf("%s", cur->name);
            printf("Enter new device model: ");
            scanf("%s", cur->model);
            printf("Enter new device price: ");
            scanf("%f", &cur->price);
            printf("Enter new device quantity: ");
            scanf("%d", &cur->quantity);
            printf("Device updated successfully!\n");
            return;
        }
        cur = cur->next;
    }
    printf("Device not found!\n");
}

// 删除设备
void deleteDevice() {
    char name[50], model[20];
    printf("Enter device name to delete: ");
    scanf("%s", name);
    printf("Enter device model to delete: ");
    scanf("%s", model);
    struct Device* cur = head;
    struct Device* prev = NULL;
    while (cur != NULL) {
        if (strcmp(cur->name, name) == 0 && strcmp(cur->model, model) == 0) {
            if (prev == NULL) {
                head = cur->next;
            }
            else {
                prev->next = cur->next;
            }
            free(cur);
            printf("Device deleted successfully!\n");
            return;
        }
        prev = cur;
        cur = cur->next;
    }
    printf("Device not found!\n");
}

// 查询设备
void searchDevice() {
    char keyword[50];
    printf("Enter keyword to search: ");
    scanf("%s", keyword);
    struct Device* cur = head;
    printf("%-50s%-20s%10s%10s\n", "Device Name", "Model", "Price", "Quantity");
    while (cur != NULL) {
        if (strstr(cur->name, keyword) != NULL || strstr(cur->model, keyword) != NULL) {
            printf("%-50s%-20s%10.2f%10d\n", cur->name, cur->model, cur->price, cur->quantity);
        }
        cur = cur->next;
    }
}

// 按价格升序排序
void sortByPriceAsc() {
    int swapped = 1;
    while (swapped) {
        swapped = 0;
        struct Device* cur = head;
        while (cur != NULL && cur->next != NULL) {
            if (cur->price > cur->next->price) {
                swapped = 1;
                struct Device* tmp = cur->next;
                cur->next = cur->next->next;
                tmp->next = cur;
                if (cur == head) {
                    head = tmp;
                }
                else {
                    struct Device* prev = head;
                    while (prev->next != cur) {
                        prev = prev->next;
                    }
                    prev->next = tmp;
                }
                cur = tmp;
            }
            cur = cur->next;
        }
    }
}

// 按价格降序排序
void sortByPriceDesc() {
    int swapped = 1;
    while (swapped) {
        swapped = 0;
        struct Device* cur = head;
        while (cur != NULL && cur->next != NULL) {
            if (cur->price < cur->next->price) {
                swapped = 1;
                struct Device* tmp = cur->next;
                cur->next = cur->next->next;
                tmp->next = cur;
                if (cur == head) {
                    head = tmp;
                }
                else {
                    struct Device* prev = head;
                    while (prev->next != cur) {
                        prev = prev->next;
                    }
                    prev->next = tmp;
                }
                cur = tmp;
            }
            cur = cur->next;
        }
    }
}

// 统计设备数量
int countDevices() {
    int count = 0;
    struct Device* cur = head;
    while (cur != NULL) {
        count++;
        cur = cur->next;
    }
    return count;
}

// 统计设备总价值
float sumDevicePrices() {
    float sum = 0;
    struct Device* cur = head;
    while (cur != NULL) {
        sum += cur->price * cur->quantity;
        cur = cur->next;
    }
    return sum;
}

// 打印设备报表
void printDeviceReport() {
    FILE* fp = fopen("device_report.txt", "w");
    fprintf(fp, "%-50s%-20s%10s%10s\n", "Device Name", "Model", "Price", "Quantity");
    struct Device* cur = head;
    while (cur != NULL) {
        fprintf(fp, "%-50s%-20s%10.2f%10d\n", cur->name, cur->model, cur->price, cur->quantity);
        cur = cur->next;
    }
    fprintf(fp, "\nTotal number of devices: %d\n", countDevices());
    fprintf(fp, "Total value of devices: %.2f\n", sumDevicePrices());
    fclose(fp);
}

// 显示菜单
void showMenu() {
    printf("============== Equipment Management System ==============\n");
    printf("1. Add device\n");
    printf("2. Show all devices\n");
    printf("3. Update device\n");
    printf("4. Delete device\n");
    printf("5. Search device\n");
    printf("6. Sort by price (ascending order)\n");
    printf("7. Sort by price (descending order)\n");
    printf("8. Print device report\n");
    printf("0. Exit\n");
}

int main() {
    loadDataFromFile();
    while (1) {
        showMenu();
        int choice;
        printf("Enter your choice: ");
        scanf_s("%d", &choice);
        switch (choice) {
        case 1:
            addDevice();
            break;
        case 2:
            showAllDevices();
            break;
        case 3:
            updateDevice();
            break;
        case 4:
            deleteDevice();
            break;
        case 5:
            searchDevice();
            break;
        case 6:
            sortByPriceAsc();
            printf("Devices sorted by price (ascending order)!\n");
            break;
        case 7:
            sortByPriceDesc();
            printf("Devices sorted by price (descending order)!\n");
            break;
        case 8:
            printDeviceReport();
            printf("Device report printed successfully!\n");
            break;
        case 0:
            saveDataToFile();
            printf("Exiting...\n");
            return 0;
        default:
            printf("Invalid choice!\n");
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值