一个创意的C++程序,用于缩放整数

2021年5月11日14:26:13 发表评论 864 次浏览

编写一个C(或C ++)程序来缩放(放大)一个整数。它应该从用户那里获取一个整数, 并使用某种模式以放大形式显示该整数的每个数字。

例子:

Input : 123
Output : 

  @
 @@
  @
  @
@@@@@
-------------------------------

@@@@
@  @
  @
 @
@@@@
-------------------------------


@@@@@
    @
@@@@@
    @
@@@@@
-------------------------------

此创意程序从用户那里获取一个整数, 然后在该整数之后打印该整数的每一位缩放它。

给定的数字首先使用转换为字符串串流。此后, 将访问每个字符(数字)并将其置于开关箱结构中, 该结构将每个数字进行分类并以图案的形式打印。

下面是C ++的实现

//C++ program to zoon digits of an integer
#include <bits/stdc++.h>
using namespace std;
  
void zoomDigits( int number)
{
     //Converting number to string
     stringstream ss;
     ss <<number;
     string str = ss.str();
  
     for ( int k=0; k<str.length(); k++)
     {
         switch (str[k]- '0' )
         {
         case 0:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (i==0 || i==4)
                         cout <<'@' ;
                     else if (j==0 || j==4)
                         cout <<'@' ;
                     else
                         cout <<" " ;
  
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 1:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (j==2)
                         cout <<'@' ;
                     else if ((i==1 && j==1))
                         cout <<'@' ;
                     else if (i==4)
                         cout <<'@' ;
                     else
                         cout <<" " ;
  
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 2:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<4; j++)
                 {
                     if (i==0 && j==4)
                         cout <<" " ;
                     else if (i==0 || i==4)
                         cout <<'@' ;
                     else if (i==1 && j==0)
                         cout <<'@' ;
                     else if (i==(4-j))
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 3:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (i==0 || i==2 || i==4)
                         cout <<'@' ;
                     else if (j==4)
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 4:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (j==4)
                         cout <<'@' ;
                     else if (i==2)
                         cout <<'@' ;
                     else if (j==0 && (i==0 || i==1))
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 5:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (i==0 || i==2 || i==4)
                         cout <<'@' ;
                     else if ((j==0 && i==1) ||
                              (j==4 && i==3))
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 6:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (i==0 || i==2 || i==4)
                         cout <<'@' ;
                     else if ((j==0 && (i==1 || i==3)) ||
                                        (j==4 && i==3))
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 7:
             for ( int i=0 ; i<5; i++)
             {
                 for ( int j=0 ; j<5; j++)
                 {
                     if (i==0 && (j!=4))
                         cout <<'@' ;
                     else if (i==2 && (j==2 || j==4))
                         cout <<'@' ;
                     else if (j==3)
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 8:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if (i==0 || i==2 || i==4)
                         cout <<'@' ;
                     else if ((j==0 && (i==1 || i==3) ||
                             (j==4 && (i==1 || i==3))))
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
  
         case 9:
             for ( int i=0; i<5; i++)
             {
                 for ( int j=0; j<5; j++)
                 {
                     if ( i==0 || i==2 || j==4)
                         cout <<'@' ;
                     else if (i==1 && j==0)
                         cout <<'@' ;
                     else
                         cout <<" " ;
                 }
                 cout <<endl;
             }
             cout <<"-------------------------------\n\n" ;
             continue ;
         }
     }
}
  
//Driver code
int main()
{
     long long number = 12305;
     zoomDigits(number);
     return 0;
}

输出如下:

@
 @@
  @
  @
@@@@@
-------------------------------

@@@@
@  @
  @
 @
@@@@
-------------------------------

@@@@@
    @
@@@@@
    @
@@@@@
-------------------------------

@@@@@
@   @
@   @
@   @
@@@@@
-------------------------------

@@@@@
@
@@@@@
    @
@@@@@
-------------------------------

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: