-
#include <iostream>
-
using std::cout;
-
using std::ios;
-
#include <iomanip>
-
using std::setw;
-
using std::setprecision;
-
using std::setiosflags;
-
using std::resetiosflags;
-
#include <cstdlib>
-
#include <ctime>
-
void shuffle( int [][ 13 ] );
-
void deal( const int [][ 13 ], const char *[], const char *[], int [][ 2 ] );
-
void pair( const int [][ 13 ], const int [][ 2 ], const char *[] );
-
void threeOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
-
void fourOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
-
void flushHand( const int [][ 13 ], const int [][ 2 ], const char *[] );
-
void straightHand( const int [][ 13 ], const int [][ 2 ], const char *[],const char *[] );
-
int main()
-
{
-
const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" },
-
*face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Jack", "Queen","King" };
-
int deck[ 4 ][ 13 ] = { 0 }, hand[ 5 ][ 2 ] = { 0 };
-
srand( time( 0 ) );
-
shuffle( deck );
-
deal( deck, face, suit, hand );
-
pair( deck, hand, face );
-
threeOfKind( deck, hand, face );
-
fourOfKind( deck, hand, face );
-
flushHand( deck, hand, suit );
-
straightHand( deck, hand, suit, face );
-
return 0;
-
}
-
void shuffle( int wDeck[][ 13 ] )
-
{
-
int row, column;
-
for ( int card = 1; card <= 52; ++card ) {
-
do {
-
row = rand() % 4;
-
column = rand() % 13;
-
} while ( wDeck[ row ][ column ] != 0 );
-
wDeck[ row ][ column ] = card;
-
}
-
}
-
void deal( const int wDeck[][ 13 ], const char *wFace[],const char *wSuit[], int wHand[][ 2 ] )
-
{
-
int r = 0;
-
cout << "The hand is:\n";
-
for ( int card = 1; card < 6; ++card )
-
for ( int row = 0; row <= 3; ++row )
-
for ( int column = 0; column <= 12; ++column )
-
if ( wDeck[ row ][ column ] == card ) {
-
wHand[ r ][ 0 ] = row;
-
wHand[ r ][ 1 ] = column;
-
cout << setw( 5 ) << wFace[ column ]<< " of " << setw( 8 ) << setiosflags( ios::left )
-
<< wSuit[ row ] << ( card % 2 == 0 ? '\n' : '\t' )
-
<< resetiosflags( ios::left );
-
++r;
-
}
-
cout << '\n';
-
}
-
void pair( const int wDeck[][ 13 ], const int wHand[][ 2 ], const char *wFace[] )
-
{
-
int counter[ 13 ] = { 0 };
-
for ( int r = 0; r < 5; ++r )
-
++counter[ wHand[ r ][ 1 ] ];
-
cout << '\n';
-
for ( int p = 0; p < 13; ++p )
-
if ( counter[ p ] == 2 )
-
cout << "The hand contains a pair of " << wFace[ p ] << "'s.\n";
-
}
-
-
void threeOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wFace[] )
-
{
-
int counter[ 13 ] = { 0 };
-
for ( int r = 0; r < 5; ++r )
-
++counter[ wHand[ r ][ 1 ] ];
-
for ( int t = 0; t < 13; t++ )
-
if ( counter[ t ] == 3 )
-
cout << "The hand contains three " << wFace[ t ] << "'s.\n";
-
}
-
113
-
114 void fourOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
-
115 const char *wFace[] )
-
116 {
-
117 int counter[ 13 ] = { 0 };
-
118
-
119 for ( int r = 0; r < 5; ++r )
-
120 ++counter[ wHand[ r ][ 1 ] ];
-
121
-
122 for ( int k = 0; k < 13; ++k )
-
123 if ( counter[ k ] == 4 )
-
124 cout << "The hand contains four " << wFace[ k ] << "'s.\n";
-
125 }
-
126
-
127 void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
-
128 const char *wSuit[] )
-
129 {
-
130 int count[ 4 ] = { 0 };
-
131
-
-
-
void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wSuit[] )
-
{
-
int count[ 4 ] = { 0 };
-
for ( int r = 0; r < 5; ++r )
-
++count[ wHand[ r ][ 0 ] ];
-
for ( int f = 0; f < 4; ++f )
-
if ( count[ f ] == 5 )
-
cout << "The hand contains a flush of " << wSuit[ f ] << "'s.\n";
-
}
-
-
-
void straightHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],const char *wSuit[], const char *wFace[] )
-
{
-
int s[ 5 ] = { 0 }, temp;
-
for ( int r = 0; r < 5; ++r )
-
s[ r ] = wHand[ r ][ 1 ];
-
for ( int pass = 1; pass < 5; ++pass )
-
for ( int comp = 0; comp < 4; ++comp )
-
if ( s[ comp ] > s[ comp + 1 ] ) {
-
temp = s[ comp ];
-
s[ comp ] = s[ comp + 1 ];
-
s[ comp + 1 ] = temp;
-
}
-
if ( s[ 4 ] - 1 == s[ 3 ] && s[ 3 ] - 1 == s[ 2 ] && s[ 2 ] - 1 == s[ 1 ] && s[ 1 ] - 1 == s[ 0 ] ) {
-
cout << "The hand contains a straight consisting of\n";
-
for ( int j = 0; j < 5; ++j )
-
cout << wFace[ wHand[ j ][ 1 ] ] << " of " << wSuit[ wHand[ j ][ 0 ] ]<< '\n';
-
}
-
}
-
-
-
-
-
######################################################
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
-
-
#include <iostream>
-
using std::cout;
-
using std::endl;
-
#include <cstdlib>
-
#include <ctime>
-
#include <iomanip>
-
using std::setw;
-
const int RACE_END = 70;
-
void moveTortoise( int * const );
-
void moveHare( int * const );
-
void printCurrentPositions( const int * const, const int * const );
-
int main()
-
{
-
int tortoise = 1, hare = 1, timer = 0;
-
srand( time( 0 ) );
-
cout << "ON YOUR MARK, GET SET\nBANG !!!!"<< "\nAND THEY'RE OFF !!!!\n";
-
while ( tortoise != RACE_END && hare != RACE_END ) {
-
moveTortoise( &tortoise );
-
moveHare( &hare );
-
printCurrentPositions( &tortoise, &hare );
-
++timer;
-
}
-
if ( tortoise >= hare )
-
cout << "\nTORTOISE WINS!!! YAY!!!\n";
-
else
-
cout << "Hare wins. Yuch.\n";
-
cout << "TIME ELAPSED = " << timer << " seconds" << endl;
-
return 0;
-
}
-
void moveTortoise( int * const turtlePtr )
-
{
-
int x = 1 + rand() % 10;
-
if ( x >= 1 && x <= 5 )
-
*turtlePtr += 3;
-
else if ( x == 6 || x == 7 )
-
*turtlePtr -= 6;
-
else
-
++( *turtlePtr );
-
if ( *turtlePtr < 1 )
-
*turtlePtr = 1;
-
else if ( *turtlePtr > RACE_END )
-
*turtlePtr = RACE_END;
-
}
-
void moveHare( int * const rabbitPtr )
-
{
-
int y = 1 + rand() % 10;
-
if ( y == 3 || y == 4 )
-
*rabbitPtr += 9;
-
else if ( y == 5 )
-
*rabbitPtr -= 12;
-
else if ( y >= 6 && y <= 8 )
-
++( *rabbitPtr );
-
else if ( y > 8 )
-
*rabbitPtr -= 2;
-
if ( *rabbitPtr < 1 )
-
*rabbitPtr = 1;
-
else if ( *rabbitPtr > RACE_END )
-
*rabbitPtr = RACE_END;
-
}
-
void printCurrentPositions( const int * const snapperPtr, const int * const bunnyPtr )
-
{
-
if ( *bunnyPtr == *snapperPtr )
-
cout << setw( *bunnyPtr ) << "OUCH!!!";
-
else if ( *bunnyPtr < *snapperPtr )
-
cout << setw( *bunnyPtr ) << 'H' << setw( *snapperPtr - *bunnyPtr ) << 'T';
-
else
-
cout << setw( *snapperPtr ) << 'T' << setw( *bunnyPtr - *snapperPtr ) << 'H';
-
cout << '\n';
-
}
-
-
-
######################################################
-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
-
-
#include <iostream>
-
using std::cout;
-
using std::endl;
-
using std::cin;
-
using std::ios;
-
#include <iomanip>
-
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 );
-
}
#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 );
}