Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2349039
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:56

#include
#include
#include
#include
void filelist()
{
fstream flist;
char ch;
cout<<"文件列表:"< flist.open("filelist.txt",ios::in);
while(flist.get(ch))
cout< flist.close();
}
//-----------------------------------------------------------------------------------------
void inti()
{
cout<<"-------------------------------------------------------------------------------"<    cout<<"1 添加"<<"\t"<<"2 删除"<<"\t"<<"3 排序"<<"\t"<<"4 查找"<<"\t"<<"5 修改"< cout<<"6 显示"<<"\t"<<"7 清屏"<<"\t"<<"8 存档"<<"\t"<<"9 读档"<<"\t"<<"10 打印"<<"\t"<<
"11 查看文件"< cout<<"-------------------------------------------------------------------------------"<}
//--------------------------------------------------------------------------------------
template                      //规范输入函数
void cin_judge(istream & cin,type& i)
{
while(!cin)
{
cin.clear();
cin.ignore(100,'\n');
cout<<"请重输入"< cin>>i;
}
}
class item
{
public:

char name[80];
int score;
item *prior;
item *next;
};
//----------------------------------------------------------------------------------------
template
class list
{
public:
list();
void add();
int remove();
void paixu();
item *find();
int change();
void show();
    int save();
    int read();
    void fileprint();
private:
type *head,*end;
};
//-----------------------------------------------------------------------------------------
template
list::list()
{
end=head=0;
}
//------------------------------------------------------------------------------------------
template
void list::add()
{
item *p;
    p=new type;
if(!p)
{
cout<<"allocation error.\n";
exit(1);
}
cout<<"名字:"< cin>>p->name;
cout<<"成绩:"< cin>>p->score;
cin_judge(cin,p->score);
if(head==0)
{
head=end=p;
p->prior=p->next=NULL;
}
else
{
p->prior=end;
end->next=p;
end=p;
end->next=0;
}
}
//------------------------------------------------------------------------------------------
template
int list::remove()
{
type*p=find();
if(p==0) return 0;
if(p->prior)
{
if(p->next)
{
p->next->prior=p->prior;
p->prior->next=p->next;
}
else
{
p->prior->next=0;
end=p->prior;
}
}
else
{
if(p->next)
{
p->next->prior=0;
head=p->next;
}
else
{
head=end=0;
}
}
    delete p;
return 0;
}
//-----------------------------------------------------------------------------------
template
void list::paixu()
{
int j=0,temp;
char temp1[80];
type *p=head;
for(;p!=0;p=p->next) j++;
p=head;
for(int i=0;i {
if(j!=1)
{
if(p->scorenext->score)
{
temp=p->score;
strcpy(temp1,p->name);
p->score=p->next->score;
strcpy(p->name,p->next->name);
            p->next->score=temp;
strcpy(p->next->name,temp1);
}
p=p->next;
if(p->next==0) p=head;
}
}
}
//------------------------------------------------------------------------------------
template
item *list::find()
{
    char fname[80];
type *p=head;
cout<<"输入名字:";
cin>>fname;
for(;p;p=p->next)
    if(!strcmp(p->name,fname))
{
cout<name<<"  "<score< return p;
}

cout<<"不存在!"< return 0;
}
//-------------------------------------------------------------------------------------
template
int list::change()
{
type *p=find();
if(p==0) return 0;
cout<name<<"的成绩改为:";
cin>>p->score;
cin_judge(cin,p->score);
return 0;
}
//-------------------------------------------------------------------------------------
template
void list::show()
{
item *p;
for(p=head;p!=0;p=p->next)
cout<name<<"   "<score<}

//----------------------------------------------

--------------------next---------------------
template
int list::save()
{
fstream file;
type *p;
if(head==0)
{
int ci=0;
cout<<"没有建立数据,保存会清空数据! 1.确定保存 2.放弃"< cin>>ci;
cin_judge(cin,ci);
if(ci!=1) return 0;
}
fstream filename;
filename.open("filelist.txt",ios::out|ios::app);
    char ch[100];
cout<<"保存文件名为:"<    cin>>ch;
filename< filename.close();
    file.open(ch,ios::out);
    for(p=head;p!=0;p=p->next)
file.write((char*)p,sizeof(item));
file.close();
cout<<"存档成功!"< return 0;
}
//----------------------------------------------------------------------------------------
template
int list::read()
{
char ch[100];
cout<<"输入要读取的文件名:"< cin>>ch;
type *p;
head=end=0;
long a=0,b=0;
fstream file;
    file.open(ch,ios::in);
if(!file)
{
cout<<"没有找到指定文件!"< return 0;
}
    file.seekg(0,ios::end);
if(file.tellg()==0)
{
cout<<"没有存档数据!"< return 0;
}
a=file.tellg();
b=sizeof(type);
a=a/b;
file.seekg(0,ios::beg);
for(;a>0;a--)
{
p=new type;
        file.read((char*)p,sizeof(item));
if(head==0)
{
end=head=p;
head->prior=head->next=0;
}
else
{
end->next=p;
p->prior=end;
end=p;
end->next=0;
}
}
file.close();
cout<<"读档成功!"< return 0;
}
//-----------------------------------------------------------------------------------------
template
void list::fileprint()
{
type *ptxt;
fstream outfile;
outfile.open("file.txt",ios::out);
for(ptxt=head;ptxt!=0;ptxt=ptxt->next)
{
outfile<name<<"\t"<score< }
outfile.close();
system("file.txt");
}


//----------------------------------------------------------------------------------------

void main()
{
inti();
list i;
int a;
while(1)
{
    cout<<"选择操作 1-10:"< cin>>a;
  cin_judge(cin,a);
switch(a)
{
case 1:
i.add();break;
case 2:
i.remove();break;
case 3:
i.paixu();break;
case 4:
i.find();break;
case 5:
i.change();break;
case 6:
i.show();break;
case 7:
system("cls");inti();break;
case 8:
i.save();break;
case 9:
i.read();break;
case 10:
i.fileprint();break;
    case 11:
filelist();break;
}
}
}


--------------------next---------------------

阅读(1677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~