How to display character as a string from the displayboard?

不羁的心 提交于 2021-01-29 14:44:30

问题


I am having problems in displaying the character as a string which I have set on my board. How do i display out my character from the board like a string.For example, when I input AABBA , the output will show AABBA as a string using the design i have set on my board for A and B.

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring> 

using namespace std ;

void display(int dimI , int dimJ, const char A[20][40])
{
    for (int i =0 ; i < dimI ; ++i)
    {
        for (int j = 0 ; j < dimJ ; ++j)
        {
            cout << A[i][j];
        }
        cout << endl ;
    }
}
void clearScreen();
void delay() ;

int main()
{

    const int dimI = 20, dimJ = 40;
    string letter ;
    string displayboard(int dimI , int dimJ, const char A[20][40]) ;

    cout << "Please enter a word or number:";   
    getline(cin, letter);


    for(int i=0; i<letter.length(); ++i)
    {

    switch(letter[i])                          
    {

    case 'A' :
    {

    char board_A[dimI][dimJ] =
       {
             {' ' , ' ' , '#' , ' ' , ' ' },
             {' ' , '#' , ' ' , '#' , ' ' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , '#' , '#' , '#' , '#' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , ' ' , ' ' ,' '  , '#' }
        };

    display(dimI,dimJ,board_A);
    break ;
    }

     case 'B'  :
    {

     char board_B[dimI][dimJ] =
       {
             {'#' , '#' , '#' , '#' , ' ' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , '#' , '#' , '#' , ' ' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , ' ' , ' ' , ' ' , '#' },
             {'#' , '#' , '#' ,'#'  , ' ' }
       };
       display(dimI,dimJ,board_B);
       break ;
    }

void display( string displayboard [20][40] ,int dimI , int dimJ )
{

    for (int i =0 ; i< dimI ; ++i)
    {
        for (int j = 0 ; j, dimJ ; ++j)
        {
            cout << displayboard[i][j];
        }

        cout << endl ;
    }
    delay();
    clearScreen();
}

void delay()  
{
    for (int i = 0 ; i < 1000000000 ; ++i) ;
}

void clearScreen() 
{
    system("cls");
}

I expect the output to be AABBA as a string and display it horizontally.When i run my program,it only display it as an individual letter instead of a string whereby the characters are side by side. Other than that, I am also thinking about making a loop so that the characters are able to scroll upwards and downwards,and left to right,making it look like a wrap around effect.


回答1:


Your program tries to print letter-by-letter.
But for the desired giant string output you need to print pixel-line-by-pixel-line.

I.e. do not print "A" then "B".
Instead print

first  line A     first  line B ...
second line A     second line B ...
third  line A     third  line B ... 
fourth line A     fourth line B ... 
print __#__  print ####_ newline
print _#_#_  print #___# newline
print #___#  print #___# newline
print #___#  print ####_ newline
print #####  print #___# newline
print #___#  print #___# newline
print #___#  print ####_ newline

I.e. you need to be aware of the whole string all the time and print parts of each letter in that string on each pixel line and only then print a single newline.




回答2:


as per @Yunnosch sir's answer you have to implement code may like below.

Show here

Your Display mathod:

void display(string letter)
{
  const int dimI = 20, dimJ = 40;
  char board_A[dimI][dimJ] =
                   {
                         {' ' , ' ' , '#' , ' ' , ' ' },
                         {' ' , '#' , ' ' , '#' , ' ' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , '#' , '#' , '#' , '#' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , ' ' , ' ' ,' '  , '#' }
                    };

char board_B[dimI][dimJ] =
                   {
                         {'#' , '#' , '#' , '#' , ' ' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , '#' , '#' , '#' , ' ' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , ' ' , ' ' , ' ' , '#' },
                         {'#' , '#' , '#' ,'#'  , ' ' }
                   };
                  for (int i =0 ; i< dimI ; ++i)
                  {                       
                        for(int k=0; k<letter.length(); ++k)
                        {

                              switch(letter[k])                          
                                {
                                    case 'A' :
                                            { 
                                              cout<<board_A[i];              
                                              break;
                                            }   

                                    case 'B'  :                    
                                            {
                                              cout<<board_B[i]; 
                                              break;
                                            }
                                }                                  


                      }
                      cout<<endl;
                  }

 }


来源:https://stackoverflow.com/questions/58037580/how-to-display-character-as-a-string-from-the-displayboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!