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

#define MAX_DEVICES 100 // 最大设备数量 #define MAX_USERS 100 // 最大用户数量

// 设备信息结构体 typedef struct { char name[50]; char model[50]; char purchase_date[20]; char location[100]; char owner[50]; char borrower[50]; char borrow_date[20]; char return_date[20]; } Device;

// 用户信息结构体 typedef struct { char username[50]; char password[50]; } User;

Device devices[MAX_DEVICES]; // 设备数组 int numDevices = 0; // 当前设备数量

User users[MAX_USERS]; // 用户数组 int numUsers = 0; // 当前用户数量

// 添加设备 void addDevice() { if (numDevices >= MAX_DEVICES) { printf('设备数量已达到上限,无法添加新设备。\n'); return; }

Device newDevice;

printf('请输入设备名称:');
scanf('%s', newDevice.name);
printf('请输入设备型号:');
scanf('%s', newDevice.model);
printf('请输入设备采购时间:');
scanf('%s', newDevice.purchase_date);
printf('请输入设备存放位置:');
scanf('%s', newDevice.location);
printf('请输入设备负责人:');
scanf('%s', newDevice.owner);

devices[numDevices++] = newDevice;

printf('设备添加成功。\n');

}

// 修改设备 void modifyDevice() { char name[50]; printf('请输入要修改的设备名称:'); scanf('%s', name);

int i;
for (i = 0; i < numDevices; i++) {
    if (strcmp(devices[i].name, name) == 0) {
        printf('请输入新的设备名称:');
        scanf('%s', devices[i].name);
        printf('请输入新的设备型号:');
        scanf('%s', devices[i].model);
        printf('请输入新的设备采购时间:');
        scanf('%s', devices[i].purchase_date);
        printf('请输入新的设备存放位置:');
        scanf('%s', devices[i].location);
        printf('请输入新的设备负责人:');
        scanf('%s', devices[i].owner);

        printf('设备信息修改成功。\n');
        return;
    }
}

printf('未找到要修改的设备。\n');

}

// 删除设备 void deleteDevice() { char name[50]; printf('请输入要删除的设备名称:'); scanf('%s', name);

int i;
for (i = 0; i < numDevices; i++) {
    if (strcmp(devices[i].name, name) == 0) {
        int j;
        for (j = i; j < numDevices - 1; j++) {
            devices[j] = devices[j + 1];
        }
        numDevices--;

        printf('设备删除成功。\n');
        return;
    }
}

printf('未找到要删除的设备。\n');

}

// 添加用户 void addUser() { if (numUsers >= MAX_USERS) { printf('用户数量已达到上限,无法添加新用户。\n'); return; }

User newUser;

printf('请输入用户名:');
scanf('%s', newUser.username);
printf('请输入密码:');
scanf('%s', newUser.password);

users[numUsers++] = newUser;

printf('用户添加成功。\n');

}

// 删除用户 void deleteUser() { char username[50]; printf('请输入要删除的用户名:'); scanf('%s', username);

int i;
for (i = 0; i < numUsers; i++) {
    if (strcmp(users[i].username, username) == 0) {
        int j;
        for (j = i; j < numUsers - 1; j++) {
            users[j] = users[j + 1];
        }
        numUsers--;

        printf('用户删除成功。\n');
        return;
    }
}

printf('未找到要删除的用户。\n');

}

// 查询设备 void searchDevice() { char name[50]; printf('请输入要查询的设备名称:'); scanf('%s', name);

int i;
for (i = 0; i < numDevices; i++) {
    if (strcmp(devices[i].name, name) == 0) {
        printf('设备名称:%s\n', devices[i].name);
        printf('设备型号:%s\n', devices[i].model);
        printf('设备采购时间:%s\n', devices[i].purchase_date);
        printf('设备存放位置:%s\n', devices[i].location);
        printf('设备负责人:%s\n', devices[i].owner);
        if (strlen(devices[i].borrower) > 0) {
            printf('设备借用人:%s\n', devices[i].borrower);
            printf('设备借用时间:%s\n', devices[i].borrow_date);
            printf('设备归还时间:%s\n', devices[i].return_date);
        }
        return;
    }
}

printf('未找到该设备。\n');

}

// 申请借用设备 void borrowDevice() { char name[50]; printf('请输入要借用的设备名称:'); scanf('%s', name);

int i;
for (i = 0; i < numDevices; i++) {
    if (strcmp(devices[i].name, name) == 0) {
        if (strlen(devices[i].borrower) > 0) {
            printf('该设备已被借用。\n');
            return;
        }

        printf('请输入借用人姓名:');
        scanf('%s', devices[i].borrower);
        printf('请输入借用日期:');
        scanf('%s', devices[i].borrow_date);

        printf('设备借用成功。\n');
        return;
    }
}

printf('未找到该设备。\n');

}

// 显示所有设备 void displayDevices() { int i; for (i = 0; i < numDevices; i++) { printf('设备名称:%s\n', devices[i].name); printf('设备型号:%s\n', devices[i].model); printf('设备采购时间:%s\n', devices[i].purchase_date); printf('设备存放位置:%s\n', devices[i].location); printf('设备负责人:%s\n', devices[i].owner); if (strlen(devices[i].borrower) > 0) { printf('设备借用人:%s\n', devices[i].borrower); printf('设备借用时间:%s\n', devices[i].borrow_date); printf('设备归还时间:%s\n', devices[i].return_date); } printf('\n'); } }

// 保存用户信息和设备信息到文件 void saveToFile() { { FILE* file = fopen('C:\Users\额额\Desktop\cvb\789', 'w'); if (file == NULL) { printf('无法打开文件。\n'); return; }

// 保存用户信息
int i;
for (i = 0; i < numUsers; i++) 
{
    fprintf(file, '%s %s\n', users[i].username, users[i].password);
}

// 保存设备信息
for (i = 0; i < numDevices; i++) {
    fprintf(file, '%s %s %s %s %s %s %s %s\n', devices[i].name, devices[i].model, devices[i].purchase_date,devices[i].location, devices[i].owner, devices[i].borrower, devices[i].borrow_date, devices[i].return_date);
}

fclose(file);

} } // 从文件中加载用户信息和设备信息

void loadFromFile() { FILE* file = fopen('C:\Users\额额\Desktop\cvb\789', 'r'); if (file == NULL) { printf('无法打开文件。\n'); return; }

// 加载用户信息
char line[100];
while (fgets(line, sizeof(line), file) != NULL) {
    sscanf(line, '%s %s', users[numUsers].username, users[numUsers].password);
    numUsers++;
}

// 加载设备信息
while (fgets(line, sizeof(line), file) != NULL) {
    sscanf(line, '%s %s %s %s %s %s %s %s', devices[numDevices].name, devices[numDevices].model,
        devices[numDevices].purchase_date, devices[numDevices].location, devices[numDevices].owner,
        devices[numDevices].borrower, devices[numDevices].borrow_date, devices[numDevices].return_date);
    numDevices++;
}

fclose(file);

} int main() { loadFromFile();

int choice;
do {
    printf('欢迎使用设备管理系统,请选择操作:\n');
    printf('1. 添加设备\n');
    printf('2. 修改设备\n');
    printf('3. 删除设备\n');
    printf('4. 添加用户\n');
    printf('5. 删除用户\n');
    printf('6. 查询设备\n');
    printf('7. 申请借用设备\n');
    printf('8. 显示所有设备\n');
    printf('0. 退出\n');

    scanf('%d', &choice);

    switch (choice) {
    case 1:
        addDevice();
        break;
    case 2:
        modifyDevice();
        break;
    case 3:
        deleteDevice();
        break;
    case 4:
        addUser();
        break;
    case 5:
        deleteUser();
        break;
    case 6:
        searchDevice();
        break;
    case 7:
        borrowDevice();
        break;
    case 8:
        displayDevices();
        break;
    case 0:
        break;
    default:
        printf('无效的选择。\n');
    }
} while (choice != 0);

saveToFile();

return 0;

}为什么无法显示先前录入文件的设备信息以及用户信息内容:根据代码分析,loadFromFile函数是用来从文件中加载用户信息和设备信息的,该函数在程序开始时被调用,将文件中的信息读取到相应的数组中。然后,在程序结束时,saveToFile函数会将用户信息和设备信息保存到文件中。

如果无法显示先前录入文件的设备信息以及用户信息,可能是以下原因之一:

  1. 文件路径不正确:请确保文件路径是正确的,可以尝试使用绝对路径或相对路径。
  2. 文件内容格式错误:请确保文件中的用户信息和设备信息按照正确的格式存储,每行数据的格式应与保存时一致。
  3. 文件权限问题:请确保程序对文件有读取和写入的权限,可以尝试更改文件的权限。

如果仍然无法解决问题,请检查代码中的其他部分,确保没有其他地方修改或重置了设备信息和用户信息的数组。另外,也可以在loadFromFile函数和saveToFile函数中添加一些调试信息,以便查看读取和保存的过程中是否出现了错误。

设备管理系统:高效管理您的设备和用户信息

原文地址: https://www.cveoy.top/t/topic/psi4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录