ASCII艺术(数字)输出C ++

时间:2017-10-05 09:18:36

标签: c++

我正在研究我的计划。该程序从用户获取整数输入,然后我的程序将打印输入的ASCII艺术。

示例:

Input : 123 Output : # ### ### # # # # ### ## # # # # ### ###

问题是如何打印彼此相邻的号码?我的程序转换了它,但输出就像这样OutputBroken

而不是:# ### ### # # # # ### ## # # # # ### ###

这是我的代码:

#include <iostream>

using namespace std;

int main(){
int angka,dpn,blk,tng;
string abel,ateng,adep;

cout<<"Masukkan Angka : ";
cin>>angka;

blk = angka % 10;
tng = angka / 10 % 10;
dpn = angka / 100;

switch(blk){
    case 0 : abel = "### \n# # \n# # \n# # \n### "; break;
    case 1 : abel = "# \n# \n# \n# \n# "; break;
    case 2 : abel = "### \n  # \n###  \n#   \n### "; break;
    case 3 : abel = "### \n  # \n ## \n  # \n### "; break;
    case 4 : abel = "# # \n# # \n### \n  # \n  # "; break;
    case 5 : abel = "### \n#   \n### \n  # \n### "; break;
    case 6 : abel = "### \n#   \n### \n# # \n### "; break;
    case 7 : abel = "### \n  # \n  # \n  # \n  # "; break;
    case 8 : abel = "### \n# # \n### \n# # \n### "; break;
    case 9 : abel = "### \n# # \n### \n  # \n### "; break;

}
switch(tng){
    case 1 : ateng = "# \n# \n# \n# \n# "; break;
    case 2 : ateng = "### \n  # \n###  \n#   \n### "; break;
    case 3 : ateng = "### \n  # \n ## \n  # \n### "; break;
    case 4 : ateng = "# # \n# # \n### \n  # \n  # "; break;
    case 5 : ateng = "### \n#   \n### \n  # \n### "; break;
    case 6 : ateng = "### \n#   \n### \n# # \n### "; break;
    case 7 : ateng = "### \n  # \n  # \n  # \n  # "; break;
    case 8 : ateng = "### \n# # \n### \n# # \n### "; break;
    case 9 : ateng = "### \n# # \n### \n  # \n### "; break;

}

switch(dpn){
    case 1 : adep = "# \n# \n# \n# \n# \n"; break;
    case 2 : adep = "### \n  # \n###  \n#   \n### "; break;
    case 3 : adep = "### \n  # \n ## \n  # \n### "; break;
    case 4 : adep = "# # \n# # \n### \n  # \n  # "; break;
    case 5 : adep = "### \n#   \n### \n  # \n### "; break;
    case 6 : adep = "### \n#   \n### \n# # \n### "; break;
    case 7 : adep = "### \n  # \n  # \n  # \n  # "; break;
    case 8 : adep = "### \n# # \n### \n# # \n### "; break;
    case 9 : adep = "### \n# # \n### \n  # \n### "; break;

}


//cout<<dpn<<endl<<blk<<endl<<tng; /*DebugNumber*/

cout<<adep<<ateng<<abel;
return 0;
}

2 个答案:

答案 0 :(得分:1)

它不是关于C ++,而是关于算法。例如,您可以将输出存储在以下结构中:

using Output = std::vector<std::vector<bool>>;

并编写一个在其中添加字符的函数:

void addCharacter(Output o, size_t offset, char theChar) {
  for (int i=0; i<allLines; i++)
    addLineOfChar(o, offset, theChar, i);
}

void addLineOfChar(Output o, size_t offset, char theChar, int line) {
  for (int i=0; i<charsInLine; i++)
    o[offset+i] = data[theChar][line][i];
}

结束打印true#false(空格)。但这只是一个想法。发明最好的算法是你的工作。

答案 1 :(得分:-1)

#include <iostream>
#include<string> 
#include<stdio.h>

using namespace std;

int main() {

int*arr;                    //'arr' is number of digits
int count=0;                //'count' is 'array size
int angka;                  // input integer

string a1, a2, a3, a4, a5;  //store ascii line by line

cout << "Masukkan Angka : ";
cin >> angka;

int temp = angka;
while (temp != 0) {
    temp /= 10;
    count++;
}

arr = new int[count];

for (int i = count-1; i >= 0; i--) {
    arr[i] = angka % 10;
    angka /= 10;
}

for(int i=0;i<count;i++)
{
    switch (arr[i]) {

    case 1: {
        a1 += "#   ";
        a2 += "#   ";
        a3 += "#   ";
        a4 += "#   ";
        a5 += "#   ";
        break; }
    case 2: {
        a1 += "###   ";
        a2 += "  #   ";
        a3 += "###   ";
        a4 += "#     ";
        a5 += "###   ";
        break; }
    case 3: {
        a1 += "###   ";
        a2 += "  #   ";
        a3 += "###   ";
        a4 += "  #   ";
        a5 += "###   ";
        break; }
    case 4: {
        a1 += "# #   ";
        a2 += "# #   ";
        a3 += "###   ";
        a4 += "  #   ";
        a5 += "  #   ";
        break; }
    case 5: {
        a1 += "###   ";
        a2 += "#     ";
        a3 += "###   ";
        a4 += "  #   ";
        a5 += "###   ";
        break; }
    case 6: {
        a1 += "###   ";
        a2 += "#     ";
        a3 += "###   ";
        a4 += "# #   ";
        a5 += "###   ";
        break; }
    case 7: {
        a1 += "###   ";
        a2 += "# #   ";
        a3 += "  #   ";
        a4 += "  #   ";
        a5 += "  #   ";
        break; }
    case 8: {
        a1 += "###   ";
        a2 += "# #   ";
        a3 += "###   ";
        a4 += "# #   ";
        a5 += "###   ";
        break; }
    case 9: {
        a1 += "###   ";
        a2 += "# #   ";
        a3 += "###   ";
        a4 += "  #   ";
        a5 += "###   ";
        break; }

    }
}

cout << a1 <<endl<< a2 << endl << a3 << endl << a4 << endl << a5 << endl;

return 0;
}

我的英语技能是初学者。请理解我的写。 这是您的样式代码。 c ++正在逐行编写。 输入是123,

1行=#### ###

2行=###

3行=#### ###

4行=###

5行=#### ###

并打印(1行\ n)+ --- +(5行\ n)。

此代码是初学者级别。我认为您的编码技巧。 希望对您有所帮助。