evanisbready 2015-01-04 13:59 采纳率: 0%
浏览 1701

C++系统中指针错误,添加不了对象,真的很急!!!!

为什么不能实现添加啊!!!!!真的很急

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

int i=0;
const int Maxp=100;//最大商品类的数量


class product{
public: 
        static double dailyprofit;//当日总利润 
        double salesprice;//销售价
        double purchasingprice;//进价   
        string name;//商品名
        int id;//商品编码  
        double totalsalesamount;//这种商品总售出量
        double inventory;// 库存量
        bool flag;      //标记售出单位  0按个数1按重量
        int tag;       //删除标记       0有    1已删除

        product(){}
        product(string name,int id,double purchasingprice,double inventory):name(name),id(id),purchasingprice(purchasingprice),inventory(inventory){}
        //初始化商品名称、商品编号。进价以及库存量
        double getsalesprice()const{return salesprice;};//显示售价 
        double getdailyprofit()const{return dailyprofit;};//友元函数,用于计算当日所有商品总销售利润
        double getsalesprice(){return salesprice;}//获得销售价
        void sell(double theamount){totalsalesamount+=theamount;
        inventory-=theamount;
        dailyprofit+=theamount*(salesprice-purchasingprice);}
        void delp(){tag=1;}                                                //删除商品
        void purchase(int purchasingamount){inventory+=purchasingamount;}; //购入,增加已经创建过得商品的库存量
        void display(){
        cout<<"商品名:  "<<name<<"  商品编号:  "<<id<<"  商品库存量:  "<<inventory<<"  商品零售价:  "<<salesprice<<endl;
}
            int gettag(){return tag;}  
            string getflag(){
                if(flag==1)
                    return "个";
                else
                    return "千克";
            }
            friend class Database;
};



double product::dailyprofit=0;






class Database{                                                  //数据库类,实现对商品的添加、查找、删除、排序
private:
    int top;
    product pro[Maxp];
public:
    Database(){                                                  //构造函数,将文件读取至u[]与p[]中
        product p;
        top=-1;
        fstream file("product.txt",ios::in);
        while (1){
            file.read((char*)&p,sizeof(p));
            if(!file)
                break;
            top++;
            pro[top]=p;
        }
        file.close();
    }
    ~Database() {                                                //析构函数,将book[]写到book.txt文件中 
   fstream file("product",ios::out); 
   for(int i=0;i<=top;i++) 
    if(pro[i].tag==0)
     file.write((char *)&pro[i],sizeof(pro[i])); 
    file.close(); 
    }
    void disp(){
        for(int i=0;i<=top;i++)
            if(pro[i].gettag()==0)
                pro[i].display();
    }

    void clear(){                                                         //全删
        top=-1;
    }

    product *search1(int productid) {                                     //按编号查找商品 
   for(int i=0;i<=top;i++) 
    if(pro[i].id==productid &&pro[i].tag==0) 
    { 
     return &pro[i]; 
    } 
    return NULL; 
}

    product *search2(string productname) {                                         //按商品名查找 
   for(int i=0;i<=top;i++) 
    if(pro[i].name==productname &&pro[i].tag==0) 
    { 
     return &pro[i]; 
    } 
    return NULL; 
}



    void productdata(){
        char choice;
        int oneid;
        string onename;
        product *p=NULL;
        char ch;
        cout<<"********************************************"<<endl;
        cout<<"*               1.仓库查看                 *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*               2.商品信息修改             *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*               3.添加商品信息             *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*               4.删除商品信息             *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*               5.返回上一层               *"<<endl;
        cout<<"*                                          *"<<endl;
        cout<<"*               6.清空商品库               *"<<endl;
        cout<<"********************************************"<<endl;
        cout<<"请输入您需要的功能";
        cin>>choice;
    switch(choice){
    case '1':
        disp();
    case '2': 
       cout<<"请输入商品编号:"; 
       cin>>oneid; 
       p=search1(oneid); 
       if(p==NULL) {  
         cout<<"该商品不存在! "<<endl;
         getch();
         break; 
        }
       p->display();
       cout<<"是否修改?( y/n ):";
       cin>>ch;
       if(ch=='y'){
         cout<<"请输入新的商品名:"<<endl;; 
         cin>>p->name;
         cout<<"请输入新的商品编码:"<<endl;
         cin>>p->id;
         cout<<"请输入新的销售价:"<<endl;
         cin>>p->salesprice;
         cout<<"请输入新的进价:"<<endl; 
         cin>>p->purchasingprice;
         cout<<"请输入总售出量:"<<endl;
         cin>>p->totalsalesamount;
         cout<<"请输入库存量:"<<endl;
         cin>>p->inventory;
         cout<<"修改商品成功!╮(╯▽╰)╭"<<endl; 
         getch();
      break;
     }
    else if(ch=='n'){
        cout<<"未修改商品信息"<<endl;
        break;
    }
    case'3':
         cout<<"请输入新商品ID:"; 
         cin>>oneid;
         p=search1(oneid); 
         if(p!=NULL){
       cout<<"该编号已经存在,不能添加!"<<endl;
       break; 
    }
         p=pro+top+1;
      cout<<"请输入新的商品名:"<<endl;; 
      cin>>p->name;
      cout<<"请输入新的商品id"<<endl;
      cin>>p->id;
      cout<<"请输入新的商品的库存量:"<<endl; 
      cin>>p->inventory;
      cout<<"请输入新的商品的进价"<<endl;
      cin>>p->purchasingprice;
      cout<<"请输入新的商品的售价"<<endl;
      cin>>p->salesprice;
      cout<<"请输入新的商品的单位 0.个 1.千克"<<endl;
      cin>>p->flag;
      cout<<"添加商品成功!( ﹁ ﹁ ) ~→"<<endl;
      getch();
      break; 
    case '4':
        cout<<"请输入要删除的ID"<<endl;
        cin>>oneid;
        p=search1(oneid);
            if(p=NULL){
                cout<<"未找到相关商品的ID"<<endl;getch();
                break;}
        p->display();
        cout<<"是否删除y/n?"<<endl;
        cin>>ch;
        if(ch='y'){
            p->delp();
            cout<<"已成功删除(≧▽≦)"<<endl;
            getch();
            break;}
    case '5':
        break;
    case '6':
        clear();break;
    default:
        cout<<"输入错误"<<endl;
        getch();
        break;
}
    }
    void salesproduct(){
        char ch;
        string onename;
        int oneid;
        double oneamount;
        product *p=NULL;
        cout<<"请输入要销售的商品名"<<endl;
        cin>>oneid;
        p=search1(oneid);
        if(p=NULL){
            /*cout<<"未找到相关商品名,请输入商品ID以继续查找"<<endl;
            cin>>oneid;
            p=search1(oneid);
            if(p=NULL){
            cout<<"未找到相关商品"<<endl;
            return;*/

            cout<<"请输入售出的商品的数量单位 "<<p->getflag()<<endl;
            cin>>oneamount;
            cout<<"确认售出"<<oneamount<<p->getflag()<<"? y/n"<<endl;
            cin>>ch;
            if(ch=='y'){
            p->sell(oneamount);
            cout<<"交易成功_(:з」∠)_"<<endl;
            }
            else
                return;
        }
}
void order1()
{
    for(int j=1;j<=top-1;j++)
    {
       for(i=1;i<=top-j;i++)
       {

           if(pro[i].totalsalesamount>pro[i+1].totalsalesamount){
               double temp1,temp2,temp3,temp4;
               int temp5,temp7;
               bool temp6;
               string tempname;
               temp1=pro[i].salesprice;
               pro[i].salesprice=pro[i+1].salesprice;
               pro[i+1].salesprice=temp1;
               tempname=pro[i].name;
               pro[i].name=pro[i+1].name;
               pro[i+1].name=tempname;
               temp2=pro[i].purchasingprice;
               pro[i].purchasingprice=pro[i+1].purchasingprice;
               pro[i+1].purchasingprice=temp2;
               temp3=pro[i].inventory;
               pro[i].inventory=pro[i+1].inventory;
               pro[i+1].inventory=temp3;
               temp4=pro[i].totalsalesamount;
               pro[i].totalsalesamount=pro[i+1].totalsalesamount;
               pro[i+1].totalsalesamount=temp4;
               temp5=pro[i].id;
               pro[i].id=pro[i+1].id;
               pro[i+1].id=temp5;
               temp6=pro[i].flag;
               pro[i].flag=pro[i+1].flag;
               pro[i+1].flag=temp6;
               temp7=pro[i].tag;
               pro[i].tag=pro[i+1].tag;
               pro[i+1].tag=temp7;
           }
       }
    }   
    cout<<setw(8)<<"商品名"<<setw(10)<<"销售量"<<setw(10)<<"单价"<<endl;
         for(i=1;i<top+1;i++)
         {
             cout<<setw(8)<<pro[i].name<<setw(10)<<pro[i].totalsalesamount<<setw(10)<<pro[i].salesprice<<endl;
         }
         cout<<endl;
         cout<<"今日的总销售额为:"<<product::dailyprofit<<endl;
         getch();
    }
void order2()
{
    for(int j=1;j<=top-1;j++)
    {
       for(i=1;i<=top-j;i++)
       {

           if(pro[i].id>pro[i+1].id){
               double temp1,temp2,temp3,temp4;
               int temp5,temp7;
               bool temp6;
               string tempname;
               temp1=pro[i].salesprice;
               pro[i].salesprice=pro[i+1].salesprice;
               pro[i+1].salesprice=temp1;
               tempname=pro[i].name;
               pro[i].name=pro[i+1].name;
               pro[i+1].name=tempname;
               temp2=pro[i].purchasingprice;
               pro[i].purchasingprice=pro[i+1].purchasingprice;
               pro[i+1].purchasingprice=temp2;
               temp3=pro[i].inventory;
               pro[i].inventory=pro[i+1].inventory;
               pro[i+1].inventory=temp3;
               temp4=pro[i].totalsalesamount;
               pro[i].totalsalesamount=pro[i+1].totalsalesamount;
               pro[i+1].totalsalesamount=temp4;
               temp5=pro[i].id;
               pro[i].id=pro[i+1].id;
               pro[i+1].id=temp5;
               temp6=pro[i].flag;
               pro[i].flag=pro[i+1].flag;
               pro[i+1].flag=temp6;
               temp7=pro[i].tag;
               pro[i].tag=pro[i+1].tag;
               pro[i+1].tag=temp7;
           }
       }
    }   
    cout<<setw(8)<<"商品名"<<setw(10)<<"ID"<<setw(10)<<"单价"<<endl;
         for(i=1;i<top+1;i++)
         {
             cout<<setw(8)<<pro[i].name<<setw(10)<<pro[i].id<<setw(10)<<pro[i].salesprice<<endl;
         }
         getch();

  • 写回答

4条回答 默认 最新

  • oyljerry 2015-01-04 14:03
    关注

    先定位具体出错的代码语句。然后来分析

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog