64,691
社区成员
发帖
与我相关
我的任务
分享
#include"functions.h"
void welcome()
{
system("cls");
int choose;
cout<<"欢迎使用网址收藏管理器"<<endl;
cout<<"请选择功能:"<<endl;
cout<<"1.创建收藏"<<endl;
cout<<"2.打开收藏"<<endl;
cout<<"3.退出"<<endl;
cout<<"请选择:";
cin>>choose;
switch(choose)
{
case 1:
system("cls");
create_collection();
break;
case 2:
system("cls");
open_collection();
break;
case 3:
system("cls");
break;
default:
system("cls");
welcome();
break;
}
}
int create_collection()
{
vector<URL> u;
cout<<"开始创建收藏文件:"<<endl;
cout<<"请依次输入名称,网址,类别(每项以空格隔开,每个记录为一行):"<<endl;
string name,add,cate;
while(cin>>name>>add>>cate)
{
URL u_temp(name,add,cate);
u.push_back(u_temp);
}
cin.clear();
int choose;
cout<<"请选择接下来的操作:"<<endl;
cout<<"1.保存收藏记录到文件"<<endl;
cout<<"2.按分类进行查询"<<endl;
cout<<"3.其它任意键返回主菜单"<<endl;
cin>>choose;
switch(choose)
{
case 1:
save_collection_to_file(u);
break;
case 2:
shown_by_cate(u);
break;
default:
welcome();
break;
}
return 0;
}
void shown_by_cate(const vector<URL> &rhs)
{
string cate;
cout<<"请输入要查询的分类:";
cin>>cate;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
int cnt=0;
for(vector<URL>::const_iterator it=rhs.begin();it!=rhs.end();++it)
{
if(it->getcate()==cate)
{
it->show_without_cate();
cout<<"------------------------------------------------------"<<endl;
cnt++;
}
}
if(cnt)
{
cout<<"分类为"<<cate<<"的共有以上"<<cnt<<"条记录:"<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
}
else
{
cout<<"分类为"<<cate<<"的记录为空!"<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
}
cout<<"1.继续查询"<<endl;
cout<<"2.其它任意键返回主菜单"<<endl;
cout<<"请选择:";
int choose;
cin>>choose;
switch(choose)
{
case 1:
shown_by_cate(rhs);
break;
default:
welcome();
break;
}
}
int save_collection_to_file(const vector<URL> &rhs)
{
string filename;
cout<<"请输入文件名:"<<endl;
cin>>filename;
filename+=".txt";
ofstream outfile(filename.c_str());
if(!outfile)
{
cerr<<"打开文件错误!"<<endl;
return -1;
}
for(vector<URL>::const_iterator it=rhs.begin();it!=rhs.end();++it)
outfile<<it->getname()<<" "<<it->getadd()<<" "<<it->getcate()<<endl;
cout<<"保存成功!按任意键返回主菜单"<<endl;
system("pause");
welcome();
return 0;
}
void open_collection()
{
vector<URL> u;
open_collection_from_file(u);
}
int open_collection_from_file(vector<URL> &rhs)
{
string filename;
cout<<"请输入文件名:"<<endl;
cin>>filename;
filename+=".txt";
ifstream infile(filename.c_str());
if(!infile)
{
cerr<<"打开文件错误!"<<endl;
return -1;
}
while(!infile.eof())
{
string name,add,cate;
infile>>name>>add>>cate;
URL u_temp(name,add,cate);
rhs.push_back(u_temp);
}
cout<<"读取成功!"<<endl;
cout<<"1.按分类查询"<<endl;
cout<<"2.排序"<<endl;
cout<<"3.其他任意键返回主菜单"<<endl;
cout<<"请选择接下来的操作:"<<endl;
int choose;
cin>>choose;
switch(choose)
{
case 1:
shown_by_cate(rhs);
break;
case 2:
//排序
break;
default:
welcome();
break;
}
return 0;
}
int main()
{
welcome();
return 0;
}
#include"functions.h"
void welcome()
{
system("cls");
int choose;
cout<<"欢迎使用网址收藏管理器"<<endl;
cout<<"请选择功能:"<<endl;
cout<<"1.创建收藏"<<endl;
cout<<"2.打开收藏"<<endl;
cout<<"3.退出"<<endl;
cout<<"请选择:";
cin>>choose;
switch(choose)
{
case 1:
system("cls");
create_collection();
break;
case 2:
system("cls");
open_collection();
break;
case 3:
system("cls");
break;
default:
system("cls");
welcome();
break;
}
}
int create_collection()
{
vector<URL> u;
cout<<"开始创建收藏文件:"<<endl;
cout<<"请依次输入名称,网址,类别(每项以空格隔开,每个记录为一行):"<<endl;
string name,add,cate;
while(cin>>name>>add>>cate)
{
URL u_temp(name,add,cate);
u.push_back(u_temp);
}
cin.clear();
int choose;
cout<<"请选择接下来的操作:"<<endl;
cout<<"1.保存收藏记录到文件"<<endl;
cout<<"2.按分类进行查询"<<endl;
cout<<"3.其它任意键返回主菜单"<<endl;
cin>>choose;
switch(choose)
{
case 1:
save_collection_to_file(u);
break;
case 2:
shown_by_cate(u);
break;
default:
welcome();
break;
}
return 0;
}
void shown_by_cate(const vector<URL> &rhs)
{
string cate;
cout<<"请输入要查询的分类:";
cin>>cate;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
int cnt=0;
for(vector<URL>::const_iterator it=rhs.begin();it!=rhs.end();++it)
{
if(it->getcate()==cate)
{
it->show_without_cate();
cout<<"------------------------------------------------------"<<endl;
cnt++;
}
}
if(cnt)
{
cout<<"分类为"<<cate<<"的共有以上"<<cnt<<"条记录:"<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
}
else
{
cout<<"分类为"<<cate<<"的记录为空!"<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * *"<<endl;
}
cout<<"1.继续查询"<<endl;
cout<<"2.其它任意键返回主菜单"<<endl;
cout<<"请选择:";
int choose;
cin>>choose;
switch(choose)
{
case 1:
shown_by_cate(rhs);
break;
default:
welcome();
break;
}
}
int save_collection_to_file(const vector<URL> &rhs)
{
string filename;
cout<<"请输入文件名:"<<endl;
cin>>filename;
filename+=".txt";
ofstream outfile(filename.c_str());
if(!outfile)
{
cerr<<"打开文件错误!"<<endl;
return -1;
}
for(vector<URL>::const_iterator it=rhs.begin();it!=rhs.end();++it)
outfile<<it->getname()<<" "<<it->getadd()<<" "<<it->getcate()<<endl;
cout<<"保存成功!按任意键返回主菜单"<<endl;
system("pause");
welcome();
return 0;
}
void open_collection()
{
vector<URL> u;
open_collection_from_file(u);
}
int open_collection_from_file(vector<URL> &rhs)
{
string filename;
cout<<"请输入文件名:"<<endl;
cin>>filename;
filename+=".txt";
ifstream infile(filename.c_str());
if(!infile)
{
cerr<<"打开文件错误!"<<endl;
return -1;
}
while(!infile.eof())
{
string name,add,cate;
infile>>name>>add>>cate;
URL u_temp(name,add,cate);
rhs.push_back(u_temp);
}
cout<<"读取成功!"<<endl;
cout<<"1.按分类查询"<<endl;
cout<<"2.排序"<<endl;
cout<<"3.其他任意键返回主菜单"<<endl;
cout<<"请选择接下来的操作:"<<endl;
int choose;
cin>>choose;
switch(choose)
{
case 1:
shown_by_cate(rhs);
break;
case 2:
//排序
break;
default:
welcome();
break;
}
return 0;
}