首頁 > 後端開發 > C++ > C程式接收一個數字並以大字印出來

C程式接收一個數字並以大字印出來

PHPz
發布: 2023-09-01 12:01:04
轉載
707 人瀏覽過

給定一個字串形式的數字n;任務是使用雜湊符號列印後面的大數字。

就像我們提供了數字「1234」

下面的數字的表示應該是-

C程式接收一個數字並以大字印出來

同樣,我們想要我們的解決方案要列印-

範例

Input: n[] = {“2234”}
Output:
登入後複製

C程式接收一個數字並以大字印出來

Input: n[] = {“987”}
Output:
登入後複製

C程式接收一個數字並以大字印出來

接近我們將用於解決給定的問題 -

  • 在字串中輸入最多4 位數字。
  • 製作陣列每個數字一一對應我們想要的數字大模式。
  • 遍歷字串並一一列印每個數字。

演算法

Start
   Step 1 -> Define Height as 7
   Step 2 -> Define W 8
   Step 3 -> In function int large(char num[])
      Declare variables i, j, k
      Set char zero[H][W]={" ##### ", // H=0
         " # # ",
         " # # ",
         " # # ",
         " # # ",
         " # # ",
         " ##### "},
      Set one[H][W]={" # ",
         " # ",
         " # ",
         " # ",
         " # ",
         " # ",
         " # "},
      Set two[H][W]={ " ##### ",
         " # ",
         " # ",
         " ##### ",
         " # ",
         " # ",
         " ##### "},
      Set three[H][W]={" ##### ",
         " # ",
         " # ",
         " ##### ",
         " # ",
         " # ",
         " ##### "},
      Set four[H][W]={" # ",
         " # # ",
         " # # ",
         " ##### ",
         " # ",
         " # ",
         " # "},
      Set five[H][W]={" ##### ",
         " # ",
         " # ",
         " ##### ",
         " # ",
         " # ",
         " ##### "},
      Set six[H][W]={ " ##### ",
         " # ",
         " # ",
         " ##### ",
         " # # ",
         " # # ",
         " ##### "},
      Set seven[H][W]={" ##### ",
         " # ",
         " # ",
         " # ",
         " # ",
         " # ",
         " # "},
      Set eight[H][W]={" ##### ",
         " # # ",
         " # # ",
         " ##### ",
         " # # ",
         " # # ",
         " ##### "},
      Set nine[H][W]={" ##### ",
         " # # ",
         " # # ",
         " ##### ",
         " # ",
         " # ",
         " # "}
      If strlen(num) > 10
         Print ”You must enter a number upto 10 digits”
      Else
         Print new line
         Set k=1
         Set j=0
         While k <= 7
            Loop For i=0 and i<strlen(num) and i++
               If num[i] == &#39;0&#39; then,
                  Print zero[j]
               Else If num[i] == &#39;1&rsquo; then,
                  Print one[j]
               Else If num[i] == &#39;2&#39; then,
                  Print two[j]
               Else If num[i] == &#39;3&#39; then,
                  Print three[j]
               Else If num[i] == &#39;4&#39; then,
                  Print four[j]
               Else If num[i] == &#39;5&#39; then,
                  Print five[j]
               Else If num[i] == &#39;6&#39; then,
                  Print six[j]
               Else If num[i] == &#39;7&#39; then,
                  Print seven[j]
               Else If (num[i] == &#39;8&#39;)
                  Print eight[j]
               Else If (num[i] == &#39;9&#39;)
                  Print nine[j]
            End For
            Print newline
            Increment k by 1
            Increment j by 1
         End While
      End Else
   Step 4 -> Declare int main()
      Declare and initialize input char n[] = {"2168"}
      Call function large(n)
Stop
登入後複製

範例

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define H 7
#define W 8
int large(char num[]) {
   int i, j, k;
   // declaring char 2D arrays and initializing
   // with hash-printed digits
   char zero[H][W]={" ##### ", // H=0
      " # # ", // H=1
      " # # ", // H=2
      " # # ", // H=3
      " # # ", // H=4
      " # # ", // H=5
      " ##### "},// H=6
   one[H][W]={" # ",
      " # ",
      " # ",
      " # ",
      " # ",
      " # ",
      " # "},
   two[H][W]={ " ##### ",
      " # ",
      " # ",
      " ##### ",
      " # ",
      " # ",
      " ##### "},
   three[H][W]={" ##### ",
      " # ",
      " # ",
      " ##### ",
      " # ",
      " # ",
      " ##### "},
   four[H][W]={" # ",
      " # # ",
      " # # ",
      " ##### ",
      " # ",
      " # ",
      " # "},
   five[H][W]={" ##### ",
      " # ",
      " # ",
      " ##### ",
      " # ",
      " # ",
      " ##### "},
   six[H][W]={ " ##### ",
      " # ",
      " # ",
      " ##### ",
      " # # ",
      " # # ",
      " ##### "},
   seven[H][W]={" ##### ",
      " # ",
      " # ",
      " # ",
      " # ",
      " # ",
      " # "},
   eight[H][W]={" ##### ",
      " # # ",
      " # # ",
      " ##### ",
      " # # ",
      " # # ",
      " ##### "},
   nine[H][W]={" ##### ",
      " # # ",
      " # # ",
      " ##### ",
      " # ",
      " # ",
      " # "};
   if (strlen(num) > 10)
      printf("</p><p>You must enter a number upto 10 digits.</p><p>Try again!</p><p>");
   else {
      printf("</p><p>");
      k=1;
      j=0; //controls H of each digit
      while (k <= 7) //controls height {
         for (i=0; i<strlen(num); i++) //reads each digit {
            if (num[i] == &#39;0&#39;)
               printf("%s", zero[j]);
            else if (num[i] == &#39;1&#39;)
               printf("%s", one[j]);
            else if (num[i] == &#39;2&#39;)
               printf("%s", two[j]);
            else if (num[i] == &#39;3&#39;)
               printf("%s", three[j]);
            else if (num[i] == &#39;4&#39;)
               printf("%s", four[j]);
            else if (num[i] == &#39;5&#39;)
               printf("%s", five[j]);
            else if (num[i] == &#39;6&#39;)
               printf("%s", six[j]);
            else if (num[i] == &#39;7&#39;)
               printf("%s", seven[j]);
            else if (num[i] == &#39;8&#39;)
               printf("%s", eight[j]);
            else if (num[i] == &#39;9&#39;)
               printf("%s", nine[j]);
         }
         printf("</p><p>");
         k++;
         j++;
      }
   }
   return 1;
}
//main fucntion
int main() {
   char n[] = {"2168"};
   large(n);
   return 0;
}
登入後複製

輸出

C程式接收一個數字並以大字印出來

以上是C程式接收一個數字並以大字印出來的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板