【用单链表管理商品库存表】

【用单链表管理商品库存表】,第1张

#define _CRT_SECURE_NO_WARNINGS//必须加上,要不然会让你必须改写eg:scanf必须写成scanf_s
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef struct goods {
	char code[5];
	char name[15];
	int min;
	int now;
}ElemType;

typedef struct LNode {
	struct goods list;
	struct LNode* next;
}LNode, * LinkList;

bool operator == (const LNode*p, const ElemType e) {

	return (strcmp(p->list.code, e.code) == 0);
}

ostream& operator <<(ostream& cout, const LinkList l) {
	cout.setf(ios::left);
	LNode* p = l->next;
	while (p != NULL) {
		cout << setw(6) << p->list.code << setw(12) << p->list.name;
		cout << setw(4) <list.min << setw(4) << p->list.now << endl;
		p = p->next;
	}
	return cout;
}

bool ListInsert(LinkList& l, ElemType e, int i) {
	if (i < 1) {
		return false;
	}
	int j = 0;//当前P指向的是第几个结点
	LNode* p = l;
	//这里是获得次数 0到i-2 有i-1个,也就是说i-1次
	while (p != NULL && j < i - 1) {
		//到了最后一个结点,但是i很大,p->next = NULL;一直循环指向自己
		p = p->next;
		j++;
	}
	if (p == NULL) {
		return false;
	}
	LNode* s = (LNode*)malloc(sizeof(LNode));
	strcpy(s->list.code,e.code );
	strcpy(s->list.name,e.name );
	//s->list->code = e.code;//数组之间赋值不能直接=,通过strcpy
	//s->list->name = e.name;//
	s->list.min = e.min;
	s->list.now = e.now;
	s->next = p->next;
	p->next = s;
	return true;
}
//必须加上const修饰,版本更新要求,和书上不一样
void SetupGoodsFile(const char* fname) {
	ofstream ofstr(fname);
	if (!ofstr) {
		cerr << "false" << endl;
		exit(1);
	}
	char a[30];
	for (int i = 0; i < 6; i++) {
		cin.getline(a, 30);
		ofstr << a << endl;
	}
	ofstr.close();
}
//必须加上const修饰,版本更新要求,和书上不一样
void SetupGoodsList(LinkList& l, const char* fname) {
	ifstream ifstr(fname, ios::in);
	if (!ifstr) {
		cerr << "文件创建失败" << endl;
		exit(1);
	}
	goods g;
	int i = 1;
	while (ifstr>>g.code) {
		ifstr >> g.name >> g.min >> g.now;
		ListInsert(l, g, i++);
		
	}
	ifstr.close();
}
//必须加上const修饰,版本更新要求,和书上不一样
void WriteGoodsFile(const char* fname, LinkList& l) {
	ofstream ofstr(fname);
	if (!ofstr) {
		cerr << "文件创建失败" << endl;
		exit(1);
	}
	LNode* p = l->next;
	while (p != NULL) {
		ofstr << p->list.code << " " << p->list.name << " " << p->list.min << " " << p->list.now << endl;
		p = p->next;
	}
	ofstr.close();
}

void InitLisk(LinkList& l) {
	l = (LNode*)malloc(sizeof(LNode));
	l->next = NULL;
}

bool FindList(LinkList l, goods g) {
	LNode* p = l->next;
	while (p != NULL) {
		if (p == g) return true;
		p = p->next;
	}
	return false;
}

bool UpdateList(LinkList l, goods g) {
	LNode* p = l->next;
	while (p != NULL) {
		if (p == g) {
			p->list.now += g.now;
			return true;
		}
		p = p->next;
	}
	return false;
}

bool DeleList(LinkList& l, goods g) {
	LNode* t;
	LNode* p = l;
	while (p->next != NULL) {
		if (p->next == g) {
			t = p->next;
			p->next = p->next->next;
			free(t);
			return true;
		}
		p = p->next;

	}
	return false;
}
//单链表的排序
void sortList(LinkList &l) {
	LNode* p, *q, *r;
	int count = 0,key = 0;
	p = l;
	q = l->next;
	while (q != NULL) {
		count++;
		q = q->next;
	}
	q = l->next;
	for (int i = 1; i <= count; i++) {
		key = 0;
		while (q->next != NULL) {
			//从小到大排序
			if (strcmp(q->list.code, q->next->list.code) > 0) {
				r = q->next->next;
				p->next = q->next;
				q->next->next = q;
				p = p->next;
				q->next = r;
				key = 1;
			}
			else {
				p = q;
				q = q->next;
			}
			
		}
		if (!key) break;
		p = l;
		q = l->next;
	}
}

int main() {
	SetupGoodsFile("D:\list.dat");
	LinkList l;
	InitLisk(l);
	SetupGoodsList(l, "D:\list.dat");
	//验证文件数据又没有传入链表中
	//LNode* p = l->next;
	//cout<list.now << endl;
	//	p = p->next;
	//}
	int i, flag = 1;//后面switch(5)退出的标志
	while (flag) {
		cout << "1:打印整个库存表" << endl;
		cout << "2:修改库存表中的记录" << endl;
		cout << "3:删除库存表中的记录" << endl;
		cout << "4:对库存表排序" << endl;
		cout << "5:结束程序" << endl;
		cout << "请输入你的选择(1--5): " << endl;
		cin >> i;
		while (!(i <= 5 && i >= 1)) {
			cout << "请重新输出选择(1--5): ";
			cin >> i;
			cout << endl;
		}
		switch (i) {
		case 1:
			cout << l;
			break;
		case 2:
			goods g ;
			int x;
			cout << "输入待修改的商品代号: ";
			cin >> g.code;
			if (FindList(l,g)){
				cout << "输入该商品的修正量: ";
					cin >> x;
					//体验到不初始化的后果   cout << g.now << endl;    g.now = -858993460
					g.now = x;
					if (UpdateList(l, g)) cout << "完成更新!" << endl;
			}
			else {
				cout << "输入新商品记录的其余字段的内容: " << endl;
				cin >> g.name >> g.min >> g.now;
				ListInsert(l, g, i++);
				cout << "新纪录已被插入到链尾!" << endl;
			}
			break;
		case 3:
			cout << "输入待删除商品的商品代号:";
			cin >> g.code;
			if (DeleList(l, g)) 
				cout << "代号为" << g.code << "的记录被删除!" << endl;
			else 
				cout << "代号为" << g.code << "的记录不存在!" << endl;
			break;
		case 4:
			sortList(l);
			cout << "商品表中的记录已按商品代号排序!" << endl;
			break;
		case 5:
			char key;
			cout << "请再次输入你的选择(Y/N)" << endl;
			cin >> key;
			if (key == 'Y') {
				cout << "本次处理结束!" << endl;
				flag = 0;
				break;
			}
			else 
				break;
		}
	}

	WriteGoodsFile("D:\list.dat", l);
	return 0;
}

 输入的时候注意空格

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存