工程实践1 项目:小超市商品管理系统

#include<stdio.h>
#include<stdlib.h>

typedef struct item
{
	int num;//编号
	char name[10];//名字
	int cost;//进价(成本)
	int price;//售价
	int initial_quantity;//进货量(初始量)
	int sale_quantity;//销售量
	int waste_quantity;//报废量
	struct item*next;
}Item;
/*商品节点*/
Item* CreatList()
{
	Item* head=NULL,*cur,*tail;
	int num;
	scanf("%d",&num);
	while(num!=0)
	{
		cur = (Item*)malloc(sizeof(Item));
		cur->next=NULL;
		cur->num=num;
		scanf("%s%d%d%d%d%d",cur->name,&(cur->cost),&(cur->price),&(cur->initial_quantity),&(cur->sale_quantity),&(cur->waste_quantity));
		if(head==NULL)
		{
			head = cur;
		}
		else
		{
			tail->next = cur;
		}
		tail = cur;
		scanf("%d",&num);
	}
	return head;
}
/*尾插法创建商品链表*/
void ListSaveFile(Item *head)
{
	FILE *fp;
	Item*p = head;
	if((fp = fopen("D:\\stu.dat","w"))==NULL)
	{
		printf("cannot open file\n");
		return;
	}
	while(p!=NULL)
	{
		if(fwrite(p,sizeof(Item),1,fp)!=1)//指针域如何存取
 		printf("file write error\n");	
 		p = p -> next;
	}
	fclose(fp);
}
/*将链表数据保存到文件"D:\\stu.dat","w"*//*"w":只写,为输出新建一个文本文件(如文件存在,则删除文件重建)*/
Item* FileLoadList()
{
	Item*head=NULL,*cur,*tail;
	Item temp;
	FILE *fp;
	if((fp=fopen("D:\\stu.dat","r"))==NULL)
	{ 	
		printf("cannot open file\n");
  		return NULL;	
  	}
	while(fread(&temp,sizeof(Item),1,fp)==1)//数据如何读取
 	{	
	 	cur = (Item *)malloc(sizeof(Item));
 	 	*cur = temp;	
		cur -> next=NULL;
 	   	if(head == NULL)
			head = cur;
		else
	  		tail->next=cur;
  		tail = cur;	  	
 	}
 	fclose(fp); 
 	return head; 
}
/*将文件"D:\\stu.dat","r"数据读取到链表*//*"r":只读,为输入打开一个文本文件(如文件不存在,则返回NULL)*/
void AddList(Item* head,int location)//location>=2,位置从1开始,第一个节点的位置为1,把第location个往后挤压一格,占据它的原位置;可插入的位置为2到尾部后面一格
{
	int i=1;
	Item *cur,*p=head;
	cur = (Item*)malloc(sizeof(Item));
	scanf("%d%s%d%d%d%d%d",&(cur->num),cur->name,&(cur->cost),&(cur->price),&(cur->initial_quantity),&(cur->sale_quantity),&(cur->waste_quantity));
	cur->next=NULL;
	while (i <location-1 && p->next!= NULL)//找到要插入元素的位置
	{
		p = p->next;
		i++;
	}
	cur->next=p->next;
	p->next=cur;

}
/*在指定位置插入商品*/
void ChangeList(Item* head)
{
	int num;
	int flag = 0;//未查找到对应商品
	Item *p = head; 
	printf("请输入要修改的商品的编号\n");
	scanf("%d",&num);
	while(p!=NULL)
	{
		if(p->num==num)
		{
			printf("请输入商品的新信息:\n");
			scanf("%d%s%d%d%d%d%d",&(p->num),p->name,&(p->cost),&(p->price),&(p->initial_quantity),&(p->sale_quantity),&(p->waste_quantity));
			flag=1;
		}
		p=p->next;
	}
	if(flag==0)
	{
		printf("未找到对应商品\n");
	}
}
/*修改商品信息*/
Item* DeleteList(Item* head)
{
	Item *p1,*p2;
	p1 = head;
	int num;
	printf("请输入要删除的商品的编号\n");
	scanf("%d",&num);
	while(p1!=NULL)
	{
		if(p1->num==num)//要删除的点
		{
			if(p1==head)
			{
				head = p1->next;//表头
			}
			else
			{
				p2->next=p1->next;//表中与结尾
			}
		}
		else//不删除的点
		{
			p2=p1;//p2是前一个点
		}
		p1=p1->next;
	}
	return head;
}
/*删除指定位置商品信息*/
void SaleConsult(Item *head)
{
	Item *p = head;
	printf("—--商品销售情况统计结果----\n");
	printf("商品编号 商品名称 销售量(个)  毛利(元)\n");
	while(p!=NULL)
	{
		printf("%6d%8s%10d%10d\n",p->num,p->name,p->sale_quantity,(p->sale_quantity)*(p->price-p->cost));
		p=p->next;
	}
}
/*统计商品的销售情况*/
void ItemSearch(Item* head)
{
	int num;
	int flag = 0;//未查找到对应商品
	Item *p = head; 
	printf("请输入要查找的商品的编号\n");
	scanf("%d",&num);
	while(p!=NULL)
	{
		if(p->num==num)
		{
			printf("商品%d的信息如下:\n",num);
			printf("编号:%d\n名字;%s\n进价:%d\n售价:%d\n进货量:%d\n销售量:%d\n报废量:%d\n",p->num,p->name,p->cost,p->price,p->initial_quantity,p->sale_quantity,p->waste_quantity);	
			flag=1;
		}
		p=p->next;
	}
	if(flag==0)
	{
		printf("未找到对应商品\n");
	}
}
/*查询商品信息*/
void ItemShow_1(Item* head)
{
	printf("******************全部商品信息*******************\n");
	Item * p=head;
		printf(" 编号  名字  进价  售价  进货量 销售量 报废量\n");
	while(p!=NULL)
	{ 
		printf("%4d%5s%6d%6d%7d%8d%9d\n",p->num,p->name,p->cost,p->price,p->initial_quantity,p->sale_quantity,p->waste_quantity);
	   	p=p->next;	
  	}
	printf("*************************************************\n");
}
/*展示全部商品信息(按原顺序展示)*/
int ListSaveArray(Item* head,Item a[])
{
	Item *p=head;
	int n=0;
	while(p!=NULL)
	{
		a[n++]=*(p);
		p=p->next;
	}
	return n;
}
/*链表数据存储到结构体数组*/
void ArraySort (Item a[],int n)
{
	Item t;
	int i,j;
	for(i=0;i<n-1;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i].price<a[j].price)
			{
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}
	}
}
/*结构体数组排序,按售价从高到低排*/
void ItemShow_2(Item *head)
{
	int n;//节点个数<=10
	int i;
	Item a[10];
	n=ListSaveArray(head,a);//链表数据存储到结构体数组便于排序
	ArraySort(a,n);//结构体数组排序,按售价从高到低排
	printf("******************全部商品信息*******************\n");
	printf(" 编号  名字  进价  售价  进货量 销售量 报废量\n");
	for(i=0;i<n;i++)
	{
		printf("%4d%5s%6d%6d%7d%8d%9d\n",a[i].num,a[i].name,a[i].cost,a[i].price,a[i].initial_quantity,a[i].sale_quantity,a[i].waste_quantity);
	}
	printf("*************************************************\n");
}
/*展示全部商品信息(按售价从高到低展示)*/
void print(Item *head)
{	
	printf("**********************************\n");
	Item * p;
	p=head;
	while(p!=NULL)
	{ 
		printf("%d\n%s\n%d\n%d\n%d\n%d\n%d\n",p->num,p->name,p->cost,p->price,p->initial_quantity,p->sale_quantity,p->waste_quantity);
	   	p=p->next;	
  	}
	printf("**********************************\n");
}	

int main ()
{
	printf("--------------------------欢迎使用超市商品管理系统--------------------------\n");
	printf("\n");
	printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
	printf("*                               主菜单                                      *\n");
	printf("*------------------------*请选择需要执行的功能*---------------------------- *\n");
	printf("*                         按1:创 建 商 品 链 表 档 案                      *\n");
	printf("*                         按2:修 改 商 品 信 息                            *\n");
	printf("*                         按3:统 计 商 品 销 售                            *\n");
	printf("*                         按4:查 询 商 品 信 息                            *\n");
	printf("*                         按5:展 示 全 部 商 品 信 息                      *\n");
	printf("*                         按0:退 出 系 统                                  *\n");
	printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
	
	int menu;//主菜单
	int menu_2;//2号菜单(修改商品信息菜单)
	int menu_5;//5号菜单(展示全部商品信息菜单)
	int Add_location;//要修改的商品的位置,>=1
	scanf("%d",&menu);
	Item*head;
	while(1)
	{
		switch(menu)
		{
			case 1://按1:创 建 商 品 链 表 档 案
				printf("欢迎来到---创建商品链表档案---界面\n");
				printf("请按行依此输入商品的\n");
				printf("编号:\n名字:\n进价:\n售价:\n进货量:\n销售量:\n报废量:\n");
				printf("---输入0停止读入信息---\n");
				head = CreatList();
				printf("----商品链表档案创建成功----\n");
				ListSaveFile(head);
				printf("----商品链表档案存取文件成功----\n");
				system("pause");
				system("cls");
				break;
			case 2://按2:修 改 商 品 信 息
				printf("欢迎来到---修 改 商 品 信 息---界面\n");
				printf("* 按1:在指定位置插入商品信息 *\n");
				printf("* 按2:修改商品信息 *\n");
				printf("* 按3:删除指定编号的商品的信息 *\n");
				scanf("%d",&menu_2);
				system("cls");
				switch(menu_2)
				{
					case 1://在指定位置插入商品信息
						printf("欢迎来到---在指定位置插入商品信息---页面\n");
						head = FileLoadList();
						if(head == NULL)
						{
							printf("文件读取失败\n");
							exit(0);
						}
						else
						{
							printf("文件读取成功\n");
						}
						printf("请输入要插入的位置\n");
						scanf("%d",&Add_location);
						printf("请按行依此输入商品的\n");
						printf("编号:\n名字:\n进价:\n售价:\n进货量:\n销售量:\n报废量:\n");
						AddList(head,Add_location);
						printf("商品信息插入成功\n");
						ListSaveFile(head);
						printf("----商品链表档案存取文件成功----\n");
						system("pause");
						system("cls");
						break;
					case 2://修改商品信息
						printf("欢迎来到---修改商品信息---页面\n");
						head = FileLoadList();
						if(head == NULL)
						{
							printf("文件读取失败\n");
							exit(0);
						}
						else
						{
							printf("文件读取成功\n");
						}
						ChangeList(head);
						printf("商品信息修改成功\n");
						ListSaveFile(head);
						printf("----商品链表档案存取文件成功----\n");
						system("pause");
						system("cls");
						break;
					case 3://删除指定位置商品信息
						printf("欢迎来到---删除指定编号的商品信息---页面\n");
						head = FileLoadList();
						if(head == NULL)
						{
							printf("文件读取失败\n");
							exit(0);
						}
						else
						{
							printf("文件读取成功\n");
						}
						head=DeleteList(head);
						printf("商品信息删除成功\n");
						ListSaveFile(head);
						printf("----商品链表档案存取文件成功----\n");
						system("pause");
						system("cls");
						break;
				}
				break;
			case 3://按3:统 计 商 品 销 售
				printf("欢迎来到---统 计 商 品 销 售---页面\n");
				head = FileLoadList();
				if(head == NULL)
				{
					printf("文件读取失败\n");
					exit(0);
				}
				else
				{
					printf("文件读取成功\n");
				}
				SaleConsult(head);
				system("pause");
				system("cls");
				break;
			case 4:
				printf("欢迎来到---查 询 商 品 信 息---页面\n");
				head = FileLoadList();
				if(head == NULL)
				{
					printf("文件读取失败\n");
					exit(0);
				}
				else
				{
					printf("文件读取成功\n");
				}
				ItemSearch(head);
				system("pause");
				system("cls");
				break;
			case 5:
				printf("欢迎来到---展 示 全 部 商 品 信 息---页面\n");
				printf("* 按1:按原顺序展示 *\n");
				printf("* 按2:按售价从高到低展示 *\n");
				scanf("%d",&menu_5);
				system("cls");
				switch(menu_5)
				{
					case 1:
						printf("欢迎来到---展示全部商品信息(按原顺序展示)---页面\n");
						head = FileLoadList();
						if(head == NULL)
						{
							printf("文件读取失败\n");
							exit(0);
						}
						else
						{
							printf("文件读取成功\n");
						}
						ItemShow_1(head);
						system("pause");
						system("cls");
						break;
					case 2:
						printf("欢迎来到---展示全部商品信息(按售价从高到低展示)---页面\n");
						head = FileLoadList();
						if(head == NULL)
						{
							printf("文件读取失败\n");
							exit(0);
						}
						else
						{
							printf("文件读取成功\n");
						}
						ItemShow_2(head);
						system("pause");
						system("cls");
						break;
				}
				break;
			case 0://按0:退 出 系 统 
				exit(0);
				break;
		}
		
		printf("*------------------------*请选择需要执行的功能*---------------------------- *\n");
		printf("*                         按1:创 建 商 品 链 表 档 案                      *\n");
		printf("*                         按2:修 改 商 品 信 息                            *\n");
		printf("*                         按3:统 计 商 品 销 售                            *\n");
		printf("*                         按4:查 询 商 品 信 息                            *\n");
		printf("*                         按5:展 示 全 部 商 品 信 息                      *\n");
		printf("*                         按0:退 出 系 统                                  *\n");
		printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
		scanf("%d",&menu);
		}
		return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值