2017-12-16 14 views
-9
string str; 

    str = "  General Knowledge Test  "; 

    cout << string(39, ' ') << string (37 , '-') << "\n"; 

    cout << string(39, ' ') << "|" << str<< "|\n "; 

    cout << string(38, ' ') << string (37 , '-') << "\n"; 

    cout << string(39, ' ') << "|"<< string (35 , ' ') << "|"<< "\n"; 

    cout << string(39, ' ') << "|" << " > Pentru a continua apasa 'k'  |\n "; 

    cout << string(38, ' ') << "|" << " > Pentru ajutor apasa 'a'   |\n "; 

    cout << string(38, ' ') << "|" << " > Pentru raspunsuri apasa 'r'  |\n "; 

    cout << string(38, ' ') << "|" << " > Pentru high score apasa 'h'  |\n "; 

    cout << string(38, ' ') << "|" << " > Pentru a iesi apasa 'q'   |\n "; 

    cout << string(38, ' ') << "|"<< string (35 , ' ') << "|"<< "\n"; 

    cout << string(39, ' ') << string (37 , '-') << "\n\n"; 

답변

1

당신은 당신의 정적 출력의 가독성과 유지 보수성 향상을 문자 그대로의 원시 문자열을 사용할 수 있습니다

#include <iostream> 
#include <string> 


int main(){ 
    std::string str = 
R"(          ------------------------------------- 
             |  General Knowledge Test  | 
             ------------------------------------- 
             |         | 
             | > Pentru a continua apasa 'k'  | 
             | > Pentru ajutor apasa 'a'   | 
             | > Pentru raspunsuri apasa 'r'  | 
             | > Pentru high score apasa 'h'  | 
             | > Pentru a iesi apasa 'q'   | 
             |         | 
             -------------------------------------)"; 
    std::cout << str; 
} 

live demo를 참조하십시오.


은 물론 당신은 또한 std::vector<std::string>을 사용할 수 있습니다 (하지만 반드시 최적화 아니다) :

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <vector> 

int main(){ 
    std::vector<std::string> strs { 
"-------------------------------------", 
"|  General Knowledge Test  |", 
"-------------------------------------", 
"|         |", 
"| > Pentru a continua apasa 'k'  |", 
"| > Pentru ajutor apasa 'a'   |", 
"| > Pentru raspunsuri apasa 'r'  |", 
"| > Pentru high score apasa 'h'  |", 
"| > Pentru a iesi apasa 'q'   |", 
"|         |", 
"-------------------------------------" 
}; 
    for(auto str : strs) { 
     std::cout << std::setw(70) << std::right << str << '\n'; 
    } 
} 

여기 다시 live demo입니다.

+0

고맙습니다. –

+0

@ MădeanDenis 질문의 질을 높이고 부정적인 표결을 없애려면 질문에 산문 텍스트를 추가하여 코드에서 _optimized_를 정확히 나타내시겠습니까? 나는 그것이 주로 가독성에 관한 것임을 짐작해야했다. – user0042