2017-04-13 76 views
-2

初學者這裏C++簡單的字符串程序

我寫的以下C++,它是目前需要2個字作爲輸入一個簡短的程序,並輸出相同的話背面但字被分成偶數和奇數代替。我希望能夠用'T'這個詞來代替它,但我無法弄清楚。我希望能夠首先輸入隨後的單詞數量,例如10.然後輸入單詞並獲得T結果。所以,而不是隻有2個字,與用戶指定的數量無限。

我需要把下面的函數放到某個函數中去,但是我想學習最好的技術來做 - 有什麼建議嗎?

謝謝! 亞歷

#include <cmath> 
#include <cstdio> 
#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <string> 
using namespace std; 


int main() { 
    int T; 
    cin >> T; 

    string FirstWord; 
    cin >> FirstWord; 
    int LengthFirst; 
    LengthFirst = FirstWord.length();   
    string EvenFirst; 
    string OddFirst; 
    for (int i = 0; i < LengthFirst; i += 2){ 
     EvenFirst = EvenFirst + FirstWord[i]; 
    } 
    for (int i = 1; i < LengthFirst; i += 2){ 
     OddFirst = OddFirst + FirstWord[i]; 
    } 
    string SecondWord; 
    cin >> SecondWord; 
    int LengthSecond; 
    LengthSecond = SecondWord.length(); 
    string EvenSecond; 
    string OddSecond; 
    for (int i = 0; i < LengthSecond; i += 2){ 
     EvenSecond += SecondWord[i]; 
    } 
    for (int i = 1; i < LengthSecond; i += 2){ 
     OddSecond += SecondWord[i]; 
    } 

    cout << EvenFirst << " " << OddFirst << endl; 
    cout << EvenSecond << " " << OddSecond << endl; 

    return 0; 
} 
+0

你是什麼意思分裂偶奇?你打印所有的偶數索引字[0,2,4,...]然後所有的奇數索引字[1,3,5,...]?] – willkill07

+0

打印所有偶數索引字母,然後打印所有奇數索引字母 例如輸入爲 堆棧 溢出 輸出將是 sak tc oefo vrlw –

+0

請閱讀並存儲一堆可以使用矢量的單詞。看看這個問題的答案:http://www.cplusplus.com/forum/beginner/14792/ – Sebastian

回答

0

想我在這裏,我過想這一個

我把它放在一個for循環,如下 - 因此,任何數量的單詞可以輸入,用戶必須輸入測試用例在

#include <cmath> 
 
#include <cstdio> 
 
#include <vector> 
 
#include <iostream> 
 
#include <algorithm> 
 
#include <string> 
 
using namespace std; 
 

 

 
int main() { 
 
    int T; 
 
    cin >> T; 
 
    
 
    for (int i = 0; i < T; i++){ 
 
     string FirstWord; 
 
     cin >> FirstWord; 
 
     int LengthFirst; 
 
     LengthFirst = FirstWord.length();   
 
     string EvenFirst; 
 
     string OddFirst; 
 
     for (int i = 0; i < LengthFirst; i += 2){ 
 
      EvenFirst = EvenFirst + FirstWord[i]; 
 
     } 
 
     for (int i = 1; i < LengthFirst; i += 2){ 
 
      OddFirst = OddFirst + FirstWord[i]; 
 
     } 
 
     cout << EvenFirst << " " << OddFirst << endl; 
 
    }  
 
    return 0; 
 
}

+0

這是真的,但你也沒有存儲這些詞,這似乎是你原來建立的要求的一部分:「然後輸入單詞並獲得T結果。」 – willkill07

+0

它是完美的回答你自己的問題。然而,由於問題不明確,因此您的答案解決什麼問題也不是很清楚。我會建議至少增加一個輸入和輸出的例子以及期望的輸入和輸出到問題中 – user463035818

0

最終數目,您正在執行相同的任務N次。

首先,讓我們討論如何存儲信息。在功能上,我們有一個字符串作爲輸入,產生兩個字符串作爲輸出。 std::pair(來自<utility>)可以讓我們輕鬆地表達這一點。但爲了偶數,std::array可能是我們更好的代表。由於我們有可變數量的字作爲輸入,因此將輸出可變數量的std::arraystd::vector(來自<vector>)是我們這裏的朋友。

其次,讓我們討論如何處理信息。對每個輸出組件使用命名變量不會縮放,因此讓我們切換到一個固定的數組(下面標記爲array<string,2>)。通過切換到固定數組輸出,尋址每個分區成爲循環索引的函數(index % 2)。解決方案,在編譯時推廣一個已知的分割大小

#include <string> 
#include <array> 
#include <vector> 
#include <iostream> 

int main() { 
    int N; 
    std::cin >> N; 

    constexpr const int Split = 2; 
    using StringPack = std::array<std::string, Split>; 
    std::vector<StringPack> output; 
    for (int wordIndex = 0; wordIndex < N; ++wordIndex) { 
    std::string word; 
    std::cin >> word; 
    StringPack out; 
    { 
     int index = 0; 
     for (char c : word) { 
     out[index % Split] += c; 
     ++index; 
     } 
    } 
    output.emplace_back(out); 
    } 
    for (const auto & out : output) { 
    for (const auto & word : out) { 
     std::cout << word << ' '; 
    } 
    std::cout << '\n'; 
    } 
}