|
题目说明:输入一个字符串,第一个字符串是DESC或者ASEC 表示递减或者递增,后面的数字串由逗号隔开;
输入参数:DESC 2334,123,534
输出参数: 123,534,2334
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool compareASCE(string a, string b)
{
return a < b;
}
bool compareDESC(string a, string b)
{
return a > b;
}
int sortStringByFirstStr()
{
string str;
getline(cin, str);
//申请字符串数组
int douNum = 0;
for (int i = 0; i != str.length(); i++)
{
if (str[i] == ' ' || str[i] == ',' || str[i] == '\t')
{
douNum++;
}
}
string* s = new string[++douNum]; // 由douNum 个就会有DouNum + 1 分段;
//使用空格、逗号、tab符、换行符进行分割
char seps[] = " ,\t\n";
char *token;
//const char* strTemp = str.c_str();
char *cstrTemp = new char[str.length() + 1];
strcpy(cstrTemp, str.c_str()); //字符串转正字符数组,为做strtok的参数;
token = strtok(cstrTemp, seps);// 使用strtok 将字符串使用规定的字符进行分割;
char* pchar = NULL;
int j = 0;
while (token != NULL)
{
/* While there are tokens in "string" */
//printf("%s\n", token);
// pchar = s + j;
// strcpy( pchar, token);
s[j] = token;
j++;
/* Get next token: */
token = strtok(NULL, seps);
}
//排序s + 1 到 s + douNum
if (s[0].find('D') != -1)
{
sort(s + 1, s + douNum, compareDESC);
}
else
{
sort(s + 1, s + douNum, compareASCE);
}
for (int z = 1; z < douNum; z++)
if (z != douNum - 1)
{
cout << s
题目说明:输入一个字符串,第一个字符串是DESC或者ASEC 表示递减或者递增,后面的数字串由逗号隔开;
输入参数:DESC 2334,123,534
输出参数: 123,534,2334
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool compareASCE(string a, string b)
{
re ...查看全文
<< ',';
}
else
cout << s
题目说明:输入一个字符串,第一个字符串是DESC或者ASEC 表示递减或者递增,后面的数字串由逗号隔开;
输入参数:DESC 2334,123,534
输出参数: 123,534,2334
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool compareASCE(string a, string b)
{
re ...查看全文
<< endl;
return 0;
}
int main()
{
sortStringByFirst();
return 0;
}
注意事项:
1、输入的字符串很长,带有空格,所以使用getline进行输入;
2、字符串进行分割使用strtok:token = strtok(cstrTemp, seps);// 使用strtok 将字符串使用规定的字符进行分割;
3、字符串 数组 进行排序sort:sort(s + 1, s + douNum, compareDESC);
|