急!我有一个C++编程题,是电话簿管理程序,已经编号了,但是有些问题。
问题是为什么我执行程序时,主函数不调用文件写入函数voidwrite()和文件读出函数voidopenfile()?请高手指导,谢谢!程序会发在第一个回答里。...
问题是为什么我执行程序时,主函数不调用文件写入函数void write()和文件读出函数void openfile()?请高手指导,谢谢!程序会发在第一个回答里。
展开
3个回答
展开全部
我重新修改了,但出现了新问题,编译时就出现错误,不能运行
// 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
// 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
展开全部
同学,调用write()和openfile()函数时,前面不要加void呀,编译器把它理解成函数的声明了,所以没有执行啊。。。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的源代码呢?QQ:951092485
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询