链表做的简易手机销售系统

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

typedef struct phone{
    int number;
    char name[30];
    int price;
    int num;
    struct phone *next;
} myphone;

myphone *head,*p,*p1,*p2;
int n =0;

void myadd(){
    p1 = (myphone*)malloc(sizeof(myphone));
    if(n == 0){
        head = p1;
    }else{
        p2->next = p1;
    }
    system("cls");
    printf("请输入手机基本信息:\n手机编号:");
    scanf("%d",&p1->number);
    printf("手机名:");
    scanf("%s",&p1->name);
    printf("手机价格:");
    scanf("%d",&p1->price);
    printf("库存数量:");
    scanf("%d",&p1->num);
    p2 = p1;
    p2->next = NULL;
    n += 1;
    system("pause");
    system("cls");    
}

void myshow(){
    if(n!=0){
        p = head;
        system("cls");
        printf("编号\t手机名\t价格\t库存数量\n");
        do{
            printf("%-8d%-8s%-8d%-8d\n",p->number,p->name,p->price,p->num);
            p = p->next;
        }while(p!=NULL);
    }else{
        printf("还没添加手机信心!\n");
    } 
    system("pause");
    system("cls");
}

void mysave(){
    FILE *fp;
    myphone *p;
    if((fp=fopen("data.txt","wb"))==NULL)
    {
        printf("不能打开文件!"); 
    }
    p = head;
    if(p==NULL)
    {
        printf("保存内容不能为空!\n");
    }else{
        while(p!= NULL){
            if(fwrite(p,sizeof(myphone),1,fp)!= 1){
                printf("写入手机信息出错\n");
                fclose(fp);
                break;
            }
            p = p->next;
        }
        fclose(fp);
        printf("手机信息存储完成\n"); 
    } 
    system("pause");
    system("cls");
}

void myread(){
    FILE *fp;
    if((fp=fopen("data.txt","rb+"))==NULL)
    {
        printf("不能打开文件!"); 
        exit(0);
    }
    
    do{
        p1 = malloc(sizeof(myphone));
        if(n==0){
            head = p1;
        }else{
            p2->next = p1;
        }
        fread(p1,sizeof(myphone),1,fp);
        p2= p1;
        n+=1;
    }while(p2->next!=NULL);
    fclose(fp);
    printf("手机信息读取完成!\n");
    system("pause") ;
    system("cls");
}

void findprice(){
    int myprice1,myprice2;
    printf("请输入要买手机的最高价格:");
    scanf("%d",&myprice1);
    printf("请输入要买手机的最低价格:");
    scanf("%d",&myprice2);
    
    p = head;
    do{
        if(p->price>=myprice2 && p->price<=myprice1)
        {
            printf("编号\t手机名\t价格\t库存数量\n");
            printf("%-8d%-8s%-8d%-8d\n",p->number,p->name,p->price,p->num);
        }
        p = p->next;
    }while(p!=NULL);
    system("pause");
    system("cls");    
}

void findnumber(){
    int mynumber;
    printf("请输入要买手机的编号:");
    scanf("%d",&mynumber);
    
    p = head;
    do{
        if(p->number == mynumber)
        {
            printf("编号\t手机名\t价格\t库存数量\n");
            printf("%-8d%-8s%-8d%-8d\n",p->number,p->name,p->price,p->num);
        }
        p = p->next;
    }while(p!=NULL);
    system("pause");
    system("cls");    
}

void findnum(){
    int mynum1,mynum2;
    printf("请输入要买手机的最大库存:");
    scanf("%d",&mynum1);
    printf("请输入要买手机的最小库存:");
    scanf("%d",&mynum2);
    
    p = head;
    do{
        if(p->num>=mynum2 && p->num<=mynum1)
        {
            printf("编号\t手机名\t价格\t库存数量\n");
            printf("%-8d%-8s%-8d%-8d\n",p->number,p->name,p->price,p->num);
        }
        p = p->next;
    }while(p!=NULL);
    system("pause");
    system("cls");    
}

void findname(){
    char a[30];
    printf("请输入要买手机的名称:");
    scanf("%d",&a);
    
    p = head;
    do{
        if(strcmp(a,p->name) == 0)
        {
            printf("编号\t手机名\t价格\t库存数量\n");
            printf("%-8d%-8s%-8d%-8d\n",p->number,p->name,p->price,p->num);
        }
        p = p->next;
    }while(p!=NULL);
    system("pause");
    system("cls");    
}

void myfind(){
    if(n!= 0){
        int a;
        system("cls");
        printf("1.按价格查找手机信息\n2.按编号查找手机信息\n");
        printf("3.按库存数量查找手机信息\n4.按手机名查找手机信息\n");
        printf("请选择操作的项(即输入数字1~4):") ;
        scanf("%d",&a);
        switch(a)
        {
            case 1: findprice();
                    break;
            case 2: findnumber();
                    break;
            case 3: findnum();
                    break;
            case 4: findname();
                    break;
        }
    }else{
        printf("还没有手机数据!\n");
        system("pause") ;
    }
}

void mybuy()
{
    if(n!=0)
    {
        int knumber;
        printf("请输入要购买手机的编号:");
        scanf("%d",&knumber);
        p = head;
        do{
            if(p->number == knumber){
                p->num = p->num -1;
                printf("已成功购买编号为%d的手机!\n",knumber);
                break;
            }
            p = p->next;
        }while(p!=NULL);
    }else{
        printf("还没手机数据信息!\n");
    }
    system("pause");
    system("cls");
}

void mydel(){
    if(n!=0){
        myphone *f,*l;
        int knumber;
        printf("请输入要删除手机的编号:");
        scanf("%d",&knumber);
        p = head;
        if(head->number == knumber){
            f=head;
            head=head->next;
            free(f);
        }else{
            do{
                if(p->number == knumber){
                    f = p;
                    l->next = p->next;
                    free(f);
                    break;
                }
                l = p;
                p = p->next;
            }while(p!=NULL);
        }
        printf("编号为%d的手机数据信息已被删除!\n",knumber);
    }else{
        printf("还没有手机数据信息!"); 
    }
    system("pause");
    system("cls");
}

int main(){
    int myn = 1;
    while(myn != 8){
        system("cls");
        printf("===================手机销售管理系统==================\n");
        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("请选择操作的项(即输入数字1~8):\n");
        scanf("%d",&myn);
        if(myn>0 && myn<9){
            switch(myn){
                case 1: myadd();
                        break;
                case 2: myshow();
                        break;
                case 3: myfind();
                        break;
                case 4: mydel();
                        break;
                case 5: mybuy();
                        break;
                case 6: mysave();
                        break;
                case 7: myread();
                        break;
            }
        }else{
            printf("输入有误,请输入1~8之间都数字,将返回主菜单!\n");
            system("pause");
        }
    }
}

 

//--------------------Add_info.h-----------------------// #ifndef Add_info_h #define Add_info_h #include"Goodnode.h" //------------------------添加节点(货物信息)的函数-------------------------// int Add_Goodnode(int Goodnum,char *Goodname,char *exp,char *pubname,int innum,float buyp,float sellp,Good* last); //-----------------------添加货物信息模块-----------------------// void Add_info(Good *p); #endif //--------------------Attached_fun.h----------------// #ifndef Attached_fun_h #define Attached_fun_h //-----------------------------------计算盈利函数----------------------------------// template //函数模板 T Cal_income(Good *p,T m) //??? { T total_income; //定义总盈利变量total_income total_income=((p->Sellp)-(p->Buyp))*(p->Innum); return total_income; } //-----------------------------------显示盈利函数----------------------------------// void Show_income(Good *p); //---------------------------显示盈利最多的货物的信息函数---------------------------// void Show_max_income(Good *p); //----------------------------显示盈利最少的货物的信息函数----------------------------// void Show_min_income(Good *p); //--------------------------------显示全部盈利函数------------------------------------// void Show_all_income(Good *p); //----------------------------------附加功能模块--------------------------------------// void Attached_fun(Good *p); #endif //-------------------Delete_info.h------------------// #ifndef Delete_info_h #define Delete_info_h #include"Goodnode.h" //--------------------显示所有货物信息的函数-----------------// void Show_info(Good *p); //--------------------删除节点函数-----------------------// void Delete_node(Good *pr,Good *p); //------------------------删除货物信息模块-----------------// void Delete_info(Good *p); #endif //--------------------Goodclass.h--------------------// //---------------------------------------------货物类头文件-----------------------------------------------// #ifndef Goodclass_h //条件编译 #define Goodclass_h #include using namespace std; class Data //定义数据类 { protected://与私有成员性质相似,唯一的差异表现在派生类中 int Goodnum; //定义货物编号 int Innum; //定义库存 float Buyp; //定义进价 float Sellp; //定义卖价 public: virtual void Set_Goodnum(int num)=0; //设置货物编号,纯虚函数 virtual void Set_Innum(int num)=0; //设置货物库存,纯虚函数 virtual void Set_Buyp(float price)=0; //设置货物进价,纯虚函数 virtual void Set_Sellp(float price)=0; //设置货物卖价,纯虚函数 int* Get_Goodnum(); //得到各个数据成员的地址,方便储存在文件中 int* Get_Innum(); float* Get_Buyp(); float* Get_Sellp(); }; class Gooddata: public Data //公有继承父类Data { protected: char Goodname[30]; //定义货物名 char Exp[30]; //定义保质期 char Pubname[30]; //定义生产厂商名 public: void Set_Goodnum(int num); //父类的虚函数在子类中实现 void Set_Innum(int num); //父类的虚函数在子类中实现 void Set_Buyp(float price); //父类的虚函数在子类中实现 void Set_Sellp(float price); //父类的虚函数在子类中实现 void Set_Goodname(char *s); void Set_Exp(char *s); void Set_Pubname(char *s); char* Get_Goodname(); //得到各个数据成员的地址,方便储存在文件中 char* Get_Exp(); char* Get_Pubname(); }; #endif //---------------------Goodnode.h---------------------// //-----------------------------定义货物链表Good-------------------------------// #ifndef Goodnode_h #define Goodnode_h #include #include #include #include"Goodclass.h" using namespace std; //---------------给已有的类型起别名,有利于提高程序的可读性---------------// typedef struct node { int Goodnum; char Goodname[30]; char Exp[30]; char Pubname[30]; int Innum; float Buyp; float Sellp; node *next; } Good; //------------------------Good链表的删除函数----------------------// void Delete_Good(Good *p); //--------------使用文件数据来建立Good链表的建立函数,形参为ifstream类的引用----------------------// Good* Create_Good(ifstream& infile); //------------------------通过链表中的数据来建立文件的函数----------------------// void Create_file(ofstream& outfile,Good *p); #endif //------------------------Load_file.h-------------------// #ifndef Load_file_h #define Load_file_h //----------------------增加链表模块-----------------------// void Add_Good(ifstream& infile,Good* last); //----------------------载入文件模块-----------------------// void Load_file(Good *last); #endif //-----------------------Save_info.h---------------// #ifndef Save_info_h #define Save_info_h //----------------------信息保存模块-------------------// void Save_info(ofstream& outfile,Good *p); #endif //----------------------Scan_info.h-----------------// #ifndef Scan_info_h #define Scan_info_h //----------------------浏览货物全部信息的函数-------------------// void Scan(Good *p); //------------------------按价格浏览货物信息------------------------------------// void Scan(Good *p,int); //----------------------按库存浏览货物信息-----------------// void Scan(Good *p,int,int); //---------------------浏览货物信息模块--------------------// void Scan_info(Good *p); #endif //-------------------Search_info.h-----------------// #ifndef Search_info_h #define Search_info_h #include"Goodnode.h" //---------------------按编号查询货物信息-------------------// Good* Search_Goodnum(Good *p,int n); //--------------------------按货物名查找------------------------------------------// Good* Search_Goodname(Good *p,char *s); //-----------------按保质期查找货物------------------// Good* Search_Exp(Good *p,char *s); //-----------------------------------------------查找与修改模块-----------------------------------------// void Search_info(Good *p); #endif //--------------------------main.cpp--------------------// #include #include #include #include//基类 #include #include #include"Add_info.h" #include"Attached_fun.h" #include"Goodclass.h" #include"Goodnode.h" #include"Delete_info.h" #include"Load_file.h" #include"Save_info.h" #include"Scan_info.h" #include"Search_info.h" using namespace std; int main() { ifstream infile; ofstream outfile; char ch; char count='0'; //标记变量,判断链表是否已经发生改变 Good *head; cout<<" 正在从磁盘打开文件..."; cout<<endl; try { infile.open("write.dat",ios::in); if(!infile) throw 1; } catch(int) { cout<<" 文件打开失败!"; cout<<endl; return 0; } head=Create_Good(infile); //使用磁盘文件来创建链表 infile.close(); Sleep(1000); system("cls"); char ch3[78]={"* * * * * * * * * * * * * * * * * * * *"}; for (int k=0;k<=77;k++) { cout<<ch3[k]; Sleep(10); } cout<<endl; char ch4[78]={"* * * * * * * * * * * * * "}; for (int a=0;a<=77;a++) { cout<<ch4[a]; Sleep(10); } cout<<endl; char ch5[78]={" * * * * * * * * * * * * * * "}; for (int b=0;b<=77;b++) { cout<<ch5[b]; Sleep(10); } cout<<endl; char ch6[78]={" * * * * * * * * * * * * * * * * * * * *"}; for (int c=0;c<=77;c++) { cout<<ch6[c]; Sleep(10); } cout<<endl; char ch7[78]={" * * * * * * * * * * * * * * "}; for (int d=0;d<=77;d++) { cout<<ch7[d]; Sleep(10); } cout<<endl; char ch8[78]={" * * * * * * * * * * * * * "}; for (int e=0;e<=77;e++) { cout<<ch8[e]; Sleep(10); } cout<<endl; char ch9[78]={" ** ** * * * * * * * * * * * * * * * * * * * * *"}; for (int f=0;f<=77;f++) { cout<<ch9[f]; Sleep(10); } cout <<endl; cout <<endl; cout <<"\t\t\t--------------------------------"; cout<<endl; cout <<" "; cout<<endl; char ch2[35]={" 欢迎进入自动售货管理系统 "}; cout<<"\t\t\t"; for (int j=0;j<=34;j++) { cout<<ch2[j]; Sleep(50); } cout <<" "; cout<<endl; cout <<"\t\t\t--------------------------------"; cout<<endl; Sleep(1000); // 等待函数 cout <<endl; cout <<endl; cout <<"\t\t\t--------------------------------"; cout<<endl; cout <<" "; cout<<endl; char ch1[36]={" 本系统将为您提供精彩的服务 "}; cout<<"\t\t\t"; for (int i=0;i<=35;i++) { cout<<ch1[i]; Sleep(50); } cout <<" "; cout<<endl; cout <<"\t\t\t--------------------------------"; cout<<endl; Sleep(1000); // 等待函数 system("cls"); // 清屏 cout <<"\t\t\t********************************"; cout<<endl; cout <<" "; cout<<endl; cout <<"\t\t\t 即将登入,请稍后 "; cout<<endl; cout <<" "; cout<<endl; cout <<"\t\t\t********************************"; cout<<endl; Sleep(1000); // 等待函数 system("cls"); // 清屏 cout <<"\n\n\n\n\n\n\n\n\t\t\t********************************"; cout<<endl; cout <<" "; cout<<endl; cout <<"\t\t\t 数据加载中 "; cout<<endl; cout <<" "; cout<<endl; cout <<"\t\t\t********************************"; cout<<endl; Sleep(1000); // 等待函数 system("cls"); // 清屏 while(1) { system("cls"); cout<<" "; cout<<endl; cout<<" _____________________________________________________________ "; cout<<endl; cout<<" 欢迎进入自动售货管理系统! "; cout<<endl; cout<<" "; cout<<endl; cout<<" 请选择功能: "; cout<<endl; cout<<" 1)添加货物信息 "; cout<<endl; cout<<" 2)浏览货物信息 "; cout<<endl; cout<<" 3)查找与修改货物信息 "; cout<<endl; cout<<" 4)删除货物信息 "; cout<<endl; cout<<" 5)保存所有信息 "; cout<<endl; cout<<" 6)载入货物信息 "; cout<<endl; cout<<" 7)附加功能查看 "; cout<<endl; cout<<" 0)退出系统 "; cout<<endl; cout<<" 按数字键0~7进行操作"; cout<<endl; cout<<" _____________________________________________________________ "; cout<<endl; cout<<" "; cout<<endl; cout<<" "; cout<<endl; ch=getch(); switch(ch) { case '1':Add_info(head);count='1';break; case '2':Scan_info(head);break; case '3':Search_info(head);count='1';break; case '4':Delete_info(head);count='1';break; case '5': try { outfile.open("write.dat",ios::out); if(!outfile) throw 1; Save_info(outfile,head); count=0; outfile.close(); } catch(int) { cout<<" 文件写入失败!"; cout<<endl; };break; case '6':Load_file(head);count='1';break; case '7':Attached_fun(head);break; case '0': if(count=='1') { cout<<" "; cout<<endl; cout<<" 文件没有保存,是否保存信息?"; cout<<endl; while(1) { cout<<" 请输入相应数字: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { try { outfile.open("write.dat",ios::out); if(!outfile) throw 1; Save_info(outfile,head);count=0; outfile.close(); } catch(int) { cout<<" 文件写入失败!"; cout<<endl; } } };return 0; default: cout<<" 输入错误,请重新输入:";cout<<endl; } } } //------------Add_info.cpp------------------------// #include #include #include #include #include"Goodnode.h" #include"Add_info.h" using namespace std; //------------------------添加节点(货物信息)的函数-------------------------// int Add_Goodnode(int goodnum,char *goodname,char *exp,char *pubname,int innum,float buyp,float sellp,Good* last) { Good *p; p=(Good *)(new Good); //动态分配内存,强制转化为Good型指针 if(p==NULL) { cout<<"创建失败!"; cout<Goodnum=goodnum; strcpy(p->Goodname,goodname); strcpy(p->Exp,exp); strcpy(p->Pubname,pubname); p->Innum=innum; p->Buyp=buyp; p->Sellp=sellp; last->next=p; last=p; last->next=NULL; return 1; } //-----------------------添加货物信息模块-----------------------// void Add_info(Good *p) //将链表的首地址作为实参 { char count='0'; //添加标记变量 int goodnum; char goodname[30]; char exp[30]; char pubname[30]; int innum; float buyp; float sellp; Good *last; Good *Search; last=p; system("cls"); //清屏 cout<<" "; cout<<endl; cout<<"-------------------添加货物信息-------------------- "; cout<<endl; cout<<" 按数字键选择 "; cout<<endl; while(1) //保证信息的正确录入 { cout<<" 返回上层(0) 添加信息(1) \n"; cout<<" "<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<next!=NULL) last=last->next; //把结点指针推向最后一个 while(1) { cout<<"请输入编号: "; cout<>goodnum; for(Search=p;Search;Search=Search->next) //用来判断编号是否重复 if(Search->Goodnum==goodnum) break; if(Search) { cout<<"有重复的编号,请重新输入!"; cout<<endl; continue; //继续while循环 } break; } cout<<"请输入货物名: "; cout<>goodname; cout<<"请输入保质期: "; cout<>exp; cout<<"请输入生产厂商: "; cout<>pubname; cout<<"请输入库存: "; cout<>innum; cout<<"请输入进价: "; cout<>buyp; cout<<"请输入卖价: "; cout<>sellp; cout<<endl; cout<<" "; cout<<endl; cout<<" 是否需要保存信息 ? "; cout<<endl; while(1) { cout<<" 请输入相应数字: 否( 0 ) 是( 1 ) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<" 输入错误,请重新输入 !"; cout<<endl; } if(count=='1') //将临时变量中数据存到链表中 { Add_Goodnode(goodnum,goodname,exp,pubname,innum,buyp,sellp,last); cout<<"信息已成功保存 !"; cout<<endl; } cout<<" "; cout<<endl; while(1) { cout<<" 是否需要继续添加信息?"; cout<<endl; cout<<" 请输入相应数字: 否( 0 ) 是( 1 ) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<" 输入错误,请重新输入 !"; cout<<endl; } if(count=='1') continue; break; } } //------------------------Attached_fun.cpp----------------// #include #include #include #include #include"Goodnode.h" #include"Attached_fun.h" using namespace std; //-----------------------------------显示盈利函数----------------------------------// void Show_income(Good *p) { Good *q; q=p; cout<<setw(12)<Goodname<<setw(7)<Buyp<<setw(7)<Sellp<<setw(7)<Innum<<setw(7)<<Cal_income(q,1.0); cout<next; q2=q1->next; max_income=Cal_income(q1,(float)1.0); bsearched=q1; while(q2!=NULL) { if(max_incomenext; } Show_income(bsearched); } //----------------------------显示盈利最少的货物的信息函数----------------------------// void Show_min_income(Good *p) { Good *bsearched; //指向盈利最少的货物 Good *q1; Good *q2; float min_income; q1=p->next; q2=q1->next; min_income=Cal_income(q1,(float)1.0); bsearched=q1; while(q2!=NULL) { if(min_income>Cal_income(q2,(float)1.0)) { bsearched=q2; //将最小盈利保存在bsearched指针中 min_income=Cal_income(q2,(float)1.0); } q2=q2->next; } Show_income(bsearched); } //--------------------------------显示全部盈利函数------------------------------------// void Show_all_income(Good *p) { Good *q; q=p->next; while(q!=NULL) { Show_income(q); q=q->next; } } //----------------------------------附加功能模块--------------------------------------// void Attached_fun(Good *p) { Good *q; char count; q=p; while(1) { system("cls"); cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<"---------------附加功能---------------"; cout<<endl; cout<<" 按数字键选择"; cout<<endl; while(1) { cout<<" "; cout<<endl; cout<<" 显示盈利最多(1)"; cout<<endl; cout<<" 显示盈利最少(2)"; cout<<endl; cout<<" 显示全部盈利(3)"; cout<<endl; cout<<" 返回上层(0)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1'||count=='2'||count=='3') break; //输入是否合法的判断 cout<<"输入错误,请重新输入 !"; cout<<endl; } while(count!='0') { switch(count) { case '1': cout<<setw(12)<<"货物名"<<setw(7)<<"进价"<<setw(7)<<"卖价"<<setw(7)<<"库存"<<setw(7)<<"盈利"; cout<<endl<<endl; Show_max_income(q); break; case '2': cout<<setw(12)<<"货物名"<<setw(7)<<"进价"<<setw(7)<<"卖价"<<setw(7)<<"库存"<<setw(7)<<"盈利"; cout<<endl<<endl; Show_min_income(q); break; case '3': cout<<setw(12)<<"货物名"<<setw(7)<<"进价"<<setw(7)<<"卖价"<<setw(7)<<"库存"<<setw(7)<<"盈利"; cout<<endl<<endl; Show_all_income(q); break; default: cout<<" 系统故障,请稍后再试..."; cout<<endl; break; } break; } if(count!='0') { cout<<" "; cout<<endl; cout<<" 是否继续显示?"; cout<<endl; cout<<" 按数字键选择"; cout<<endl; while(1) { cout<<" 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; //输入是否合法的判断 cout<<"输入错误,请重新输入 "; cout<<endl; } } if(count!='0') continue; //继续while循环 break; } } //----------------------Delete_info.cpp----------------// #include #include #include #include"Delete_info.h" #include"Goodnode.h" using namespace std; //--------------------显示所有货物信息的函数-----------------// void Show_info(Good *head) { Good *q; q=head->next; cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价\n"; while(q) { cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" "; cout<Innum<<" "<Buyp<<" "<Sellp; cout<next; } } //--------------------删除节点函数-----------------------// void Delete_node(Good *pr,Good *p) { pr->next=p->next; delete p; cout<<"货物信息已经成功删除 !"; cout<next; qr=head; system("cls"); cout<<" "; cout<<endl; cout<<"-------------------删除货物信息-------------------- "; cout<<endl; cout<<" 按数字键选择 "; cout<<endl; while(1) //保证信息的正确录入 { cout<<" 返回上层(0) 删除信息(1) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } while(count!='0') { Show_info(qr); cout<<"请输入要删除货物的编号: "; cout<next; //防止在出现一次输入错误后不能再次输入 cin>>num; while(q) { if(q->Goodnum==num) break; //寻找直到输入编号与货物编号相同,跳出循环删除信息 { qr=q; q=q->next; } } if(q==NULL) { cout<<"输入错误,请重新输入: "; cout<<endl; } else break; } Delete_node(qr,q); qr=head; //让qr指针再次指向链表首地址 cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<"删除后货物信息如下: "; cout<<endl; Show_info(qr); cout<<"是否继续删除 ?"; cout<<endl; while(1) { cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') continue; break; } } //----------------------------Goodclass.cpp------------------// #include"Goodnode.h" #include"Goodclass.h" #include using namespace std; //--------------------------------成员函数的实现-------------------------------// //__________________________________________________________________________________________________ int* Data::Get_Goodnum() //得到各个数据成员的地址,方便储存在文件中 { return &Goodnum; } int* Data::Get_Innum() { return &Innum; } float* Data::Get_Buyp() { return &Buyp; } float* Data::Get_Sellp() { return &Sellp; } //_____________________________________________________________________________________________________ void Gooddata::Set_Goodnum(int num) { Goodnum=num; } void Gooddata::Set_Innum(int num) { Innum=num; } void Gooddata::Set_Buyp(float price) { Buyp=price; } void Gooddata::Set_Sellp(float price) { Sellp=price; } void Gooddata::Set_Goodname(char *s) { strcpy(Goodname,s); } void Gooddata::Set_Exp(char *s) { strcpy(Exp,s); } void Gooddata::Set_Pubname(char *s) { strcpy(Pubname,s); } char* Gooddata::Get_Goodname() //得到各个数据成员的地址,方便储存在文件中 { return Goodname; } char* Gooddata::Get_Exp() { return Exp; } char* Gooddata::Get_Pubname() { return Pubname; } //----------------------------Goodnode.cpp-------------// #include #include"Goodnode.h" #include"Goodclass.h" #include using namespace std; //-----------------------各种共用函数的实现-------------------// //------------------------Good链表的删除函数----------------------// void Delete_Good(Good *p) { Good *pr;//前驱结点 while(p!=NULL) { pr=p; p=p->next; delete pr; } } //--------------使用文件数据来建立Good链表的建立函数,形参为ifstream输入流类对象的引用----------------------// Good* Create_Good(ifstream& infile) { Good *p;//结构体指针 Good *last; //指向p->next Good *pr; Good *head;//链表的首地址 head=(Good *)(new Good);//动态分配内存,强制转化为Good型指针 p=(Good *)(new Good); //新建立一个节点,强制转化为Good型指针 if(p==NULL||head==NULL) //判断节点是否建立 { cout<<"内存不足,创建失败 !"; cout<next=p; while(1) { infile.read((char *)&(p->Goodnum), sizeof(int)); //为p指针所指节点的数据域赋值 infile.read(p->Goodname,sizeof(char)*30); infile.read(p->Exp,sizeof(char)*30); infile.read(p->Pubname,sizeof(char)*30); infile.read((char *)&(p->Innum), sizeof(int));//把数字换成字符串,强制转换 infile.read((char *)&(p->Buyp),sizeof(float)); infile.read((char *)&(p->Sellp),sizeof(float)); (int *)&(p->Goodnum); //把字符串换成数字,强制转换 (int *)&(p->Innum); (float *)&(p->Buyp); (float *)&(p->Sellp); if(!infile.eof( ))//文件结束标记 { last=(Good *)(new Good);//新建立一个节点,强制转化为Good型指针 if(last==NULL) { cout<<"内存不足,创建失败 !"; cout<next=last; //将现有节点与新建立的节点连接起来 pr=p; p=last; } else break; } delete last; p=pr;//让p成为链表的尾部 p->next=NULL; return head; //返回首地址给函数 } //------------------------通过链表中的数据来建立文件的函数----------------------// void Create_file(ofstream& outfile,Good *head) { Good *pn;//后驱结点 Good *p; p=head->next; Gooddata obj; while(pn!=NULL) { pn=p->next; obj.Set_Goodnum(p->Goodnum); //将链表中的信息赋值给类的对象 obj.Set_Goodname(p->Goodname); obj.Set_Exp(p->Exp); obj.Set_Pubname(p->Pubname); obj.Set_Innum(p->Innum); obj.Set_Buyp(p->Buyp); obj.Set_Sellp(p->Sellp); outfile.write((char *)(obj.Get_Goodnum()),sizeof(int)); //将对象中的数据写到文件 outfile.write(obj.Get_Goodname(),sizeof(char)*30); outfile.write(obj.Get_Exp(),sizeof(char)*30); outfile.write(obj.Get_Pubname(),sizeof(char)*30); outfile.write((char *)(obj.Get_Innum()),sizeof(int)); outfile.write((char *)(obj.Get_Buyp()),sizeof(float)); outfile.write((char *)(obj.Get_Sellp()),sizeof(float)); p=pn; (int *)(obj.Get_Goodnum()); //将被强制转换为字符串类型的数据转换为原数据类型 (int *)(obj.Get_Innum()); (float *)(obj.Get_Buyp()); (float *)(obj.Get_Sellp()); } } //-------------------Load_file.cpp----------------// #include #include #include #include #include #include"Goodnode.h" #include"Load_file.h" using namespace std; //----------------------增加链表模块-----------------------// void Add_Good(ifstream& infile,Good* last) //? { Good * p; p=Create_Good(infile); last->next=p->next; delete p; //删除无用的头结点 } //----------------------载入文件模块-----------------------// void Load_file(Good *head) { while(1) { ifstream infile; char count; //操作符 Good *last; char Filename[100]; last=head; while(last->next!=NULL) last=last->next; system("cls"); cout<<"---------------------载入文件------------------"; cout<<endl; while(1) { cout<<" 请输入相应数字: "; cout<<endl; cout<<" 返回上层(0) 载入文件(1) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<" 输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { cout<<" 请输入要载入文件的路径: "; cout<>Filename; try { infile.open(Filename,ios_base::in); //ios::nocreate|ios::in只打开已有文件,不创建新文件 if(!infile) throw 1; } catch(int) { cout<<"文件打开失败,请重新输入路径 !"; cout<<endl; } if(!infile) continue; Add_Good(infile,last); cout<<" 装载成功!"; cout<<endl; } infile.close(); break; } } //-----------------------Save_info.cpp----------------// #include #include #include #include #include"Goodnode.h" #include"Goodclass.h" #include"Save_info.h" using namespace std; //----------------------信息保存模块-------------------// void Save_info(ofstream& outfile,Good *p) { char count; //操作符 system("cls"); cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<" 你确定要保存修改过后的信息吗?"; cout<<endl; while(1) { cout<<" 请输入相应数字: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<" 输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { Create_file(outfile,p); //调用Goodnode.h中声明的“通过链表中的数据来建立文件的函数” cout<<" 保存成功!"; cout<<endl; } } //--------------------------Scan_info.cpp-------------------// #include #include #include #include"Goodnode.h" #include"Scan_info.h" using namespace std; //----------------------浏览货物全部信息的函数-------------------// void Scan(Good *p) { Good *q; q=p; cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价"; cout<<endl; while(q) { cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname; cout<<" "<Innum<<" "<Buyp<<" "<Sellp; cout<next; } } //------------------------按价格浏览货物信息------------------------------------// void Scan(Good *p,int) //函数的重载 { Good *q; q=p; cout<<"货物编号 货物名 进价 卖价"; cout<<endl; while(q!=NULL) { cout<Goodnum<<" "<Goodname<<" "<Buyp<<" "<Sellp; cout<next; } } //----------------------按库存浏览货物信息-----------------// void Scan(Good *p,int,int) //函数的重载 { Good *q; q=p; cout<<"货物编号 货物名 库存"; cout<<endl; while(p!=NULL) { cout<Goodnum<<" "<Goodname<<" "<Innum; cout<next; } p=q; } //---------------------浏览货物信息模块--------------------// void Scan_info(Good *head) { char count; Good *q; q=head->next; system("cls"); cout<<" "; cout<<endl; cout<<"----------------------浏览货物信息信息------------------"; cout<<endl; cout<<" 按数字键选择 "; cout<<endl; while(1) { cout<<" 返回上层(0) 浏览信息(1) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } while(count=='1') { cout<<" 选择浏览条件(按数字键选择)"; cout<<endl; cout<<" 浏览全部货物信息(1)"; cout<<endl; cout<<" 按价格浏览货物信息(2)"; cout<<endl; cout<<" 按库存浏览信息(3)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='1'||count=='2'||count=='3') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } switch(count) { case '1': Scan(q);break; case '2': Scan(q,1);break; //参数1用来区分调用哪个scan函数 case '3': Scan(q,0,1);break; //参数0,1用来区分调用哪个scan函数 default: cout<<" 系统故障,请稍后再试...\n";break; } cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<" 是否继续浏览?"; cout<<endl; while(1) { cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { system("cls"); continue; } break; } } //------------------Search_info.cpp-----------------// #include #include #include #include #include"Goodnode.h" #include"Goodclass.h" #include"Search_info.h" using namespace std; //---------------------按编号查询货物信息-------------------// Good* Search_Goodnum(Good *p,int n) { Good *q; int count=0; //操作符 q=p; //对q进行操作,不改变p的地址 while(q!=NULL) { if(q->Goodnum==n) //若查找成功,则输出商品信息 { cout<<"查找成功 !"; cout<<endl; cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价"; cout<<endl; cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" "; cout<Innum<<" "<Buyp<<" "<Sellp; cout<next; } if(count==0) cout<<"查找失败 !"; cout<Goodname,s)) //若查找成功,则输出商品信息 { cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价"; cout<<endl; cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" "; cout<Innum<<" "<Buyp<<" "<Sellp; cout<<endl; cout<<" 查找成功!"; cout<next; } if(count==0) cout<<" 查找失败!"; cout<Exp,s)) //若查找成功,则输出商品信息 { cout<<"查找成功 !"; cout<<endl; cout<<"货物编号 货物名 保质期 生产厂商 库存 进价 卖价"; cout<<endl; cout<Goodnum<<" "<Goodname<<" "<Exp<<" "<Pubname<<" "; cout<Innum<<" "<Buyp<<" "<Sellp; cout<next; } if(count==0) cout<<"查找失败 !"; cout<next; system("cls"); cout<<" "; cout<<endl; cout<<"-----------------查找与修改----------------"; cout<<endl; cout<<" 按数字键选择"; cout<<endl; while(1) { cout<<" 返回上层(0) 查找信息(1) "; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; //输入是否合法的判断 cout<<"输入错误,请重新输入 !"; cout<<endl; } while(count!='0') //输入合法时选择查找条件 { cout<<" 选择查找条件(按数字键选择)"; cout<<endl; cout<<" 按编号查找(1)"; cout<<endl; cout<<" 按货物名查找(2)"; cout<<endl; cout<<" 按保质期查找(3)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='1'||count=='2'||count=='3') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } switch(count) { case '1': //按编号查找 { int goodnum; cout<<"请输入要查找的编号:"; cout<>goodnum; bsearched=Search_Goodnum(q,goodnum); //调用函数查找 } break; case '2': //按货物名查找 { char goodname[30]; cout<<"请输入要查找的货物:"; cout<>goodname; bsearched=Search_Goodname(q,goodname); //调用函数查找 } break; case '3': //按保质期查找 { char exp[30]; cout<<"请输入要查找的货物保质期:"; cout<>exp; bsearched=Search_Exp(q,exp); //调用函数查找 } break; default: cout<<" 系统故障,请稍后再试..."; cout<<endl; break; } if(bsearched==NULL) //查找失败bsearched值为NULL { cout<<" 查找失败,是否继续查找?"; cout<<endl; while(1) { cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') continue; break; } else { cout<<" 是否修改货物信息?"; cout<<endl; //选择是否修改货物 while(1) { cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { cout<<" 是否修改编号?"; cout<<endl; //是否修改编号 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { int goodnum; cout<<"请输入要修改的编号:"; cout<>goodnum; bsearched->Goodnum=goodnum; } cout<<" 是否修改货物名? "; cout<<endl; //是否修改货物名 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { char goodname[30]; cout<<"请输入要修改的货物:"; cout<>goodname; strcpy(bsearched->Goodname,goodname); } cout<<" 是否修改保质期? "; cout<<endl; //是否修改保质期 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { char exp[30]; cout<<"请输入要修改的保质期:"; cout<>exp; strcpy(bsearched->Exp,exp); } cout<<" 是否修改生产厂商? "; cout<<endl; //是否修改生产厂商 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { char pubname[30]; cout<<"请输入要修改的生产厂商:"; cout<>pubname; strcpy(bsearched->Pubname,pubname); } cout<<" 是否修改库存?"; cout<<endl; //是否修改库存 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { int innum; cout<<"请输入要修改的库存:"; cout<>innum; bsearched->Innum=innum; } cout<<" 是否修改进价?"; cout<<endl; //是否修改进价 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { float buyp; cout<<"请输入要修改的进价:"; cout<>buyp; bsearched->Buyp=buyp; } cout<<" 是否修改卖价?"; cout<<endl; //是否修改卖价 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') { float sellp; cout<<"请输入要修改的卖价:"; cout<>sellp; bsearched->Sellp=sellp; } cout<<" 修改已完成!"; cout<<endl; } cout<<" "; cout<<endl; cout<<" "; cout<<endl; cout<<" 是否继续查找?"; cout<<endl; //修改完成时询问是否继续查找 cout<<" 按数字键选择: 否(0) 是(1)"; cout<<endl; while(1) { cout<<" "; cout<<endl; count=getch(); if(count=='0'||count=='1') break; cout<<"输入错误,请重新输入 !"; cout<<endl; } if(count=='1') continue; break; } } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值