| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 417 人关注过本帖
标题:请问这个错在哪?
只看楼主 加入收藏
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:5 
请问这个错在哪?
程序代码:
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
const char*file="save.bat";
struct SaveDate
{
    int Sclass;
    int Snum;
    float Sprice;
};
class Date
{
private:
    int num;
    float price;
public:
    Date(int a,float b):num(a),price(b){};
    virtual ~Date(){};
    virtual void print()=0;
    int getnum()const{return num;}
    float getpirce()const{return price;}
};
class book:public Date
{
public:
    book(int a,float b):Date(a,b){};
    virtual void print();
   
};
void book::print()
{
    cout<<"图书的编号为"<<getnum()<<""<<endl;
    cout<<"图书的价格为"<<getpirce()<<""<<endl;
}
class drug:public Date
{
public:
    drug(int a,float b):Date(a,b){};
    virtual void print();
   
};
void drug::print()
{
    cout<<"药品的编号为"<<getnum()<<""<<endl;
    cout<<"药品的价格为"<<getpirce()<<""<<endl;
}
class Node
{
private:
    Node *next;
    Date *D;
public:
    Node( Date* a):D(a),next(0){};
    ~Node(){delete D;D=0;}
    Node* getn()const{return next;}
    Date* getD()const{return D;}
    void setn(Node *a){next=a;}
};
class list
{
private:
    Node* head;
    int total;
public:
    void Inser( Date *  a);
    void all()const;
    list(){head=0;total=0;}
    ~list();
    Date* find(const int x);
    Date* operator[](const int x);
    void clear(const int x );
    int gett(){return total;}
    Node* gethead(){return head;}
    int gettotal()const{return total;}
};                        
void list::Inser(Date*  a)
{
    total++;
    Node*q=head;
   
    Node*f=new Node(a);
   
    if(!head)
    { 
        head=f;
    return;
    }
    if(head->getD()->getnum()>f->getD()->getnum())
    {         
        head=f;
        f->setn(q);
            
             return;
    }
    if(!head->getn())
    {
        head->setn(f);
       
        return;
    }
    while(q)
    {
    Node*e=q->getn();
    int c=e->getD()->getnum();
    if(c>f->getD()->getnum())
    {
        q->setn(f);
        f->setn(e);
        return;
    }
    if(!e->getn())
    {
        e->setn(f);
        return;
    }
    q=q->getn();
    }
}
void list::all()const
{
   
    if(!head)
    {
        cout<<"没有数据"<<endl;
    return;
    }
    Node *k=head;
    while(k)
    {
    k->getD()->print();
    k=k->getn();
    }
   
}
list::~list()
{
    Node *a;
    int i=1;
    while(head)
    {   
        a=head;
        head=head->getn();
        delete a;
        cout<<"删除第"<<i<<"个节点"<<endl;
        i++;
    }
}
Date* list::operator [](const int x)
{
    Node *a=head;
    if(x>=total)
    {
        cout<<"找不到数据!"<<endl;
        return NULL;
    }
    for(int i=0;i<x;i++)
        a=a->getn();
    return a->getD();
}
Date* list::find(const int x)
{
    Node *a=head;
    if(!head)
    {
        cout<<"无数据!"<<endl;
        return NULL;
    }
    while(a)
    {
    if(a->getD()->getnum()==x)
        return a->getD();
    a=a->getn();
    }
    cout<<"找不到数据!"<<endl;
    return NULL;
}
void list::clear(const int x)
{
     if(!head)
       {
           cout<<"无数据删除!"<<endl;
           return;
       }
       Node *a=head;
       Node *b;
       int c;
      if(head->getD()->getnum()==x)
       {
            if(!head->getn())
       {
           delete head;
           head=0;
          cout<<"数据清空!"<<endl;
           return;
       }
            else
            {
           head=head->getn();
           // cout<<head->getD()<<endl;;
           delete a;
           total--;
           a=0;
           cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
           //cout<<head->getD()<<endl;
           return;
            }
       }
     
       while(a)
       {
           if(a->getn()==0)
           {
               cout<<"无数据"<<endl;
               return;
           }
        b=a->getn();
       c=b->getD()->getnum();
       if(c==x)
       {
           a->setn(b->getn());
            delete b;
            total--;
            b=0;
               cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
            return;
       }
       a=a->getn();
       //cout<<"没有该编号"<<endl;
       }
      
       cout<<"没有该编号"<<endl;
}
class measure:private list
{
private:
    //list l;
public:
    void relase(Date*);
    void run();
};
void measure::relase(Date*a)
{
      Node*head1;
        
      head1=gethead();
    while(head1)
    {
        if(head1->getD()->getnum()==a->getnum())
        {
            cout<<"你输入的商品与编号"<<head1->getD()->getnum()<<"相等!"<<endl;
            return;
        }
        head1=head1->getn();
    }
    Inser(a);
}

void measure::run()
{
    bool g=false;
    Date* p=0;
    //measure l;
    //list l;
    int w=0;
    int q=0;
    while(1)
    {
    cout<<"1.增加商品 2.列出所有商品 3.查找商品 4.删除商品 5.商品数目 6.保存 7.读取 8.退出 "<<endl;
      cin>>q;
      switch(q)
      {
      case 1:
          {
          cout<<"1.图书类 2.药品类"<<endl;
          cin>>w;
          if(w==1||w==2)
          {
              int a;float b;
             cout<<"请输入编号"<<endl;
             cin>>a;
             cout<<"请输入价格"<<endl;
             cin>>b;
             if(w==1)
             {
                 p=new book(a,b);
                 relase(p);
                 break;
                }
             if(w==2)
             {
                  p=new drug(a,b);
                 relase(p);
                 break;
              }
          }
          else
              cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 2:all();
          break;
      case 3:
          {
              cout<<"1.按编号查找,2按序列号查找"<<endl;
              cin>>w;
              if(w==1||w==2)
              {    int s;
                  if(w==1)
                  {
                      cout<<"请输入编号"<<endl;
                          cin>>s;
                          if(!find(s))
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                      find(s)->print();
                      break;
                  }
                  if(w==2)
                  {
                     cout<<"请输入序列号"<<endl;
                     cin>>s;
                     if(!(*this)[s-1])
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                     (*this)[s-1]->print();
                     break;
                  }
                  }
              else
                  cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 4:
          {
              cout<<"请输入要删除商品的编号"<<endl;
              cin>>w;
              clear(w);
            
          }
          break;
      case 5:cout<<"商品的数量为"<<gett()<<endl;
          break;
      case 6:
          {
          long num=gettotal();
          ofstream fcout;
           fcout.open(file,ios::out|ios::binary);
          SaveDate *date2=new SaveDate;
          for(int i=0;i<num;i++)
          {
            
            
                 Date *a=(*this)[i];
               if(typeid(*a).name()==typeid(book).name())
               {
                   date2->Sclass=1;
               }
               if(typeid(*a).name()==typeid(drug).name())
               {
                   date2->Sclass=2;
               }
               date2->Snum=(*this)[i]->getnum();
               date2->Sprice=(*this)[i]->getpirce();
               cout<<"*::"<<date2->Sclass<<date2->Snum<<date2->Sprice<<endl;//没有问题
               fcout.write((char*)&date2,sizeof date2); // 保存1个没有问题,2个就出问题了,比如第一个数据为111 第2个为222 但读取的结果2个都是222
              fcout.seekp(sizeof date2,ios::beg);
               
              //   fcout.close();
          }
          fcout.clear();
            fcout.close();
        

          }
          break;

      case 7:
          {
              long num=gettotal();
          SaveDate *ax=new SaveDate;
                ifstream fcin(file,ios::binary);
               if(!fcin.is_open())
          {
              cout<<"读取失败!"<<endl;
          }
          else
          {
       
        while(fcin.read((char*)&ax,sizeof ax))
        {
          cout<<ax->Sclass<<ax->Snum<<ax->Sprice<<endl;
        }
        if(fcin.eof())
                                                      
        {
               fcin.clear();
               fcin.close();
               }
               }
          }
          break;
      case 8:g=true;
          break;


       
      }
      if(g)
          break;//switch
    }//while
}

void main()
{

    measure r;
    r.run();
}
该程序不是完整的程序,所以有点乱
搜索更多相关主题的帖子: std 
2011-09-10 09:41
hoho568
Rank: 5Rank: 5
等 级:职业侠客
帖 子:101
专家分:378
注 册:2009-7-14
收藏
得分:14 
我尝试了一下没有错误啊,除了main()改成int类型之外。不过有很多问题,像一些输入,一旦出现错误输入流,则会进入死循环。。
2011-09-10 10:34
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
收藏
得分:0 
回复 2楼 hoho568
我说了这不是完整的程序,不考虑用户输错问题,这个程序关键是保存出错
比如创建 一个图书类和一个药品类 保存完毕之后读取就出错了,读取的内容始终是最后一个写入程序的内容而不是完整的内容

都不结贴我郁闷那!
2011-09-10 12:19
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
收藏
得分:0 
我知道错哪里了

都不结贴我郁闷那!
2011-09-10 16:04
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
收藏
得分:0 
程序代码:
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
const char*file="save.bat";
struct SaveDate
{
    int Sclass;
    int Snum;
    float Sprice;
};
class Date
{
private:
    int num;
    float price;
public:
    Date(int a,float b):num(a),price(b){};
    virtual ~Date(){};
    virtual void print()=0;
    int getnum()const{return num;}
    float getpirce()const{return price;}
};
class book:public Date
{
public:
    book(int a,float b):Date(a,b){};
    virtual void print();
   
};
void book::print()
{
    cout<<"图书的编号为"<<getnum()<<""<<endl;
    cout<<"图书的价格为"<<getpirce()<<""<<endl;
}
class drug:public Date
{
public:
    drug(int a,float b):Date(a,b){};
    virtual void print();
   
};
void drug::print()
{
    cout<<"药品的编号为"<<getnum()<<""<<endl;
    cout<<"药品的价格为"<<getpirce()<<""<<endl;
}
class Node
{
private:
    Node *next;
    Date *D;
public:
    Node( Date* a):D(a),next(0){};
    ~Node(){delete D;D=0;}
    Node* getn()const{return next;}
    Date* getD()const{return D;}
    void setn(Node *a){next=a;}
};
class list
{
private:
    Node* head;
    int total;
public:
    void Inser( Date *  a);
    void all()const;
    list(){head=0;total=0;}
    ~list();
    Date* find(const int x);
    Date* operator[](const int x);
    void clear(const int x );
    int gett(){return total;}
    Node* gethead(){return head;}
    int gettotal()const{return total;}
};                        
void list::Inser(Date*  a)
{
    total++;
    Node*q=head;
   
    Node*f=new Node(a);
   
    if(!head)
    { 
        head=f;
    return;
    }
    if(head->getD()->getnum()>f->getD()->getnum())
    {         
        head=f;
        f->setn(q);
            
             return;
    }
    if(!head->getn())
    {
        head->setn(f);
       
        return;
    }
    while(q)
    {
    Node*e=q->getn();
    int c=e->getD()->getnum();
    if(c>f->getD()->getnum())
    {
        q->setn(f);
        f->setn(e);
        return;
    }
    if(!e->getn())
    {
        e->setn(f);
        return;
    }
    q=q->getn();
    }
}
void list::all()const
{
   
    if(!head)
    {
        cout<<"没有数据"<<endl;
    return;
    }
    Node *k=head;
    while(k)
    {
    k->getD()->print();
    k=k->getn();
    }
   
}
list::~list()
{
    Node *a;
    int i=1;
    while(head)
    {   
        a=head;
        head=head->getn();
        delete a;
        cout<<"删除第"<<i<<"个节点"<<endl;
        i++;
    }
}
Date* list::operator [](const int x)
{
    Node *a=head;
    if(x>=total)
    {
        cout<<"找不到数据!"<<endl;
        return NULL;
    }
    for(int i=0;i<x;i++)
        a=a->getn();
    return a->getD();
}
Date* list::find(const int x)
{
    Node *a=head;
    if(!head)
    {
        cout<<"无数据!"<<endl;
        return NULL;
    }
    while(a)
    {
    if(a->getD()->getnum()==x)
        return a->getD();
    a=a->getn();
    }
    cout<<"找不到数据!"<<endl;
    return NULL;
}
void list::clear(const int x)
{
     if(!head)
       {
           cout<<"无数据删除!"<<endl;
           return;
       }
       Node *a=head;
       Node *b;
       int c;
      if(head->getD()->getnum()==x)
       {
            if(!head->getn())
       {
           delete head;
           head=0;
          cout<<"数据清空!"<<endl;
           return;
       }
            else
            {
           head=head->getn();
           // cout<<head->getD()<<endl;;
           delete a;
           total--;
           a=0;
           cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
           //cout<<head->getD()<<endl;
           return;
            }
       }
     
       while(a)
       {
           if(a->getn()==0)
           {
               cout<<"无数据"<<endl;
               return;
           }
        b=a->getn();
       c=b->getD()->getnum();
       if(c==x)
       {
           a->setn(b->getn());
            delete b;
            total--;
            b=0;
               cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
            return;
       }
       a=a->getn();
       //cout<<"没有该编号"<<endl;
       }
      
       cout<<"没有该编号"<<endl;
}
class measure:private list
{
private:
    //list l;
public:
    void relase(Date*);
    void run();
};
void measure::relase(Date*a)
{
      Node*head1;
        
      head1=gethead();
    while(head1)
    {
        if(head1->getD()->getnum()==a->getnum())
        {
            cout<<"你输入的商品与编号"<<head1->getD()->getnum()<<"相等!"<<endl;
            return;
        }
        head1=head1->getn();
    }
    Inser(a);
}

void measure::run()
{
    bool g=false;
    Date* p=0;
    //measure l;
    //list l;
    int w=0;
    int q=0;
    while(1)
    {
    cout<<"1.增加商品 2.列出所有商品 3.查找商品 4.删除商品 5.商品数目 6.保存 7.读取 8.退出 "<<endl;
      cin>>q;
      switch(q)
      {
      case 1:
          {
          cout<<"1.图书类 2.药品类"<<endl;
          cin>>w;
          if(w==1||w==2)
          {
              int a;float b;
             cout<<"请输入编号"<<endl;
             cin>>a;
             cout<<"请输入价格"<<endl;
             cin>>b;
             if(w==1)
             {
                 p=new book(a,b);
                 relase(p);
                 break;
                }
             if(w==2)
             {
                  p=new drug(a,b);
                 relase(p);
                 break;
              }
          }
          else
              cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 2:all();
          break;
      case 3:
          {
              cout<<"1.按编号查找,2按序列号查找"<<endl;
              cin>>w;
              if(w==1||w==2)
              {    int s;
                  if(w==1)
                  {
                      cout<<"请输入编号"<<endl;
                          cin>>s;
                          if(!find(s))
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                      find(s)->print();
                      break;
                  }
                  if(w==2)
                  {
                     cout<<"请输入序列号"<<endl;
                     cin>>s;
                     if(!(*this)[s-1])
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                     (*this)[s-1]->print();
                     break;
                  }
                  }
              else
                  cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 4:
          {
              cout<<"请输入要删除商品的编号"<<endl;
              cin>>w;
              clear(w);
            
          }
          break;
      case 5:cout<<"商品的数量为"<<gett()<<endl;
          break;
      case 6:
          {
          long num=gettotal();
          ofstream fcout;
           fcout.open(file,ios::out|ios::binary);
          SaveDate *date2=new SaveDate[num];
          for(int i=0;i<num;i++)
          { 

                    SaveDate *D;
                    D=&date2[i];
            
                 Date *a=(*this)[i];
               if(typeid(*a).name()==typeid(book).name())
               {
                  D->Sclass=1;
               }
               if(typeid(*a).name()==typeid(drug).name())
               {
                   D->Sclass=2;
               }
               D->Snum=(*this)[i]->getnum();
               D->Sprice=(*this)[i]->getpirce();
              
               fcout.write((char*)&D,sizeof D);
              fcout.seekp(sizeof D,ios::beg);
             // fcout.clear();

               
              //   fcout.close();
          }
          fcout.clear();
            fcout.close();
        

          }
          break;

      case 7:
          {
              long num=gettotal();
          SaveDate *ax=new SaveDate;
                ifstream fcin(file,ios::binary);
               if(!fcin.is_open())
          {
              cout<<"读取失败!"<<endl;
          }
          else
          {
              fcin.seekg(0);
       
        while(fcin.read((char*)&ax,sizeof ax))
        {
          cout<<ax->Sclass<<ax->Snum<<ax->Sprice<<endl;
        }
        if(fcin.eof())
                                                      
        {
               fcin.clear();
               fcin.close();
               }
               }
          }
          break;
      case 8:g=true;
          break;


       
      }
      if(g)
          break;//switch
    }//while
}

void main()
{

    measure r;
    r.run();
}
或者将 date2++也可以.靠别人不如靠自己

[ 本帖最后由 xg5699 于 2011-9-10 16:27 编辑 ]

都不结贴我郁闷那!
2011-09-10 16:23
xg5699
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:140
专家分:522
注 册:2011-7-27
收藏
得分:0 
程序代码:
#include <iostream>
#include <string>
#include<iomanip>
#include<fstream>
using namespace std;
const char*file="save.bat";
const int N=15;
struct SaveDate
{
    int Sclass;
    int Snum;
    float Sprice;
};
class Date
{
private:
    int num;
    float price;
public:
    Date(int a,float b):num(a),price(b){};
    virtual ~Date(){};
    virtual void print()=0;
    int getnum()const{return num;}
    float getpirce()const{return price;}
};
class book:public Date
{
public:
    book(int a,float b):Date(a,b){};
    virtual void print();
   
};
void book::print()
{
    cout<<"图书的编号为"<<getnum()<<""<<endl;
    cout<<"图书的价格为"<<getpirce()<<""<<endl;
}
class drug:public Date
{
public:
    drug(int a,float b):Date(a,b){};
    virtual void print();
   
};
void drug::print()
{
    cout<<"药品的编号为"<<getnum()<<""<<endl;
    cout<<"药品的价格为"<<getpirce()<<""<<endl;
}
class Node
{
private:
    Node *next;
    Date *D;
public:
    Node( Date* a):D(a),next(0){};
    ~Node(){delete D;D=0;}
    Node* getn()const{return next;}
    Date* getD()const{return D;}
    void setn(Node *a){next=a;}
};
class list
{
private:
    Node* head;
    int total;
public:
    void Inser( Date *  a);
    void all()const;
    list(){head=0;total=0;}
    ~list();
    Date* find(const int x);
    Date* operator[](const int x);
    void clear(const int x );
    int gett(){return total;}
    Node* gethead(){return head;}
    int gettotal()const{return total;}
};                        
void list::Inser(Date*  a)
{
    total++;
    Node*q=head;
   
    Node*f=new Node(a);
   
    if(!head)
    { 
        head=f;
    return;
    }
    if(head->getD()->getnum()>f->getD()->getnum())
    {         
        head=f;
        f->setn(q);
            
             return;
    }
    if(!head->getn())
    {
        head->setn(f);
       
        return;
    }
    while(q)
    {
    Node*e=q->getn();
    int c=e->getD()->getnum();
    if(c>f->getD()->getnum())
    {
        q->setn(f);
        f->setn(e);
        return;
    }
    if(!e->getn())
    {
        e->setn(f);
        return;
    }
    q=q->getn();
    }
}
void list::all()const
{
   
    if(!head)
    {
        cout<<"没有数据"<<endl;
    return;
    }
    Node *k=head;
    while(k)
    {
    k->getD()->print();
    k=k->getn();
    }
   
}
list::~list()
{
    Node *a;
    int i=1;
    while(head)
    {   
        a=head;
        head=head->getn();
        delete a;
        cout<<"删除第"<<i<<"个节点"<<endl;
        i++;
    }
}
Date* list::operator [](const int x)
{
    Node *a=head;
    if(x>=total)
    {
        cout<<"找不到数据!"<<endl;
        return NULL;
    }
    for(int i=0;i<x;i++)
        a=a->getn();
    return a->getD();
}
Date* list::find(const int x)
{
    Node *a=head;
    if(!head)
    {
        cout<<"无数据!"<<endl;
        return NULL;
    }
    while(a)
    {
    if(a->getD()->getnum()==x)
        return a->getD();
    a=a->getn();
    }
    cout<<"找不到数据!"<<endl;
    return NULL;
}
void list::clear(const int x)
{
     if(!head)
       {
           cout<<"无数据删除!"<<endl;
           return;
       }
       Node *a=head;
       Node *b;
       int c;
      if(head->getD()->getnum()==x)
       {
            if(!head->getn())
       {
           delete head;
           head=0;
          cout<<"数据清空!"<<endl;
           return;
       }
            else
            {
           head=head->getn();
           // cout<<head->getD()<<endl;;
           delete a;
           total--;
           a=0;
           cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
           //cout<<head->getD()<<endl;
           return;
            }
       }
     
       while(a)
       {
           if(a->getn()==0)
           {
               cout<<"无数据"<<endl;
               return;
           }
        b=a->getn();
       c=b->getD()->getnum();
       if(c==x)
       {
           a->setn(b->getn());
            delete b;
            total--;
            b=0;
               cout<<"删除成功!现在还有"<<total<<"个节点"<<endl;
            return;
       }
       a=a->getn();
       //cout<<"没有该编号"<<endl;
       }
      
       cout<<"没有该编号"<<endl;
}
class measure:private list
{
private:
    //list l;
public:
    void relase(Date*);
    void run();
};
void measure::relase(Date*a)
{
      Node*head1;
        
      head1=gethead();
    while(head1)
    {
        if(head1->getD()->getnum()==a->getnum())
        {
            cout<<"你输入的商品与编号"<<head1->getD()->getnum()<<"相等!"<<endl;
            return;
        }
        head1=head1->getn();
    }
    Inser(a);
}

void measure::run()
{
    bool g=false;
    Date* p=0;
    //measure l;
    //list l;
    int w=0;
    int q=0;
    while(1)
    {
    cout<<"1.增加商品 2.列出所有商品 3.查找商品 4.删除商品 5.商品数目 6.保存 7.读取 8.退出 "<<endl;
      cin>>q;
      switch(q)
      {
      case 1:
          {
          cout<<"1.图书类 2.药品类"<<endl;
          cin>>w;
          if(w==1||w==2)
          {
              int a;float b;
             cout<<"请输入编号"<<endl;
             cin>>a;
             cout<<"请输入价格"<<endl;
             cin>>b;
             if(w==1)
             {
                 p=new book(a,b);
                 relase(p);
                 break;
                }
             if(w==2)
             {
                  p=new drug(a,b);
                 relase(p);
                 break;
              }
          }
          else
              cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 2:all();
          break;
      case 3:
          {
              cout<<"1.按编号查找,2按序列号查找"<<endl;
              cin>>w;
              if(w==1||w==2)
              {    int s;
                  if(w==1)
                  {
                      cout<<"请输入编号"<<endl;
                          cin>>s;
                          if(!find(s))
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                      find(s)->print();
                      break;
                  }
                  if(w==2)
                  {
                     cout<<"请输入序列号"<<endl;
                     cin>>s;
                     if(!(*this)[s-1])
                          {
                              cin.get();
                              cin.get();
                              cout<<"按回车返回"<<endl;
                              break;
                          }
                     (*this)[s-1]->print();
                     break;
                  }
                  }
              else
                  cout<<"请输入1~2之间的数字"<<endl;
          }
          break;
      case 4:
          {
              cout<<"请输入要删除商品的编号"<<endl;
              cin>>w;
              clear(w);
            
          }
          break;
      case 5:cout<<"商品的数量为"<<gett()<<endl;
          break;
      case 6:
          {
          long num=gettotal();
          ofstream fcout;
           fcout.open(file,ios::out|ios::binary);
          SaveDate *date2=new SaveDate[num];
           SaveDate *D;
          for(int i=0;i<num;i++)
          { 

                  
                    D=&date2[i];
            
                 Date *a=(*this)[i];
               if(typeid(*a).name()==typeid(book).name())
               {
                  D->Sclass=1;
               }
               if(typeid(*a).name()==typeid(drug).name())
               {
                   D->Sclass=2;
               }
               D->Snum=(*this)[i]->getnum();
               D->Sprice=(*this)[i]->getpirce();
              
               fcout.write((char*)&D,sizeof D);
                 long x=sizeof D;
                       long x2  =x*(i+1);
              fcout.seekp( x2,ios::beg);
       
          }
          fcout.clear();
            fcout.close();
          cout<<"保存成功"<<endl;

          }
          break;

      case 7:
          {
              long num=gettotal();
          SaveDate *ax=new SaveDate;
                ifstream fcin(file,ios::binary);
               if(!fcin.is_open())
          {
              cout<<"读取失败!"<<endl;
          }
          else
          {
              fcin.seekg(0);
             
        while(fcin.read((char*)&ax,sizeof ax))
        {
            cout<<"类型:";
                if(ax->Sclass==1)
                    cout<<" 图书类:"<<setw(N);
                else
                    cout<<" 药品类:"<<setw(N);
            cout<<"编号:"<<ax->Snum<<setw(N);
            cout<<"价格:"<<fixed<<showpoint<<setprecision(2)<<ax->Sprice<<endl;
       
        
        }
        if(fcin.eof())
                                                      
        {
               fcin.clear();
               fcin.close();
               }
               }
          }
          break;
      case 8:g=true;
          break;


       
      }
      if(g)
          break;//switch
    }//while
}

void main()
{

    measure r;
    r.run();
}
完整的
程序代码:
#include<iostream>
using namespace std;
template <class T,class T2=double>
class electrician
{
    T um;//最大值
    T ω;//角频率
    T φ;//初相位
    T2 Л;//圆周率
public:
    electrician(T _um,T _ω,T _φ,T2 _Л):um(_um),ω(_ω),φ(_φ),Л(_Л){};
    void untie1();
    void untie2();
    void untie3();
    void end(){cout<<"相位差:φ1-φ2:\t"<<((100*Л*(1/(100*Л/2*Л))))+60-((100*Л*(1/(100*Л/2*Л)))-30)<<endl;}
};
template <class T,class T2>
void electrician<T,T2>::untie1()
{
    cout<<"最大值:\t:"<<um<<endl;
    cout<<"有效值:1/根号2x最大值\t:\t"<<(1/1.41)*um<<endl;   
}
template <class T,class T2>
void electrician<T,T2>::untie2()
{
    cout<<"频率:f=1/t;ω=2Лf;f=ω/2Л;e1:\t"<<(ω*Л)/(2*Л)<<endl;
    cout<<"周期:t=1/f;\t"<<1/(ω*Л/2*Л)<<endl;   
}
template <class T,class T2>
void electrician<T,T2>::untie3()
{
    cout<<"相位:(100Лt+60°)\t:"<<(ω*Л)*(1/(ω*Л/2*Л))+φ<<endl;
    cout<<"初相位:\t"<<φ<<endl;
}
void main()
{
    cout<<"已知两正弦电动势e1=100sin(100Лt+60°)V,e2=65sin(100Лt-30°)V."<<endl;
    cout<<"1.)求电动势的最大值和有效值:"<<endl;
    cout<<"2.)频率,周期"<<endl;
    cout<<"3.)相位,初相位,相位差"<<endl;
    //解答
    electrician<int> one(100,100,60,3.14);
    electrician<int> two(65,100,-30,3.14);
    cout<<"e1:";
    one.untie1();
    cout<<"e2:";
    two.untie1();
    cout<<"e1:";
    one.untie2();
    cout<<"e2:";
    two.untie2();

    cout<<"e1:";
    one.untie3();
    cout<<"e2:";
    two.untie3();
    one.end();
   
}









[ 本帖最后由 xg5699 于 2011-10-3 00:23 编辑 ]

都不结贴我郁闷那!
2011-09-10 23:21
快速回复:请问这个错在哪?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.099181 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved