Question

I want to insert a standard 52 card deck into a stack of arrays and then...

I want to insert a standard 52 card deck into a stack of arrays and then randomly output the deck of cards until the deck is empty. I need this to be implement in c++ using a class of a stacked array

please someone help me :(

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
using namespace std;

enum Suit{
   Spade,Heart ,Diamond,Club
};

template <class T>
class stack{
   private:
       int top;
       int capacity;
       T*arr;
      
   public:
       stack(int size){
           capacity = size;
           arr = new T[capacity];
           top=-1;
       }
       bool isEmpty(){
           return top==-1;
       }
       bool isFull(){
           return top==capacity-1;
       }
       T Top(){
           return arr[top];
       }
       void pop(){
           top--;
       }
       void push(T data){
           arr[++top]=data;
       }
};

class card{
   private:
   int number;
   enum Suit suit;
   string description;
  
   public:
       card(){}
       void setNumber(int n){
           number=n;
       }
       void setSuit(enum Suit s){
           suit=s;
       }
       void setDescription(){
           char ch ;
           string str="";
           if(number>=2&&number<=9)
               str+='0'+number;
          
           else if(number==1)str+="Ace";
           else if(number==10)str+="10";
           else if(number==11)str+="Jack";
           else if(number==12)str+="Queen";
           else if(number==13)str+="King";
          
           if(suit==0)description="Spade "+str;
           else if(suit==1)description="Heart "+str;
           else if(suit==2)description="Diamond "+str;
           else if(suit==3)description="Club "+str;
       }
      
       int getNumber(){
           return number;
       }
       enum Suit getSuit(){
           return suit;
       }
       string getDescription(){
           return description;
       }
};


int main(){
   card deck[52];
   stack<card> s(52);
  
   for(int i=0;i<13;i++){
       deck[i].setNumber(i+1);
       deck[i+13].setNumber(i+1);
       deck[i+26].setNumber(i+1);
       deck[i+39].setNumber(i+1);
      
       deck[i].setSuit(Spade);
       deck[i+13].setSuit(Heart);
       deck[i+26].setSuit(Diamond);
       deck[i+39].setSuit(Club);
      
       deck[i].setDescription();
       deck[i+13].setDescription();
       deck[i+26].setDescription();
       deck[i+39].setDescription();
      
   }
   for(int i=0;i<52;i++)s.push(deck[i]);
  
   while(!s.isEmpty()){
       card temp = s.Top();
       cout<<temp.getDescription()<<"\n";
       s.pop();
   }
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
I want to insert a standard 52 card deck into a stack of arrays and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT