Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program will consist of three files: 1. card.h : card class 2. stack.h : st

ID: 3758684 • Letter: T

Question

This program will consist of three files:

1. card.h: card class

2. stack.h: stack class

3. main.cpp: main program

card class

Create the following class representing a playing card:

class card

{

    private:

      char suit; // h, d, c, s

      int rank;   // 1: ace, 11: jack, 12: queen, 13: king

    public:

      // sets suit and rank to the given values

      void create (char s, int r);

      // prints a card in the form: Ace of

      void print ();

};

stack class

Make the following changes to the stack class written in class:

1. Include the card class in the stack.h file

2. Change the data type in the class to be an array of card type instead of char type. This requires changes in a number of places in the file.

3. Add a function called print that prints the elements in the stack, each card on a separate line, from the top element to the bottom element. print should not change the stack.

main program

The main program should make a standard deck of 52 cards (this will be a stack), print the deck, sort the deck, and print the deck again. You are required to write the following functions:

// puts the standard 52 cards into the stack of

// cards

void make_deck (stack& deck);

//

// mixes the cards in the deck using the following

// algorithm:

// declare two stacks temp1 and temp2

// repeat 100 times

// {

//     for each card in the deck

//     {

//       remove a card from the deck

//       randomly place that card on temp1 or temp2

//     }

//     move all the cards from temp1 back to deck

//     move all the cards from temp2 back to deck

// }

//         

void mix_deck (stack& deck);

Stack code below:

Explanation / Answer

Below is the required answer

// CARD CLASS
#include <iostream>
#include <clocale>
using namespace std;

const wchar_t heart[] = L"u2665";
const wchar_t diamond[] = L"u2666";
const wchar_t club[] = L"u2663";
const wchar_t spade[] = L"u2660";
setlocale(LC_ALL, "");

class card
{
private:
   char suit;
   int rank;
public:
   void create(char s, int r)
   {
   suit = s;
   rank = r;
   }

   void print()
   {
   string denom = NULL;
   if(rank == 1) denom = "Ace";
   else if(rank == 11) denom = "Jack";
   else if(rank == 12) denom = "Queen";
   else if(rank == 13) denom = "King";

   if(suit == 'h')
   {
       cout<< denom << " of ";
       wcout << heart << L' ';
   }
   if(suit == 'd')
   {
       cout<< denom << " of ";
       wcout << diamond << L' ';
   }
   if(suit == 'c')
   {
       cout<< denom << " of ";
       wcout << club << L' ';
   }
   if(suit == 's')
   {
       cout<< denom << " of ";
       wcout << spade << L' ';
   }
}

//-------------------

// STACK CLASS
#include <iostream>
using namespace std;

const int stack_size = 100;

class stack
{
private:
card data [stack_size]; // array holding the stack
int top; // index of the top of the stack
public:
stack ();
void push (char item, int r); // pushes item on top of stack
card pop (); // removes the item from the top of
// the stack and returns it
bool full (); // returns true if the stack is full
bool empty (); // returns true if the stack is empty
void print (); // prints the content of stack
};

stack::stack ()
{
top = -1;
}

void stack::push (char item, int r)
{
if (full ())
{
cout << " Stack Error: Trying to push on a full stack";
cout << " Try to push the value" << item;
}
else // OK to add the element
{
top++;
data[top].suit = item;
data[top].rank = r;
}
}

card stack::pop ()
{
if (empty ())
{
cout << " Stack error: trying to pop an empty stack";
cout << " Returning a ?";
return '?';
}
else // OK to pop the stack
{
top--;
return data[top + 1];
}
}

bool stack::full ()
{
return top == stack_size - 1;
}

bool stack::empty ()
{
return top == -1;
}

void stack::print()
{
for(int i=0; i<stack_size; i++)
{
   cout<<" Suit : "<< data[i].suit << " Rank : " << data[i].rank;
}
}

//--------------------

//MAIN PROGRAM

void make_deck(stack& deck)
{
int i;
for(i=0; i<13; i++)
{
   deck.data[i].suit = 'h';
   deck.data[i].rank = i+1;
}

for(i=13; i<26; i++)
{
   deck.data[i].suit = 'd';
   deck.data[i].rank = i+1;
}

for(i=26; i<39; i++)
{
   deck.data[i].suit = 'c';
   deck.data[i].rank = i+1;
}

for(i=39; i<52; i++)
{
   deck.data[i].suit = 's';
   deck.data[i].rank = i+1;
}
}

void mix_deck(stack &deck)
{
stack temp1, temp2;
int i, cnt1=0, cnt2=0;
card cTmp;
while(!deck.empty())
{
   cTmp = deck.pop();
   if(i%2 == 0)
   {
   temp1.data[cnt1++].suit = cTmp.suit;
   temp1.data[cnt1++].rank = cTmp.rank;
   }
   else
   {
   temp2.data[cnt2++].suit = cTmp.suit;
   temp2.data[cnt2++].rank = cTmp.rank;
   }
}

while(!temp1.empty())
{
   cTmp = temp1.pop();
   deck.push(cTmp.suit, cTmp.rank);
}
while(!temp2.empty())
{
   cTmp = temp2.pop();
   deck.push(cTmp.suit, cTmp.rank);
}
}

int main()
{
stack deck;
make_deck(deck);
mix_deck(deck);
deck.print();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote