C语言大作业 商品库存管理系统

利用c语言做的课程设计(链表),在DEV C++等编译器上可通过

可根据需求自行更改为图书管理系统或者其他类似的系统


本课题的主要任务是设计和实现一个“商品库存管理系统”,并满足以下要求:

1.系统以菜单方式工作;

2.使用链表或结构数组对商品信息进行管理和维护;

3.使用二进制文件在磁盘上保存商品信息;

4. 链表中各结点或结构数组中各元素包括的商品信息: 商品编号、 商品名

称、 商品类型(如食品、 体育用品、 生活用品、 儿童玩具、 音像制品等) 、

单价、 库存数量、 是否进口等。

5.实现如下基本功能:

(1) 新增商品

(2) 商品浏览 (输出所有商品信息)

(3) 商品删除 (删除指定编号的商品)

(4) 商品修改 (修改指定编号的商品信息)- 14 -

(5) 商品排序 ( 根据商品编号进行排序)

(6) 商品查询统计 ( 提供商品类型、 是否进口方式两种方式对商品进行

统计查询功能)

(7)将商品信息保存到文件存盘 ( 将链表或结构数组中的数据以文件的

形式存盘)

(8)从文件中读入商品信息 ( 将已存盘的文件读入内存, 进行管理)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FORMAT "%d%s%s%d%d%d"
struct product
{
long num;                    //商品编号 
char name[20];               //商品名称 
char type[20];               //商品类型 
long price;	                 //商品单价 
long res;                    //库存数量  
long import;                  //是否进口,1代表进口,0代表非进口 
struct product *next; 
};
int n=0;

struct product *creat();
void print(struct product *head);
void save_to_file(struct product *head);
void change(int search_num,struct product *head);
void insert(struct product *head,struct product *newpro);
void del(int del_num,struct product *head);
void swap(long *a,long *b);
void sort(struct product *head);
void type_inquire(struct product *head);
void import_inquire(struct product *head);

struct product *creat()              /*创建链表*/ 
{
	struct product *head,*L;
	struct product *p1,*p2;
	FILE *fp1;
	fp1=fopen("D:\\goods.txt","r");
	n=0;
	head=(struct product *)malloc(sizeof(struct product));
	head->next=NULL;
	p2=head; 
	p1=(struct product *)malloc(sizeof(struct product));
	fscanf(fp1,FORMAT,&p1->num,p1->name,p1->type,&p1->price,&p1->res,&p1->import);
while (p1->num!=0)
  { n++;
    p2->next=p1;
    p2=p1;
    p1=(struct product *)malloc(sizeof(struct product));                //p1在前方开辟,p2来保存之前的节点 
    fscanf(fp1,FORMAT,&p1->num,p1->name,p1->type,&p1->price,&p1->res,&p1->import);
  }
	free(p1);
    p2->next=NULL; 
    fclose(fp1);                              
    return(head);                        //head指向第一个空白节点,其内的元素为空
}

void print(struct product *head)                  //用于将表在屏幕上输出,一个汉字等于两个空格    
{ 
	struct product *p; 
	printf("\n此表中有%d个记录\n",n);
	p=head->next;
	if(p!=NULL)
	{
		printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");
		printf("│编号│ 商 品 名 称 │ 商 品 类 型 │ 单 价  │ 库存数 │ 是否进口 │\n");
		while(p!=NULL)
		{
			printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");
			if(p->import) 
				printf("│%4d│%13s│%13s│%8d│%8d│    是    │\n",p->num,p->name,p->type,p->price,p->res);
			else 
				printf("│%4d│%13s│%13s│%8d│%8d│    否    │\n",p->num,p->name,p->type,p->price,p->res);
			p=p->next;
		}
		printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");
	}
}

void save_to_file(struct product *head)
{
	struct product *p;
	p=head->next;
	FILE *fp;
	fp=fopen("D:\\goods.txt","w");
	while(p!=NULL){
		fprintf(fp,"%d %s %s %d %d %d \n",p->num,p->name,p->type,p->price,p->res,p->import);
		p=p->next;
	}
	fprintf(fp,"0 0 0 0 0 0");                  //用于作为文件结束标记 
	fclose(fp);
}

void change(int search_num,struct product *head)
{
	struct product *p;
	p=head->next;
	while(p!=NULL&&p->num!=search_num)
		p=p->next;
	if(p==NULL)printf("\n————————表中无所查商品————————\n");
	else {
		printf("请输入新的商品信息,包括:\n编号  名称   类型   单价  库存数量 是否进口(用1表示进口,0表示非进口)\n"); 
		scanf(FORMAT,&p->num,p->name,p->type,&p->price,&p->res,&p->import);
		printf("\n————————商品信息修改成功!————————\n");
	}
}   

void insert(struct product *head,struct product *newpro)
{
	struct product *p;
	p=head;
	while(p->next!=NULL)        
		p=p->next;
    newpro->next=p->next;
	p->next=newpro;
	n++;
	printf("\n————————新增商品成功!————————\n");
}

void del(int del_num,struct product *head)
{
	struct product *p,*q;
	p=head;
	while(p->next&&p->next->num!=del_num)          //查找要删除的元素,最终用p指向要删除的节点的 前驱 节点 
		p=p->next;
	if(p->next==NULL)
	    printf("————————未找到要删除的商品————————\n");
	else {
	    q=p->next;
	    p->next=q->next;
	    free(q);
	    n--;
	    printf("————————商品删除成功!————————");
	} 
}

void swap(long *a,long *b) 
{
	long temp;
	temp=*a;*a=*b;*b=temp;
}

void sort(struct product *head)         //链表的冒泡排序法 
{
	struct product *p;
	struct product *pend=NULL;
	p=head->next;
	char temp[20];
	while(p!=pend)
	{
		while(p->next!=pend)
		{
			if(p->num>p->next->num)
			{
				swap(&p->num,&p->next->num);
				strcpy(temp,p->name);strcpy(p->name,p->next->name);strcpy(p->next->name,temp);
				strcpy(temp,p->type);strcpy(p->type,p->next->type);strcpy(p->next->type,temp);
				swap(&p->price,&p->next->price);
				swap(&p->res,&p->next->res);
				swap(&p->import,&p->next->import);
			}
			p=p->next;
		}
		pend=p;
		p=head->next;
	}
	printf("————————商品排序成功!————————");
}
 
void type_inquire(struct product *head)
{
	struct product *p;
	int count=0;
	char get_type[20];
	p=head->next;
	printf("请输入要统计的商品类型:");
	gets(get_type); 
	printf("找到的所有此类型商品信息如下:\n");
	printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");
	printf("│编号│ 商 品 名 称 │ 商 品 类 型 │ 单 价  │ 库存数 │ 是否进口 │\n");
	while(p!=NULL){
		if(strcmp(p->type,get_type)==0)
		{	count++;
			printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");
			if(p->import) 
				printf("│%4d│%13s│%13s│%8d│%8d│    是    │\n",p->num,p->name,p->type,p->price,p->res);
			else 
				printf("│%4d│%13s│%13s│%8d│%8d│    否    │\n",p->num,p->name,p->type,p->price,p->res);			
		}
		p=p->next;
	}
	printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");
	printf("——————该类型商品共有%d种——————\n",count);
 } 
 
void import_inquire(struct product *head)
{ 
	struct product *p;
	int count=0,k;
	p=head->next;
	printf("统计非进口商品信息,输入0\n统计进口商品信息,输入1\n");
	printf("请输入:");
	scanf("%d",&k);
	printf("找到的所有此类型商品信息如下:\n");
	printf("┌────┬─────────────┬─────────────┬────────┬────────┬──────────┐\n");
	printf("│编号│ 商 品 名 称 │ 商 品 类 型 │ 单 价  │ 库存数 │ 是否进口 │\n");
	while(p!=NULL){
		if(p->import==k)
		{	count++;
			printf("├────┼─────────────┼─────────────┼────────┼────────┼──────────┤\n");
			if(p->import) 
				printf("│%4d│%13s│%13s│%8d│%8d│    是    │\n",p->num,p->name,p->type,p->price,p->res);
			else 
				printf("│%4d│%13s│%13s│%8d│%8d│    否    │\n",p->num,p->name,p->type,p->price,p->res);			
		}
		p=p->next;
	}
	printf("└────┴─────────────┴─────────────┴────────┴────────┴──────────┘\n");
	printf("——————该类型商品共有%d种——————\n",count);
} 

void main()
{
  struct product *head,*newpro,*p;
  int c;
  char get_type[20];
  int len,del_num,change_num,flag=1;
  head=creat();  
  print(head); 
  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"); 
  while(1)
  {
  	printf("     请输入功能选项(1..9)  :"); 
  	scanf("%d",&c);
  	getchar(); 
  switch(c)
  {  
	  case 1:printf("请输入一种新产品的信息,系统将在表中新增该商品信息后输出(用1表示进口,0表示非进口)\n");
	    	newpro=(struct product *)malloc(sizeof(struct product));
		    scanf(FORMAT,&newpro->num,newpro->name,newpro->type,&newpro->price,&newpro->res,&newpro->import);                
		    insert(head,newpro);
	    	print(head);
	    	break;
	  case 2:print(head); 
	    	break;
	  case 3:if(head->next==NULL){
	  			printf("表已空,无法删除\n");
				break;
	  		}
	  		else{
	  			printf("请输入要删除的产品的编号:");
	  	    	scanf("%d",&del_num);
	  	    	del(del_num,head);
	  	    	print(head);
	  	    	break;
				  }
	  case 4:printf("请输入要修改的商品编号:");
	  		scanf("%d",&change_num);
	  		change(change_num,head);
	  		break;
	  case 5:sort(head);
	  		print(head);
	    	break;
	  case 6:type_inquire(head); 
	  		break;
	  case 7:import_inquire(head); 
	  		break;
	  case 8:save_to_file(head);
	  		printf("数据已成功保存至指定文件中\n");
	  		break;
	  case 9:flag=0;break;
	  default:printf("输入有误\n");
  	}
  	if(flag==0)break;
  }
  printf("\n——————————已退出系统——————————\n");
  
}




代码运行部分:增删改查等操作根据已有格式即可

 注:此程序采用文件储存信息,需自行根据要求建立一个txt文档,根据fscanf函数,格式同上(同行用空格分开,隔行用回车),如在此程序中是在D盘

  • 31
    点赞
  • 274
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值