當前位置:編程學習大全網 - 編程語言 - 急!我有壹個C++編程題,是電話簿管理程序,已經編號了,但是有些問題。

急!我有壹個C++編程題,是電話簿管理程序,已經編號了,但是有些問題。

我重新修改了,但出現了新問題,編譯時就出現錯誤,不能運行

// structs.h Begin

//#ifndef _STRUCTS_H

//#define _STRUCTS_H

#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#include<fstream.h>

//using namespace std;

typedef enum Kind {Array, Pointers} Kind;

class Data

{ char szName[20];

char szCode[20];

public:

Data(){strcpy(szName,"");strcpy(szCode,"");}

Data(char *name,char *code){strcpy(szName,name);strcpy(szCode,code);}

Data(const Data &data)

{ strcpy(szName,data.szName);

strcpy(szCode,data.szCode);

}

Data &operator =(const Data & data)

{ strcpy(szName,data.szName);

strcpy(szCode,data.szCode);

return *this;

}

int operator >=(const Data &data)

{ if(strcmp(szName,data.szName)>=0)

return 1;

else return 0;

}

int operator !=(char * num)

{ if(szCode!=num)return 1;

else return 0;

}

int operator !=(const Data & data)

{ if(szName==data.szName&&szCode==data.szCode)

return 1;

else return 0;

}

friend ostream& operator<<(ostream &os,Data &data)

{ os<<data.szName<<" 的電話號碼為 "<<data.szCode;

return os;

}

friend istream& operator>>(istream &is,Data &data)

{ is>>data.szName>>data.szCode;

return is;

}

};

class UList //基類

{

public:

virtual int Size()=0;

virtual bool Insert(const Data&,int)=0;

virtual int Find(const Data &)=0;

virtual bool Get(int, Data&)=0;

virtual void Print()const=0;

virtual bool InsertSort()=0;

virtual bool create()=0;

virtual bool Remove(int place)=0;

virtual void write()=0;

virtual void openfile()=0;

};

class PList:public UList //鏈表操作類

{

class Node{

Data item;

Node *next;

public:

Node(const Data &dat):item(dat),next(NULL){}

Node(const Node &nod):item(nod.item),next(NULL){}

friend class PList;

};

Node *begin;

Node *end;

int num;

public:

PList():begin(NULL),end(NULL),num(0){}

~PList(){

Node *tmp=begin;

while(num>0){

begin=begin->next;

delete tmp;

tmp=begin;

}

}

int Size(){return num;}

bool Insert(const Data&,int);

int Find(const Data &x);

bool Get(int, Data&);

void Print()const;

bool InsertSort();

bool Remove(int);

bool create();

void write();

void openfile();

};

class AList:public UList //矩陣操作類

{

Data *arr; // holds the dynamic Array

int num; // cells filled so far

public:

static int MAX_SIZE; // max_size beginning value

AList():arr(new Data[MAX_SIZE]),num(0){}

~AList(){delete[] arr;}

int Size(){return num;}

bool Insert(const Data&,int);

int Find(const Data &x);

bool Get(int, Data&);

void Print()const;

bool InsertSort();

bool Remove(int);

bool create();

void write();

void openfile();

};

//#endif

// structs.h end

//structs.cpp Begin

//#include"structs.h"

int AList::MAX_SIZE = 4;

UList* init_list(Kind k) //初始化函數

{

UList *tmp;

switch(k){

case Array:

tmp=new AList;

break;

case Pointers:

tmp=new PList;

break;

default:

cout<<endl<<"unknown option"<<endl;

return NULL;

}

return tmp;

}

bool PList::Insert(const Data &x,int place) //鏈表插入操作

{

Node *tmp;

if ((place<=0)||(place>num+1))

return false;

if (place==1) // insert at the begining of the list

{

tmp=begin;

begin=new Node(x);

begin->next=tmp;

if (num==0) // in case is the only item in the lis

{

end=begin;

end->next=NULL;

}

}

else if (place==num+1) // insert at the end of list

{

tmp=end;

end=new Node(x);

end->next=NULL;

tmp->next=end;

}

else

{

Node *holder;

tmp=begin;

for(int i=1;i<place-1;++i) // forward till list(place-1)

tmp=tmp->next;

holder=tmp->next;

tmp->next=new Node(x);

tmp->next->next=holder;

}

++num;

return true;

}

bool PList::InsertSort() //鏈表的排序插入

{ Data D0;

int a=1;

cout<<"請輸入要插入的人名和號碼:"<<endl;

cin>>D0;

Node *tmp,*D1,*D2;

while(a==1){

if (begin==NULL)

{ tmp=begin;

begin=new Node(D0);

begin->next=tmp;

if (num==0) // in case is the only item in the list

{

end=begin;

end->next=NULL;

}

}

else

{ D1=begin;

while(D1->item>= D0)

{ D2=D1;D1=D1->next; }

if(D1->next!=NULL)

{ tmp=new Node(D0);

D2->next=tmp;

tmp->next=D1->next;

}

else

{ tmp=new Node(D0);

D2->next=tmp;

tmp->next=NULL;

}

}

cout<<"是否繼續插入?1.Yes 2.No"<<endl;

}

return true;

}

int PList::Find(const Data &x) //鏈表節點查找

{

Node *tmp=begin;

int i=1;

while((tmp!=NULL)&&(tmp->item!=x))

{

++i;

tmp=tmp->next;

}

if (tmp==NULL)

return 0;

return i;

}

bool PList::Get(int place, Data &ret) //得到節點數據

{

Node *tmp=begin;

if((place<=0)||(place>num+1))

return false;

for(int i=1;i<place;++i)

tmp = tmp->next;

ret = tmp->item;

return true;

}

bool PList::create() //創建鏈表

{ Node *p1,*p2;

int p=1;

Data i;

cout<<"請輸入姓名和號碼:"<<endl;

p1=new Node(i);

cin>>p1->item;

num++;

cout<<"是否繼續創建?"<<endl;

cout<<"1.yes 2.no"<<endl;

cin>>p;

if(p==2){p=0;p2=p1;}

while(p){

if(begin==NULL)

begin=p1;

else

p2->next=p1;

p2=p1;

cout<<"請輸入姓名和號碼:"<<endl;

p1=new Node(i);

cin>>p1->item;

num++;

cout<<"是否繼續創建?"<<endl;

cout<<"1.yes 2.no"<<endl;

cin>>p;

if(p==2)p=0;

}

p2->next=NULL;

return true;

}

bool PList::Remove(int place) //鏈表移出操作

{

Node *tmp=begin;

if((place<=0)||(place>num+1))

return false;

if(place==1) // remove the 1st item

{

begin=begin->next;

delete tmp;

}

else if(place==num) // remove the last item

{

for(int i=1;i<place-1;++i)

tmp=tmp->next;

delete end;

end=tmp;

end->next=NULL;

}

else

{

Node *holder;

for(int i=1;i<place-1;++i)

tmp=tmp->next;

holder=tmp->next;

tmp->next=holder->next;

delete holder;

}

if(num==1)

end=begin=NULL;

--num;

return true;

}

void PList::Print(void)const{ //輸出鏈表

if (num==0) return;

for(Node *tmp=begin;tmp!=NULL;tmp=tmp->next){

cout<<tmp->item;

}

cout<<endl;

return;

}

void PList::write() //保存文件

{ ofstream outfile;

char Filename[20];

cout<<"請輸入數據文件名:"<<endl;

cin.getline(Filename,20);

outfile.open(Filename);

if(!outfile)

{ cout<<"打開文件錯誤!"<<endl;

exit(0);

}

Node *p;

p=begin;

while(p)

{outfile<<p->item<<endl;

p=p->next;

}

cout<<"文件存儲完畢。"<<endl;

}

void PList::openfile()

{ ifstream infile; //讀出鏈表文件

char FileName[20]; //存放從鍵盤輸入的文件名

int end_loop;

//char aNode[22] ; //存放從數據文件讀入的記錄

cout<<"請輸入文件名:"<<endl;

cin>>FileName;

infile.open(FileName);

if(!infile)

{ cout<<"打開文件錯誤!"<<endl;

exit(0);

}

Node *p1,*p2,*p;

Data x;

p=begin;

p1=new Node(x);

p1=p;

p2=p1;

p=p->next;

do

{ if(p)

{p1=new Node(x);//開辟壹個新的結點

p1=p;

p2->next=p1;

p1=p1->next;

p2=p1;

p=p->next;

end_loop=1;

}

else //如果結點指針分配空間出錯,輸出提示信息

{ cout<<"WARNING:Memory error.Load from disk was unsuccessful.\n";

end_loop=0;

}

p1=NULL;

}while(end_loop==0);

infile.close();//關閉輸入文件

}

bool AList::Insert(const Data &x,int place=1) //矩陣插入壹元素

{

if (num==MAX_SIZE) // if the array is full

for(int i=0;i<=num;i++)

{ Data *tmp;

tmp=new Data[MAX_SIZE+1];

tmp[i]=arr[i];

arr=tmp;

}

if((place<=0)||(place>num+1))

return false;

for(int i=num-1;i>=place-1;--i)

arr[i+1]=arr[i];

arr[place-1]=x;

++num;

++MAX_SIZE;

return true;

}

bool AList::InsertSort() //矩陣排序插入

{

if(num==MAX_SIZE)

{

Data *temp;

for(int i=0;i<=num;i++)

{ temp=new Data[MAX_SIZE+1];

temp[i]=arr[i];

}

MAX_SIZE++;

num++;

arr=temp;

}

Data D;

int a=1;

cout<<"輸入要插入的姓名和號碼:"<<endl;

cin>>D;

while(a==1)

{

for(int m=0;m<num;m++)

if(D>=arr[m])

{ for(int j=num;j>m+1;j--)

{ arr[j]=arr[j-1];

arr[m]=D;

}

arr[m+1]=D;

}

cout<<"是否繼續插入:1.Yes 2.No"<<endl;

cin>>a;

}

return true;

}

int AList::Find(const Data &x) //矩陣查找壹元素

{

int i=0;

while((i<num)&&(arr[i]!=x)) ++i;

if (i==num) i=-1; // we got to the end of the array

return i+1;

}

bool AList::Get(int place, Data &ret) //得到元素值

{

if((place<=0)||(place>num+1))

return false;

ret = arr[place-1];

return true;

}

bool AList::Remove(int place) //矩陣移出壹元素

{

if((place<=0)||(place>num+1))

return false;

for(int i=place-1;i<num;++i)

arr[i]=arr[i+1];

--num;

return true;

}

bool AList::create() //創建數組

{

int m;

cout<<"輸入姓名和號碼:"<<endl;

cin>>arr[0];

num++;

for(int i=1;i<MAX_SIZE;i++)

{cout<<"是否繼續創建?"<<endl;

cout<<"1.Yes 2.No"<<endl;

cin>>m;

if (m==2)break;

else

{ cout<<"輸入姓名和號碼:"<<endl;

cin>>arr[i];

num++;

MAX_SIZE++;

}

}

cout<<"create successfully"<<endl;

return true;

}

void AList::Print()const //輸出矩陣

{

for(int i=0;i<num;++i) cout<<arr[i];

cout<<endl;

return;

}

void AList::write() //保存文件

{ ofstream outfile;

char Filename[20];

cout<<"請輸入數據文件名:"<<endl;

cin.getline(Filename,20);

outfile.open(Filename);

if(!outfile)

{ cout<<"打開文件錯誤!"<<endl;

exit(0);

}

Data *p;

p=arr;

while(p )

{for(int i=0;i<=MAX_SIZE;i++)

outfile<<arr[i]<<endl;

}

cout<<"文件存儲完畢。"<<endl;

}

void AList::openfile()

{ ifstream infile; //讀出文件

Data *p;

int i=0;

char FileName[20]; //存放文件名

cout<<"請輸入文件名:"<<endl;

cin>>FileName;

infile.open(FileName);

if(!infile)

{ cout<<"打開文件錯誤!"<<endl;

exit(0);

}

p=new Data[MAX_SIZE];

p[0]=arr[0];

if(arr[i]!=0)

{ for(i=1;i<=MAX_SIZE;i++)//將數據寫入結點

p[i]=arr[i];

}

infile.close();//關閉文件

}

bool Test_insert (UList * &tmp)

{

bool res = true;

Data x;

cout<<"輸入要插入的姓名和號碼:"<<endl;

cin>>x;

if (!tmp->Insert(x,1))

{

cerr << "Failure on Insert test no. 1 - insert into a valid cell number" << endl;

res = false;

}

else

cout << "Success on Insert test no. 1 - insert into a valid cell number" << endl;

return res;

}

bool Test_InsertSort(UList * &tmp)

{

bool res=true;

if (!tmp->InsertSort())

{

cerr << "Failure on InsertSort test no. 1 - insert into a valid cell number" << endl;

res = false;

}

else

cout << "Success on InsertSort test no. 1 - insert into a valid cell number" << endl;

return true;

}

bool Test_Remove(UList * &tmp)

{ Data m;

int q;

cout<<"輸入要刪除的姓名和號碼:"<<endl;

cin>>m;

q=tmp->Find(m);

tmp->Remove(q);

return true;

}

void Test_output(UList * &tmp)

{

tmp->Print();

}

bool Test_create(UList * &tmp)

{ tmp->create();

cout<<"Success on create test"<<endl;

return true;

}

void Test_write(UList * &tmp)

{ tmp->write();}

void Test_openfile(UList * &tmp)

{ tmp->openfile();}

int main()

{

while (1)

{

int ch;

Kind k;

cout<<"choose Kind"<<endl<<"1. Array"<<endl<<"2. Pointers"<<

endl<<"any other no. to exit"<<endl;

cin>>ch;

//cin.ignore();

switch(ch){

case 1:

k=Array;

break;

case 2:

k=Pointers;

break;

default:

cout<<"bye bye"<<endl;

exit(0);

}

UList *tmp=init_list(k);

Test_openfile(tmp);

int p=1;

while(p){

cout<<"choose test"<<endl<<"1. insert (LIST)"<<endl

<<"2.insertsort(LIST)"<<endl<<"3.remove(List)"<<endl<<"4.output (List)"<<endl

<<"5.create (List)"<<endl<<"any other no. to exit"<<endl;

cin>>ch;

switch(ch){

case 1:

if(Test_insert(tmp))

cout<<endl<<"insert test seccessful"<<endl;

break;

case 2:

if(Test_InsertSort(tmp))

cout<<endl<<"insertsort test seccessful"<<endl;

break;

case 3:

if(Test_Remove(tmp))

cout<<endl<<"remove test seccessful"<<endl;

break;

case 4:

Test_output(tmp);

break;

case 5:

Test_create(tmp);

cout<<endl<<"create test seccessful"<<endl;

break;

default:

cout<<"goodbye"<<endl;

exit(0);

}

cout<<"是否繼續操作"<<endl;

cout<<"1.yes 2.no"<<endl;

cin>>p;

if(p==2)p=0;

}

cout << endl << endl;

}

Test_write(tmp);

return 0;

}

//main.cpp end

  • 上一篇:編程石
  • 下一篇:小學奧數周期問題(壹)
  • copyright 2024編程學習大全網