2016-10-21 8 views
3
#include <stdio.h> 
#include <string.h> 

int row; 
int col; 
int input; 
int offset; 
char ascii[7][580] = { 
    "  * * * * * * **  **  * *  *          * *** ***** ***** ***** ***** ***** ***** ***** ***** *****    *   *  ** *** *** **** **** **** ***** ***** **** * * ***** ***** * * *  * * * * *** **** *** **** **** ***** * * * * * * * * * * ***** *** *  *** *   *   *    *   **  *      **            *           ** * **  ", 
    "  * * * * * **** ** * * *  * *  * * * * *      * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** *   *  * * * * * * * *  * * *  *  *  * * *  * * * *  ** ** ** * * * * * * * * * *  * * * * * * * * * * *  * *  *  * * *   *  *    *  * *  *       *            *           *  *  *  ", 
    "  * * * ***** * *  * * *  * *   * *** *      * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** *  * * ** * * * * *  * * *  *  *  * * *  * * * *  ** ** ** * * * * * * * * * *  * * * * * * * * * * *  * *  *  * * *   * *** *  ***  * *** *  **** *  *  * * * * **** **** *** **** **** * ** *** ***** * * * * * * * * * * ***** *  *  * * ", 
    "  *   * * *** * *   *   * * ***  *****   * * * ***** ***** ***** ***** ***** ***** ***** ***** *****    *    * ** * * * ***** **** *  * * ***** ***** * *** ***** *  * ** *  * * * * * * * * **** * * **** *** * * * * * * * * *  *  * *  *  *      * **** * * **** * * ***** * * ****    * *  * * * * * * * * * * * * * * *  * * * * * * * * * * *  * *  *  * * * *", 
    "  *   ***** * * * * * *  *   * *** *  **   ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** * ***** * * * * * * * * *  * * *  *  * * * * *  * * * *  * * * ** * * *  * * * * *  * * * * * * ** ** * * * *  *  *  *     **** * * *  * * ***** *  **** * * *  * **  * * * * * * * * **** **** *  *** * * * * * * * * *  *  * *  *  *  * ",   
    "     * * **** * ** * *   *  * * * * *  **   ** * * * ***** ***** ***** ***** ***** ***** ***** ***** ***** ** ** *   *   *  * * * * *  * * *  *  * * * * * * * * * *  * * * ** * * *  * ** * *  * * * * * * ** ** * * * *  *  *  *     * * * * * * * * *  *  * * * *  * **  * * * * * * * * *   * *   * * * * * * * * * * * * *  *  *  *  *  ", 
    "  *   * *  *  ** ** *   *  *     *    *  *** ***** ***** ***** ***** ***** ***** ***** ***** *****   *  *   *  * *** * * **** **** **** ***** *  *** * * ***** ** * * ***** * * * * *** *  **** * * **** * *** * * * * * * ***** ***  * ***  *****  *** *** *** *** *** *  *** * * *** ** * * *** * * * * * *** *  ** *  *** ** **** * * * * * * *  ***** ** * **  ", 

}; 


int main(int argc, char const *argv[]){ 



//for loop for the rows down 
for(row = 0; row < 7; row++){ 
    printf("%c", ascii[row][col]); 
     //columns for each segment of stars or space 
     for(col = 0; col < 6; col++){ 

     }//end of cols 
    }//end of rows 
} 

私は2D配列を使用してASCII値のモックアップを保存しました。文字や記号を入力するために端末を使用すると、スター値で印刷することができます。誰でもこれを行う方法を理解するのを助けることができます。 forループをコメントアウトすると仮定すると、端末で文章を入力する際に​​get charを使用する方法の理解を助ける必要があります

+0

に動作します。 –

+0

'ascii [7] [580]'は[UB]です(http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviours-that-ac-programmer-should-know -a)配列の境界を越えて読み込み中です。私はあなたが 'ascii [行] [col]をしたいと思う。 –

+0

あなたが声を出して試したことの例を追加することで、どこにいらっしゃっているのか理解できます。 –

答えて

2

は、変数chに現在の文字を割り当てます。

putchar(ascii[row][(ch - 32)*6 + col]); 

(ch-32)「フォント」の先頭に表示されます。

(ch-32)*6は、フォント内のすべての前の文字のすべての「星の列」をスキップします。

(ch-32)*7 + colは、現在の文字の現在の「星の列」を選択します。

void print_line(char *str) 
{ 
    char ch, *p; 
    int row, col; 
    for (row = 0; row < 7; row++) 
    { 
     for (p = str; ch = *p; p++) 
     { 
      /* Check that ch is in the printable range of ASCII. */ 
      if (ch < 32 || ch > 95) 
       ch = '?'; /* or continue; */ 
      for (col = 0; col < 6; col++) 
       putchar(ascii[row][(ch - 32)*6 + col]); 
     } 
     putchar('\n'); 
    } 
} 

この関数は、一度に1行しか処理できません。

+0

ありがとう、これは私が探しているものです! –

2

ほとんどの場合、データの構造を変更する必要があります。

各文字は7文字の6文字で定義されているようです(そのうちの1つは常に空白です - つまり空の列)。

big_char asterisk = { '*' , { 
    "  ", 
    "* * *", 
    " * * ", 
    " * ", 
    " * * ", 
    "* * *", 
    "  " 
}}; 

あなたは

for (row = 0; row < 7; ++row) { 
    printf ("%s\n", asterisk.representation[row]); 
} 

することができますして印刷することができます:あなたは、このように単一の文字を表すことができ、今

typedef struct big_char { 
    unsigned char the_char; 
    char *representation[7]; 
} big_char; 

ではなく、このような構造のものを考えてみましょうこれが大きな文字を印刷するための単純な関数の基礎をなす方法を見てください。

asciiテーブルは、これらの構造の配列です。

ユーザーが文字を入力すると、ascii[i].the_charと一致するものを探して、そのエントリを印刷します。

1

構造体に似たようなことをしたい場合は、オフセットを計算することもできます。次のコード例は、オフセットを計算します。それぞれのAsciiシンボルは、0から127の間の対応する整数値を持ち、[space]は32です。したがって、オフセットは6カラムの倍数で、32のアスキーシンボルの整数です([space]はオフセットを持たない)。

編集:適切にコードをフォーマットしてください、私は正しいアスキー番号でそれをテストし、今では

// variable to read in letter 

char input = 0; 
int offset = 0; 

// read in of letter 

printf("Please enter the letter you want: \n"); 
scanf("%c", &input); 

// get offset from letter. [space] is ascii symbol nb. 32 


offset = 6*((int) input - 32); 

// print letter 

for(int row = 0; row < 7; row++){ 

    for(int col = 0; col < 6; col++){ 

     printf("%c", ascii[row][offset+col]); 

    } 

    printf("\n"); 
} 

return 0; 
関連する問題