输入回车结束输入 即最后一行什么都不输只敲回车
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <sstream>
#include <numeric>
using namespace std;
class InputString
{
int key;
vector<int> data;
string tag;
void analyse(const string& s);
public:
InputString(const string& s) : key(0) { analyse(s); }
friend ostream& operator<<(ostream& os, const InputString& i)
{
os << '{' << i.key << '}';
copy(i.data.begin(), i.data.end(),
ostream_iterator<int>(os, " "));
return os;
}
friend bool operator<(const InputString& left,
const InputString& right)
{
return left.key > right.key;
}
};
void InputString::analyse(const string& s)
{
size_t i = s.find(']');
tag = s.substr(1, i - 1);
string number(s.begin() + i + 1, s.end());
stringstream ss;
ss << number;
while(ss) {
int a;
ss >> a;
data.push_back(a);
}
data.pop_back();
sort(data.begin(), data.end());
if(tag == "Max") key = data.back();
else if(tag == "Min") key = data.front();
else if(tag == "Median") key = (data.front() + data.back()) / 2;
else key = accumulate(data.begin(), data.end(), key) / data.size();
}
int main()
{
string input;
vector<InputString> vis;
while(1) {
getline(cin, input);
if(input.size() < 1) break;
vis.push_back(input);
}
sort(vis.begin(), vis.end());
copy(vis.begin(), vis.end(),
ostream_iterator<InputString>(cout, "\n"));
system("PAUSE");
}
[此贴子已经被作者于2006-12-12 11:02:49编辑过]