2015-02-05 65 views
0

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

如果你打的标记,你必须瞄准它一点;每个箭头都会感受到地球的吸引力。 Henry Wadsworth Longfellow

永远不要永不放弃!温斯顿丘吉尔

伟大的作品不是靠实力,而是靠坚韧不拔。 塞缪尔·约翰逊

更多的人通过缺乏目的失败比缺乏人才。比利周日

孩子们从来没有很好听过他们的长辈,但他们 从来没有失败模仿他们。 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); 
} 

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

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

+0

什么华南简介的std ::而不是阵列载体? – icbytes 2015-02-05 21:05:46

+0

为什么不把整个报价整理成一个字符串然后排序?你可以使用'getline()'来输入整行字符串。 http://www.cplusplus.com/reference/string/string/getline/ – NathanOliver 2015-02-05 21:08:11

+0

我不知道什么关于std :: vector。我怎样才能使用getline()从.txt文件输入的引号?我认为那只是输入键盘的字符串。 – 2015-02-05 21:24:35

回答

1

对不起,我没有理会结合的解决方案到您现有的代码,但是这肯定工程:

#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; 
}