超市管理系统 基础程序

来源:互联网 发布:aps软件 编辑:程序博客网 时间:2024/06/11 01:44

#include <fstream>//输入/输出文件流类
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
#include <windows.h>
int newmain();
struct Goods      // 定义结构体类型
{
 char    name[40];  // 商品名
 char    id[10];   // 条形码
    float    sum;   // 总价
// int             score2;      // 进价
 float     score1;   // 市场价
 char            Member[20];     //  会员号
    int             memp;        // 会员积分
 int             amount;      //商品数量

};
   const int m_nRecMax = 50;
   const int n_nRecMax = 100;
   const int    i_nRecMax = 20;
   Goods *theArray =  new Goods[m_nRecMax]; // 预先分配50个商品数据的内存空间
   Goods  *theNumber=new Goods[n_nRecMax];
   Goods  *theMember=new Goods[i_nRecMax];
   int   m_nCurRecNum = 0;    // 当前商品结构数组中的记录数
   int          n_nCurRecNum = 0;               // 当前商品数
   int          i_nCurRecNum = 0;               // 当前会员人数
   int          p_nCurRecNum = 0;               // 当前消费人数
      // 函数声明和实现
      //将Goods.txt读到theArray[]中
void read()//构造函数,将goods.txt读到theArray[]中
    {
         struct Goods stu;
         fstream file("Goods.txt",ios::out|ios::in);
      if(!file)
    {
     return;
    }
  file.read((char *)&stu,sizeof(stu));
        while (!file.eof())
  {
     theArray[m_nCurRecNum++]=stu;
        file.read((char *)&stu,sizeof(stu));
        if (file.eof())
        m_nCurRecNum--;
  }
     file.close();
    }
      //将consume.txt读到theNumber[]中
void read1()//构造函数,将consume.txt读到theNumber[]中
    {
         struct Goods stu;
         fstream file("consume.txt",ios::out|ios::in);
      if(!file)
    {
     return;
    }
  file.read((char *)&stu,sizeof(stu));
        while (!file.eof())
  {
     theNumber[n_nCurRecNum++]=stu;
        file.read((char *)&stu,sizeof(stu));
        if (file.eof())
        n_nCurRecNum--;
  }
     file.close();
    }
void read2()//构造函数,将Member.txt读到theMember[]中
    {
         struct Goods stu;
         fstream file("Member.txt",ios::out|ios::in);
      if(!file)
    {
     return;
    }
  file.read((char *)&stu,sizeof(stu));
        while (!file.eof())
  {
     theMember[i_nCurRecNum++]=stu;
        file.read((char *)&stu,sizeof(stu));
        if (file.eof())
        i_nCurRecNum--;
  }
     file.close();
    }
   //将theArray[]中数据保存到Goods.txt文件中
void save()
  {
     fstream file("Goods.txt",ios::out);
     if(!file)
  {
  cout<<"文件不能打开/n";
  return;
  }
     for (int i=0;i<=m_nCurRecNum;i++)
     file.write((char *)&theArray[i],sizeof(theArray[i]));
     file.close();
     cout<<"保存成功"<<endl;
  }
void save1()
  {
     fstream file("consume.txt",ios::out);
     if(!file)
  {
  cout<<"文件不能打开/n";
  return;
  }
     for (int i=0;i<=n_nCurRecNum;i++)
     file.write((char *)&theNumber[i],sizeof(theNumber[i]));
     file.close();
     cout<<"保存成功"<<endl;
  }
void save2()
  {
     fstream file("Member.txt",ios::out);
     if(!file)
  {
  cout<<"文件不能打开/n";
  return;
  }
     for (int i=0;i<=i_nCurRecNum;i++)
     file.write((char *)&theMember[i],sizeof(theMember[i]));
     file.close();
     cout<<"保存成功"<<endl;
  }

void Output( Goods one )   // 输出单个记录
 {

 cout<<one.name<<"/t/t"<<one.id<<"/t/t"<<one.score1<<"/t/t"<<one.amount <<endl;

 }
void Output1( Goods one )   // 输出单个记录
 {

 cout<<one.name <<"/t/t"<<one.id<<"/t/t"<<one.score1<<"/t/t"<<one.amount <<"/t/t"<<one.sum<<endl;

 }
void Output2( Goods one )   // 输出单个记录
 {

 cout<<one.name<<"/t/t"<<one.Member <<"/t/t"<<one.memp<<endl;

 }
   
void Copy( Goods *d,  Goods s )
{
    strcpy( d->name, s.name ); 
    strcpy( d->id, s.id );
    d->score1=s.score1;
    d->amount=s.amount ;
}
void Copy1( Goods *d,  Goods s ,Goods p)
{
   strcpy( d->name, p.name ); 
    strcpy( d->id, s.id );
    d->score1=s.score1;
    d->amount =s.amount;
       d->sum =s.sum ;
    //d->memp=s.memp ;
}
void Copy2( Goods *d,  Goods s )
{
    strcpy( d->name, s.name ); 
    strcpy( d->id, s.id );
    d->memp=s.memp ;     
}
void Add(void)        // 添加记录
{
    Goods stu;                            // 输入数据
 cout<<"商品名:";  cin>>stu.name;
 cout<<"条形码:";  cin>>stu.id;
    cout<<"市场价: ";       cin>>stu.score1;
 cout<<"商品个数:";     cin>>stu.amount;
    Copy( &theArray[m_nCurRecNum], stu ); // 存储到结构数组中
    m_nCurRecNum++; // 记录计数
 if ( m_nCurRecNum >= m_nRecMax )   // 空间不够时的处理
 {
  cout<<"内存空间不够,无法添加记录!"<<endl;
  exit( 1 );
 }
}
void Add2(void)      // 添加记录
{
    Goods stu;                          // 输入数据
 cout<<"会员名:";    cin>>stu.name;
 cout<<"会员号:";    cin>>stu.Member;cout<<endl;
    cout<<"会员积分初始为零: ";     stu.memp=0;
    Copy2( &theMember[i_nCurRecNum], stu ); // 存储到结构数组中
    i_nCurRecNum++;                         // 记录计数
 if ( i_nCurRecNum >= i_nRecMax )     // 空间不够时的处理
 {
  cout<<"内存空间不够,无法添加记录!"<<endl;
  exit( 1 );
 }
}
int Find2(char* id)    // 查找记录,结果可能有多条
{
 bool isFind = false;
    int     a=0;
 for (int i=0; i<m_nCurRecNum; i++)
 {
  if ( strcmp(theArray[i].id ,id) == 0 ) // 相等
  {
   isFind = true; 
   a=i;
   //Output( theArray[i] );
  }
 }
 if (!isFind)
  cout<<"没有找到!"<<endl;
  return  a;
}
int finder(char* Member)    // 查找记录,结果可能有多条
{
 bool isFind = false;
    int     a=0;int b=-1;
 for (int i=0; i<i_nCurRecNum; i++)
 {
  if ( strcmp(theMember[i].Member ,Member) == 0 ) // 相等
  {
   isFind = true; 
   a=i;
   //Output( theArray[i] );
  }
 }
 if (!isFind){
  cout<<"没有找到!"<<endl;a=b;}
  return  a;
}
void    addmemp(float sum)
{
   Goods  a;
      cout<<"请输入会员号";
   cin>>a.Member ;
   int b=finder(a.Member );
   theMember[b].memp +=int(sum) ;
   cout<<"本次积分为"<<sum;
}
void Add1(void)      // 添加记录
{
     Goods stu;
  float sum = 0;
  while(1)
  {
start:
      cout<<"条形码  :";       cin>>stu.id  ;        // 输入数据
   int a=Find2(stu.id );cout<<"数组中存放位置:"<<a<<endl;
   if(a==-1){

    goto start;}
   else{
   cout<<theArray[a].name<<endl;
first:
      cout<<"商品数量:";       cin>>stu.amount;
        
         if(theArray[a].amount< stu.amount )
   {
     cout<<"商品数量不足:"<<theArray[a].amount;
     goto  first;
   }                       
  
        theArray[a].amount-= stu.amount; 
        sum += stu.amount*theArray[a].score1;
  stu.sum=sum;
  stu.score1=theArray[a].score1;
        Copy1( &theNumber[n_nCurRecNum], stu, theArray[a]);           // 存储到结构数组中
     n_nCurRecNum++;                                   // 记录计数
     if ( n_nCurRecNum >= n_nRecMax )               // 空间不够时的处理
  {
    cout<<"内存空间不够,无法添加记录!"<<endl;
       exit( 1 );
  }
         int s;
   cout<<"是否继续添加购买商品 是输1... 否 输零:"<<endl;
   cin>>s;
      if(s==0)
   {
         goto finish;
   }
   }
  }
  theNumber[n_nCurRecNum].sum=sum;
 
finish:
  float df;
  cout<<"总价:"<<sum;
  cout<<"实收款:";cin>>df;cout<<endl;df-=sum;
  cout<<"应找:"<<df;cout<<endl;
  p_nCurRecNum++;cout<<"判断是否是会员  是输 1 否 0:";
  int as;
  cin>>as;
  if(as==1)
  {
        addmemp(sum);
     }
}
void List1(void)                         // 遍历结构数组并输出
{
    cout<<"/n商品名/t/t条形码/t/t价格/t/t数量/t/t累计商品价/n";
 for (int i=0; i<n_nCurRecNum; i++)
  Output1( theNumber[i] );
}
void List2(void)      // 遍历结构数组并输出
{
    cout<<"/n会员/t/t会员号/t/t会员积分/n";
 for (int i=0; i<i_nCurRecNum; i++)
  Output2( theMember[i] );
}
void List(void)      // 遍历结构数组并输出
{
    cout<<"/n商品名/t/t条形码/t/t价格/t/t商品数量/n";
 for (int i=0; i<m_nCurRecNum; i++)
  Output( theArray[i] );
}

void Find(char* name)    // 查找记录,结果可能有多条
{
 bool isFind = false;

 for (int i=0; i<m_nCurRecNum; i++)
 {
  if ( strcmp(theArray[i].name, name) == 0 ) // 相等
  {
   isFind = true;  
   Output( theArray[i] );
  }
 }
 if (!isFind)
  cout<<"没有找到!"<<endl;
}
void Find1(char* name)    // 查找记录,结果可能有多条
{
 bool isFind = false;

 for (int i=0; i<i_nCurRecNum; i++)
 {
  if ( strcmp(theMember[i].name, name) == 0 ) // 相等
  {
   isFind = true;  
   Output( theMember[i] );
  }
 }
 if (!isFind)
  cout<<"没有找到!"<<endl;
}
void sort1() //  按商品名排序
{
 struct Goods stu;
 for (int i=0; i<i_nCurRecNum-1; i++) 
     for (int j=i+1; j<i_nCurRecNum; j++)
  if (strcmp(theMember[j].name ,theMember[i].name )<0)
  {
   stu=theMember[j];
   theMember[j]=theMember[i];
   theMember[i]=stu;
        }
  cout<<"排序成功"<<endl;
}
void sort2() //  按商品名排序
{
 struct Goods stu;
 for (int i=0; i<i_nCurRecNum-1; i++) 
 for (int j=i+1; j<i_nCurRecNum; j++)
  if (theMember[j].memp >theMember[i].memp)
  {
   stu=theMember[j];
   theMember[j]=theMember[i];
   theMember[i]=stu;
  }
     cout<<"排序成功"<<endl;
}
void sort() //  按商品名排序
{
 struct Goods stu;
 for (int i=0; i<m_nCurRecNum-1; i++) 
 for (int j=i+1; j<m_nCurRecNum; j++)
  if (strcmp(theArray[j].name ,theArray[i].name )<0)
  {
   stu=theArray[j];
   theArray[j]=theArray[i];
   theArray[i]=stu;
  }
     cout<<"排序成功"<<endl;
}
void del(char* name) //  按商品名删除商品信息
{
    bool isfind=false;
   for(int i=0;i<m_nCurRecNum;i++)
   {
       if(strcmp(theArray[i].name,name)==0)
       {
       isfind=true;   m_nCurRecNum--;
       for(int j=i;j<m_nCurRecNum;j++)
       theArray[j]=theArray[j+1];
       cout<<"删除成功"<<endl;
    }
   }
    if(!isfind)
    cout<<"没有找到该客户"<<endl; 
}
void del1(char* name) //  按会员名删除会员信息
{
    bool isfind=false;
   for(int i=0;i<i_nCurRecNum;i++)
   {
       if(strcmp(theMember[i].name,name)==0)
       {
       isfind=true;   i_nCurRecNum--;
       for(int j=i;j<i_nCurRecNum;j++)
       theMember[j]=theMember[j+1];
       cout<<"删除成功"<<endl;
    }
   }
    if(!isfind)
    cout<<"没有找到该客户"<<endl; 
}
void modify( char* name )            //按商品名修改商品信息
 {
    bool isfind=false;
    for(int i=0;i<m_nCurRecNum;i++)
 {
       if(strcmp(theArray[i].name,name)==0)
    {
        isfind=true;   Goods stu;
        cout<<"输入商品名";cin>>stu.name;cout<<endl;
        cout<<"输入条形码";cin>>stu.id;cout<<endl;
        cout<<"输入商品价格";cin>>stu.score1;cout<<endl;
  cout<<"输入商品数量";cin>>stu.amount ;cout<<endl;
        Copy( &theArray[i], stu );
        cout<<"修改成功/n";
    }
   
 } 
  if(!isfind)
    cout<<"没有找到该客户"<<endl;
}
void Copy3( Goods *d,  Goods s )      //修改会员名
{
    strcpy( d->name, s.name ); 
     
}
void Copy4( Goods *d,  Goods s )      //修改会员号
{
   
    strcpy( d->Member, s.Member  );
        
}
void modify1( char* name )            //按会员名修改会员信息
 {
    bool isfind=false;
    for(int i=0;i<i_nCurRecNum;i++)
 {
       if(strcmp(theMember[i].name,name)==0)
    {
        isfind=true;   Goods stu;
        cout<<"输入会员名";cin>>stu.name;cout<<endl;
   Copy3( &theMember[i] , stu );
        cout<<"输入会员号";cin>>stu.Member ;cout<<endl;
        //cout<<"输入商品价格";cin>>stu.score1;cout<<endl;
      
  Copy4( &theMember[i] , stu );
        cout<<"修改成功/n";
    }
   
 } 
  if(!isfind)
    cout<<"没有找到该会员"<<endl;
}
void RemoveAll( void )
{
 if (theArray)
 {
  delete []theArray;   theArray = NULL;
 }
}
int consume()
{
 int nSelect;
    while (1)
 { 
  system("cls");  // 执行DOS下的清屏命令
        cout<<"/n/t/t商品销售记录:"<<n_nCurRecNum<<", 请选择要操作的命令号:/n/n";
  cout<<"/t/t1 ---- 累加该顾客的消费商品并显示商品信息            /n/n";
  cout<<"/t/t2 ---- 列表显示该客户消费信息                        /n/n";
  cout<<"/t/t3 ---- 保存数据                                      /n/n";
     cout<<"/t/t4 ---- 返回主界面                                    /n/n";
  cout<<"/t/t9 ---- 退出                                          /n";
  cout<<"/t请选择:";
  cin>>nSelect;

  switch(nSelect)
  {
   case 1: Add1(); save1(); save(); save2(); List1();  break;
   case 2: List1();          break;
   case 3: save1();             break;
            case 4: newmain();             break;
   case 9: RemoveAll(); exit( 0 ); break;   
   default: cout<<"选择有误。";
  }
  cout<<"按任何键继续...";
  cin.get(); cin.get();
 }
 return 0;
}
int goods()
{
   
 char name[10];
 int nSelect;
 while (1)
 { 
  system("cls");  // 执行DOS下的清屏命令
        cout<<"/n/t/t⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙¤⊙";
        cout<<"/n/t/t当前商品记录:"<<m_nCurRecNum<<", 请选择要操作的命令号:/n/n";
  cout<<"/t/t1 ---- 添加并保存商品信息                              /n/n";
  cout<<"/t/t2 ---- 列表显示商品信息                                /n/n";
  cout<<"/t/t3 ---- 按商品名查找                                    /n/n";
  cout<<"/t/t4 ---- 保存数据                                        /n/n";   
  cout<<"/t/t5 ---- 按商品名排序                                    /n/n";
  cout<<"/t/t6 ---- 按商品名删除商品信息                            /n/n";
  cout<<"/t/t7 ---- 按商品名修改商品信息                            /n/n";
        cout<<"/t/t8 ---- 返回主界面                                      /n/n";
  cout<<"/t/t9 ---- 退出                                            /n";
  cout<<"/t请选择:";
  cin>>nSelect;

  switch(nSelect)
  {
   case 1: Add();save();List(); break;
   case 2: List();  break;
   case 3:
    cout<<"请输入要查找的商品名:"<<endl;
    cin>>name;
    Find( name ); break;
   case 4: save();     break;
   case 5: sort();     break;
   case 6:
    cout<<"请输入要删除的商品名:"<<endl;
    cin>>name;
    del(name );     break;
   case 7:
    cout<<"请输入要修改的商品名:"<<endl;
    cin>>name;
    modify(name );save(); break; 
   case 8:newmain() ;  break;
   case 9: RemoveAll(); exit( 0 ); break;   
   default: cout<<"选择有误。";
  }
  cout<<"按任何键继续...";
  cin.get(); cin.get();
 }
 return 0;
}
int   marketmem()
{
 char name1[20];
    int nSelect;
 while (1)
 { 
  char name[20];
  system("cls");  // 执行DOS下的清屏命令
  cout<<"/n/t/t当前会员记录:"<<i_nCurRecNum<<", 请选择要操作的命令号:/n/n";
        cout<<"/n/t/t 请选择要操作的命令号:                            /n/n";
  cout<<"/t/t1 ---- 添加并保存会员信息                            /n/n";
     cout<<"/t/t2 ---- 修改会员信息                                  /n/n";
  cout<<"/t/t3 ---- 按积分从高到低排序                            /n/n";
        cout<<"/t/t4 ---- 列表显示所有会员信息                          /n/n";
        cout<<"/t/t5 ---- 注销会员信息                                  /n/n";
        cout<<"/t/t6 ---- 按会员名排序                                  /n/n";
  cout<<"/t/t7 ---- 保存会员信息                                  /n/n";
  cout<<"/t/t8 ---- 返回主界面                                    /n/n";
  cout<<"/t/t9 ---- 退出                                          /n";
  cout<<"/t请选择:";
  cin>>nSelect;
        switch(nSelect)
  {
   case 1: Add2(); save2();  break;
   case 2:
    cout<<"请输入要修改的会员名:"<<endl;
    cin>>name1;
    modify1(name1 ); break; 
            case 3: sort2();   break;
   case 4: List2();   break;
   case 5:
    cout<<"请输入要删除的会员名:"<<endl;
    cin>>name;
    del1(name );    break;
            case 6: sort1();   break;
            case 7: save2();   break;
            case 8: newmain();       break;
   case 9: RemoveAll(); exit( 0 ); break;   
   default: cout<<"选择有误。";
  }
  cout<<"按任何键继续...";
  cin.get(); cin.get();
 }
 return 0;
}
void Draw()
{
    cout<<"    °★.☆° .★·°∴°★.°·∴°☆ ·°∴° ☆..·/n";
    cout<<"     ☆°★°∴°°∴ ☆°.·★°∴°./n";
 cout<< setw(4)<<"   ◢◣。       ◢◣。     ☆圣★/n";
 cout<< setw(3)<<"  ◢★◣。     ◢★◣。     ★诞☆ /n";
 cout<< setw(2)<<" ◢■■◣。   ◢■■◣。     ☆节★/n";
 cout<<         "◢■■■◣。 ◢■■■◣。      ★快☆/n";
 cout<<        "︸︸||︸︸ !!︸︸||︸︸        ☆乐★/n";
 cout<<      "祝圣诞节快乐 ^_^!!☆°★°∴°°∴ ☆°.·★°∴°./n";
 cout<<    "°★.☆° .★·°∴°★.°·∴°☆ ·°∴° ☆..·./n";
}
int newmain()
{
   int nSelect;
 while (1)
 { 
  system("cls");                // 执行DOS下的清屏命令
        cout<<"/n/t/t 请选择要操作的命令号:/n/n";
  cout<<"/t/t1 ---- 商品管理系统      /n/n";
     cout<<"/t/t2 ---- 进入收银系统      /n/n";
        cout<<"/t/t3 ---- 进入会员住册系统  /n/n";
        cout<<"/t/t4 ---- 节日贺卡          /n/n";
  cout<<"/t/t9 ---- 退出              /n";
  cout<<"/t请选择:";
  cin>>nSelect;
        switch(nSelect)
  {
   case 1: goods();  break;
   case 2: consume();  break;
      case 3: marketmem();    break;
            case 4: Draw();      break;
   case 9: RemoveAll(); exit( 0 ); break;   
   default: cout<<"选择有误。";
  }
  cout<<"按任何键继续...";
  cin.get(); cin.get();
 }
 return 0;
}
void main()
{
    read();
    read1();
 read2();
 newmain();
}
   

 

原创粉丝点击