:)
我有问题。 我的程序正在从words.txt
文件中读取文本,数据用逗号分隔。 这就是为什么我决定删除逗号后面的所有内容,因为我只想要文本
例如,我的文件words.txt
如下所示:
phrase,jewish gematria,english gematria,simple gematria,search num
elvis presly,1479,972,162,52
the young ones,995,1008,168,6
hassanyahu talks about psychopathic toys on austins birfday,3967,3948,658,0
anu to arrive by jewish gematria word jewish gematria english is wine he is unforgivable because i dont have to do they trembled and kissed his mission markbot,11993,8988,1498,82
isis is propaganda,601,1062,177,0
so are you to my thoughts as food to life or as sweet seasond showers are to the ground,5964,5502,917,88
正如您在第一行看到的短语,犹太gematria,简单gematria,search num
是search num
,这是我想用它来排序结果的索引。 最后一个逗号后面的最后一个数字是我想用来对所有结果进行排序的索引。
问题是我怎么才能做到这一点?
这是我的源码
#include <iostream>
#include <string>
#include <map>
#include <cmath>
#include <fstream>
#include <sstream>
#include <numeric>
#include <limits>
using namespace std;
void print( string::size_type n, string const & s )
{
if( n == string::npos ) {
cout << "not found\n";
} else {
cout << "found: " << s.substr( n ) << '\n';
}
}
map < char, int > make_pythagorean_map()
{
return {
{ 'A', 1 },
{ 'a', 1 },
{ 'B', 2 },
{ 'b', 2 },
{ 'C', 3 },
{ 'c', 3 },
{ 'D', 4 },
{ 'd', 4 },
{ 'E', 5 },
{ 'e', 5 },
{ 'F', 6 },
{ 'f', 6 },
{ 'G', 7 },
{ 'g', 7 },
{ 'H', 8 },
{ 'h', 8 },
{ 'I', 9 },
{ 'i', 9 },
{ 'J', 1 },
{ 'j', 1 },
{ 'K', 2 },
{ 'k', 2 },
{ 'L', 3 },
{ 'l', 3 },
{ 'M', 4 },
{ 'm', 4 },
{ 'N', 5 },
{ 'n', 5 },
{ 'O', 6 },
{ 'o', 6 },
{ 'P', 7 },
{ 'p', 7 },
{ 'Q', 8 },
{ 'q', 8 },
{ 'R', 9 },
{ 'r', 9 },
{ 'S', 1 },
{ 's', 1 },
{ 'T', 2 },
{ 't', 2 },
{ 'U', 3 },
{ 'u', 3 },
{ 'V', 4 },
{ 'v', 4 },
{ 'W', 5 },
{ 'w', 5 },
{ 'X', 6 },
{ 'x', 6 },
{ 'Y', 7 },
{ 'y', 7 },
{ 'Z', 8 },
{ 'z', 8 }
};
}
map < char, int > make_simpleeng_map()
{
return {
{ 'A', 1 },
{ 'a', 1 },
{ 'B', 2 },
{ 'b', 2 },
{ 'C', 3 },
{ 'c', 3 },
{ 'D', 4 },
{ 'd', 4 },
{ 'E', 5 },
{ 'e', 5 },
{ 'F', 6 },
{ 'f', 6 },
{ 'G', 7 },
{ 'g', 7 },
{ 'H', 8 },
{ 'h', 8 },
{ 'I', 9 },
{ 'i', 9 },
{ 'J', 10 },
{ 'j', 10 },
{ 'K', 11 },
{ 'k', 11 },
{ 'L', 12 },
{ 'l', 12 },
{ 'M', 13 },
{ 'm', 13 },
{ 'N', 14 },
{ 'n', 14 },
{ 'O', 15 },
{ 'o', 15 },
{ 'P', 16 },
{ 'p', 16 },
{ 'Q', 17 },
{ 'q', 17 },
{ 'R', 18 },
{ 'r', 18 },
{ 'S', 19 },
{ 's', 19 },
{ 'T', 20 },
{ 't', 20 },
{ 'U', 21 },
{ 'u', 21 },
{ 'V', 22 },
{ 'v', 22 },
{ 'W', 23 },
{ 'w', 23 },
{ 'X', 24 },
{ 'x', 24 },
{ 'Y', 25 },
{ 'y', 25 },
{ 'Z', 26 },
{ 'z', 26 }
};
}
map < char, int > make_gfive_map()
{
return {
{ 'A', 7 },
{ 'a', 7 },
{ 'B', 8 },
{ 'b', 8 },
{ 'C', 1 },
{ 'c', 1 },
{ 'D', 2 },
{ 'd', 2 },
{ 'E', 3 },
{ 'e', 3 },
{ 'F', 4 },
{ 'f', 4 },
{ 'G', 5 },
{ 'g', 5 },
{ 'H', 6 },
{ 'h', 6 },
{ 'I', 7 },
{ 'i', 7 },
{ 'J', 8 },
{ 'j', 8 },
{ 'K', 9 },
{ 'k', 9 },
{ 'L', 1 },
{ 'l', 1 },
{ 'M', 2 },
{ 'm', 2 },
{ 'N', 3 },
{ 'n', 3 },
{ 'O', 4 },
{ 'o', 4 },
{ 'P', 5 },
{ 'p', 5 },
{ 'Q', 6 },
{ 'q', 6 },
{ 'R', 7 },
{ 'r', 7 },
{ 'S', 8 },
{ 's', 8 },
{ 'T', 9 },
{ 't', 9 },
{ 'U', 1 },
{ 'u', 1 },
{ 'V', 2 },
{ 'v', 2 },
{ 'W', 3 },
{ 'w', 3 },
{ 'X', 4 },
{ 'x', 4 },
{ 'Y', 5 },
{ 'y', 5 },
{ 'Z', 6 },
{ 'z', 6 }
};
}
map < char, int > select_map( int choice )
{
switch( choice )
{
case 1:
{
return make_pythagorean_map();
}
case 2:
{
return make_simpleeng_map();
}
case 3:
{
return make_gfive_map();
}
}
}
int main()
{
int cho = 0, d;
char o;
string phrasetwo;
while( 1 )
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
cin >> phrasetwo;
istringstream asd( phrasetwo );
if( !( asd >> d ) || asd >> o )
{
system( "cls" );
cout << "You want to use: " << endl << "1) Pythagorean Gematria" << endl << "2) English/Simple Gematria" << endl << "3) Extra: " << endl << "Gematria with G = 7" << endl << endl;
}
else
{
cho = stoi( phrasetwo );
if( cho <= 3 && cho >= 1 )
{
break;
}
}
}
auto m = select_map( cho );
string::size_type n;
ifstream input( "words.txt" );
ofstream output( "results.txt" );
string str, phrase;
int counter = 1, cnt = 0, choice = 0, chosennum = 0, asd = 0, f = 0, x;
bool flag;
char c;
while( 1 )
{
system( "cls" );
cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
cin >> phrase;
istringstream s( phrase );
if( !( s >> x ) || s >> c )
{
system( "cls" );
cout << "You want to check:" << '\n' << "1) Number" << '\n' << "2) Phrase" << endl;
}
else
{
choice = stoi( phrase );
if( choice <= 2 && choice >= 1 )
{
break;
}
}
}
switch( choice )
{
case 1:
{
cout << "Which number do you want to check?: ";
while( 1 )
{
cin >> phrase;
istringstream s( phrase );
if( !( s >> x ) || s >> c )
{
cout << endl << "Which number do you want to check?: ";
}
else
{
chosennum = stoi( phrase );
break;
}
}
cout << endl << "All words found: " << endl;
break;
}
case 2:
{
do
{
flag = false;
cout << "Which phrase do you want to check?: ";
cin.ignore( numeric_limits < streamsize >::max(), '\n' );
getline( cin, phrase );
for( int i = 0; i < phrase.length(); i++ )
{
if( isdigit( phrase[ i ] ) )
{
flag = true;
}
}
cout << endl;
} while( flag == true );
for( char charr: phrase )
{
chosennum += m[ charr ];
}
cout << '\t' << phrase << " in gematria = " << chosennum << endl << endl << "All words found: " << endl;
break;
}
}
output << "All words found: " << endl;
while( getline( input, str ) )
{
int sum = 0;
n = str.find( ',' );
if( n != string::npos )
{
str.resize( n );
}
for(int i = 0; i < str.length(); i++)
{
if( ( ( int )str[ i ] ) > 127 || ( ( int )str[ i ] ) < 0 )
{
str.clear();
}
}
for( char charr: str )
{
sum += m[ charr ];
}
if( sum == chosennum )
{
cout << "No[" << counter << "]: " << str << "(" << sum << ")" << '\n';
output << "No[" << counter << "]: " << '\t' << str << " = " << sum << endl;
counter++;
}
}
cout << "\n\n You have found: " << counter << " words" << '\n';
output << "\n\n You have found: " << counter << " words" << '\n';
return 0;
}
因为这里我跳过所有逗号以获得正确的结果
while( getline( input, str ) )
{
int sum = 0;
n = str.find( ',' );
if( n != string::npos )
{
str.resize( n );
}
for(int i = 0; i < str.length(); i++)
{
if( ( ( int )str[ i ] ) > 127 || ( ( int )str[ i ] ) < 0 )
{
str.clear();
}
}
for( char charr: str )
{
sum += m[ charr ];
}
if( sum == chosennum )
{
cout << "No[" << counter << "]: " << str << "(" << sum << ")" << '\n';
output << "No[" << counter << "]: " << '\t' << str << " = " << sum << endl;
counter++;
}
}
寻找快速答案。 非常感谢你的帮助。
您可以修改顶部的答案:在C++中使用内置函数(或任何其他方法)排序2D数组?
或为每一行创建结构,并定义运算符<; 并对这些结构的向量调用std::sort。
你可以通过以下方法来解决你的问题
下面是一个例子:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cout;
using std::istream;
using std::ostream;
using std::stoi;
using std::string;
using std::stringstream;
using std::vector;
namespace {
char const* input_txt =
R"ing(phrase,jewish gematria,english gematria,simple gematria,search num
elvis presly,1479,972,162,52
the young ones,995,1008,168,6
hassanyahu talks about psychopathic toys on austins birfday,3967,3948,658,0
anu to arrive by jewish gematria word jewish gematria english is wine he is unforgivable because i dont have to do they trembled and kissed his mission markbot,11993,8988,1498,82
isis is propaganda,601,1062,177,0
so are you to my thoughts as food to life or as sweet seasond showers are to the ground,5964,5502,917,88)ing";
auto parse_into_lines(istream&) -> vector<string>;
auto parse_into_fields(vector<string> const&) -> vector<vector<string>>;
auto sort_by_last(vector<vector<string>>) -> vector<vector<string>>;
auto operator<<(ostream&, vector<string> const&) -> ostream&;
auto operator<<(ostream&, vector<vector<string>> const&) -> ostream&;
} // anon
int main() {
auto ss = stringstream(input_txt);
auto lines = parse_into_lines(ss);
auto fields = parse_into_fields(lines);
if (fields.size() == 0) throw "not enough data";
auto headers = fields[0];
fields.erase(fields.begin());
auto sorted = sort_by_last(fields);
cout << headers << "," << sorted;
}
namespace {
auto parse_into_lines(istream& in) -> vector<string> {
auto result = vector<string>();
auto s = string();
while (getline(in, s)) {
if (!s.empty()) {
result.push_back(s);
}
}
return result;
}
auto parse_into_fields(vector<string> const& v) -> vector<vector<string>> {
auto result = vector<vector<string>>();
auto field = string();
for (auto const& s : v) {
auto ss = stringstream(s);
auto fields = vector<string>();
while(getline(ss, field, ',')) {
fields.push_back(field);
}
result.push_back(fields);
}
return result;
}
auto sort_by_last(vector<vector<string>> data) -> vector<vector<string>> {
auto pred = [](vector<string> const& a, vector<string> const& b) {
if (a.empty()) throw "not enough data";
if (b.empty()) throw "not enough data";
return stoi(a.back()) < stoi(b.back());
};
sort(data.begin(), data.end(), pred);
return data;
}
auto operator<<(ostream& out, vector<string> const& v) -> ostream& {
char const* sep = "";
for (auto const& s : v) {
out << sep << s;
sep = ",";
}
return out;
}
auto operator<<(ostream& out, vector<vector<string>> const& v) -> ostream& {
for (auto const& row : v) {
out << row << "\n";
}
return out;
}
} // anon
更新
如果标题行的字段searchnum
可以位于任何位置(或者可以通过某种方式选择),那么您将需要一个额外的步骤来查找标题行中该字段的索引。
如果可以指定标题行的标题,而不是硬编码的searchnum
,则必须使用它来查找排序列的索引。
我冒昧地使用stoi
将final字段转换为整数,但根据升序或降序以及列是否为数字,这样做并不总是正确的。