⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shop.cpp

📁 1.本程序用以实现对线性链表的维护、插入、删除、输出等功能
💻 CPP
字号:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

bool boole;                //循环控制
FILE *fp;                  //文件结构体指针

typedef struct shop{      //链表的类型
	int sn;                //编号
	char name[20];         //名称
	char type[20];         //类型
	double price;          //价格
	int number;            //数量
	struct shop *next;    //下一节点指针
}shop;

struct shop *head,*now;   //头结点和当前节点

void init(shop &sh)      //初始化
{
	shop *p,*q;
	now=head=new shop;    //初始化头指针
	p=new shop;
	q=new shop;
	now->sn=1001;
	strcpy(now->name,"彩电");
	strcpy(now->type,"   TCL超频29寸");
	now->price=2100.00;
	now->number=134;
	now->next=NULL;
	now->next=p;
	now=p;
	now->sn=1003;
	strcpy(now->name,"TCL洗衣机");
	strcpy(now->type,"XQB45-SL");
	now->price=879.50;
	now->number=356;
	now->next=NULL;
	now->next=q;
	now=q;
	now->sn=1005;
	strcpy(now->name,"TCL");
	strcpy(now->type,"X45-SL");
	now->price=999.90;
	now->number=56;
	now->next=NULL;
}//init

void del()     //删除指定的商品
{
	int snu,i;
	char c[1];
	shop *p;
	boole=1;
	if(head==NULL) { cout<<"   <!!>  列表为空  <!!>"<<endl; return; }  //判断头指针是否为空
    cout<<">> 请输入要销毁的商品编号(4位):";
L1: cin>>snu;
	while(snu<1000||snu>9999)     //编号为4位
	{
		cout<<">> <!> 输入有误,请重新输入(4位): ";
		cin>>snu;
	}
    i=1;
	now=head;	
	while(now!=NULL&&boole)   //查找该编号是否存在
	{
		if(now->sn!=snu) 
		{
			now=now->next;
			i++;
			boole=1;
		}
		else boole=0;
	}//while
	p=now;			
	if(p==NULL) 
	{ 
		cout<<">> <!> 该编号不存在,请重新输入:";
		goto L1; 				            //如果编号不存在,返回
	}			
	else
	{
    L2: cout<<">> !>!> 您确定要销毁Y/N:";  //确认操作
		cin>>c;
		while(strlen(c)>1) 
		{
			cout<<">> !>!> 您确定要销毁Y/N:";  //确认操作
			cin>>c;
		}//while
		if(*c=='n'||*c=='N') return;
		if(*c=='y'||*c=='Y')
		{    //删除并输出删除的信息
			cout<<"  > 销毁 :"<<endl<<" -> "<<now->sn<<"  "<<now->name<<"  "<<now->type<<"  "<<now->price<<"  ";
		    cout<<now->number<<endl<<endl;
			now=head;
			if(p==head)  head=p->next;   //删除第一个节点,改变头指针
			else
			{
				while(now->next!=p)  now=now->next;  //找要删除的前区
				now->next=p->next;   
				delete p;                            //释放空间
			}//else
		}//if(*c=='y'||*c=='Y')
		else goto L2;
	}//else		
	cout<<" -> 销毁位置为 "<<i<<" "<<endl<<endl;   //输出删除的位置
}//del

void output()     //打印链表
{
	
	int j=1;   //列表个数
	if(head==NULL) {cout<<"   <!!>  列表为空  <!!>"<<endl; return;}
	else
	{
	now=head;
	cout<<endl<<"       [编号]    [名称]    [ 类型]    [价格]    [数量]    "<<endl;
	while(now)      //逐步输出每条信息
		{	         
			cout<<"  ->  List "<<j++<<":"<<endl;
			cout<<"       "<<now->sn<<"       ";
			cout<<now->name<<"        "<<now->type<<"        ";
			cout<<now->price<<" ¥/件   "<<now->number<<endl<<endl;
			now=now->next;
		}//while
	}//else
}//output

int dif_sn(int sns)    //检验编号
{	
	boole=1;
L0: now=head;
	while(now)         //判断该编号是否已经存在
	{
		if(now->sn==sns) 
		{ 
			cout<<">> <!> 该编号已经存在,请重新输入:";
			cin>>sns;
			goto L0;
		}
		now=now->next;
	}//while
	return sns;
}//dif_sn

void adds(shop &s,int sns,char na[20],char ty[20],double pr,int num)   //插入新值
{	         
	boole =1;
	shop *p,*q;         //当前指针
	q=new shop;
	if(head==NULL) { head=new shop;now=head; p=NULL;}  //若链表为空则给head赋值
	else
	{
		p=head;
		while(boole&&p)   //使编号按大到小排列
		{		
			if(p->sn<sns) { p=p->next; boole=1;}
			else boole=0;
		}//while		
		now=head;
		if(p==head)  head=q; //插入第一个节点,改变头指针
		else
		{
			while(now->next!=p)  now=now->next; //寻找插入的编号的前区
			now->next=q;
		}
		now=q;
	}
	now->sn=sns;
	strcpy(now->name,na);
	strcpy(now->type,ty);
	now->price=pr;
	now->number=num;	
	now->next=p;
	//cout<<"  -> 添加位置为 "<<i<<" "<<endl<<endl;
}//adds 

void search_sn()      //按编号查询
{
	int i=0,s_sn;	
	cout<<">> 请输入要查询的编号(4位):";
L0: cin>>s_sn;
	if(s_sn<1000||s_sn>9999)   //编号为4位
	{
		cout<<">> <!> 输入有误,请重新输入(4位): ";
		goto L0;
	}
	while(now)         //查找输入的编号是否存在
	{
		if(now->sn==s_sn)
		{
			cout<<endl<<" -> "<<now->sn<<"  "<<now->name<<"  "<<now->type<<"  "<<now->price<<"  ";
			cout<<now->number<<endl<<endl;  			
			i++;
		} 
		now=now->next;
	}//while
	if(i==0) { cout<<"   <!>  不存在  <!>"<<endl; return; }
}//search_sn

void search_type()     //按类型查询
{
	int i=0;     //找到个数
	char s_type[20];
	puts(">> 请输入要查询的类型:");
	gets(s_type);
	while(now)          //查找输入的类型是否存在
	{
		if(strcmp(now->type,s_type)==0)
		{
			cout<<endl<<" -> "<<now->type<<"  "<<now->sn<<"  "<<now->name<<"  "<<now->price<<"  ";
			cout<<now->number<<endl;  			
			i++;
		}
		now=now->next;
	}//while
	if(i==0) { cout<<"   <!>  不存在  <!>"<<endl; return; }
	else cout<<" -> 找到"<<i<<"个"<<endl;
}//search_type

void search_name()      //按商品名称查询
{
	int i=0;     //找到的个数
	char s_name[20];
	puts(">> 请输入要查询的商品名称:");
	gets(s_name);
    cout<<endl;
	while(now)         //查找输入的名称是否存在
	{		
		if(strcmp(now->name,s_name)==0)   
		{
			cout<<" -> "<<now->name<<"  "<<now->sn<<"  "<<now->type<<"  "<<now->price<<"  ";
			cout<<now->number<<endl;  			
			i++;
		}		
		now=now->next;
	}//while
	cout<<endl;
	if(i==0) { cout<<"   <!>  不存在  <!>"<<endl; return; }
	else cout<<" -> 找到 "<<i<<" 个"<<endl;
}//search_name

void search()     //查询
{
	char sl[1],c[1];
	if(head==NULL){ cout<<"   <!!>  列表为空  <!!>"<<endl; return; }	
L5: cout<<endl<<"<   1. 编号"<<"  2. 类型"<<"  3. 商品名称   >"<<endl;
	now=head;
L8: cout<<">> 请输入要执行的操作:";
	cin>>sl;  if(strlen(sl)>1) goto L8;
	switch(*sl)
	{
	case '1':
		search_sn();     //编号查询
		break;
	case '2':
		search_type();   //类型查询
		break;
	case '3':
		search_name();   //名称查询
		break;
	default:
		cout<<"   <!>  输入不正确  <!>"<<endl;
		goto L5;
		break;
	}
	cout<<"  <- 完成 ->"<<endl;
	cout<<endl<<"<   S. 查询  "<<"B. 返回   >"<<endl;
L6: cout<<">> 请输入要执行的操作:";
	cin>>c; if(strlen(c)>1) goto L6;
	if(*c=='s'||*c=='S') goto L5;
	else if(*c=='b'||*c=='B') return;
	else { cout<<"   <!>  输入不正确  <!>"<<endl; goto L6; }
}//search

void new_arrange(shop *p)     //修改后重新排序
{	
	if(p==head) { head=p->next; }   //修改的为头结点
	else //删除该结点
	{
		now=head;
		while(now->next!=p) now=now->next;  //寻找修改的结点的前区
		now->next=p->next;		
	}//else
	adds(*p,p->sn,p->name,p->type,p->price,p->number);  //重新插入
}//new_arrange

void modify()       //修改
{
	int s_sn,i;
	char c[1];
	shop *p;
	if(head==NULL) { cout<<"   <!!>  列表不存在  <!!>"<<endl; return; }
L0: cout<<">>  请输入要修改的商品编号(4位):";
    cin>>s_sn; cout<<endl; i=0;
	while(s_sn<1000||s_sn>9999)   //编号为4位
	{
		cout<<">> <!> 输入有误,请重新输入(4位): ";
		cin>>s_sn; cout<<endl;;
	}	
	now=head;
	while(now)    //查找要修改的编号是否存在
	{
		if(now->sn==s_sn) { i++; p=now; break; }   //p指向找到的当前指针
		now=now->next;
	}//while
	if(i==0) { cout<<"   <!>  不存在  <!>"<<endl;}
	else         //选择要修改的数据
	{
    L1: cout<<"<    1. 编号"<<" 2. 类型"<<" 3. 名称"<<" 4. 价格"<<" 5. 数量   >"<<endl;
        cout<<">> 请选择要修改的:";
		cin>>c; 
		while(strlen(c)>1) { cout<<">> 请选择要修改的:";	cin>>c; }
		switch(*c)
		{
		case '1':
			cout<<" **请输入新的编号: ";
        L2: cin>>s_sn;
			while(s_sn<1000||s_sn>9999) //编号为4位
			{
				cout<<">> <!> 输入有误,请重新输入(4位): ";
				cin>>s_sn; cout<<endl;
			}//while
			now=head;
			while(now)
			{
				if(now->sn==s_sn) { cout<<" >> 该编号已经存在:";goto L2; }				
				now=now->next;
			}//while
			p->sn=s_sn;       //修改编号			
			new_arrange(p);   //重新排列
			break;
		case '2':
			cout<<" **请输入新的类型: ";
			cin>>p->type;//修改类型
			break;
		case '3':
			cout<<" **请输入新的名称: ";
			cin>>p->name;//修改名称
			break;
		case '4':
			cout<<" **请输入新的价格: ";
			cin>>p->price;  //修改价格
			break;
		case '5':
			cout<<" **请输入新的数量: ";
			cin>>p->number;  //修改数量
			break;
		default:
			cout<<"   <!>  输入不正确  <!>"<<endl;
			goto L1;
			break;
		}//switch
	}//else
	cout<<endl<<"     <- 完成 ->"<<endl;
	cout<<endl<<"<   C. 修改  "<<"B. 返回   >"<<endl;
L6: cout<<">> 请输入要执行的操作:";
	cin>>c;   
	while(strlen(c)>1) { cout<<">> 请输入要执行的操作:";cin>>c; }
	if(*c=='c'||*c=='C') goto L0;
	else if(*c=='b'||*c=='B') return;
	else { cout<<"   <!>  输入不正确  <!>"<<endl; goto L6; }
}//modify

void take_sh()     //提货
{
	int s_sn,i,s_num=0;
	char c[1];
	if(head==NULL) { cout<<"   <!!>  列表不存在  <!!>"<<endl; return; }
L2: cout<<">> 请输入商品编号(4位):";  i=0;   //如果找到则i值不为0
    cin>>s_sn; cout<<endl;
	if(s_sn<1000||s_sn>9999)   //编号为4位
	{
		cout<<">> <!> 输入有误,请重新输入(4位): ";
		cin>>s_sn;
	}	
	now=head;
	while(now)   //查找是否存在该编号
	{
		if(now->sn==s_sn)    //找到后输出数据
		{ 
			i++;
			cout<<endl<<" -> "<<now->sn<<"  "<<now->name<<"  "<<now->type<<"  "<<now->price<<"  "<<now->number;
			break;
		}
		now=now->next;
	}//while
	if(i==0) { cout<<"   <!>  不存在  <!>"<<endl; }
	else
	{
    	cout<<endl<<">> 请输入要提取的数量:";
        cin>>s_num;		
		while(s_num>now->number||s_num<=0)   //检测输入的数量 
		{ 
			cout<<endl;
			cout<<"   <!>  库存不足或输入有误  <!>"<<endl;		
			cout<<endl<<">> 请输入要提取的数量:";
			cin>>s_num;
		}//while
		now->number=now->number-s_num;   //更新数据
		if(now->number==0) cout<<endl<<"       *** <库存为0请进货> ***"<<endl;
		cout<<"            <- 完成 ->"<<endl;
	}//else
	cout<<endl<<"<   T. 提货  "<<"B. 返回   >"<<endl;
L1: cout<<">> 请输入要执行的操作:";
	cin>>c;   while(strlen(c)>1) { cout<<">> 请输入要执行的操作:"; cin>>c; }
	if(*c=='t'||*c=='T') goto L2;
	else if(*c=='b'||*c=='B') return;
	else goto L1;
}//take_sh

void write(shop &sh)       //保存数据
{
   	int i=0;
	if((fp=fopen("shop.rec","wb"))==NULL)    //保存在shop.rec文件中
	{
		printf("Can't open file shop.rec");
		exit(0);
	}
	now=head;
	while(now)
	{		
		fwrite(now,sizeof(sh),1,fp);   //将信息保存
		now=now->next;
	}
	fclose(fp); //关闭文件
	return;
}//write

void main()        //主函数
{
	shop s;
	int i=0;	
	char ch[1]; 
	head=NULL;

	if((fp=fopen("shop.rec","rb"))==NULL)    //如果打不开则还原为最原始的数据
	{	
		init(s);    //初始化	
	} 
	else
	{
		while(fread(&s,sizeof(s),1,fp)==1)
			adds(s,s.sn,s.name,s.type,s.price,s.number);   //还原保存的数据
		fclose(fp);
	}//else	

	cout<<endl<<"    ぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁぁ"<<endl;
	cout<<"       *           某商厦家电部的库存              *"<<endl;             
	cout<<"       *   1. 新增"<<"                 2. 修改         *"<<endl;  
	cout<<"       *   3. 销毁"<<"                 4. 总览         *"<<endl;  
    cout<<"       *   5. 查询"<<"                 6. 提货         *"<<endl;  
 	cout<<"       *   7. 退出                                 *"<<endl;                                  
	cout<<endl<<"    ぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜぜ"<<endl;
L3: cout<<">>   请选择要执行的操作: ";
L2: cin>>ch; if(strlen(ch)>1) goto L3;
	switch(*ch)
	{
	case '1':
		int sns,num;
		char na[20],ty[20];
		double pr;
		cout<<">> -SN-编号(4位): ";
		boole=1;
   L0:  cin>>sns;
		if(sns<1000||sns>9999) //编号为4位
		{
			cout<<">> <!> 输入有误,请重新输入(4位): ";
			goto L0;
		}
		sns=dif_sn(sns);          //检测编号
		puts(">> -NAME-名称:");
		gets(na);
		puts(">> -TYPE-类型:");
		gets(ty);
		cout<<">> -PRICE-价格:";
		cin>>pr;
		cout<<">> -NUMBER-数量:";
		cin>>num;
		adds(s,sns,na,ty,pr,num);	//添加	
		break;
	case '2':
		modify();   //修改
		break;
	case '3':
		del();      //删除
		break;
	case '4':
		output();   //输出
		break;
	case '5':
		search();   //查询
		break;
	case '6':
		take_sh();   //提货
		break;
	case '7':
		char c[1];
    L4: cout<<">> !>!> 退出并保存Y/N:";
		cin>>c;  if(strlen(c)>1) goto L4;
		if(*c=='Y'||*c=='y') { write(s); exit(0); }
		else if(*c=='N'||*c=='n') goto L3;
		else goto L4;
		break;
	default:
		cout<<">> <!> -输入不正确,请重新输入:";
		goto L2;
		break;
	}//switch
	cout<<endl<<"<  --1. 新增   "<<"2. 修改  "<<"3. 销毁  "<<"4. 总览  "<<"5. 查询  "<<"6. 提货  "<<"7. 退出   >"<<endl;
	cout<<endl;
	goto L3;
}//main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -