Bootstrap

用C语言实现商品信息管理系统

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 20

typedef struct Node { //商品信息结构体
	char ID[MAXSIZE];//商品的编号
	char name[MAXSIZE];//商品名称
	double cost;//成本
	double price;//售价
	int stock;//进货量
	int sold;//销售量
	double profit;//毛利
	int rest;//剩余数
	double sale;//销售额
	struct Node *next;//后继指针
} Node;

Node *Init_Node() { //初始化函数
	Node *p = (Node *)malloc(sizeof(Node)); //动态分配内存空间
	p->next = NULL;//后继指针指向NULL
	return p;
}

int Isempty_Node(Node *p) { //判断商品信息是否为空
	Node *q = p->next; //将q指针指向首结点
	if (q == NULL) {
		return 1; //q指针为空则返回1
	}
	return 0;//非空返回0
}

void Create_Node(Node *p) { //创建商品管理系统
	char name[MAXSIZE], ID[MAXSIZE], temp;
	double cost;//临时变量存储成本价格
	double price;//临时变量存储售价
	int stock, rest, sold;//存储进货量、剩余量、销售量
	printf("请输入商品的编号:");
	scanf("%s", ID);
	printf("请输入商品的名称:");
	scanf("%s", name);
	printf("请输入商品的成本:");
	scanf("%lf", &cost); //从键盘获取成本
	printf("请输入商品的售价:");
	scanf("%lf", &price); //从键盘获取售价
	printf("请输入商品的进货量:");
	scanf("%d", &stock);
	printf("请输入商品的销售量:");
	scanf("%d", &sold);
	Node *q = (Node *)malloc(sizeof(Node)); //申请新结点空间
	q->next = NULL;//新结点后继置为NULL,方便下一次插入
	strcpy(q->name, name); //商品名称赋值
	strcpy(q->ID, ID);
	q->cost = cost;
	q->price = price;
	q->sold = sold;
	q->stock = stock;
	q->profit = (q->price * q->sold) - (q->cost * q->sold);
	q->rest = q->stock - q->sold;
	q->sale = (q->sold) * (q->price);
	while (p->next) {
		p = p->next;//调整p指针位置,使其指向尾结点
	}
	p->next = q;//尾插法插入新结点
	printf("恭喜您,商品信息插入成功!\n");
}

void Print_LinkList(Node *L) {//打印商品信息
	Node *p = L->next; //将p从首结点开始遍历
	int n = 1;
	printf("-----------------------------------------------------------------------------\n");
	printf(" 序号  商品编号  商品名称  商品成本  商品售价  毛利  剩余数  销售量  进货量  \n");
	printf("-----------------------------------------------------------------------------\n");
	if (L == NULL || L->next == NULL)
		printf("商品信息为空\n");
	else
		while (p) {
			printf("%3d%10s%10s%10.2f%10.2f%10.2f%5d%7d%8d", n, p->ID, p->name, p->cost, p->price, p->profit, p->rest, p->sold,
				p->stock);
			p = p->next;
			n++;
			printf("\n");
		}
	printf("-----------------------------------------------------------------------------\n");
}

int Search_Name(Node *p, char *name) { //按商品名称查询
	Node *q = p->next;
	while (q) {
		if (strcmp(q->name, name) == 0) {
			printf("-----------------------------------------------------------------------------\n");
			printf(" 商品编号  商品名称  商品成本  商品售价  毛利  剩余数  销售量  进货量  \n");
			printf("-----------------------------------------------------------------------------\n");
			printf("%6s%10s%10.2f%10.2f%10.2f%5d%7d%8d", q->ID, q->name, q->cost, q->price, q->profit, q->rest, q->sold, q->stock);
			printf("\n");
			return 1;
		}
		q = q->next;
	}
	printf("-----------------------------------------------------------------------------\n");
	return 0;
}

int Search_ID(Node *p, char *ID) { //按商品名称查询
	Node *q = p->next;
	while (q) {
		if (strcmp(q->ID, ID) == 0) {
			printf("-----------------------------------------------------------------------------\n");
			printf(" 商品编号  商品名称  商品成本  商品售价  毛利  剩余数  销售量  进货量  \n");
			printf("-----------------------------------------------------------------------------\n");
			printf("%6s%10s%10.2f%10.2f%10.2f%5d%7d%8d", q->ID, q->name, q->cost, q->price, q->profit, q->rest, q->sold, q->stock);
			printf("\n");
			return 1;
		}
		q = q->next;
	}
	printf("-----------------------------------------------------------------------------\n");
	return 0;
}

int Search_Rest(Node *p, int rest) {
	Node *q = p->next;
	while (q) {
		if (q->rest == rest) {
			printf("-----------------------------------------------------------------------------\n");
			printf(" 商品编号  商品名称  商品成本  商品售价  毛利  剩余数  销售量  进货量  \n");
			printf("-----------------------------------------------------------------------------\n");
			printf("%6s%10s%10.2f%10.2f%10.2f%5d%7d%8d", q->ID, q->name, q->cost, q->price, q->profit, q->rest, q->sold, q->stock);
			printf("\n");
			return 1;
		}
		q = q->next;
	}
	printf("-----------------------------------------------------------------------------\n");
	return 0;
}

int Delete_name(Node *p, char *name) { //按商品名称删除相关记录
	if (Isempty_Node(p) == 1) {
		return 0;
	} else {
		Node *pre = p;
		Node *current = p->next;
		while (current) {
			if (strcmp(current->name, name) == 0) {
				pre->next = current->next;
				free(current);
				return 1;
			}
			pre = current;
			current = current->next;
		}
		return 0;
	}
}

int Delete_id(Node *p, char *ID) { //按商品编号删除相关记录
	if (Isempty_Node(p) == 1) {
		return 0;
	} else {
		Node *pre = p;
		Node *current = p->next;
		while (current) {
			if (strcmp(current->ID, ID) == 0) {
				pre->next = current->next;
				free(current);
				return 1;
			}
			pre = current;
			current = current->next;
		}
		return 0;
	}
}

void BubbleSort_Sold(Node *mylist) { //按销售量降序排序
	if ((mylist -> next == NULL) || (mylist -> next -> next == NULL)) {
		return;
	}
	Node *head, * pre, * cur, *next, * end, * temp;
	head = mylist;
	end = NULL;
	//从链表头开始将较大值往前移动
	while (head -> next != end) {
		for (pre = head, cur = pre -> next, next = cur -> next; next != end;
			pre = pre -> next, cur = cur -> next, next = next -> next) {
			//相邻的节点比较
			if (cur ->sold < next -> sold) {
				cur -> next = next -> next;
				pre -> next = next;
				next -> next = cur;
				temp = next;
				next = cur;
				cur = temp;
			}
		}
		end = cur;
	}
}

void BubbleSort_profit(Node *mylist) {
	if ((mylist -> next == NULL) || (mylist -> next -> next == NULL)) {
		return;
	}
	Node *head, * pre, * cur, *next, * end, * temp;
	head = mylist;
	end = NULL;
	//从链表头开始将较大值往前移动
	while (head -> next != end) {
		for (pre = head, cur = pre -> next, next = cur -> next; next != end;
			pre = pre -> next, cur = cur -> next, next = next -> next) {
			//相邻的节点比较
			if (cur ->profit < next -> profit) {
				cur -> next = next -> next;
				pre -> next = next;
				next -> next = cur;
				temp = next;
				next = cur;
				cur = temp;
			}
		}
		end = cur;
	}
}

int Edit(Node *p, char *name,int stock,int sold) {
	Node *q = p->next;//将q指向首结点 
	if(Isempty_Node(p)==1){
		return 0;
	}else{
		while (q) {
			if (strcmp(q->name,name)==0){
				q->sold = sold;//更新销售量
				q->stock = stock;//更新进货量
				q->rest = q->stock - q->sold;//更新剩余数
				q->profit = (q->price * q->sold) - (q->cost * q->sold); //再次更新与进货量有关的变量值
				return 1;//找到并修改成功,返回1
			}
			q = q->next;//移动指针
		}
	}
	return 0;//否则返回0
}

void Print(Node *p) {
	Node *q = p->next;
	printf("-----------------------------------------------------------------------------\n");
	printf(" 商品编号   商品名称     商品成本    商品售价   剩余量     销售额  \n");
	printf("-----------------------------------------------------------------------------\n");
	while (q) {
		printf("%6s%10s%10.2f%10.2f%7d%9d%12.2f", q->ID, q->name, q->cost, q->price, q->rest, q->sold, q->sale);
		printf("\n");
		q = q->next;
	}
}

int main(void) {
	Node *p;
	Node *q;
	int choice, flag = 1;
	while (flag) {
		printf("*****************************************\n");
		printf("*              商店商品管理系统         *\n");
		printf("*          [0]退出程序                  *\n");
		printf("*          [1]创建空的信息管理系统      *\n");
		printf("*          [2]插入新记录                *\n");
		printf("*          [3]查看商品信息              *\n");
		printf("*          [4]查询记录                  *\n");
		printf("*          [5]删除记录                  *\n");
		printf("*          [6]修改记录                  *\n");
		printf("*****************************************\n");
		printf("请输入您要执行的操作:");
		scanf("%d", &choice); //从键盘输入操作数编号
		switch (choice) {
			case 0: {
				flag = 0;
				break;
			}
			case 1: {
				p = Init_Node(); //初始化商品信息系统
				if (Isempty_Node(p) == 1) {
					printf("初始化成功!\n");
				}
				break;
			}
			case 2: {
				Create_Node(p);
				break;
			}
			case 3: {
				Node *q = p;
				Node *r = p;
				printf("---------------------------\n");
				printf("[1]按毛利降序排序显示结果\n");
				printf("[2]按销量降序排序显示结果\n");
				printf("[3]直接显示结果\n");
				printf("[4]显示商品销售额\n");
				printf("---------------------------\n");
				printf("请输入执行操作的编码:");
				scanf("%d", &choice);
				switch (choice) {
					case 1: {
						BubbleSort_profit(q);
						Print_LinkList(q);
						break;
					}
					case 2: {
						BubbleSort_Sold(r);
						Print_LinkList(r);
						break;
					}
					case 3: {
						Print_LinkList(p);
						break;
					}
					case 4: {
						Print(p);
						break;
					}
				}
				break;
			}
			case 4: {
				printf("------------------------------\n");
				printf("     [1]按商品名称查询\n");
				printf("     [2]按商品编号查询\n");
				printf("     [3]按商品剩余数查询\n");
				printf("------------------------------\n");
				printf("请输入您要查询的方式编号:");
				scanf("%d", &choice);
				switch (choice) {
					case 1: {
						char s[MAXSIZE], temp;
						printf("请输入您要查询的名称:");
						temp = getchar();
						gets(s);
						int k = Search_Name(p, s);
						if (k == 0) {
							printf("查询失败\n");
						}
						break;
					}
					case 2: {
						char ID[MAXSIZE];
						printf("请输入您要查询的编号:");
						scanf("%s", ID);
						int g = Search_ID(p, ID);
						if (g == 0) {
							printf("查询失败\n");
						}
						break;
					}
					case 3: {
						int rest;
						printf("请输入要查询商品的剩余数:");
						scanf("%d", &rest);
						int h = Search_Rest(p, rest);
						if (h == 0) {
							printf("查找失败!\n");
						}
						break;
					}
				}
				break;
			}
			case 5: {
				printf("------------------------------------\n");
				printf("        [1]按商品名称删除\n");
				printf("        [2]按商品编号删除\n");
				printf("------------------------------------\n");
				printf("请选择您要删除该用户的方式:");
				scanf("%d", &choice);
				switch (choice) {
					case 1: {
						char name[MAXSIZE];
						printf("请输入您要删除的名称:");
						scanf("%s", name);
						int g = Delete_name(p, name);
						if (g == 1) {
							printf("删除成功!\n");
						} else {
							printf("删除失败\n");
						}
						break;
					}
					case 2: {
						char ID[MAXSIZE];
						printf("请输入您要删除的编号:");
						scanf("%s", ID);
						int g = Delete_id(p, ID);
						if (g == 1) {
							printf("删除成功!\n");
						} else {
							printf("删除失败\n");
						}
						break;
					}
					break;
				}
				break;
			}
				case 6: {
					char name[MAXSIZE];
					int stock;
					int sold;
					printf("请输入您要修改的商品的名称:");
					scanf("%s", name);
					printf("请输入更正后的进货量:");
					scanf("%d", &stock);
					printf("请输入更正后的售货量:");
					scanf("%d", &sold);
					int j = Edit(p, name,stock,sold);
					if (j == 0) {
						printf("修改失败!\n");
					} else {
						printf("修改成功!\n");
					}
					break;
				}
				break;
			}
		}
	return 0;
}

菜单效果图:

运行结果图:

(1)初始化系统:

(2)插入新纪录:

(3)查看现有记录

至于修改、查询请诸位爱好者自行摸索!

;