AlgorithmQuiz/LcdDisplay C++로 작성 --destine, 2004-09-21

runMain 부분은 정말 끔찍하네요 --;;

Toggle line numbers
   1 // LcdPlay.cpp : Defines the entry point for the console application.
   2 //
   3 
   4 #include "stdafx.h"
   5 #include <iostream>
   6 
   7 using namespace std;
   8 #define _X(x) (x+2)
   9 #define _Y(y) (2*y+3)
  10 #define _SIZE(n) ((_X(n))*(_Y(n)))
  11 
  12 class number{
  13 protected:
  14         typedef enum FLAG{ T, TL, TR, M, BL, BR, B, E };
  15         int flags[7];
  16         
  17 protected:
  18         number()
  19         {
  20                 memset(flags,0,sizeof(flags));
  21         }
  22         void set(FLAG x)
  23         {
  24                 flags[x] = 1;
  25         }
  26         int get(FLAG x)
  27         {
  28                 if ( x == number::FLAG::E )
  29                         return 0;
  30                 return flags[x];
  31         }
  32         static FLAG status(int x, int y, int nSize)
  33         {
  34                 if ( x == 0 )
  35                 {
  36                         if ( y != 0 && y < (_Y(nSize) - 1 )/2 )
  37                                 return number::FLAG::TL;
  38                         else if ( y != _Y(nSize) - 1 && y > (_Y(nSize) - 1 )/2 )
  39                                 return number::FLAG::BL;
  40                 }
  41                 else if ( x == _X(nSize) - 1 )
  42                 {
  43                         if ( y != 0 && y < (_Y(nSize) - 1 )/2 )
  44                                 return number::FLAG::TR;
  45                         else if ( y != _Y(nSize) - 1 && y > (_Y(nSize) - 1 )/2 )
  46                                 return number::FLAG::BR;
  47                 }
  48                 else 
  49                 {
  50                         if ( y == 0 )
  51                                 return number::FLAG::T;
  52                         else if ( y == (_Y(nSize) - 1 )/2 )
  53                                 return number::FLAG::M;
  54                         else if ( y == _Y(nSize) - 1 )
  55                                 return number::FLAG::B;
  56                 }
  57                 return number::FLAG::E; 
  58         }
  59         char getChar(FLAG x)
  60         {
  61                 switch( x )
  62                 {
  63                 case number::FLAG::T:
  64                 case number::FLAG::M:
  65                 case number::FLAG::B:
  66                         return '_';
  67                         break;
  68                 case number::FLAG::E:
  69                         return ' ';
  70                 default:
  71                         return '|';
  72                 }
  73                 return ' ';
  74         }
  75 public:
  76         void lcd(int nSize, char* buf)
  77         {
  78                 int nIndex;
  79                 for ( int x = 0; x < _X(nSize); x++ )
  80                 {
  81                         for ( int y = 0; y < _Y(nSize); y++ )
  82                         {
  83                                 nIndex = y * _X(nSize) + x;
  84                                 buf[nIndex] = ' ';
  85                                 if ( get(status(x,y,nSize)) == 1 )
  86                                 {
  87                                         buf[nIndex] = getChar(status(x,y,nSize));
  88                                 }
  89                         }
  90                 }
  91         }
  92 };
  93 
  94 class one:public number{
  95 public:
  96         one()
  97         {
  98                 set(TL);
  99                 set(BL);
 100         }
 101 };
 102 class two:public number{
 103 public:
 104         two()
 105         {
 106                 set(T);
 107                 set(TR);
 108                 set(M);
 109                 set(BL);
 110                 set(B);
 111         }
 112 };
 113 class three:public number{
 114 public:
 115         three()
 116         {
 117                 set(T);
 118                 set(TR);
 119                 set(M);
 120                 set(BR);
 121                 set(B);
 122         }
 123 };
 124 class four:public number{
 125 public:
 126         four()
 127         {
 128                 set(TL);
 129                 set(TR);
 130                 set(M);
 131                 set(BR);
 132         }
 133 };
 134 class five:public number{
 135 public:
 136         five()
 137         {
 138                 set(T);
 139                 set(TL);
 140                 set(M);
 141                 set(BR);
 142                 set(B);
 143         }
 144 };
 145 class six:public number{
 146 public:
 147         six()
 148         {
 149                 set(T);
 150                 set(TL);
 151                 set(M);
 152                 set(BL);
 153                 set(BR);
 154                 set(B);
 155         }
 156 };
 157 class seven:public number{
 158 public:
 159         seven()
 160         {
 161                 set(T);
 162                 set(TR);
 163                 set(BR);
 164         }
 165 };
 166 class eight:public number{
 167 public:
 168         eight()
 169         {
 170                 set(T);
 171                 set(TL);
 172                 set(TR);
 173                 set(M);
 174                 set(BL);
 175                 set(BR);
 176                 set(B);
 177         }
 178 };
 179 class nine:public number{
 180 public:
 181         nine()
 182         {
 183                 set(T);
 184                 set(TL);
 185                 set(TR);
 186                 set(M);
 187                 set(BR);
 188                 set(B);
 189         }
 190 };
 191 class zero:public number{
 192 public:
 193         zero()
 194         {
 195                 set(T);
 196                 set(TL);
 197                 set(TR);
 198                 set(BL);
 199                 set(BR);
 200                 set(B);
 201         }
 202 };
 203 
 204 class numberFac{
 205         static one a1;
 206         static two a2;
 207         static three a3;
 208         static four a4;
 209         static five a5;
 210         static six a6;
 211         static seven a7;
 212         static eight a8;
 213         static nine a9;
 214         static zero a0;
 215 public:
 216         static number getInstance(int nNumber)
 217         {
 218                 switch(nNumber)
 219                 {
 220                 case 1:
 221                         return a1;
 222                 case 2:
 223                         return a2;
 224                 case 3:
 225                         return a3;
 226                 case 4:
 227                         return a4;
 228                 case 5:
 229                         return a5;
 230                 case 6:
 231                         return a6;
 232                 case 7:
 233                         return a7;
 234                 case 8:
 235                         return a8;
 236                 case 9:
 237                         return a9;
 238                 case 0:
 239                         return a0;
 240                 }
 241                 return a0;
 242         }
 243 };
 244 
 245 one numberFac::a1;
 246 two numberFac::a2;
 247 three numberFac::a3;
 248 four numberFac::a4;
 249 five numberFac::a5;
 250 six numberFac::a6;
 251 seven numberFac::a7;
 252 eight numberFac::a8;
 253 nine numberFac::a9;
 254 zero numberFac::a0;
 255 
 256 bool assert( int x, int y )
 257 {
 258         if ( x == y )
 259         {
 260                 cout<<"assert equal\n";
 261                 return true;
 262         }
 263         else 
 264         {
 265                 cout<<"assert not equal\n";
 266                 return false;
 267         }
 268 }
 269 
 270 bool assert(char* x, char* y )
 271 {
 272         bool bRet = true;
 273         
 274         if ( sizeof(x) != sizeof(y) )
 275         {
 276                 bRet = false;
 277         }
 278         else
 279         {
 280                 for ( int i = 0; i < sizeof(x); i ++ )
 281                 {
 282                         if ( x[i] != y[i] )
 283                         {
 284                                 bRet = false;
 285                                 break;
 286                         }
 287                 }
 288         }
 289         
 290         assert( true, bRet );
 291         return bRet;
 292 }
 293 bool input( int& nSize, int& nNumber )
 294 {
 295         cin>>nSize;
 296         
 297         if ( nSize == 0 )
 298                 return false;
 299         
 300         cin>>nNumber;
 301         
 302         return true;
 303 }
 304 
 305 bool LcdNumber(int nSize, int nNumber, char* buf)
 306 {
 307         number num = numberFac::getInstance(nNumber);
 308         num.lcd( nSize, buf );
 309         return true;
 310 }
 311 
 312 void test2_4()
 313 {
 314         char buf[_SIZE(2)];
 315         char* szResult = 
 316                 "    "
 317                 "|  |"
 318                 "|  |"
 319                 " __ "
 320                 "   |"
 321                 "   |"
 322                 "    ";
 323         LcdNumber(2,4,buf);
 324         
 325         assert(buf, szResult);
 326 }
 327 
 328 void test1_4()
 329 {
 330         char buf[_SIZE(1)];
 331         char* szResult = 
 332                 "   "
 333                 "| |"
 334                 " _ "
 335                 "  |"
 336                 "   ";
 337         
 338         LcdNumber(1,4,buf);
 339         
 340         assert(buf, szResult);
 341 }
 342 
 343 void test1_3()
 344 {
 345         char buf[_SIZE(1)];
 346         char* szResult = 
 347                 " _ "
 348                 "  |"
 349                 " _ "
 350                 "  |"
 351                 " _ ";
 352         
 353         LcdNumber(1,2,buf);
 354         
 355         assert(buf, szResult);
 356 }
 357 
 358 void test1_2()
 359 {
 360         char buf[_SIZE(1)];
 361         char* szResult = 
 362                 " _ "
 363                 "  |"
 364                 " _ "
 365                 "|  "
 366                 " _ ";
 367         
 368         LcdNumber(1,2,buf);
 369         
 370         assert(buf, szResult);
 371 }
 372 
 373 void test1_1()
 374 {
 375         char buf[_SIZE(1)];
 376         char* szResult = 
 377                 "   "
 378                 "|  "
 379                 "   "
 380                 "|  "
 381                 "   ";
 382         
 383         LcdNumber(1,1,buf);
 384         
 385         assert(buf, szResult);
 386 }
 387 
 388 void testMain()
 389 {
 390         assert( " |", " | ");
 391         assert( " | ", " | ");
 392         assert( _X(1), 3 );
 393         assert( _Y(1), 5 );
 394         assert( _SIZE(1), (1+2)*(2*1+3) );
 395         test1_1();
 396         test1_2();
 397         test1_3();
 398         test1_4();
 399         test2_4();
 400 }
 401 
 402 void print(int nSize, char* pBuffer)
 403 {
 404         for( int j = 0; j < _Y(nSize); j++ )
 405         {
 406                 for( int i = 0; i < _X(nSize); i ++ )
 407                 {
 408                         cout<<pBuffer[(j*_X(nSize))+i];
 409                 }
 410                 cout<<"\n";
 411         }
 412 }
 413 
 414 void runMain()
 415 {
 416         int nSize;
 417         int nNumber;
 418         while( true )
 419         {
 420                 cin>>nSize;
 421                 if ( nSize <= 0 )
 422                         return;
 423                 cin>>nNumber;
 424                 if ( nNumber < 0 )
 425                         return;
 426                 
 427                 if ( nNumber == 0 )
 428                 {
 429                         char* pBuffer = new char[_SIZE(nSize)];
 430                         LcdNumber( nSize, nNumber, pBuffer );
 431                         print( nSize, pBuffer );
 432                         delete[] pBuffer;
 433                         continue;
 434                 }
 435                 
 436                 for( int i = 1; true; i *= 10 )
 437                 {
 438                         if ( nNumber / i == 0 )
 439                         {
 440                                 int tempNum = nNumber;
 441                                 char* pBuffer = new char[_SIZE(nSize)];
 442                                 for( int j = i/10; j >= 1 ; j /= 10 )
 443                                 {
 444                                         LcdNumber( nSize, tempNum / j, pBuffer );
 445                                         print( nSize, pBuffer );
 446                                         tempNum -= ((tempNum / j) * j);
 447                                 }
 448                                 delete[] pBuffer;
 449                                 break;
 450                         }
 451                 }
 452         }
 453 }
 454 
 455 int main(int argc, char* argv[])
 456 {
 457         char szPause;
 458         //      testMain();
 459         runMain();
 460         cin>>szPause;
 461         return 0;
 462 }

AlgorithmQuiz/LcdDisplay/destine (last edited 2012-08-14 14:22:03 by 182)

web biohackers.net