用c语言实现仓库管理系统

本文使用c语言实现仓库管理系统

新手的练手之作,代码中还有一些不足之处,后续会进一步修改,虚心学习各位大佬的意见

使用结构体数组及文件操作

其中有个账号密码匹配功能借鉴了站内一位大佬的文章“C语言程序设计:图书管理系统(超详细有登录系统,附代码和实验报告)”

界面有点丑陋,后面可能会发一个新的版本,对于一些细节的优化

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <iostream>
#define maxsize 20
#include <conio.h>
using namespace std;
//建立一个仓库管理程序,
//可以按顺序和货物名称查询仓库存储情况,
//也可以增加或删除货物信息、
//实现货物的入库出库,
//要求能用文件保存仓库货品信息。
//增加了采购员信息,用户信息,加入登录功能


void Create();
void Add();
void Agent();
void Buyer();
void Delete();
void name_Inquire();
void time_Inquire();
void Change();
void Inquire();
void DispList();
int choose();
void see();
void timeout();
int match_num();
int Len = 0;


typedef struct Info {//仓库货物信息
	char num[5];
	char name[10];
	int amount;
	char buy[20];
	char store[10];
} Info;

Info info[10];
char idname[10];


void normalmenu() {
	printf("欢迎来到仓库管理系统");
	printf("   1.按照入库顺序查询仓库存储情况   \n");
	printf("   2.按照货物名称查询仓库存储情况   \n");
}

void GMmenu1() {
	while (1) {
		printf("\n");
		printf("     欢迎来到仓库管理系统(管理员)   \n");
		printf("****************菜单****************\n");
		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("               9.退出               \n");
		printf("                                    \n");
		printf("************************************\n");
		printf("      请选择你想要的功能(序号)      \n");
		int n;
		scanf("%d", &n);

		while (n < 1 || n > 9) {
			printf("输入指令有误,请重新输入:\n");
			scanf("%d", &n);
		}

		switch (n) {
			case 1:
				Create();
				break;

			case 2:
				Inquire();
				break;

			case 3:
				DispList();
				break;

			case 4:
				Add();
				break;

			case 5:
				Delete();
				break;

			case 6:
				Change();
				break;

			case 7:
				Agent();
				break;

			case 8:
				Buyer();
				break;

			case 9:
				printf("谢谢您的使用!\n");
				exit(0);
		}


	}
}


void read() {
	Len = 0;
	FILE *fp1;
	fp1 = fopen("data.txt", "r+");

	if (fp1 == NULL) {
		printf("注意:你的数据库内没有商品信息");
	}

	while (!feof(fp1)) {
		fscanf(fp1, "%s %s %d %s %s\n", &info[Len].num, &info[Len].name, &info[Len].amount, &info[Len].buy, &info[Len].store);
		++Len;
	}

	fclose(fp1);
}

void write() {
	//将结构体数组存入文件中
	FILE *fp;

	fp = fopen("data.txt", "w+");  //打开文件

	for (int i = 0; i < Len; i++) {

		fprintf(fp, "%s %s %d %s %s\n", info[i].num, info[i].name, info[i].amount, info[i].buy, info[i].store);
	}

	fclose(fp);    //关闭文件
}

void Create() {
	FILE *fp;
	char s;
	printf("\n如果你之前建立过数据集将会被重置");
	printf("\n请再次确认你是否要建立数据集(Y/N)");
	fflush(stdout);
	scanf("%s", &s);
	int num;

	if (s == 'n' || s == 'N') {
		printf("\n已退出\n");
		return;
	} else if (s == 'y' || s == 'Y') {
		Info info[maxsize];
		FILE *fp, *fpp;


		printf("有多少行信息\n");
		scanf("%d", &num);

		for (int i = 0; i < num; i++) {

			printf("\n请输入编号");
			scanf("%s", &info[i].num);
			printf("\n请输入货物的名字");
			scanf("%s", &info[i].name);
			printf("\n请输入数量");
			scanf("%d", &info[i].amount);
			printf("\n请输入采购员的名字");
			scanf("%s", &info[i].buy);
			printf("\n请输入厂商的名字");
			scanf("%s", &info[i].store);
		}

		Len = num;

		//将结构体数组存入文件中


		fp = fopen("data.txt", "w+");  //打开文件

		for (int i = 0; i < Len; i++) {

			fprintf(fp, "%s %s %d %s %s\n", info[i].num, info[i].name, info[i].amount, info[i].buy, info[i].store);
		}

		fclose(fp);    //关闭文件
		printf("功能已完成,倒计时5秒后将自动退出");

		int nnn = 5;
		printf("\n");

		do {
			printf("%d ", nnn);
			putchar('\a');
			Sleep(1000);
		} while (nnn--);
		system("cls");
		return;
	} else {
		Create();
	}



}

void Inquire() {
	int tem;
	printf("请选择你想按照什么方式进行查找?(输入号码)");
	printf("1.按照入库顺序进行查找");
	printf("2.按照物品名称进行查找");
	scanf("%d", &tem);

	if (tem == 1) {
		time_Inquire();
	} else if (tem == 2) {
		name_Inquire();
	} else {
		printf("号码输入错误,请重新输入");
	}

}

void time_Inquire() {

	read();

	while (1) {
		char a[5];
		printf("请输入想要查询货物的编号:\n");
		scanf("%s", &a);

		for (int i = 0; i < Len; i++) {

			if (strcmp(info[i].num, a) == 0) {
				printf("编号:%s\n", info[i].num);
				printf("名称:%s\n", info[i].name);
				printf("数量:%d\n", info[i].amount);
				printf("采购员:%s\n", info[i].buy);
				printf("厂商:%s\n", info[i].store);
				printf("\n");
			}
		}

		printf("还要继续查询吗?(Y/N)\n");
		char ap;
		fflush(stdin);
		scanf("%c", &ap);

		if (ap == 'n' || ap == 'N') {
			timeout();
			break;
		} else if (ap == 'y' || ap == 'Y') {
			continue;
		} else {
			printf("输入错误字符将自动退出程序\n");
			timeout();
			return;
		}
	}




}

void name_Inquire() {

	read();

	while (1) {
		char check_name[10];
		printf("请输入你想要查询货物的名字:");
		scanf("%s", &check_name);

		for (int i = 0; i < Len; i++) {

			if (strcmp(info[i].name, check_name) == 0) {
				printf("编号:%s\n", info[i].num);
				printf("名称:%s\n", info[i].name);
				printf("数量:%d\n", info[i].amount);
				printf("采购员:%s\n", info[i].buy);
				printf("厂商:%s\n", info[i].store);
				printf("\n");
			}
		}

		printf("还要继续查询吗?(Y/N)\n");
		char ap;
		fflush(stdin);
		scanf("%c", &ap);

		if (ap == 'n' || ap == 'N') {
			timeout();
			break;
		} else if (ap == 'y' || ap == 'Y') {
			continue;
		} else {
			printf("输入错误字符将自动退出程序\n");
			timeout();
			return;
		}
	}



}



void DispList() {
	read();
	printf("编号    名称	数量    采购员    厂商\n");

	for (int i = 0; i < Len; i++) {

		printf("%-8s %-8s %-8d %-8s %-8s\n", info[i].num, info[i].name, info[i].amount, info[i].buy, info[i].store);
	}

}

void num_DispList() {
	read();
	printf("序号   编号    名称	 数量    采购员    厂商\n");

	for (int i = 0; i < Len; i++) {

		printf("%-8d% -8s %-8s %-8d %-8s %-8s\n", i, info[i].num, info[i].name, info[i].amount, info[i].buy, info[i].store);
	}
}

void Add() {
	int addinfo = Len + 1;
	FILE *fp;
	char i;
	system ("cls");

	if ((fp = fopen("data.txt", "r")) == NULL) {
		fp = fopen("data.txt", "w");
		fclose(fp);
	}

	fp = fopen("data.txt", "a");

	printf("\n请按以下格式输入货物信息:\n编号 名称 数量 采购员 厂商");
	fprintf(fp, "\n");

	for (; i != 27;) { //为了实现输入一次后按esc退出

		printf("请输入:\n");
		scanf("%s %s %d %s %s", &info[addinfo].num, &info[addinfo].name, &info[addinfo].amount, &info[addinfo].buy,
		      &info[addinfo].store);
		fprintf(fp, "%s %s %d %s %s\n", info[addinfo].num, info[addinfo].name, info[addinfo].amount, info[addinfo].buy,
		        info[addinfo].store);
		printf("继续输入请按回车,结束输入请按Esc\n");
		i = getch(); //暂停程序当i接收后继续下一条指令

		for (; i != 13 && i != 27;) //保证只能是CR和ESC才能退出循环,输入其他字符无用,暂停程序,按'CR'继续。

			i = getch();
	}

	fclose(fp);
	printf("\n保存成功,按任意键返回上一层!");
	getch();
	return;
}

void Delete() {

	system("cls");//清屏函数
	int tip, i, j;
	char sg;
	char id_num[5];
	DispList();
	printf("请输入您要删除的编号:\n");
	scanf("%s", &id_num);
	FILE *fp;
	int sign = 0;
	int tp, zz;

	for ( i = 0; i < Len; i++) {
		if (strcmp(info[i].num, id_num) == 0) {//有点小问题
			++sign;
			printf("编号:%s\n", info[i].num);
			printf("名称:%s\n", info[i].name);
			printf("数量:%d\n", info[i].amount);
			printf("采购员:%s\n", info[i].buy);
			printf("厂商:%s\n", info[i].store);
			printf("\n请再次确认您是否删除:(Y/N)");
			fflush(stdin);
			char gchar;
			gchar = getch();
			zz = i;

			if (gchar == 'Y' || gchar == 'y' ) {
				for (tp = zz; tp < Len; tp++) {

					strcpy(info[tp].num, info[tp + 1].num);
					strcpy(info[tp].name, info[tp + 1].name);
					info[tp].amount = info[tp + 1].amount;
					strcpy(info[tp].buy, info[tp + 1].buy);
					strcpy(info[tp].store, info[tp + 1].store);
				}

				printf("删除成功");
				Len--;
				write();
				timeout();
				return;
			}
		}

	}



	if (sign == 0) {
		printf("\n没有找到该用户");
		timeout();
		return;
	}



}

void select() {//修改中的菜单
	printf("\n1:修改编号");
	printf("\n2:修改货物名称");
	printf("\n3:修改数量");
	printf("\n4:修改采购员");
	printf("\n5:修改厂商");
	printf("\n6:退出");
	printf("\n请选择你需要的操作:");
}

int choose() {
	int pink;
	printf("请选择你要修改的位置的序号:");
	scanf("%d", &pink);
	return pink - 1;//返回物理位置
}

void Change() {
	system("cls");//清屏函数
	num_DispList();
	int a;
	int tap;
	select();
	scanf("%d", &a);

	while (1) {
		switch (a) {
			case 1: {
				read();
				tap = choose();
				printf("请输入修改后的编号");
				scanf("%s", &info[tap].num);
				printf("%s", info[tap].num);
				printf("修改成功!");
				write();
				return;
				break;
			}

			case 2: {
				read();
				tap = choose();
				printf("请输入修改后的货物名称");
				scanf("%s", &info[tap].name);
				printf("%s", info[tap].name);
				printf("修改成功!");
				write();
				return;
				break;
			}

			case 3: {
				read();
				tap = choose();
				printf("请输入修改后的数量");
				scanf("%lld", &info[tap].amount);
				printf("%lld", info[tap].amount);
				printf("修改成功!");
				write();
				return;
				break;
			}

			case 4: {
				read();
				tap = choose();
				printf("请输入修改后的采购员的名字");
				scanf("%d", &info[tap].buy);
				printf("%d", info[tap].buy);
				printf("修改成功!");
				write();
				return;
				break;
			}

			case 5: {

				read();
				tap = choose();
				printf("请输入修改后的厂商");
				scanf("%d", &info[tap].store);
				printf("%d", info[tap].store);
				printf("修改成功!");
				write();
				return;
				break;
			}


			case 6: {
				return;
			}


			default: {
				printf("你输入的编号不对\n");
				break;
			}
		}
	}
}


void Agent() {
	read();


	while (1) {
		char check_store[10];
		printf("请输入想要查询厂商:\n");
		scanf("%s", &check_store);

		for (int i = 0; i < Len; i++) {

			if (strcmp(info[i].store, check_store) == 0) {
				printf("编号:%s\n", info[i].num);
				printf("名称:%s\n", info[i].name);
				printf("数量:%d\n", info[i].amount);
				printf("采购员:%s\n", info[i].buy);
				printf("厂商:%s\n", info[i].store);
				printf("\n");
			}
		}

		printf("还要继续查询吗?(Y/N)\n");
		char ap;
		fflush(stdin);
		scanf("%c", &ap);

		if (ap == 'n' || ap == 'N') {
			timeout();
			break;
		} else if (ap == 'y' || ap == 'Y') {
			continue;
		} else {
			printf("输入错误字符将自动退出程序\n");
			timeout();
			return;
		}
	}
}

void Buyer() {
	read();

	while (1) {
		char check_buyer[10];
		printf("请输入想要查询采购员:\n");
		scanf("%s", &check_buyer);

		for (int i = 0; i < Len; i++) {

			if (strcmp(info[i].buy, check_buyer) == 0) {
				printf("编号:%s\n", info[i].num);
				printf("名称:%s\n", info[i].name);
				printf("数量:%d\n", info[i].amount);
				printf("采购员:%s\n", info[i].buy);
				printf("厂商:%s\n", info[i].store);
				printf("\n");
			}
		}

		printf("还要继续查询吗?(Y/N)\n");
		char ap;
		fflush(stdin);
		scanf("%c", &ap);

		if (ap == 'n' || ap == 'N') {
			timeout();
			break;
		} else if (ap == 'y' || ap == 'Y') {
			continue;
		} else {
			printf("输入错误字符将自动退出程序\n");
			timeout();
			return;
		}
	}
}

int match(int m, char a[20]) { //匹配数据库中的账号密码
	FILE *fp;
	int n = 0, i = 0;
	int account;
	char password[20];

	if ((fp = fopen("welcome.txt", "r")) == NULL) { //不存在读者文件
		system ("cls");
		printf("\n 还未存在用户!请联系最高权限拥有者");
		getch();
		system("cls");
		return 0;

	}


	for (; !feof(fp);) {

		fscanf(fp, "%d %s", &account, password);

		if (m == account) {
			if (strcmp(a, password) == 0)
				return 1;
			else {
				return -1;
			}
		}
	}

	return 0;
}

void GMwelcome() { //输入账户密码的登录函数
	int account;
	char password[20];
	int i = 2, j, k, n;
	char hit = 0;
	system("cls");

	do {
		printf("\n请输入账号:\n");
		scanf("%d", &account);
		printf("确认输入请安回车,重新输入请按ECS");
		hit = getch(); //暂停程序当i接收后继续下一条指令

		for (; hit != 13 && hit != 27;) { //保证只能是CR和ESC才能退出循环,输入其他字符无用,暂停程序,按'CR'继续。

			hit = getch();
		}
	} while (hit == 27);
	printf("\n请输入密码:\n");
	scanf("%s", password);
	i = match(account, password);

	if (i == 1) {
		system("cls");
		printf("登陆成功!按任意键继续\n");
		GMmenu1();


	} else {
		if (i == -1) {
			printf("密码错误!");
			getch();
			GMwelcome();
		}

		if (i == 0)
			printf("不存在此用户");
		getch();
		system("cls");
		printf("感谢您的使用");
		printf("倒计时5秒后将自动退出程序\n");
		int nnn = 5;
		printf("\n");

		do {
			printf("%d ", nnn);
			putchar('\a');
			Sleep(1000);
		} while (nnn--);
	}
}

void Nomenu() {
	while (1) {
		int uu = 0;
		printf("              用户你好              \n");
		printf("        欢迎来到仓库管理系统        \n");
		printf("         请选择你想要的功能         \n");
		printf("****************菜单****************\n");
		printf("                                    \n");
		printf("                                    \n");
		printf("         1.查询仓库存储情况         \n");
		printf("               2.退出               \n");
		printf("                                    \n");
		printf("                                    \n");
		printf("************************************\n");
		printf("请输入序号:\n");
		scanf("%d", &uu);

		switch (uu) {
			case 1: {
				see();
				break;
			}

			case 2: {
				exit(1);
				break;
			}

			default: {
				printf("请输入正确的序号\n");
				Nomenu();
				break;
			}
		}
	}


}

void see() {
	read();
	int tem = Len + 1;
	printf("请输入你想要查询的货物:");
	char temp_name[10];
	scanf("%s", &temp_name);
	printf("\n名称    数量\n");

	for (int i = 0; i < tem; i++) {

		if (strcmp(info[i].name, temp_name) == 0) {
			printf("%s", info[i].name);
			printf("%6d\n", info[i].amount);

		}
	}

	timeout();

}

int Nomatch(int m, char a[20]) { //匹配数据库中的账号密码
	FILE *fp;
	int n = 0, i = 0;
	int account;
	char password[20];

	if ((fp = fopen("Nowelcome.txt", "r")) == NULL) { //不存在读者文件
		system ("cls");
		printf("\n 还未存在用户!请联系最高权限拥有者");
		getch();
		system("cls");
		return 0;

	}


	for (; !feof(fp);) {

		fscanf(fp, "%d %s", &account, password);

		if (m == account) {
			if (strcmp(a, password) == 0)
				return 1;
			else {
				return -1;
			}
		}
	}

	return 0;
}


void Nowelcome() {
	int account;
	char password[20];
	int i = 2, j, k, n;
	char hit = 0;
	system("cls");

	do {
		printf("\n请输入账号:\n");
		scanf("%d", &account);
		printf("确认输入请按回车,重新输入请按ECS");
		hit = getch(); //暂停程序当i接收后继续下一条指令

		for (; hit != 13 && hit != 27;) { //保证只能是CR和ESC才能退出循环,输入其他字符无用,暂停程序,按'CR'继续。

			hit = getch();
		}
	} while (hit == 27);
	printf("\n请输入密码:\n");
	scanf("%s", password);
	i = Nomatch(account, password);

	if (i == 1) {
		printf("登陆成功!按任意键继续");
		Nomenu();

	} else {
		if (i == -1) {
			printf("密码错误!");
			getch();
			Nowelcome();
		}

		if (i == 0)
			printf("不存在此用户");
		getch();
		system("cls");
		printf("感谢您的使用");
		printf("倒计时5秒后将自动退出程序\n");
		int nnn = 5;
		printf("\n");

		do {
			printf("%d ", nnn);
			putchar('\a');
			Sleep(1000);
		} while (nnn--);
	}
}

void Allwelcome() {
	printf("请选择登录方式\n");
	printf("1、用户登录\n");
	printf("2、管理员登录\n");
	int t = 0;
	scanf("%d", &t);

	if (t == 1) {
		Nowelcome();

	} else if (t == 2) {
		GMwelcome();

	} else {
		system("cls");
		printf("出错了!!!请重试\n");
		Allwelcome();
	}
}

void timeout() {
	printf("\n输入任意键结束\n");
	getch();
	fflush(stdout);
	system("cls");
}

int main() {
	int n = 0;
	Allwelcome();
	return 0;
}

  • 11
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值