Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837762
  • 博文数量: 109
  • 博客积分: 650
  • 博客等级: 上士
  • 技术积分: 1483
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 17:29
文章分类

全部博文(109)

文章存档

2016年(5)

2015年(21)

2014年(16)

2013年(38)

2012年(29)

分类: LINUX

2013-04-08 23:51:55


点击(此处)折叠或打开

  1. #include <iostream>
  2. using std::cout;
  3. using std::ios;
  4. #include <iomanip>
  5. using std::setw;
  6. using std::setprecision;
  7. using std::setiosflags;
  8. using std::resetiosflags;
  9. #include <cstdlib>
  10. #include <ctime>
  11. void shuffle( int [][ 13 ] );
  12. void deal( const int [][ 13 ], const char *[], const char *[], int [][ 2 ] );
  13. void pair( const int [][ 13 ], const int [][ 2 ], const char *[] );
  14. void threeOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
  15. void fourOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
  16. void flushHand( const int [][ 13 ], const int [][ 2 ], const char *[] );
  17. void straightHand( const int [][ 13 ], const int [][ 2 ], const char *[],const char *[] );
  18. int main()
  19. {
  20.    const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" },
  21.               *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Jack", "Queen","King" };
  22.    int deck[ 4 ][ 13 ] = { 0 }, hand[ 5 ][ 2 ] = { 0 };
  23.    srand( time( 0 ) );
  24.    shuffle( deck );
  25.    deal( deck, face, suit, hand );
  26.    pair( deck, hand, face );
  27.    threeOfKind( deck, hand, face );
  28.    fourOfKind( deck, hand, face );
  29.    flushHand( deck, hand, suit );
  30.    straightHand( deck, hand, suit, face );
  31.    return 0;
  32. }
  33. void shuffle( int wDeck[][ 13 ] )
  34. {
  35.    int row, column;
  36.    for ( int card = 1; card <= 52; ++card ) {
  37.       do {
  38.         row = rand() % 4;
  39.         column = rand() % 13;
  40.       } while ( wDeck[ row ][ column ] != 0 );
  41.       wDeck[ row ][ column ] = card;
  42.    }
  43. }
  44. void deal( const int wDeck[][ 13 ], const char *wFace[],const char *wSuit[], int wHand[][ 2 ] )
  45. {
  46. int r = 0;
  47. cout << "The hand is:\n";
  48. for ( int card = 1; card < 6; ++card )
  49. for ( int row = 0; row <= 3; ++row )
  50. for ( int column = 0; column <= 12; ++column )
  51. if ( wDeck[ row ][ column ] == card ) {
  52. wHand[ r ][ 0 ] = row;
  53. wHand[ r ][ 1 ] = column;
  54. cout << setw( 5 ) << wFace[ column ]<< " of " << setw( 8 ) << setiosflags( ios::left )
  55. << wSuit[ row ] << ( card % 2 == 0 ? '\n' : '\t' )
  56.  << resetiosflags( ios::left );
  57. ++r;
  58. }
  59. cout << '\n';
  60. }
  61. void pair( const int wDeck[][ 13 ], const int wHand[][ 2 ], const char *wFace[] )
  62. {
  63. int counter[ 13 ] = { 0 };
  64. for ( int r = 0; r < 5; ++r )
  65. ++counter[ wHand[ r ][ 1 ] ];
  66. cout << '\n';
  67. for ( int p = 0; p < 13; ++p )
  68. if ( counter[ p ] == 2 )
  69. cout << "The hand contains a pair of " << wFace[ p ] << "'s.\n";
  70. }

  71. void threeOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wFace[] )
  72. {
  73. int counter[ 13 ] = { 0 };
  74. for ( int r = 0; r < 5; ++r )
  75. ++counter[ wHand[ r ][ 1 ] ];
  76. for ( int t = 0; t < 13; t++ )
  77. if ( counter[ t ] == 3 )
  78. cout << "The hand contains three " << wFace[ t ] << "'s.\n";
  79. }
  80. 113
  81. 114 void fourOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
  82. 115 const char *wFace[] )
  83. 116 {
  84. 117 int counter[ 13 ] = { 0 };
  85. 118
  86. 119 for ( int r = 0; r < 5; ++r )
  87. 120 ++counter[ wHand[ r ][ 1 ] ];
  88. 121
  89. 122 for ( int k = 0; k < 13; ++k )
  90. 123 if ( counter[ k ] == 4 )
  91. 124 cout << "The hand contains four " << wFace[ k ] << "'s.\n";
  92. 125 }
  93. 126
  94. 127 void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
  95. 128 const char *wSuit[] )
  96. 129 {
  97. 130 int count[ 4 ] = { 0 };
  98. 131


  99. void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wSuit[] )
  100. {
  101. int count[ 4 ] = { 0 };
  102. for ( int r = 0; r < 5; ++r )
  103. ++count[ wHand[ r ][ 0 ] ];
  104. for ( int f = 0; f < 4; ++f )
  105. if ( count[ f ] == 5 )
  106. cout << "The hand contains a flush of " << wSuit[ f ] << "'s.\n";
  107.  }


  108. void straightHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wSuit[], const char *wFace[] )
  109. {
  110. int s[ 5 ] = { 0 }, temp;
  111. for ( int r = 0; r < 5; ++r )
  112. s[ r ] = wHand[ r ][ 1 ];
  113. for ( int pass = 1; pass < 5; ++pass )
  114. for ( int comp = 0; comp < 4; ++comp )
  115. if ( s[ comp ] > s[ comp + 1 ] ) {
  116. temp = s[ comp ];
  117. s[ comp ] = s[ comp + 1 ];
  118. s[ comp + 1 ] = temp;
  119. }
  120. if ( s[ 4 ] - 1 == s[ 3 ] && s[ 3 ] - 1 == s[ 2 ] && s[ 2 ] - 1 == s[ 1 ] && s[ 1 ] - 1 == s[ 0 ] ) {
  121. cout << "The hand contains a straight consisting of\n";
  122. for ( int j = 0; j < 5; ++j )
  123. cout << wFace[ wHand[ j ][ 1 ] ] << " of " << wSuit[ wHand[ j ][ 0 ] ]<< '\n';
  124. }
  125. }

  126.  


  127. ######################################################
  128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  129. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

  130. #include <iostream>
  131. using std::cout;
  132. using std::endl;
  133. #include <cstdlib>
  134. #include <ctime>
  135. #include <iomanip>
  136. using std::setw;
  137. const int RACE_END = 70;
  138. void moveTortoise( int * const );
  139. void moveHare( int * const );
  140. void printCurrentPositions( const int * const, const int * const );
  141. int main()
  142. {
  143. int tortoise = 1, hare = 1, timer = 0;
  144. srand( time( 0 ) );
  145. cout << "ON YOUR MARK, GET SET\nBANG !!!!"<< "\nAND THEY'RE OFF !!!!\n";
  146. while ( tortoise != RACE_END && hare != RACE_END ) {
  147. moveTortoise( &tortoise );
  148. moveHare( &hare );
  149. printCurrentPositions( &tortoise, &hare );
  150. ++timer;
  151. }
  152. if ( tortoise >= hare )
  153. cout << "\nTORTOISE WINS!!! YAY!!!\n";
  154. else
  155. cout << "Hare wins. Yuch.\n";
  156. cout << "TIME ELAPSED = " << timer << " seconds" << endl;
  157. return 0;
  158. }
  159. void moveTortoise( int * const turtlePtr )
  160. {
  161. int x = 1 + rand() % 10;
  162. if ( x >= 1 && x <= 5 )
  163. *turtlePtr += 3;
  164. else if ( x == 6 || x == 7 )
  165. *turtlePtr -= 6;
  166. else
  167. ++( *turtlePtr );
  168. if ( *turtlePtr < 1 )
  169. *turtlePtr = 1;
  170. else if ( *turtlePtr > RACE_END )
  171. *turtlePtr = RACE_END;
  172. }
  173. void moveHare( int * const rabbitPtr )
  174. {
  175. int y = 1 + rand() % 10;
  176. if ( y == 3 || y == 4 )
  177. *rabbitPtr += 9;
  178. else if ( y == 5 )
  179. *rabbitPtr -= 12;
  180. else if ( y >= 6 && y <= 8 )
  181. ++( *rabbitPtr );
  182. else if ( y > 8 )
  183. *rabbitPtr -= 2;
  184. if ( *rabbitPtr < 1 )
  185. *rabbitPtr = 1;
  186. else if ( *rabbitPtr > RACE_END )
  187. *rabbitPtr = RACE_END;
  188. }
  189. void printCurrentPositions( const int * const snapperPtr, const int * const bunnyPtr )
  190. {
  191. if ( *bunnyPtr == *snapperPtr )
  192. cout << setw( *bunnyPtr ) << "OUCH!!!";
  193. else if ( *bunnyPtr < *snapperPtr )
  194. cout << setw( *bunnyPtr ) << 'H' << setw( *snapperPtr - *bunnyPtr ) << 'T';
  195. else
  196. cout << setw( *snapperPtr ) << 'T' << setw( *bunnyPtr - *snapperPtr ) << 'H';
  197. cout << '\n';
  198. }


  199. ######################################################
  200. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  201. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

  202. #include <iostream>
  203. using std::cout;
  204. using std::endl;
  205. using std::cin;
  206. using std::ios;
  207. #include <iomanip>
  208. using std::setfill;
  209. using std::setw;
  210. using std::setiosflags;
  211. using std::resetiosflags;
  212. const int SIZE = 100, MAX_WORD = 9999, MIN_WORD = -9999;
  213. const long SENTINEL = -99999;
  214. enum Commands { READ = 10, WRITE, LOAD = 20, STORE, ADD = 30, SUBTRACT,DIVIDE, MULTIPLY, BRANCH = 40, BRANCHNEG, BRANCHZERO, HALT };
  215. void load( int * const );
  216. void execute( int * const, int * const, int * const, int * const, int * const, int * const);

  217. void dump( const int * const, int, int, int, int, int );
  218. bool validWord( int );

  219. int main()
  220. {
  221. int memory[ SIZE ] = { 0 }, accumulator = 0, instructionCounter = 0, opCode = 0, operand = 0, instructionRegister = 0;
  222. load( memory );
  223. execute( memory, &accumulator, &instructionCounter, &instructionRegister,&opCode, &operand );
  224. dump( memory, accumulator, instructionCounter, instructionRegister,opCode, operand );
  225. return 0;
  226. }
  227. void load( int * const loadMemory )
  228. {
  229. long instruction;
  230. int i = 0;
  231. cout << "*** Welcome to Simpletron ***\n"
  232. << "*** Please enter your program one instruction ***\n"
  233. << "*** (or data word) at a time. I will type the ***\n"
  234. << "*** location number and a question mark (?). ***\n"
  235. << "*** You then type the word for that location. ***\n"
  236. << "*** Type the sentinel -99999 to stop entering ***\n"
  237. << "*** your program. ***\n" << "00 ? ";
  238.    cin >> instruction;
  239.  while ( instruction != SENTINEL ) {
  240. if ( !validWord( instruction ) )
  241. cout << "Number out of range. Please enter again.\n";
  242. else
  243. loadMemory[ i++ ] = instruction;

  244.       cout << setw( 2 ) << setfill( '0' ) << i << " ? ";
  245.       cin >> instruction;
  246.    }
  247. }

  248. void execute( int * const memory, int * const acPtr, int * const icPtr, int * const irPtr, int * const opCodePtr, int * const opPtr )

  249. {

  250.    bool fatal = false;

  251.    int temp;

  252.    const char *messages[] = { "Accumulator overflow ***",

  253.                               "Attempt to divide by zero ***",

  254.                               "Invalid opcode detected ***" },

  255.          *termString = "\n*** Simpletron execution abnormally terminated ***",

  256.          *fatalString = "*** FATAL ERROR: ";


  257.    cout << "\n************START SIMPLETRON EXECUTION************\n\n";


  258.    do {

  259.       *irPtr = memory[ *icPtr ];

  260.       *opCodePtr = *irPtr / 100;
  261.       *opPtr = *irPtr % 100;
  262.       switch ( *opCodePtr ) {
  263.          case READ:
  264.             cout << "Enter an integer: ";
  265.             cin >> temp;
  266.             while ( !validWord( temp ) ) {
  267.                cout << "Number out of range. Please enter again: ";
  268.                cin >> temp;
  269.             }
  270.             
  271.             memory[ *opPtr ] = temp;
  272.             ++( *icPtr );
  273.             break;
  274.           case WRITE:
  275.              cout << "Contents of " << setw( 2 ) << setfill( '0' ) << *opPtr
  276.                   << ": " << memory[ *opPtr ] << '\n';
  277.              ++( *icPtr );
  278.              break;
  279.          case LOAD:
  280.              *acPtr = memory[ *opPtr ];
  281.              ++( *icPtr );
  282.              break;
  283.           case STORE:
  284.              memory[ *opPtr ] = *acPtr;
  285.              ++( *icPtr );
  286.              break;
  287.           case ADD:
  288.              temp = *acPtr + memory[ *opPtr ];
  289.              
  290.              if ( !validWord( temp ) ) {
  291.                 cout << fatalString << messages[ 0 ] << termString << '\n';
  292.                 fatal = true;
  293.              }
  294.              else {
  295.                 *acPtr = temp;
  296.                ++( *icPtr );
  297.             }
  298.                 
  299.            break;
  300.         case SUBTRACT:
  301.              temp = *acPtr - memory[ *opPtr ];
  302.             
  303.             if ( !validWord( temp ) ) {
  304.                 cout << fatalString << messages[ 0 ] << termString << '\n';
  305.                 fatal = true;
  306.              }
  307.              else {
  308.                 *acPtr = temp;
  309.                 ++( *icPtr );
  310.              }
  311.                 
  312.              break;
  313.           case DIVIDE:
  314.              if ( memory[ *opPtr ] == 0 ) {
  315.                 cout << fatalString << messages[ 1 ] << termString << '\n';
  316.                 fatal = true;
  317.              }
  318.              else {
  319.                 *acPtr /= memory[ *opPtr ];
  320.                 ++( *icPtr );
  321.              }
  322.              break;
  323.           case MULTIPLY:
  324.              temp = *acPtr * memory[ *opPtr ];
  325.              
  326.              if ( !validWord( temp ) ) {
  327.                 cout << fatalString << messages[ 0 ] << termString << '\n';
  328.                 fatal = true;
  329.              }
  330.              else {
  331.                 *acPtr = temp;
  332.                 ++( *icPtr );
  333.              }
  334.              break;
  335.           case BRANCH:
  336.              *icPtr = *opPtr;
  337.              break;
  338.          case BRANCHNEG:
  339.              *acPtr < 0 ? *icPtr = *opPtr : ++( *icPtr );
  340.              break;
  341.          case BRANCHZERO:
  342.             *acPtr == 0 ? *icPtr = *opPtr : ++( *icPtr );
  343.              break;
  344.        case HALT:
  345.              cout << "*** Simpletron execution terminated ***\n";
  346.             break;
  347.          default:
  348.             cout << fatalString << messages[ 2 ] << termString << '\n';
  349.            fatal = true;
  350.              break;
  351.        }
  352.    } while ( *opCodePtr != HALT && !fatal );
  353.    
  354.    cout << "\n*************END SIMPLETRON EXECUTION*************\n";
  355.  }

  356. void dump( const int * const memory, int accumulator, int instructionCounter,
  357.            int instructionRegister, int operationCode, int operand )
  358. {
  359.    void output( const char * const, int, int, bool ); // prototype

  360.     cout << "\nREGISTERS:\n";
  361.     output( "accumulator", 5, accumulator, true );
  362.     output( "instructionCounter", 2, instructionCounter, false );
  363.    output( "instructionRegister", 5, instructionRegister, true );
  364.    output( "operationCode", 2, operationCode, false );
  365.     output( "operand", 2, operand, false );
  366.    cout << "\n\nMEMORY:\n";

  367.    int i = 0;
  368.     cout << setfill( ' ' ) << setw( 3 ) << ' ';

  369.    // print header
  370.    for ( ; i <= 9; ++i )
  371.       cout << setw( 5 ) << i << ' ';
  372.       
  373.    for ( i = 0; i < SIZE; ++i ) {
  374.      if ( i % 10 == 0 )
  375.         cout << '\n' << setw( 2 ) << i << ' ';

  376.      cout << setiosflags( ios::internal | ios::showpos )
  377.           << setw( 5 ) << setfill( '0' ) << memory[ i ] << ' '
  378.           << resetiosflags( ios::internal | ios::showpos );

  379.    }
  380.    
  381.     cout << endl;
  382. }

  383. bool validWord( int word )
  384.  {
  385.    return word >= MIN_WORD && word <= MAX_WORD;
  386.  }

  387. void output( const char * const sPtr, int width, int value, bool sign )
  388. {
  389.     // format of "accumulator", etc.
  390.    cout << setfill( ' ' ) << setiosflags( ios::left ) << setw( 20 )
  391.         << sPtr << ' ';
  392.     
  393.    // is a +/- sign needed?
  394.    if ( sign )
  395.       cout << setiosflags( ios::showpos | ios::internal );

  396.    // setup for displaying accumulator value, etc.
  397.     cout << resetiosflags( ios::left ) << setfill( '0' );

  398.     // determine the field widths and display value
  399.     if ( width == 5 )
  400.        cout << setw( width ) << value << '\n';
  401.    else // width is 2
  402.       cout << setfill( ' ' ) << setw( 3 ) << ' ' << setw( width )
  403.            << setfill( '0' ) << value << '\n';

  404.     // disable sign if it was set
  405.    if ( sign )
  406.      cout << resetiosflags( ios::showpos | ios::internal );
  407.  }

 

#include
using std::cout;
using std::endl;
using std::cin;
using std::ios;
#include
using std::setfill;
using std::setw;
using std::setiosflags;
using std::resetiosflags;
const int SIZE = 100, MAX_WORD = 9999, MIN_WORD = -9999;
const long SENTINEL = -99999;
enum Commands { READ = 10, WRITE, LOAD = 20, STORE, ADD = 30, SUBTRACT,DIVIDE, MULTIPLY, BRANCH = 40, BRANCHNEG, BRANCHZERO, HALT };
void load( int * const );
void execute( int * const, int * const, int * const, int * const, int * const, int * const);

void dump( const int * const, int, int, int, int, int );
bool validWord( int );

int main()
{
int memory[ SIZE ] = { 0 }, accumulator = 0, instructionCounter = 0, opCode = 0, operand = 0, instructionRegister = 0;
load( memory );
execute( memory, &accumulator, &instructionCounter, &instructionRegister,&opCode, &operand );
dump( memory, accumulator, instructionCounter, instructionRegister,opCode, operand );
return 0;
}
void load( int * const loadMemory )
{
long instruction;
int i = 0;
cout << "***           Welcome to Simpletron           ***\n"
<< "*** Please enter your program one instruction ***\n"
<< "*** (or data word) at a time. I will type the ***\n"
<< "*** location number and a question mark (?).  ***\n"
<< "*** You then type the word for that location. ***\n"
<< "*** Type the sentinel -99999 to stop entering ***\n"
<< "*** your program.                             ***\n" << "00 ? ";
   cin >> instruction;
 while ( instruction != SENTINEL ) {
if ( !validWord( instruction ) )
cout << "Number out of range. Please enter again.\n";
else
loadMemory[ i++ ] = instruction;

      cout << setw( 2 ) << setfill( '0' ) << i << " ? ";
      cin >> instruction;
   }
}

void execute( int * const memory, int * const acPtr, int * const icPtr, int * const irPtr, int * const opCodePtr, int * const opPtr )

{

   bool fatal = false;

   int temp;

   const char *messages[] = { "Accumulator overflow          ***",

                              "Attempt to divide by zero     ***",

                              "Invalid opcode detected       ***" },

         *termString = "\n*** Simpletron execution abnormally terminated ***",

         *fatalString = "*** FATAL ERROR: ";


   cout << "\n************START SIMPLETRON EXECUTION************\n\n";


   do {

      *irPtr = memory[ *icPtr ];

      *opCodePtr = *irPtr / 100;
      *opPtr = *irPtr % 100;
      switch ( *opCodePtr ) {
         case READ:
            cout << "Enter an integer: ";
            cin >> temp;
            while ( !validWord( temp ) ) {
               cout << "Number out of range. Please enter again: ";
               cin >> temp;
            }
           
            memory[ *opPtr ] = temp;
            ++( *icPtr );
            break;
          case WRITE:
             cout << "Contents of " << setw( 2 ) << setfill( '0' ) << *opPtr
                  << ": " << memory[ *opPtr ] << '\n';
             ++( *icPtr );
             break;
         case LOAD:
             *acPtr = memory[ *opPtr ];
             ++( *icPtr );
             break;
          case STORE:
             memory[ *opPtr ] = *acPtr;
             ++( *icPtr );
             break;
          case ADD:
             temp = *acPtr + memory[ *opPtr ];
            
             if ( !validWord( temp ) ) {
                cout << fatalString << messages[ 0 ] << termString << '\n';
                fatal = true;
             }
             else {
                *acPtr = temp;
               ++( *icPtr );
            }
               
           break;
        case SUBTRACT:
             temp = *acPtr - memory[ *opPtr ];
           
            if ( !validWord( temp ) ) {
                cout << fatalString << messages[ 0 ] << termString << '\n';
                fatal = true;
             }
             else {
                *acPtr = temp;
                ++( *icPtr );
             }
               
             break;
          case DIVIDE:
             if ( memory[ *opPtr ] == 0 ) {
                cout << fatalString << messages[ 1 ] << termString << '\n';
                fatal = true;
             }
             else {
                *acPtr /= memory[ *opPtr ];
                ++( *icPtr );
             }
             break;
          case MULTIPLY:
             temp = *acPtr * memory[ *opPtr ];
            
             if ( !validWord( temp ) ) {
                cout << fatalString << messages[ 0 ] << termString << '\n';
                fatal = true;
             }
             else {
                *acPtr = temp;
                ++( *icPtr );
             }
             break;
          case BRANCH:
             *icPtr = *opPtr;
             break;
         case BRANCHNEG:
             *acPtr < 0 ? *icPtr = *opPtr : ++( *icPtr );
             break;
         case BRANCHZERO:
            *acPtr == 0 ? *icPtr = *opPtr : ++( *icPtr );
             break;
       case HALT:
             cout << "*** Simpletron execution terminated ***\n";
            break;
         default:
            cout << fatalString << messages[ 2 ] << termString << '\n';
           fatal = true;    
             break;
       }     
   } while ( *opCodePtr != HALT && !fatal );
  
   cout << "\n*************END SIMPLETRON EXECUTION*************\n";
 }

void dump( const int * const memory, int accumulator, int instructionCounter,
           int instructionRegister, int operationCode, int operand )
{
   void output( const char * const, int, int, bool );   // prototype

    cout << "\nREGISTERS:\n";
    output( "accumulator", 5, accumulator, true );
    output( "instructionCounter", 2, instructionCounter, false );
   output( "instructionRegister", 5, instructionRegister, true );
   output( "operationCode", 2, operationCode, false );
    output( "operand", 2, operand, false );
   cout << "\n\nMEMORY:\n";

   int i = 0;
    cout << setfill( ' ' ) << setw( 3 ) << ' ';

   // print header
   for ( ; i <= 9; ++i )
      cout << setw( 5 ) << i << ' ';
     
   for ( i = 0; i < SIZE; ++i ) {
     if ( i % 10 == 0 )
        cout << '\n' << setw( 2 ) << i << ' ';

     cout << setiosflags( ios::internal | ios::showpos )
          << setw( 5 ) << setfill( '0' ) << memory[ i ] << ' '
          << resetiosflags( ios::internal | ios::showpos );

   }
  
    cout << endl;
}

bool validWord( int word )
 {
   return word >= MIN_WORD && word <= MAX_WORD;
 }

void output( const char * const sPtr, int width, int value, bool sign )
{       
    // format of "accumulator", etc.
   cout << setfill( ' ' ) << setiosflags( ios::left ) << setw( 20 )
        << sPtr << ' ';
   
   // is a +/- sign needed?
   if ( sign )
      cout << setiosflags( ios::showpos | ios::internal );

   // setup for displaying accumulator value, etc.
    cout << resetiosflags( ios::left ) << setfill( '0' );

    // determine the field widths and display value
    if ( width == 5 )
       cout << setw( width ) << value << '\n';
   else  // width is 2
      cout << setfill( ' ' ) << setw( 3 ) << ' ' << setw( width )
           << setfill( '0' ) << value << '\n';

    // disable sign if it was set
   if ( sign )
     cout << resetiosflags( ios::showpos | ios::internal );
 }
       

 

 

阅读(1422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~