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

📄 e1.cpp

📁 这是用VC++语言做的一个链表的建立插入 删除等操作
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>
#include<stdio.h>

typedef struct ET{
	char name[50];
	char type[50];
	float price;
	int number;
}ElemType;

FILE *f1,*f2;
char name[50];char type[50];float price;int number;

typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
LNode *p,*q,*s,*r;
LinkList H,L;
int j,i;char m;
ElemType e[50];

void putout(LinkList &L)
{
	cout<<"产品名\t"<<"品牌\t"<<"价格(¥)\t"<<"库存量(台)"<<endl;
	s=L;
	while(s){
		
		cout<<s->data.name<<"\t"<<s->data.type<<"\t"<<s->data.price<<"\t\t"<<s->data.number<<"\t->"<<endl; ;
		s=s->next ;
	}
	cout<<endl;
}


void charu(LinkList &L,LNode* s)
{
	if (s->data .price <=L->data .price ) {  
        s->next = L;  L = s;
      }//if
      else {
        q = L;
       while(q)
	   {
		   p=q->next ;
		   if(p!=NULL&&s->data .price >q->data .price &&s->data.price <=p ->data .price )
		   {q->next = s;  s->next = p;break;}
		   else if(s->data.price >q->data .price&&p==NULL ){q->next =s;s->next =NULL;break;} 
           else
		   {q = q->next;}   
      }
	  }

}

void CreateList_L(LinkList &L, ElemType A[]) 
{
	  L=NULL;
      do{
        p = new LNode;
ab1:	cout<<"bookname:";
		cin>>A[i].name ;
		for(j=0;j<i;j++)
		{if(strcmp(A[i].name ,A[j].name )==0)
		{cout<<"the produce has existed!";goto ab1;}}
		cout<<"type:";
		cin>>A[i].type ;
		cout<<"price:";
		cin>>A[i].price ;
ab6:	cout<<"number:";
		cin>>A[i].number ;if(A[i].number <0){cout<<"Error!";goto ab6;}
		if(A[i].price >0){
        p->data = A[i];
		if(L==NULL){L=p;L->next =NULL;}
		else
		charu(L,p);
		}
		i++;
      }while(A[i-1].price >0);
	  strcpy(A[i].name ," ");//p=NULL;
	  putout(L);
}// CreateList_L


LNode* LocateElem_L( LinkList &L,ElemType e ) 
{
	  cout<<"please enter the produce's name:";
	  cin>>e.name ;
	  for(p=L;p!=NULL;p=p->next )
	  {if(strcmp(p->data.name ,e.name)==0)return p;
	  else if(strcmp(p->data.name ,e.name)!=0&&p->next ==NULL)
	  { cout<<"the produce does not exist!"<<endl;return p->next ;}
	  }	  
} // LocateElem_L

void ListInsert_L( LinkList &L, LNode* s ) 
{
			s = new LNode;
ab2:cout<<"enter the produce's name(char):";
		cin>>s->data .name ;
		for(p=L;p;p=p->next )
		{if(strcmp(s->data .name,p->data .name )==0) 
		{cout<<"the produce has existed!";goto ab2;}}
		cout<<"type(char):";
		cin>>s->data .type ;
ab3:cout<<"price:(float)";
	cin>>s->data.price ;if(s->data .price <0){cout<<"Error!";goto ab3;}
ab4:cout<<"number(int):";
	cin>>s->data .number ;if(s->data .number <=0){cout<<"Error!";goto ab4;}
      if (s->data .price <=L->data .price ) {  
        s->next = L;  L = s;
      }//if
      else {
        q = L;
       while(q)
	   {
		   p=q->next ;
		   if(p!=NULL&&s->data .price >q->data .price &&s->data.price <=p ->data .price )
		   {q->next = s;  s->next = p;break;}
		   else if(s->data.price >q->data .price&&p==NULL ){q->next =s;s->next =NULL;break;}  
           else
		   {q = q->next;}   
      }
	  }//else
} // ListInsert_L

void ListDelete_L( LinkList &L, LNode* p, ElemType &e )
 {
		p=LocateElem_L( L,e ); 
     if(p!=NULL)
	 {	if (p == L) {                  
        L = p->next;
      }//if
      else {
        q = L;
        while (q->next->data .name  != p->data .name  ) q = q->next;   
        q->next = p->next ;                  
      }//else
     strcpy(e.name,p->data.name)  ;
	 strcpy(e.type,p->data .type) ;
	 e.number =p->data .number;
	 e.price=p->data .price;
	 delete p;}
	 else cout<<"the produce is not existed!"<<endl;
}// ListDelete_L

void save()
{
f1=fopen("a.txt","wb+");
  p=L; 
  while(p)
  {
	  fwrite(&p->data,sizeof(ET),1,f1);
	  p=p->next;
  }
  fclose(f1);
}

void ListInsert( LinkList &L,char name[50],char type[50],float price,int number)
{
    r=new LNode;
    strcpy(r->data .name ,name);
	strcpy(r->data.type ,type);
	r->data.price =price;
	r->data .number =number;
	p->next =r;r->next =NULL;
}

void read()
{
f2=fopen("a.txt","r");
if(!f2)cout<<"no file!"<<endl;
else
{
  p=new LNode;L=p;L->next =NULL; 
  while(!feof(f2))
  {     fread(&p->data,sizeof(ET),1,f2);
         if(feof(f2)) break;
			 ListInsert(L,p->data .name ,p->data .type ,p->data .price ,p->data .number );
	p=p->next ;
   }
  fclose(f2);
  s=L;
  while(s->next->next  )
  {s=s->next; }delete s->next ;s->next =NULL;
}
}


void update(LinkList &L,LNode* p)
{
ad:	p=LocateElem_L( L,*e );if(p==NULL)goto ad;else
	cout<<"产品名\t"<<"品牌\t"<<"价格(¥)\t"<<"库存量(台)"<<endl;
	cout<<p->data .name<<"\t"<<p->data .type <<"\t"<<p->data .price <<"\t\t"<<p->data .number <<endl; 
loop:	cout<<"。。。。。。"<<endl
			<<"a.修改name"<<endl
			<<"b.修改type"<<endl
			<<"c.修改number"<<endl
			<<"d.修改price"<<endl
			<<"e.退出"<<endl
			<<"choose:";
	cin>>m;
	switch(m)
	{
	case('a'):
ac1:	cout<<"name(char):";
		cin>>name ;
		for(s=L;s;s=s->next )
		{if(strcmp(name,s->data .name )==0) 
		{cout<<"the produce has existed!";goto ac1;}}
		strcpy(p->data .name ,name);save();goto loop;
	case('b'):
		cout<<"type(char):";
		cin>>type;
		strcpy(p->data .type ,type);save();goto loop;
	case('d'):
ac2:	cout<<"price(float)";
		cin>>price;
		if(price<=0){cout<<"Error!";goto ac2;}
		p->data .price =price;
		
		if(p==L&&L->next !=NULL)L=p->next ;
		else if(p==L&&L->next ==NULL)goto loop;
		else{
		s=L;
		while(s->next !=p)s=s->next ;
		s->next =p->next ;
		}
		r=new LNode;
		strcpy(r->data .name ,p->data .name );
		strcpy(r->data .type ,p->data .type );
		r->data .price =p->data .price ;
		r->data .number =p->data .number ;
		delete p;
		charu(L,r);
		save();
		goto loop;
	case('c'):
ac3:	cout<<"number(int):";
		cin>>number;
		if(number<=0){cout<<"Error!";goto ac3;}
		p->data .number =number;save();goto loop;
	case('e'):
		break;
	default:
		goto loop;
	}
	
}


void choose()
{
ab:cout<<"............."<<endl
	   <<"0.创置链表"<<endl
	   <<"1.插入"<<"2.删除"<<endl
	   <<"3.查找"<<"4.查看"<<endl
	   <<"5.退出"<<"6.保存"<<endl
	   <<"7.打开"<<"8.修改"<<endl
	   <<"choose the struction,please:";
	
	cin>>m;
	switch(m)
	{
	case('0'): CreateList_L(L,e);break;
	case('1'): if(L==NULL)cout<<"无库存!"<<endl;else ListInsert_L(L,s);break;
	case('2'): if(L==NULL)cout<<"无库存!"<<endl;else ListDelete_L(L,p,*e);break;
	case('3'): if(L==NULL)cout<<"无库存!"<<endl;else {p=LocateElem_L(L,*e);
		if(p!=NULL){cout<<"产品名\t"<<"品牌\t"<<"价格(¥)\t"<<"库存量(台)"<<endl;
		cout<<p->data .name<<"\t"<<p->data .type <<"\t"<<p->data .price <<"\t\t"<<p->data .number <<endl;}}break;
	case('4'):if(L==NULL)cout<<"无库存!"<<endl;else putout(L);break;
	case('5'): return;
	case('6'):if(L==NULL)cout<<"无库存!"<<endl;else save();break;
	case('7'): read();if(L)putout(L);break;
	case('8'):if(L==NULL)cout<<"无库存!"<<endl;else update(L,p);break;
	default:
		goto ab;
	
	}
	choose();
}




void main()
{
	cout<<"***************"<<"                  "<<"*****************"<<endl
		<<"                   商厦电部的库存管理                     "<<endl
		<<"***************"<<"                  "<<"*****************"<<endl
		<<"-------------------------------------------------"<<endl
		<<" /*创建连表时单价和数量分别以float、int格式输入;          "<<endl
		<<"        当单价值为负数时,连表自动结束;        */          "<<endl
		<<"-------------------------------------------------"<<endl;  
	choose();
}

⌨️ 快捷键说明

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