2016-10-13 8 views
0

だから私は、私が作成した2枚のASCIIの写真を取り、それらを並べて印刷します方法、そのメソッド呼び出しにする必要があります。C++:2つのASCII画像を並べて印刷するには?

画像は文字列としてASCIIの画像を格納するオブジェクトである
concatHz(Picture l, Picture r); 

をフィールドl.resultとr.resultにあります。

rは

+------+ 
|This | 
|is the| 
|String| 
+------+ 

とLであった場合は、次の

This 
is the 
String 

、結果は次のようになります。

This +------+ 
is the|This | 
String|is the| 
     |String| 
     +------+ 

私はこれを行う方法を考えてきたが、彼らがいるようです複雑すぎると簡単な方法があるかもしれません。私はforループを使用して各文字列の行を調べて最初の文字列を印刷し、次に2番目の文字列を印刷することを考えていましたが、上記の例のようにインデックスエラーの問題が発生します。これを行う簡単な方法はありますか?私は考えていませんか?あなたは既に画面の内容を表すために文字列のベクトルを使用している

Picture Picture::create(std::vector<std::string> v){ 
    Picture c; //default constructor called without parenthesis 
    c.picList=v; 
    c.result=""; 
    int max1=0; 
    for(int i=0;i<v.size();i++){ 
     if(v.at(i).length()>max1){ 
      max1=v.at(i).length(); 
     } 
     c.rows++; 
     c.result+=v.at(i)+"\n"; 
    } 
    c.maxLen=max1; 
    return c; 
} 
+1

何を考えているのはそれができるほど単純です。物事を正確に「索引付け」して、最後の行の最初の画像に存在しない行の代わりに空白のセットを印刷するだけです。これはロケット科学ではない。論理的には単純である。 –

+1

'Picture'データ構造がどのように見えるかによって異なります。 –

+0

主なことは、新しい行の\ nを持つasciiの表現としてresultという文字列を格納することです。 –

答えて

1

完全な画像を単一のstd::stringとして生成しないでください。std::vectorを構成する個々のstd::stringの値にアクセスする必要があります。こうすることで、1つのループを実行して、各反復でl.maxLen文字に埋め込まれた次のl文字列を出力し、次にr文字列を出力して改行を出力します。両方の画像が使い尽くされたらループを終了します。例えば

#include <string> 
#include <vector> 

class Picture 
{ 
private: 
    std::vector<std::string> picList; 
    std::size_t maxLen; 
public: 
    Picture(); 
    Picture(const std::vector<std::string> &v); 

    static Picture create(const std::vector<std::string> &v); 

    std::size_t getMaxRowLen() const; 
    std::size_t getRows() const; 
    std::string getRow(std::size_t index) const; 

    // just in case you really need it 
    std::string getResult() const; 
}; 

#include <iostream> 
#include <sstream> 
#include <iomanip> 

Picture Picture::create(const std::vector<std::string> &v) 
{ 
    return Picture(v); 
} 

Picture::Picture() 
    : maxLen(0) 
{ 
} 

Picture::Picture(const std::vector<std::string> &v) 
    : picList(v), maxLen(0) 
{ 
    for(std::vector<std::string>::const_iterator iter = picList.begin(); iter != picList.end(); ++iter) { 
     if (iter->length() > maxLen) { 
      maxLen = iter->length(); 
     } 
    } 
} 

std::size_t Picture::getMaxRowLen() const 
{ 
    return maxLen; 
} 

std::size_t Picture::getRows() const 
{ 
    return picList.size(); 
} 

std::string Picture::getRow(std::size_t index) const 
{ 
    std::string row; 
    if (index < picList.size()) { 
     row = picList[index]; 
    } 
    std::ostringstream oss; 
    oss << std::setw(maxLen) << std::left << std::setfill(' ') << row; 
    return oss.str(); 
} 

std::string Picture::getResult() const 
{ 
    std::ostringstream oss; 
    for(std::vector<std::string>::const_iterator iter = picList.begin(); iter != picList.end(); ++iter) { 
     oss << std::setw(maxLen) << std::left << std::setfill(' ') << *iter << "\n"; 
    } 
    return oss.str(); 
} 

void concatHz(const Picture &l, const Picture &r) 
{ 
    std::size_t rows = std::max(l.getRows(), r.getRows()); 
    for (std::size_t i = 0; i < rows; ++i) { 
     std::cout << l.getRow(i) << r.getRow(i) << "\n"; 
    } 
} 
0

:ここ

は、基礎となるASCIIの絵を作成する方法です。

このようなベクトルに2つのASCIIアート画像を保存して出力するだけです。

また、カーソルの位置を、 ncursesライブラリ。

関連する問題