C语言:设备管理系统

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct {
	int year;
	int month;
	int day;
}date;
typedef struct {
	int num;
	char type[15];
	char name[15];
	double price;
	date grrq;//购入日期
	char baofei;//是否报废
	date bfrq;//报废日期
}device;
#define LEN sizeof(device)
#define Filename "d:\\123.txt"
device *Dev;//动态数组,就是元素数可变的数组
int count;//动态数组的元素数(设备数)
void loadfile() {//读取文件
	device d; FILE *fp; int i;
	count = 0;
	fp = fopen(Filename, "r+");
	Dev = (device*)malloc(LEN);//分配内存
	if (fp == NULL) {
		fp = fopen(Filename, "w");//如果文件d:\123.txt不存在就创建一个
		fclose(fp);
		return;
	}
	i = fscanf(fp, "%d %s %s %lf %d %d %d %c %d %d %d", &d.num, d.type, d.name, &d.price, &d.grrq.year, &d.grrq.month, &d.grrq.day, &d.baofei, &d.bfrq.year, &d.bfrq.month, &d.bfrq.day);
	while (i != -1) {//fscanf读取到最后一行会返回-1
		Dev[count] = d;
		count++;
		Dev = (device*)realloc((void*)Dev, (count + 1)*LEN);//扩展元素数(重新分配内存)
		i = fscanf(fp, "%d %s %s %lf %d %d %d %c %d %d %d", &d.num, d.type, d.name, &d.price, &d.grrq.year, &d.grrq.month, &d.grrq.day, &d.baofei, &d.bfrq.year, &d.bfrq.month, &d.bfrq.day);
	}
	fclose(fp);
}
void search1() {
	int i, num;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备编号:");
	scanf("%d", &num);
	for (i = 0; i<count; i++) {
		if (Dev[i].num == num) {
			printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
			return;
		}
	}
	printf("\n该设备编号不存在\n\n");
}
void search2() {
	int i, c = 0, bf = 0; char type[15]; double sum = 0;
	getchar();//去除scanf后缓冲区留下的回车
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备种类:");
	scanf("%s", type);
	for (i = 0; i<count; i++) {
		if (0 == strcmp(type, Dev[i].type)) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备种类为“%s”的设备\n\n", type);
	else printf("\n共有%d个设备种类为“%s”的设备,%d个已报废,总价格为%.2lf\n\n", c, type, bf, sum);
}
void search3() {
	int i, c = 0, bf = 0; char name[15]; double sum = 0;
	getchar();//去除scanf后缓冲区留下的回车
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备名称:");
	scanf("%s", name);//gets(name);
	for (i = 0; i<count; i++) {
		if (0 == strcmp(name, Dev[i].name)) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备名称为“%s”的设备\n\n", name);
	else printf("\n共有%d个设备名称为“%s”的设备,%d个已报废,总价格为%.2lf\n\n", c, name, bf, sum);
}
void search41() {
	int i, c = 0, bf = 0; double price, sum = 0;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备价格:");
	scanf("%lf", &price);
	for (i = 0; i<count; i++) {
		if (Dev[i].price == price) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备价格为%.2lf的设备\n\n", price);
	else printf("\n共有%d个设备价格为%.2lf的设备,%d个已报废,总价格为%.2lf\n\n", c, price, bf, sum);
}
void search42() {
	int i, c = 0, bf = 0; double price, sum = 0;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备价格下限:");
	scanf("%lf", &price);
	for (i = 0; i<count; i++) {
		if (Dev[i].price >= price) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备价格高于%.2lf的设备\n\n", price);
	else printf("\n共有%d个设备价格高于%.2lf的设备,%d个已报废,总价格为%.2lf\n\n", c, price, bf, sum);
}
void search43() {
	int i, c = 0, bf = 0; double price, sum = 0;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备价格上限:");
	scanf("%lf", &price);
	for (i = 0; i<count; i++) {
		if (Dev[i].price <= price) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备价格低于%.2lf的设备\n\n", price);
	else printf("\n共有%d个设备价格低于%.2lf的设备,%d个已报废,总价格为%.2lf\n\n", c, price, bf, sum);
}
void search44() {
	int i, c = 0, bf = 0; double min, max, t, sum = 0;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要查询的设备价格下限:");
	scanf("%lf", &min);
	printf("请输入要查询的设备价格上限:");
	scanf("%lf", &max);
	if (min>max) { t = min; min = max; max = t; }
	for (i = 0; i<count; i++) {
		if (Dev[i].price >= min&&Dev[i].price <= max) {
			if (c == 0)printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			c++; sum += Dev[i].price; if ('Y' == Dev[i].baofei)bf++;
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		}
	}
	if (c == 0)printf("\n不存在设备价格高于%.2lf且低于%.2lf的设备\n\n", min, max);
	else printf("\n共有%d个设备价格高于%.2lf低于%.2lf的设备,%d个已报废,总价格为%.2lf\n\n", c, min, max, bf, sum);
}
void search4() {
	int i;
	while (1) {
		system("cls");//清屏
		printf("----------------\n");
		printf("设备资产管理系统\n");
		printf("----------------\n\n");
		printf("1.查询价格为某一数值的设备\n2.查询价格高于某一数值的设备\n3.查询价格低于某一数值的设备\n4.查询价格在某一区间的设备\n0.返回上一层菜单\n\n");
		printf("请选择:");
		scanf("%d", &i);
		switch (i) {
		case 1:search41(); system("pause"); break;
		case 2:search42(); system("pause"); break;
		case 3:search43(); system("pause"); break;
		case 4:search44(); system("pause"); break;
		case 0:return;
		}
	}
}
void search() {//查询设备信息子菜单
	int i;
	while (1) {
		system("cls");//清屏
		printf("----------------\n");
		printf("设备资产管理系统\n");
		printf("----------------\n\n");
		printf("1.按设备编号查询\n2.按设备种类查询\n3.按设备名称查询\n4.按设备价格查询\n0.返回主菜单\n\n");
		printf("请选择:");
		scanf("%d", &i);
		switch (i) {
		case 1:search1(); system("pause"); break;
		case 2:search2(); system("pause"); break;
		case 3:search3(); system("pause"); break;
		case 4:search4(); break;
		case 0:return;
		}
	}
}
void sort() {//对设备编号选择排序(升序)
	int i, j, k; device t;
	for (i = 0; i<count - 1; i++) {
		k = i;
		for (j = i; j<count; j++)if (Dev[k].num>Dev[j].num)k = j;
		if (k != i) {
			t = Dev[k]; Dev[k] = Dev[i]; Dev[i] = t;
		}
	}
}
void writefile() {//将数组写入文件
	FILE *fp; int i;
	fp = fopen(Filename, "w");
	for (i = 0; i<count; i++) {
		fprintf(fp, "%d %s %s %.2lf %d %d %d %c %d %d %d\r\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
	}//写进文件换行必须\r\n,只有\n不能换行
	fclose(fp);
}
void add() {//添加设备信息
	device d; int i;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("添加设备信息:\n");
	printf("请输入设备编号:");
	scanf("%d", &d.num);
	for (i = 0; i<count; i++) {//检查设备编号是否重复
		if (d.num == Dev[i].num) {
			printf("\n该设备编号与已有设备编号重复,请重新输入\n\n");
			system("pause");
			return;
		}
	}
	getchar();//去掉scanf后留在缓冲区里的回车
	printf("请输入设备种类:");
	scanf("%s", d.type);
	printf("请输入设备名称:");
	scanf("%s", d.name);
	printf("请输入设备价格:");
	scanf("%lf", &d.price);
	printf("请输入购入日期(格式为年-月-日):");
	scanf("%d-%d-%d", &d.grrq.year, &d.grrq.month, &d.grrq.day);
	getchar();//去掉scanf后留在缓冲区里的回车
	printf("请输入是否报废(Y=已报废,N=未报废):");
	scanf("%c", &d.baofei);
	printf("请输入报废日期(格式为年-月-日):");
	scanf("%d-%d-%d", &d.bfrq.year, &d.bfrq.month, &d.bfrq.day);
	Dev = (device*)realloc((void*)Dev, (count + 1)*LEN);//扩展元素数
	Dev[count] = d;//新增的设备加在数组最后
	count++;//设备数+1
	sort();//排序
	writefile();//写入进文件
}
void modify() {//修改设备信息
	int i, j, num; device d;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要修改的设备编号:");
	scanf("%d", &num);
	for (i = 0; i<count; i++) {
		if (Dev[i].num == num) {
			printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
			break;//如果找到,拿出来处理,都搁在循环里太乱
		}
	}
	if (i == count) { printf("\n该设备编号不存在\n\n"); system("pause"); return; }
	printf("请输入修改后的信息:\n");
	printf("请输入设备编号:");
	scanf("%d", &d.num);
	for (j = 0; j<count; j++) {//检查设备编号是否重复
		if (i != j&&d.num == Dev[j].num) {//如果不修改设备编号,会提示设备编号重复,加入条件i!=j,即不判断修改后设备编号与修改前的是否相同
			printf("\n该设备编号与已有设备编号重复,请重新输入\n\n");
			system("pause");
			return;
		}
	}
	getchar();//去掉scanf后留在缓冲区里的回车
	printf("请输入设备种类:");
	scanf("%s", d.type);
	printf("请输入设备名称:");
	scanf("%s", d.name);
	printf("请输入设备价格:");
	scanf("%lf", &d.price);
	printf("请输入购入日期(格式为年-月-日):");
	scanf("%d-%d-%d", &d.grrq.year, &d.grrq.month, &d.grrq.day);
	getchar();//去掉scanf后留在缓冲区里的回车
	printf("请输入是否报废(Y=已报废,N=未报废):");
	scanf("%c", &d.baofei);
	printf("请输入报废日期(格式为年-月-日):");
	scanf("%d-%d-%d", &d.bfrq.year, &d.bfrq.month, &d.bfrq.day);
	Dev[i] = d;
	sort();//排序
	writefile();//写入文件
}
void del() {//删除设备
	int i, num;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("请输入要删除的设备编号:");
	scanf("%d", &num); getchar();//去除scanf后缓冲区留下的回车
	for (i = 0; i<count; i++) {
		if (Dev[i].num == num) {
			printf("\n设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
			break;//如果找到,拿出来处理,都搁在循环里太乱
		}
	}
	if (i == count) { printf("\n该设备编号不存在\n\n"); system("pause"); return; }
	printf("你确定要删除此设备的信息吗?(输入Y确定,输入其他返回主菜单)");
	if ('Y' == getchar()) {
		for (; i<count - 1; i++) {
			Dev[i] = Dev[i + 1];//i之后的所有设备信息向前移动一个元素(没有下标为count的元素,所以不用循环至count-1)
		}
		count--;//设备数-1
		writefile();//写入文件
	}
}
void outputall() {//全部列出
	int i;
	double pricewbf = 0, pricebf = 0;//未报废总价格,报废总价格
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("全部设备信息:\n\n");
	printf("设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
	for (i = 0; i<count; i++) {
		printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
		if (Dev[i].baofei == 'Y')pricebf += Dev[i].price;//计算总价格
		else pricewbf += Dev[i].price;
	}
	printf("\n共有%d条设备信息\n所有设备总价格  %.2lf\n未报废设备总价格%.2lf\n报废设备总价格  %.2lf\n\n", count, pricewbf + pricebf, pricewbf, pricebf);
	system("pause");//按任意键继续(不会退出)
}
void outputbf() {//列出报废设备
	int i, c = 0; double pricebf = 0;
	system("cls");//清屏
	printf("----------------\n");
	printf("设备资产管理系统\n");
	printf("----------------\n\n");
	printf("报废设备信息:\n\n");
	printf("设备编号 设备种类       设备名称       设备价格  购入日期   是否报废 报废日期\n");
	for (i = 0; i<count; i++) {
		if (Dev[i].baofei == 'Y') {
			printf("%8d %-14s %-14s %9.2lf %04d-%02d-%02d %c        %04d-%02d-%02d\n", Dev[i].num, Dev[i].type, Dev[i].name, Dev[i].price, Dev[i].grrq.year, Dev[i].grrq.month, Dev[i].grrq.day, Dev[i].baofei, Dev[i].bfrq.year, Dev[i].bfrq.month, Dev[i].bfrq.day);
			pricebf += Dev[i].price;
			c++;
		}
	}
	printf("\n共有%d个报废设备\n总价格%.2lf\n\n", c, pricebf);
	system("pause");//按任意键继续(不会退出)
}
int main() {
	int i;
	loadfile();//读取文件
	while (1) {
		system("cls");//清屏
		printf("----------------\n");
		printf("设备资产管理系统\n");
		printf("----------------\n\n");
		printf("1.列出全部设备信息\n2.列出报废设备信息\n3.查询设备信息\n4.添加设备信息\n5.修改设备信息\n6.删除设备信息\n0.退出\n\n");
		printf("请选择:");
		scanf("%d", &i);
		switch (i) {
		case 1:outputall(); break;
		case 2:outputbf(); break;
		case 3:search(); break;
		case 4:add(); break;
		case 5:modify(); break;
		case 6:del(); break;
		case 0:return 0;
		}
	}
	return 0;
}

  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值