qq_18881007 2017-06-24 06:36 采纳率: 0%
浏览 1040

c++vector排序问题,学生党一枚 求解答

//****************************************
//*train.h类声明头文件
//****************************************

#ifndef train_h_
#define train_h_
#include
using namespace std;
class Train
{
protected:
string num;
string ssta;//发站
string asta;//到站
string stime;//发时
string atime;//到时
string time;//历时
public:
bool Set_num(string ); //设置信息
bool Set_ssta(string );
bool Set_asta(string );
bool Set_stime(string );
bool Set_atime(string );
bool Set_time(string );

string Get_num( ); //获取信息
string Get_ssta( );
string Get_asta( );
string Get_stime( );
string Get_atime( );
string Get_time( );
};
#endif

//*****************************************
//*train.cpp类方法定义文件
//*****************************************

#include"train.h"
bool Train::Set_num( string n )
{
num = n;
return true;
}
bool Train::Set_ssta(string ss )
{
ssta = ss;
return true;
}
bool Train::Set_asta(string as )
{
asta = as;
return true;
}
bool Train::Set_stime(string st )
{
stime = st;
return true;
}
bool Train::Set_atime(string as )
{
atime = as;
return true;
}
bool Train::Set_time(string t)
{
time = t;
return true;
}

string Train::Get_num( )

{
return num;
}
string Train::Get_ssta( )
{
return ssta;
}
string Train::Get_asta( )
{
return asta;
}
string Train::Get_stime( )
{
return stime;
}
string Train::Get_atime( )
{
return atime;
}
string Train::Get_time( )
{
return time;
}

//*****************************************
//*main.cpp系统主文件
//*****************************************

#include
#include
#include
#include
#include
#include
#include"train.h"
#define NULL 0
string infilenum = "Num.txt";
string infileinfo= "Info.txt";
vectorcard;
using namespace std;

Train * Creat( ) //添加信息
{
Train *temp;
string info;
temp =new Train;
cout << "车 次 : ";
cin >> info;
temp ->Set_num(info);
cout << "发 站 : ";
cin >> info;
temp ->Set_ssta(info);
cout << "到 站 : ";
cin >> info;
temp ->Set_asta(info);
cout << "发 时 : ";
cin >> info;
temp ->Set_stime(info);
cout << "到 时 : ";
cin >> info;
temp ->Set_atime(info);
cout << "历 时 : ";
cin >> info;
temp ->Set_time(info);
cout<<endl<<"添加成功 O(∩_∩)O~"<<endl<<endl;
return temp;
}

Train * Search_num( )// 按照车次查找
{
int i = 1;
string tempnum;
cin >> tempnum;

Train * temp;
temp = card.begin( );
while( i <= card.size( ) )
{
if( temp-> Get_num( ) == tempnum )
{
return temp;
}
temp ++;
i ++;
}
return NULL;
}

Train * Search_sta( ) // 按照到站查找
{
int i = 1;
string tempsta;
cout << "请输入要查找的到站 : " ;

cin >> tempsta;
Train * temp;
temp = card.begin( );
while( i <= card.size( ) )
{
if( temp-> Get_asta( ) == tempsta )
{
return temp;
}
temp++;
i++;
}
return NULL;
}

void Show( Train * temp ) // 显示信息

{
cout<Get_num( )< coutGet_ssta( )< coutGet_asta( )< coutGet_stime( )< coutGet_atime( )< coutGet_time( )<<endl;
cout<<endl;
}

void Save( ) //保存信息(从容器到文件)
{
int i = 1;
Train * temp;
temp = card.begin( );
ofstream os( infileinfo.c_str( ), ios_base::trunc); //文件操作
while( i <= card.size( ) )
{
os << temp->Get_num() <<" "<< temp->Get_ssta() <<" "<< temp->Get_asta()<<" "<< temp->Get_stime()<<" "<< temp->Get_atime()<<" "<< temp->Get_time()<<" ";
temp++;
i++;
}
os.close( );
ofstream is(infilenum.c_str(), ios_base::trunc );
is<< --i;
is.close( );
cout<<endl<<"保存成功 O(∩_∩)O~"<<endl;
}

void Read( ) //文件操作(从文件到容器)
{
int i= 1,m;
Train * temp;
string num1,ssta1,asta1,stime1,atime1,time1;

ifstream is( infilenum.c_str( ) );
is>>m;
is.close();
ifstream os(infileinfo.c_str( ) );
while (i <= m)
{
temp = new Train;
os>>num1>>ssta1>>asta1>>stime1>>atime1>>time1;
temp -> Set_num(num1);
temp -> Set_ssta(ssta1);
temp -> Set_asta(asta1);
temp -> Set_stime(stime1);
temp -> Set_atime(atime1);
temp -> Set_time(time1);
card.push_back(* temp);
i++;
}
cout<<endl;
os.close();
}

void Delete( ) //删除容器中信息
{
Train * temp1;
temp1 = Search_num(); //按车次查找要删除的信息
if( temp1 != NULL)
{
card.erase(temp1);
cout<<endl<<"删除成功 O(∩_∩)O~"<<endl;
}
}

int main( )
{
int i;
char index = '0';
Train temp;
card.reserve(20);
Read();
index = 1;
while(index != '0')
{
cout<<endl;
cout<<"
*******列车车次信息的查询与检索系统********"< cout cout cout cout cout cout cout cout cin>>index;
switch(index)
{
case '1': //浏览
{
temp = card.begin( );
if( card.size() == 0 )
cout<<endl<<"暂无车次信息 ╮(╯_╰)╭"<<endl;
else
{
i = 1;
cout<<endl<<"车次信息如下 : "<<endl;
while(i <= card.size() )
{
cout<<endl;
cout<<setw(10)<<"编 号 : "<< i++<<endl;
Show(temp);
temp++;
}
}
break;
}
case '2': //添加到容器
{
if( card.size() <= card.max_size( ) )
{

cout << endl << "请输入车次信息 : "<<endl;
temp = Creat( );
card.push_back( *temp );

}
else
{

cout << endl << "请输入车次信息 : "< card.reserve(10);
temp = Creat( );
card.push_back(*temp);
}
break;
}
case '3': //查询
{
cout cout cout char in='0';
cin>> in;
switch(in)
{
case '1': //按车次查询
{cout << endl << "请输入车次信息 : ";
temp = Search_num();
if(temp != NULL)
{
cout< Show(temp);
break;
}
else
cout break;
}
case '2': //按到站查询
{
temp = Search_sta();
if(temp != NULL)
{
cout Show(temp);
break;
}
else
cout break;
}
default:
{
cout break;
}
}
break;
}
case '4': //修改容器信息
{
cout temp = Search_num();
Show(temp);
if( temp != NULL)
{
card.erase(temp);
}
cout if(card.size() {
temp = Creat();
card.push_back( *temp);
}
else
{
card.reserve(10);
temp = Creat( );
card.push_back(*temp);
}
break;
}
case '5': //删除容器信息
{
cout Delete();
break;
}
case '0': //退出
{
cout char x;
do
{
cin>> x;
switch( x )
{
case 'y':
Save();
break;
case 'n':
cout<<"未保存所做的修改"<<endl;
break;
default:
cout<<"输入有误 ╮(╯_╰)╭ 请重新输入 "<<endl;
break;
}
}while( x!='y' && x!= 'n');
cout<<endl<<"感谢您的使用,再见 O(∩_∩)O~"<<endl<<endl;
break;
}
default:
cout<<endl<<"出错了 ╮(╯_╰)╭"<<endl;
}
}
return 0;
}

如何实现车次大小 由小到大排序 谢谢 学生党一枚

  • 写回答

1条回答 默认 最新

  • I糖醋鱼I 2017-06-24 08:31
    关注

    你可以可以贴出代码高亮

    这样的话,很少有人看得

    评论

报告相同问题?

悬赏问题

  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改