参考资料:
https://blog.csdn.net/u012534008/article/details/54601045
http://www.cnblogs.com/smyhvae/p/4148458.html
https://blog.csdn.net/qq_41226029/article/details/79438131
编译环境:VS 2017
EEMS.h
#ifndef EEMS
#include
#include
#include
#include
#include
#define N sizeof(struct EQUIPMENT)
/*
*结构体定义
*/
struct EQUIPMENT
{
char number[11]; //登录号
char name[11]; //设备名
char type[11]; //设备类型
char price[11]; //设备价格
char in_time[11]; //购入时间
char status[11]; //设备状态,1:正常 ; 0:报废
char out_time[11]; //报废时间
int x;
struct EQUIPMENT *next; //指向结构体的指针
};
typedef struct EQUIPMENT Equipment;
typedef Equipment *equipment;
/*
*设置信息
*/
void menu(); //主菜单
void color(short x); //颜色设置
void HideCursor(); //隐藏光标
void toxy(short x, short y); //光标移动
void find_equipment(); //设备查询菜单栏
void over(); //退出EEMS
/*
*设备操作
*/
void input_library(); //设备入库
void amend_equipment(); //修改设备信息
void del_equipment(); //删除设备信息
void print_equipment(); //显示全部设备
void find_name_equipment(); //按设备名查询
void find_type_equipment(); //按设备类型查询
void find_number_equipment(); //按设备编号查询
/*
*文件操作
*/
void save_equipment(equipment p); //存储设备信息
equipment read_file();
void empty_file();
#endif // !EEMS
setting.cpp
#include"EEMS.h"
void menu() //菜单
{
do
{
system("cls"); //清屏
system("mode con cols=150 lines=40"); //设置屏幕大小
HideCursor(); //隐藏光标
color(15); //设置一个好看的颜色
char t;
toxy(50, 5);//将光标移动到(50,5)坐标处
printf("\t\t 实验设备管理系统");
toxy(48, 8);
printf("\t\t| 1.设备入库 |");
toxy(48, 10);
printf("\t\t| 2.修改信息 |");
toxy(48, 12);
printf("\t\t| 3.删除信息 |");
toxy(48, 14);
printf("\t\t| 4.设备查询 |");
toxy(48, 16);
printf("\t\t| 5.设备总览 |");
toxy(48, 18);
printf("\t\t| 6.退出软件 |");
t = _getch(); //不回显函数
switch (t)
{
case '1':input_library(); break;
case '2':amend_equipment(); break;
case '3':del_equipment(); break;
case '4':find_equipment(); break;
case '5':print_equipment(); break;
case '6':over(); break;
default:break;
}
} while (1);
}
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void toxy(short x, short y) //将光标移动到X,Y坐标处
{
COORD pos = { x , y };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void color(short x)
{
if (x >= 0 && x <= 15)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
}
/*
*图书查询菜单栏
*/
void find_equipment() //查询图书
{
do
{
system("cls"); //清屏
color(8);
char t;
toxy(50, 5);
printf("\t设备查询");
toxy(48, 8);
printf("| 1.设备名查询 |");
toxy(48, 10);
printf("| 2.登录号查询 |");
toxy(50, 12);
printf("请键入[1--2]选择查询方式,键入数字[0]返回主菜单");
t = _getch();
switch (t)
{
case '0':menu(); break;
case '1':find_name_equipment(); break;
case '2':find_number_equipment(); break;
default:break;
}
} while (1);
}
/*
*退出BMS
*/
void over() //退出软件
{
char t;
toxy(48, 11);
printf("-----------------------");
toxy(48, 12);
printf("| 您确定要退出吗? |");
toxy(48, 14);
printf("| 1.确定 2.取消 |");
toxy(48, 15);
printf("-----------------------");
while (1)
{
t = _getch(); //输入t
switch (t)
{
case '1':
system("cls");
color(6);
toxy(48, 10);
printf("正在安全退出....");
Sleep(1000); //暂停1秒
system("cls");
color(8);
toxy(48, 10);
printf("已安全退出软件");
toxy(48, 12);
printf("谢谢使用!");
toxy(48, 14);
printf("by-by^_^");
exit(0); break; //终止程序
case '2':
menu(); break; //调用函数,进入菜单
default:break;
}
}
}
equipment_operation.cpp
#include"EEMS.h"
/*
*设备入库
*/
void input_library()
{
do
{
system("cls");
color(10);
char t;
equipment p;
p = (equipment)malloc(N); //申请空间
//输入设备信息
toxy(48, 8);
printf("请输入设备登录号(小于10位数):");
scanf_s("%s", p->number, 10); getchar();
toxy(48, 10);
printf("请输入设备名(小于10位数):");
scanf_s("%s", p->name, 10); getchar();
toxy(48, 12);
printf("请输入设备类型(小于10位数):");
scanf_s("%s", p->type, 10); getchar();
toxy(48, 14);
printf("请输入设备价格(小于10位数):");
scanf_s("%s", p->price,10); getchar();
toxy(48, 16);
printf("请输入设备购入时间(小于10位数)[年月日] :");
scanf_s("%s", p->in_time, 10); getchar();
toxy(48, 18);
printf("请输入设备状态[1:正常 0:报废] :");
scanf_s("%s", p->status, 10); getchar();
toxy(48, 20);
printf("请输入设备报废时间(若设备正常则填'#')[年月日] :");
scanf_s("%s", &p->out_time,10); getchar();
toxy(48, 22);
save_equipment(p);
toxy(48, 24);
printf("正在保存....");
Sleep(1000); //暂停1秒
system("cls");
toxy(46, 8);
printf("-------------------------");
toxy(46, 9);
printf("| |");
toxy(46, 10);
printf("| 保存成功!是否继续? |");
toxy(46, 12);
printf("| 1.是 2.否 |");
toxy(46, 13);
printf("| |");
toxy(46, 14);
printf("-------------------------");
while (1) //利用死循环可有效防止其他按键干扰
{
t = _getch();
if (t == '1')
{
break;
}
else if (t == '2')
{
menu();
}
}
} while (1);
}
/*
* 修改设备信息
*/
void amend_equipment()
{
do
{
system("cls");
color(10);
equipment head, p;
int i = 11, j = 0, x;
char ch, t;
FILE *fp; //文件指针
char _name[11];
char number[11]; //登录号
char name[11]; //设备名
char type[11]; //设备类型
char price[11]; //设备价格
char in_time[11]; //购入时间
char status[11]; //设备状态,1:正常 ; 0:报废
char out_time[11]; //报废时间
head = read_file();
p = head;
toxy(48, 10);
printf("请输入你要修改设备的设备名:");
gets_s(_name, 10);
while (p != NULL) //初始化p->x为0
{
p->x = 0;
p = p->next;
}
p = head; //让p重新指向表头
toxy(20, 5);
printf("***********************************************设备信息******************************************************");
toxy(20, 8);
printf("-------------------------------------------------------------------------------------------------------------");
toxy(20, 9);
printf("登录号 设备名 设备种类 设备价格 购入时间 设备状态 报废时间");
toxy(20, 10);
printf("-------------------------------------------------------------------------------------------------------------");
while (p != NULL)
{
if (p != NULL && strcmp(p->name, _name) == 0)
{
toxy(20, i);
j++;
printf("%s %s %14s %14s %14s %s %14s\n", p->number, p->name, p->type, p->price, p->in_time, p->status, p->out_time);
p->x = j; //给符合查询标准的结点标号
i++;
}
p = p->next;
}
if (j == 0) //如果j=0,即没有进入前面的搜索循环,也就是没有找到相应的信息
{
toxy(50, i);
printf("没有找到相应的信息!(按0返回,按1重新搜索)");
while (1) //死循环是为了防止除0和1的其他按键干扰
{
ch = _getch();
if (ch == '0')
{
menu(); break;
}
else if (ch == '1')
{
break;
}
}
if (ch == '1') //如果输入的ch等于1,则结束本次循环
continue;
}
while (1)
{
toxy(45, i);
printf("请输入您要修改的设备的编号:");
scanf_s("%d", &x); getchar();
if (x > j || x == 0)
{
toxy(45, ++i);
printf("输入错误,请重新输入!");
Sleep(500);
}
else
{
break;
}
}
p = head; //让p重新指向表头
while (p != NULL && p->x != x) //遍历链表查询符合条件的结点
{
p = p->next;
}
if (p) //如果p不为空
{
system("cls");
//输入要修改的信息
toxy(48, 8);
printf("请输入设备登录号(小于10位数):");
scanf_s("%s", &number, 10); getchar(); strcpy_s(p->number, 10, number);
toxy(48, 10);
printf("请输入设备名(小于10位数):");
scanf_s("%s", &name, 10); getchar(); strcpy_s(p->name, 10, name);
toxy(48, 12);
printf("请输入设备类型(小于10位数):");
scanf_s("%s", &type, 10); getchar(); strcpy_s(p->type, 10, type);
toxy(48, 14);
printf("请输入设备价格(小于10位数):");
scanf_s("%s", &price, 10); getchar(); strcpy_s(p->price, 10, price);
toxy(48, 16);
printf("请输入设备购入时间(小于10位数)[年/月/日]: ");
scanf_s("%s", &in_time, 10); getchar(); strcpy_s(p->in_time, 10, in_time);
toxy(48, 18);
printf("请输入设备状态[1:正常 0:报废]: ");
scanf_s("%s", &status, 10); getchar(); strcpy_s(p->status, 10, status);
toxy(48, 20);
printf("请输入设备报废时间(若设备正常则填'#')[年/月/日]: ");
scanf_s("%s", &out_time,10); getchar(); strcpy_s(p->out_time, 10, out_time);
}
color(7);
toxy(46, 8);
printf("-------------------------");
toxy(46, 9);
printf("| |");
toxy(46, 10);
printf("| 是否确认修改? |");
toxy(46, 12);
printf("| 1.是 2.否 |");
toxy(46, 13);
printf("| |");
toxy(46, 14);
printf("-------------------------");
while (1) //利用死循环防止其他按键干扰
{
t = _getch();
if (t == '1')
{
break;
}
else if (t == '2')
{
menu();
}
}
system("cls");
toxy(46, 10);
printf("正在修改,请稍后....");
fopen_s(&fp, "myequipment", "wb"); //以只写的方式打开名为myequipment的二进制文件,打开的同时清空文件中的内容
if (fp == NULL)
{
printf("cannot open file");
}
if (fwrite(head, N, 1, fp) != 1) //将head写入fp所指向的文件中
{
printf("write error!");
}
fclose(fp); //关闭文件
if (head != NULL) //如果head不为空
{
p = head->next; //让p指向第二个结点
fopen_s(&fp, "myequipment", "ab"); //以追加的方式打开文件
if (fp == NULL)
{
printf("cannot open file");
}
while (p != NULL)
{
if (fwrite(p, N, 1, fp) != 1)//将p写入fp所指向的文件中
{
printf("write error!");
}
p = p->next;
}
fclose(fp); //关闭文件
}
Sleep(500); //暂停0.5秒
system("cls");
toxy(46, 10);
printf("修改成功!即将自动返回主菜单....");
Sleep(500);
break;
} while (1);
menu();
}
/*
*删除设备信息
*/
void del_equipment() //删除信息
{
do
{
system("cls");
color(9);
FILE *fp;
equipment head = NULL;
equipment p = NULL;
equipment pre = NULL;
int j = 0, x, i = 11;
char name[11];
char t, c, ch;
head = read_file(); //调用函数,返回表头地址
toxy(48, 10);
printf("请输入你要删除设备的设备名:");
scanf_s("%s", name, 10);
p = head;
while (p != NULL)
{
p->x = 0;
p = p->next;
}
p = head;
toxy(20, 5);
printf("***********************************************设备信息******************************************************");
toxy(20, 8);
printf("-------------------------------------------------------------------------------------------------------------");
toxy(20, 9);
printf("登录号 设备名 设备种类 设备价格 购入时间 设备状态 报废时间");
toxy(20, 10);
printf("-------------------------------------------------------------------------------------------------------------");
while (p != NULL)
{
if (p != NULL && strcmp(p->name, name) == 0)
{
toxy(20, i);
j++;
printf("%s\t\t %s %14s %14s %14s\t\t\t%s %14s\n", p->number, p->name, p->type, p->price, p->in_time, p->status, p->out_time);
p->x = j;
i++;
}
p = p->next;
}
if (j == 0) //如果j=0,即没有进入前面的搜索循环,也就是没有找到相应的信息
{
toxy(50, i);
printf("没有找到相应的信息!(按0返回,按1重新搜索)");
while (1) //死循环是为了防止除0和1的其他按键干扰
{
ch = _getch();
if (ch == '0')
{
menu(); break;
}
else if (ch == '1')
{
break;
}
}
if (ch == '1') //如果输入的ch等于1,则结束本次循环
continue;
}
while (1)
{
toxy(45, i);
printf("请输入您要删除的设备的编号:");
scanf_s("%d", &x); getchar();
if (x > j || x == 0)
{
toxy(45, ++i);
printf("输入错误,请重新输入!");
Sleep(500);
}
else
{
break;
}
}
color(7);
toxy(46, 8);
printf("-------------------------");
toxy(46, 9);
printf("| |");
toxy(46, 10);
printf("| 是否确认删除? |");
toxy(46, 12);
printf("| 1.是 2.否 |");
toxy(46, 13);
printf("| |");
toxy(46, 14);
printf("-------------------------");
while (1)
{
t = _getch();
if (t == '1')
{
break;
}
else if (t == '2')
{
menu();
}
}
p = head;
while (p != NULL && p->x != x)
{
pre = p;
p = p->next;
}
if (p != NULL)
{
if (pre == NULL)
{
head = head->next;
}
else
{
pre->next = p->next;
}
}
free(p);
fopen_s(&fp, "myequipment", "wb");
if (fp == NULL)
{
printf("cannot open file");
}
if (fwrite(head, N, 1, fp) != 1)
{
printf("write error!");
}
fclose(fp);
if (head != NULL)
{
p = head->next;
fopen_s(&fp, "myequipment", "ab");
if (fp == NULL)
{
printf("cannot open file");
}
while (p != NULL)
{
if (fwrite(p, N, 1, fp) != 1)
{
printf("write error!");
}
p = p->next;
}
fclose(fp);
}
system("cls");
toxy(46, 10);
printf("正在删除,请稍后....");
Sleep(500);
system("cls");
toxy(46, 8);
printf("-------------------------");
toxy(46, 9);
printf("| |");
toxy(46, 10);
printf("| 删除成功,是否继续? |");
toxy(46, 12);
printf("| 1.是 2.否 |");
toxy(46, 13);
printf("| |");
toxy(46, 14);
printf("-------------------------");
while (1)
{
c = _getch();
if (c == '1')
{
break;
}
else if (c == '2')
{
menu();
}
}
} while (1);
}
/*
*显示全部设备信息
*/
void print_equipment()
{
system("cls");
color(6);
equipment head, p;
int i = 11;
int sum = 0;
head = read_file();
toxy(20, 5);
printf("***********************************************设备信息******************************************************");
toxy(20, 8);
printf("-------------------------------------------------------------------------------------------------------------");
toxy(20, 9);
printf("登录号 设备名 设备种类 设备价格 购入时间 设备状态 报废时间");
toxy(20, 10);
printf("-------------------------------------------------------------------------------------------------------------");
if (head == NULL)
{
toxy(45, 11);
printf("库中没有设备!(按任意键返回)");
getchar();
_getch();
menu();
}
p = head;
while (p != NULL)
{
toxy(20, i);
printf("%s\t\t %s %14s %14s %14s\t\t\t%s %14s\n", p->number, p->name, p->type, p->price, p->in_time, p->status, p->out_time);
i++;
//sum += p->num;//计算设备总量
p = p->next;
}
toxy(48, 7);
//printf("图书总量为:%d", sum);
toxy(45, i);
printf("按任意键返回");
getchar();
_getch();//不回显函数
menu();
}
void find_name_equipment() //按名字查询
{
system("cls");
color(8);
equipment head, p;
int i = 11;
head = read_file();
char name[11];
toxy(48, 8);
printf("请输入您要查询设备的设备名:");
gets_s(name, 10);
toxy(48, 10);
printf("正在查询....");
Sleep(500);
p = head;
toxy(20, 5);
printf("***********************************************设备信息******************************************************");
toxy(20, 8);
printf("-------------------------------------------------------------------------------------------------------------");
toxy(20, 9);
printf("登录号 设备名 设备种类 设备价格 购入时间 设备状态 报废时间");
toxy(20, 10);
printf("-------------------------------------------------------------------------------------------------------------");
while (p != NULL)
{
if (p != NULL && strcmp(p->name, name) == 0)
{
toxy(20, i);
printf("%s %s %14s %14s %14s %s %14s\n", p->number, p->name, p->type, p->price, p->in_time, p->status, p->out_time);
i++;
}
p = p->next;
}
toxy(45, i);
printf("按任意键返回!");
getchar();
find_equipment();
}
void find_number_equipment() //按设备编号查询
{
system("cls");
color(8);
equipment head, p;
int i = 11;
head = read_file();
char number[11];
toxy(48, 8);
printf("请输入您要查询设备的登录号:");
gets_s(number, 10);
toxy(48, 10);
printf("正在查询....");
Sleep(500);
p = head;
toxy(20, 5);
printf("***********************************************设备信息******************************************************");
toxy(20, 8);
printf("-------------------------------------------------------------------------------------------------------------");
toxy(20, 9);
printf("登录号 设备名 设备种类 设备价格 购入时间 设备状态 报废时间");
toxy(20, 10);
printf("-------------------------------------------------------------------------------------------------------------");
while (p != NULL)
{
if (p != NULL && strcmp(p->number, number) == 0)
{
toxy(20, i);
printf("%s %s %14s %14s %14s %s %14s\n", p->number, p->name, p->type, p->price, p->in_time, p->status, p->out_time);
i++;
}
p = p->next;
}
toxy(45, i);
printf("按任意键返回!");
getchar();
_getch();
find_equipment();
}
file_operation.cpp
#include"EEMS.h"
void save_equipment(equipment p) //将p中内容写入文件
{
FILE *fp; //文件指针
fopen_s(&fp, "myequipment", "ab"); //以追加的方式打开名字为myequipment的二进制文件
if (fp == NULL)
{
printf("cannot open file");
}
if (fwrite(p, N, 1, fp) != 1) //将p所指向的一段大小为N的内容存入fp所指向的文件中
{
printf("write error");
}
fclose(fp); //关闭文件
}
equipment read_file() //将文件中的内容读出到链表中,返回值为表头地址
{
FILE *fp; //文件指针
int n = 0;
equipment head = NULL;
equipment p2 = NULL;
equipment p = NULL;
equipment pr = NULL;
fopen_s(&fp, "myequipment", "ab+"); //以只读的方式打开文件
if (fp == NULL)
{
printf("cannot open file\n");
}
while (!feof(fp)) //判断文件位置标志是否移动到文件末尾
{
n++;
p = (equipment)malloc(N); //向内存申请一段空间
fread(p, N, 1, fp); //将fp所指向的文件中的内容赋给p
if (n == 1)
{
head = p;
p2 = p;
}
else //创建链表
{
pr = p2;
p2->next = p;
p2 = p;
}
}
if (pr != NULL)
pr->next = NULL;
else
head = NULL;
fclose(fp); //关闭文件
return head; //返回头指针
}
/*make empty file*/
void empty_file()
{
FILE *fp; //文件指针
fopen_s(&fp, "myequipment", "w");
fclose(fp); //关闭文件
}
运行测试
running_test.cpp
#include"EEMS.h"
int main()
{
//empty_file(); //初始化二进制文件
menu();
getchar();
return 0;
}