求C++高手解答!!!急求!!!!

输入一批字符串(不超过30条).以字符串“end”作为输入结束标志,请按要求编程完成下述功能:1).字符串输入;2).输入最大、最小字符串排序;3).对字符串进行升序排序... 输入一批字符串(不超过30条).以字符串“end”作为输入结束标志,请按要求编程完成下述功能:
1).字符串输入;
2).输入最大、最小字符串排序;
3).对字符串进行升序排序;
4).插入一条字符串,结果仍按升序排序;
5).查找指定字符串的位置;
6).删除指定的字符串。
要求:
1).上述各功能请分别用函数实现;
2).输入输出要有必要的提示说明。
展开
 我来答
moxsone
2010-07-06 · TA获得超过3334个赞
知道大有可为答主
回答量:2796
采纳率:50%
帮助的人:1631万
展开全部
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

//获取输入字符串
void GetInput(vector<string> *InOutArr)
{
string TmpStr("");

while ( true )
{
cout<<"输入一个字符串,如果要结束,请输入end =";
cin>>TmpStr;
if ( strcmp(TmpStr.c_str(),"end") == 0 )
{
return;
}

InOutArr->push_back(TmpStr);
}
}

//字符排序比较函数
bool compStr(string &s1,string &s2)
{
if ( strcmp(s1.c_str(),s2.c_str()) < 0 )
{
return true;
}
return false;
}

//输出数据
void OutPutData(vector<string> *pInData)
{
for ( unsigned long i = 0 ; i < pInData->size() ; i ++ )
{
cout<<pInData->at(i)<<endl;
}
}

//按序插入
void InsertSort(vector<string> *pInData)
{
string WantInsert("");
cout<<"输入要插入的字符串 =";
cin>>WantInsert;
for ( unsigned long i = 0 ; i < pInData->size() ; i++ )
{
if ( ! compStr(pInData->at(i), WantInsert) )
{
vector<string>::iterator Pos = pInData->begin() + i;
pInData->insert(Pos,WantInsert);
break;
}
}
}

//查找
void FindWhat(vector<string> *pInData)
{
string FindWant("");
bool HadFind = false;
cout<<"输入要查找的字符串 =";
cin>>FindWant;
for ( unsigned long i = 0 ; i < pInData->size() ; i++ )
{
if ( strcmp(pInData->at(i).c_str(), FindWant.c_str()) == 0 )
{
cout<<"找到:"<<FindWant<<"在"<<i+1<<"位置"<<endl;
HadFind = true;
break;
}
}
if ( ! HadFind )
{
cout<<"未找到:"<<FindWant<<endl;
}
}

//删除
void DeleteWant(vector<string> *pInData)
{
string DeleteWant("");
bool HadFind = false;
cout<<"输入要删除的字符串 =";
cin>>DeleteWant;
for ( unsigned long i = 0 ; i < pInData->size() ; i++ )
{
if ( strcmp(pInData->at(i).c_str(), DeleteWant.c_str()) == 0 )
{
vector<string>::iterator Pos = pInData->begin() + i;
pInData->erase(Pos);
cout<<"已删除:"<<DeleteWant<<"在"<<i+1<<"位置"<<endl;
HadFind = true;
break;
}
}
if ( ! HadFind )
{
cout<<"未找到:"<<DeleteWant<<endl;
}
}

void main(void)
{
vector<string> InputedStrs;

//获取输入
GetInput(&InputedStrs);
//排序
sort(InputedStrs.begin(),InputedStrs.end(),compStr);
OutPutData(&InputedStrs);

//按序插入
InsertSort(&InputedStrs);
OutPutData(&InputedStrs);

//查找
FindWhat(&InputedStrs);
OutPutData(&InputedStrs);

//删除
DeleteWant(&InputedStrs);
OutPutData(&InputedStrs);

int puase;
cin>>puase;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式