This program will consist of three files: 1. card.h : card class 2. stack.h : st
ID: 3810142 • 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. You will use the print function in the card class to print the individual cards. 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. The order of the cards is not important
// as long as all 52 are there.
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.h: stack class
Explanation / Answer
C++ CODE:
1) Stack.h
// implementation file for the stack class
#include <iostream>
using namespace std;
#include "Card.h"
const int stack_size = 100;
class Stack
{
private:
Card deck[stack_size]; // array of stack elements
int top; // top is the index of the top element of the stack
public:
Stack (); // creates an empty stack
void push (Card c); // pushes item on top of stack
Card pop (); // removes and returns top of stack
bool empty (); // returns true if stack is empty
bool full (); // returns true if stack is full
void print();
};
// constructor creates an empty stack
Stack::Stack ()
{
top = -1;
}
// push adds item to the top of the stack
void Stack::push (Card c)
{
// if the stack is full, print an error message
if (full ())
{
cout << " Stack Class Error: Pushing on a full stack";
}
else // ok to push
{
top++;
deck [top] = c;
}
}
// pop removes and returns the top element of the stack
Card Stack::pop ()
{
// if the stack is empty, print an error message
if (empty ())
{
cout << " Stack Class Error: popping an empty stack";
}
else // ok to pop
{
top--;
return deck [top + 1];
}
}
// empty returns true if the stack is empty, else
// it returns false
bool Stack::empty ()
{
return top == -1;
}
// full returns true if the stack is full, else it
// returns false
bool Stack::full ()
{
return top == stack_size - 1;
}
void Stack::print(){
int i;
for(i=0;i<=top;i++){
deck[i].print();
cout<<endl;
}
}
2)Card.h
#include <iostream>
using namespace std;
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){
suit=s;
rank=r;
}
// prints a card in the form: Ace of
void print (){
if(rank==1){
if(suit=='h')
cout<<"Ace of "<< "u2665";
else if(suit=='d')
cout<<"Ace of "<<"u2666";
else if(suit=='c')
cout<<"Ace of "<<"u2663";
else
cout<<"Ace of "<<"u2660";
}else if(rank==11){
if(suit=='h')
cout<<"Jack of "<<"u2665";
else if(suit=='d')
cout<<"Jack of "<<"u2666";
else if(suit=='c')
cout<<"Jack of "<<"u2663";
else
cout<<"Jack of "<<"u2660";
}else if(rank==12){
if(suit=='h')
cout<<"Queen of "<<"u2665";
else if(suit=='d')
cout<<"Queen of "<<"u2666";
else if(suit=='c')
cout<<"Queen of "<<"u2663";
else
cout<<"Queen of "<<"u2660";
}else if(rank==13){
if(suit=='h')
cout<<"King of "<<"u2665";
else if(suit=='d')
cout<<"King of "<<"u2666";
else if(suit=='c')
cout<<"King of "<<"u2663";
else
cout<<"King of "<<"u2660";
}else {
if(suit=='h')
cout<<rank<<" of "<<"u2665";
else if(suit=='d')
cout<<rank<<" of "<<"u2666";
else if(suit=='c')
cout<<rank<<" of "<<"u2663";
else
cout<<rank<<" of "<<"u2660";
}
}
};
3) main.cpp
#include <iostream>
using namespace std;
#include "Stack.h"
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
void make_deck (Stack& deck);
void mix_deck (Stack& deck);
int main(){
Stack deck;
make_deck(deck);
cout<<"Printing deck after make_deck:"<<endl;
deck.print();
mix_deck(deck);
cout<<endl<<endl<<"printing deck after mix_deck:"<<endl;
deck.print();
}
void make_deck (Stack& deck){
int i,j;
for(i=0;i<4;i++){//run a loop for every suit
for(j=1;j<=13;j++){//iterate suit create card and push to deck
Card c;
if(i==0)
c.create('h',j);
if(i==1)
c.create('d',j);
if(i==2)
c.create('c',j);
if(i==3)
c.create('s',j);
deck.push(c);//pushing the created card to deck
}
}
}
void mix_deck (Stack& deck){
Stack temp1,temp2;//initialize two temporary stacks
srand (time(NULL));
int i;
for(i=0;i<100;i++){//repeat 100 times
while(!deck.empty())//repeat until deck is empty
{
if(rand()%2==0)//generate a random number 0 or 1
temp1.push(deck.pop());//if 0 pop from deck and push temp1
else
temp2.push(deck.pop());//if 1 pop from deck and psh to temp2
}
while(!temp1.empty()){//repeat until temp1 is empty
deck.push(temp1.pop());// pop from temp1 and push to deck
}
while(!temp2.empty()){//repeat until temp2 is empty
deck.push(temp2.pop());//pop from temp2 and push to deck
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.