2015-02-05 3 views
0

.txt 파일에서 따옴표를 정렬하고 다른 파일로 출력해야하는 프로그램을 작성하고 있습니다. 정렬 할 배열에 넣으려고하는 QUOTES.txt 파일에 이와 같은 따옴표가 있습니다..txt 파일의 전체 문장을 배열로 입력하십시오.

마크를 누르는 경우 약간 위에 올려야합니다. 파리의 모든 화살표는 지구의 매력을 느낍니다. Henry Wadsworth Longfellow

절대 절대로 절대로 포기하지 마십시오! 윈스턴 처칠

위대한 작품은 힘에 의해서가 아니라 인내로 수행됩니다. 사무엘 존슨

많은 사람들이 재능 부족보다 목적이 부족합니다. 빌리 일요일

아이들은 결코 장로들에게 귀를 기울이지 않았지만, 은 결코 그들을 모방하지 않았습니다. James Baldwin

템플릿을 변경하지 않고 전체 문장을 정렬 할 수있는 방법이 있습니까? 지금은 단어를 개별적으로 입력하고 단어를 정렬하지만 문장을 그대로 유지하고 문장의 첫 번째 문자로만 정렬합니다. 여기에 내가 작성한 코드는 다음과 같습니다 ---------- 편집 ---------------

I가 입력을 변경

#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];까지 이제는 한 번에 한 줄씩 스캔합니다. 이제이 새로운 배열을 정렬하고 따옴표를 그대로 유지하는 방법을 알아야합니다.

+0

어떤 표준 abour : 벡터 대신 배열을? – icbytes

+0

전체 견적을 한 문자열로 가져 와서 정렬하지 않으시겠습니까? 전체 문자열 입력 행에 대해'getline()'을 사용할 수 있습니다. http://www.cplusplus.com/reference/string/string/getline/ – NathanOliver

+0

std :: vector에 대해 아무것도 몰라요. .txt 파일에서 따옴표를 입력 할 때 어떻게 getline()을 사용할 수 있습니까? 나는 그것이 키보드에 입력 된 문자열에만 있다고 생각했다. –

답변

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; 
} 
관련 문제