gpt4 book ai didi

c++ - 将 .txt 文件中的完整句子输入数组

转载 作者:行者123 更新时间:2023-11-28 06:27:00 25 4
gpt4 key购买 nike

我正在编写一个程序,需要对 .txt 文件中的引号进行排序并输出到另一个文件。我试图放入数组中进行排序的 QUOTES.txt 文件有这样的引号

If you would hit the mark, you must aim a little above it; every arrow that flies feels the attraction of earth. Henry Wadsworth Longfellow

Never, never, never give up! Winston Churchill

Great works are performed not by strength but by perseverance. Samuel Johnson

More people fail through lack of purpose than lack of talent. Billy Sunday

Children have never been very good at listening to their elders, but they have never failed to imitate them. James Baldwin

有没有办法在不改变我的模板的情况下对完整的句子进行排序?现在它正在单独输入单词并对单词进行排序,但我希望句子保持完整并且只按句子的第一个字母排序。这是我写的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

template < typename T >
T sorting(T rays [], int size)
{
int minIndx, i;
T temp;

for (int passCount = 0; passCount < size - 1; passCount++)
{
minIndx = passCount;

for (int searchIndx = passCount + 1; searchIndx < size; searchIndx++)
if (rays[searchIndx] < rays[minIndx])
minIndx = searchIndx;

temp = rays[minIndx];
rays[minIndx] = rays[passCount];
rays[passCount] = temp;
}

cout << endl << "Sorted:" << endl;
for (i = 0; i < size; ++i)
cout << rays[i] << endl;

cout << endl;

return (0);
}

int main()
{

ifstream inNumbers("IntFile.txt");
ifstream inFloaters("FloatFile.txt");
ifstream inWords("QUOTES.txt");
ofstream outNumbers("SortedInt.txt");
ofstream outFloaters("SortedFloat.txt");
ofstream outWords("SortedQuotes.txt");

int i, length = 0, lengt = 0, leng = 0;
int data[100];
double data2[100];
string data3[100];

if (!inNumbers)
{
cerr << "IntFile.txt file could not be opened" << endl;
exit(1);
}

if (!inFloaters)
{
cerr << "FloatFile.txt file could not be opened" << endl;
exit(1);
}

if (!inWords)
{
cerr << "QUOTES.txt file could not be opened" << endl;
exit(1);
}

for (i = 0; i < 100 && inNumbers; ++i)
{
inNumbers >> data[i];
if (inNumbers)
{
length += 1;
}
}

sorting(data, length);

for (i = 0; i < 100 && inFloaters; ++i)
{
inFloaters >> data2[i];
if (inFloaters)
{
lengt += 1;
}
}

sorting(data2, lengt);

for (i = 0; i < 100 && inWords; ++i)
{
inWords >> data3[i];
if (inWords)
{
leng += 1;
}
}

sorting(data3, leng);
}

------------编辑----------------

我将 inWords 的输入从 inWords >> data3[i]; 更改为 getline(inWords, data3[i]; 所以现在它一次扫描一行。现在我只需要找出如何对这个新数组进行排序并仍然保持引号完整。

最佳答案

抱歉,我没有费心将解决方案合并到您现有的代码中,但这绝对有效:

#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
#include <vector>
using namespace std;

int main() {
ifstream iFile("QUOTES.txt");
assert(iFile.is_open());

vector<string> quoteLines, quotes;

for (string s; getline(iFile, s);) quoteLines.push_back(s);

iFile.close();

/* deal with multi-line quotes by merging the stuff separated
by empty lines into single strings */
string tmpStr;
for (const auto& s : quoteLines) {
if (s == "") {
/* if we come across an empty line, put the stuff we had so far into the vector
and clear the temporary string */
quotes.push_back(tmpStr);
tmpStr.clear();
} else {
/* if there's already stuff in the temporary string, then append a space to it. */
/* then, append the current line */
tmpStr += ((tmpStr.size() == 0)?"":" ") + s;
}
}

/* sort the quotes */
sort(quotes.begin(), quotes.end());

for (const auto& s : quotes) cout << s << endl << endl;

return 0;
}

关于c++ - 将 .txt 文件中的完整句子输入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28353961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com