商品管理系统

#include 
#include 
#include 

#define MAXN_NAME 1010 
#define MAXN_KIND 1010 

struct Data;
struct Gods;
struct Infor;
struct List_infor;
void Box();
void pas();

int nowid;
int GetID();
struct Data{
	int year;
	int month;
	int day;
};

struct Gods{
	char name[MAXN_NAME];//名字
	char kind[MAXN_KIND];//类别
	int number;//库存数
	char manu[MAXN_KIND];//厂家
	int id;//商品编号
	struct Data *exp_data;//保质期
	struct Infor *head;
	struct Gods *next;
};//商品信息

struct Infor{
	float num;//数量
	int kind;//入库或者出库
	struct Infor *next;
}; 

struct Gods *head_gods;

void in_data(Data *a) {
	int y, m, d;
	scanf("%d%d%d", &y, &m, &d);
	a -> year = y;
	a -> day = d;
	a -> month = m;
	return;
}

void inout(struct Gods *np, int kind) {
	system("cls");
	struct Infor *now_in = (struct Infor*)malloc(sizeof(struct Infor));
	int nown;
	printf("\n\n\n\n\n\n");
	printf("              要修改的数量是:\n                  ");
	scanf("%d", &nown);
	if (kind == 2) {
		if (nown > np -> number) {
			printf("              没有足够的商品!\n                  ");
			pas();
			inout(np, kind);
		} else {
			np -> number -= nown;
		}
	} else {
		np -> number += nown;
	}
	now_in -> next = np -> head;
	np -> head - now_in;
	now_in -> num = nown;
	now_in -> kind = kind;
	printf("                   修改成功!\n");
	pas();
	Box();
}

void see(struct Gods* np) {
	system("cls");
	printf("\n\n\n\n\n\n");
	printf("              商品的名字是:\n                  ");
	printf("%s\n", np -> name);
	printf("              商品的厂商是:\n                  ");
	printf("%s\n", np -> manu);
	printf("              商品的种类是:\n                  ");
	printf("%s\n", np -> kind);
	printf("              商品的保质期是:\n                  ");
	printf("%d %d %d\n", np -> exp_data -> year, np -> exp_data -> month, np -> exp_data -> day);
	printf("              商品的编号是:%d\n                  ", np -> id);
/*	printf("              最近的商品改动:\n                  ");
	struct Infor *ni = np -> head;
	while (ni != 0) {
		printf("\n          %d ", ni -> num);
		if (ni -> kind == 1) {
			printf("入库");
		} else {
			printf("出库");
		} ni = ni -> next;
	}
*/	pas();
	Box();
}

void changeit(struct Gods* np) {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.修改名字\n"); 
	printf("                   2.修改厂商\n");
	printf("                   3.修改保质期\n");
	printf("                   4.修改种类\n");	
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	if (key > 6 || key < 1) Box();
	switch (key) {
		case 1:{
			printf("              商品的名字是:\n                  ");
			scanf("%s", np -> name);
			printf("修改成功!");
			pas();
			Box();
		}
		case 2:{
			printf("              商品的厂商是:\n                  ");
			scanf("%s", np -> manu);
			printf("修改成功!");
			pas();
			Box();
		}
		case 3:{
			printf("              商品的保质期是:\n                  ");
			in_data(np -> exp_data);
			printf("修改成功!");
			pas();
			Box();
		}
		case 4:{
			printf("              商品的种类是:\n                  ");
			scanf("%s", np -> kind);
			printf("修改成功!");
			pas();
			Box();
		}
	}
}

void pas() {
	printf("\n             请输入任意字符继续\n                    ");
	int test;
	scanf("%d", &test); 
}

int GetID() {
	int renum = 0;
	char x;
	int tag = 0;
	while(scanf("%c", &x) != EOF) {
		if (x == '\n') {
			if (tag == 1) return -1;
			else return renum;
		}
		if (x < '0' || x > '9') tag = 1;
		else renum = renum * 10 + x - '0';
	}
	if (tag == 1) return -1;
	else return renum;
}

int check(char a[], char b[]) {
	int i = 0;
	for (; a[i] != '\0';i++) {
		if (a[i] != b[i]) return 0;
	}
	if (b[i] != '\0') return 0;
	return 1;
}

struct Gods *find_name() {
	char nn[MAXN_KIND];
	scanf("%s", nn);
	struct Gods *np = head_gods;
	if (np == 0) return 0;
	while (check(nn, np -> name) == 0) {
			np = np -> next;
			if (np == 0) return 0;
	}
	return np;
}

struct Gods *find_id() {
	int ida;
	scanf("%d", &ida);
	struct Gods *np = head_gods;
	if (np == 0) return 0;
	while (np -> id != ida) {
			np = np -> next;
			if (np == 0) return 0;
	}
	return np;
}

void cre() {
	system("cls");
	struct Gods *now_Gods = (struct Gods*)malloc(sizeof(struct Gods));
	printf("\n\n\n\n\n\n");
	printf("              商品的名字是:\n                  ");
	scanf("%s", now_Gods -> name);
	printf("              商品的厂商是:\n                  ");
	scanf("%s", now_Gods -> manu);
	printf("              商品的种类是:\n                  ");
	scanf("%s", now_Gods -> kind);
	now_Gods -> exp_data = (struct Data*)malloc(sizeof(struct Data));
	printf("              商品的保质期是:\n                  ");
	in_data(now_Gods -> exp_data);
	printf("              商品的编号是:%d\n                  ", ++nowid);
	now_Gods -> id = nowid;
	now_Gods -> next = head_gods;
	head_gods = now_Gods;
	now_Gods -> head = 0;
	now_Gods -> number = 0;
	pas();
	Box();
}

void del() {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.按名称搜索\n"); 
	printf("                   2.按编号搜索\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	struct Gods *nowp = 0;
	if (key > 2 || key < 1) del();
	switch (key) {
		case 1: {
			printf("                   请输入商品名字\n"); 
			nowp = find_name();
			break;
		}
		case 2: {
			printf("                   请输入商品编号\n"); 
			nowp = find_id();
			break;
		}
	}
	if (nowp == 0) {
		printf("                     不存在该商品!");
		pas();
		Box();
	} else {
		if (head_gods == nowp) {
			head_gods = nowp -> next;
		} else {
			struct Gods *sp = head_gods;
			while (sp -> next != nowp) {
				sp = sp -> next;
			}
			sp -> next = nowp -> next;
		}
		printf("                     已删除该商品!");
		pas();
		Box();
	}
}

void inGod() {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.按名称搜索\n"); 
	printf("                   2.按编号搜索\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	struct Gods *nowp = 0;
	if (key > 2 || key < 1) del();
	switch (key) {
		case 1: {
			printf("                   请输入商品名字\n"); 
			nowp = find_name();
			break;
		}
		case 2: {
			printf("                   请输入商品编号\n"); 
			nowp = find_id();
			break;
		}
	}
	if (nowp == 0) {
		printf("                     不存在该商品!");
		pas();
		Box();
	} else {
		inout(nowp, 1);
		pas();
		Box();
	}	
}

void outGod() {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.按名称搜索\n"); 
	printf("                   2.按编号搜索\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	struct Gods *nowp = 0;
	if (key > 2 || key < 1) del();
	switch (key) {
		case 1: {
			printf("                   请输入商品名字\n"); 
			nowp = find_name();
			break;
		}
		case 2: {
			printf("                   请输入商品编号\n"); 
			nowp = find_id();
			break;
		}
	}
	if (nowp == 0) {
		printf("                     不存在该商品!");
		pas();
		Box();
	} else {
		inout(nowp, 2);
		pas();
		Box();
	}	
}

void findGod() {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.按名称搜索\n"); 
	printf("                   2.按编号搜索\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	struct Gods *nowp = 0;
	if (key > 2 || key < 1) del();
	switch (key) {
		case 1: {
			printf("                   请输入商品名字\n"); 
			nowp = find_name();
			break;
		}
		case 2: {
			printf("                   请输入商品编号\n"); 
			nowp = find_id();
			break;
		}
	}
	if (nowp == 0) {
		printf("                     不存在该商品!");
		pas();
		Box();
	} else {
		see(nowp);
		pas();
		Box();
	}	
}

void changeGod() {
		system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.按名称搜索\n"); 
	printf("                   2.按编号搜索\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	struct Gods *nowp = 0;
	if (key > 2 || key < 1) del();
	switch (key) {
		case 1: {
			printf("                   请输入商品名字\n"); 
			nowp = find_name();
			break;
		}
		case 2: {
			printf("                   请输入商品编号\n"); 
			nowp = find_id();
			break;
		}
	}
	if (nowp == 0) {
		printf("                     不存在该商品!");
		pas();
		Box();
	} else {
		changeit(nowp);
		pas();
		Box();
	}	
}

void Box() {
	system("cls"); 
	printf("\n\n\n\n\n\n");
	printf("                请输入您要选择的操作\n");
	printf("                   1.创建商品\n"); 
	printf("                   2.删除商品\n");
	printf("                   3.入库商品\n");
	printf("                   4.出库商品\n");
	printf("                   5.查询商品\n");
	printf("                   6.修改商品\n");
	printf("                  请输入数字并回车\n                    ");
	int key = GetID();
	if (key > 6 || key < 1) Box();
	switch (key) {
		case 1:
			cre();
		case 2:
			del();
		case 3:
			inGod();
		case 4:
			outGod();
		case 5:
			findGod();
		case 6:
			changeGod();
	}
}

int main() {
	Box();
	return 0;
}

你可能感兴趣的:(数据结构,c语言)