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

Dear Chegg Experts, I am a beginner C++ programmer seeking help on my current as

ID: 3672098 • Letter: D

Question

Dear Chegg Experts,

I am a beginner C++ programmer seeking help on my current assignment on the following possible topics:

STL containers and the container templates.

I am grateful for any assistance.

An STL list application

In the lectures we instantiated an stl list of floats and, above it, built some insert() and remove() methods on the client side that managed the list in increasing sorted order. In this assignment we will do the same for Card objects, using a natural ordering and a random Card generator that will be provided at the end of this document.:

You will provide global scope insert(), remove() and removeAll() like in the lessons. They take an stl list of Cards (first parameter) and a Card (second parameter). This is very much like the iTunes example that follows the float example, so you'll want to study that.

Part 1: STL list specifications

There are very few changes you'll need to make to the overall design of what you saw:

bool removeAll(CardList &myList, Card &x) - Notice that this method returns a bool which should be true if x was in the list, and false if it was not.

For Cards, there is no < operator. Use compareTo() in your insert() and/or remove() methods, as needed.

Nothing should be changed in any of the classes or methods brought in from the earlier assignments. Everything you do should be added as client scope code.

Client notes for part 1

Instantiate about five to ten cards randomly, but produce duplicates of every card in the list: every card you generate goes into your list twice. Display the list immediately after you build it. (Note, you might have four, not two, of some cards because of generateRandomCard(), and that's fine. (Maybe even six or eight or ... .)

Display the list.

Next, pick up to half of the cards you inserted for removal, but do it as follows. If you instantiated five cards initially and put them into the list twice, then your list has 10 cards. Pick, say, two (or three or all five) distinct cards from your initial five and remove() all traces of each of those two cards. I don't want you to use removeAll() for this step. Instead use remove(), which only removes one instance of a card. So you'll have to use remove() on the same card repeatedly. I.e., if 3 of diamonds is a card, remove all copies of 3 of Diamonds, not just the first. Don't rely on any knowledge you might have about how many copies of the 3 of diamonds are in the list, but use an intelligent loop to keep remove()-ing it until the return value of remove() tells you the loop is done.

Display the list and confirm that it looks right after the removal.

Finally, test removeAll()'s return value somehow after the above test.

Submit an entire source and run.

Here are the additional Card class members and a new global scope random Card generator that you will need. Add this to the existing Card class and test it out with the provided main() to be sure it works. Then you can use this as a basis for this week's assignment.

Placed into my code below:

//

// main.cpp

//

//

// Created by Macintosh User on 2/26/16.

// Copyright © 2016 Macintosh User. All rights reserved.

//

#include <iostream>

#include <string>

#include <ctime>

#include <list>

using namespace std;

class Card

{

public:

enum Suit { clubs, diamonds, hearts, spades };

static char DEFAULT_VAL;

static Suit DEFAULT_SUIT;

static const int NUM_CARD_VALS = 13;

static const int NUM_CARD_SUITS = 4;

  

private:

char value;

Suit suit;

bool errorFlag;

  

public:

Card(char value = DEFAULT_VAL, Suit suit = DEFAULT_SUIT);

string toString();

bool set(char value = DEFAULT_VAL, Suit suit = DEFAULT_SUIT);

  

char getVal() { return value; }

Suit getSuit() { return suit; }

bool getErrorFlag() { return errorFlag; }

bool equals(Card card);

  

// comparison members and methods

const static char valueRanks[NUM_CARD_VALS];

const static Suit suitRanks[NUM_CARD_SUITS];

  

int compareTo(Card &other);

static int getSuitRank(Suit st);

static int getValueRank(char val);

  

// helpers

private:

bool isValid(char value, Suit suit);

};

// global scope method prototypes

typedef list<Card> CardList;

void showList(CardList &myList);

void insert(CardList &myList, Card &x);

bool remove(CardList &myList, Card &x);

bool removeAll(CardList &myList, Card &x);

// for easy comparisons

int operator==(Card first, Card other)

{ return first.compareTo(other) == 0; }

// for client Card generation

Card generateRandomCard();

char Card::DEFAULT_VAL = 'A';

Card::Suit Card::DEFAULT_SUIT = Card::spades;

// for comparisons -- ordering values and ranks

const char Card::valueRanks[NUM_CARD_VALS] // const forces correct # initializers

= { '2', '3', '4', '5', '6', '7', '8', '9', 'T',

'J', 'Q', 'K', 'A'};

const Card::Suit Card::suitRanks[NUM_CARD_SUITS] = {Card::clubs, Card::diamonds,

Card::hearts, Card::spades};

// main to test comparison mechanism and random card generation

int main()

{

int k;

Card card1, card2;

  

srand(time(NULL)); // or not, if you want repetition

  

cout << "should all be 0: ";

card1.set('A', Card::spades); card2.set('A', Card::spades);

cout << card1.compareTo( card2 ) << endl;

card1.set('4', Card::hearts); card2.set('4', Card::hearts);

cout << card1.compareTo( card2 ) << endl;

card1.set('T', Card::clubs); card2.set('T', Card::clubs);

cout << card1.compareTo( card2 ) << endl;

  

cout << "should all be < 0 : ";

card1.set('A', Card::clubs); card2.set('A', Card::spades);

cout << card1.compareTo( card2 ) << endl;;

card1.set('4', Card::hearts); card2.set('5', Card::hearts);

cout << card1.compareTo( card2 ) << endl;

card1.set('9', Card::hearts); card2.set('T', Card::clubs);

cout << card1.compareTo( card2 ) << endl;

  

cout << "should all be > 0 : ";

card1.set('A', Card::clubs); card2.set('K', Card::clubs);

cout << card1.compareTo( card2 ) << endl;

card1.set('6', Card::hearts); card2.set('5', Card::spades);

cout << card1.compareTo( card2 ) << endl;

card1.set('K', Card::diamonds); card2.set('K', Card::clubs);

cout << card1.compareTo( card2 ) << endl;

  

cout << " Some random cards: ";

for ( k = 0; k < 50; k++ )

{

cout << generateRandomCard().toString() << " ";

}

cout << endl << endl;

  

return 0;

}

// new global scope method -------------------------------------------

Card generateRandomCard()

{

Card::Suit suit;

char val;

  

suit = (Card::Suit) ( rand() % Card::NUM_CARD_SUITS );

val = Card::valueRanks[ rand() % Card::NUM_CARD_VALS ];

  

return Card(val, suit);

}

// Card comparison method definitions -------------------------------------------

int Card::compareTo(Card &other)

{

if (this->value == other.value)

return ( getSuitRank(this->suit) - getSuitRank(other.suit) );

// else

return getValueRank(this->value) - getValueRank(other.value) ;

}

int Card::getSuitRank(Suit st)

{

int k;

  

for (k = 0; k < NUM_CARD_SUITS; k++)

if (suitRanks[k] == st)

return k;

  

// should not happen

return 0;

}

int Card::getValueRank(char val)

{

int k;

  

for (k = 0; k < NUM_CARD_VALS; k++)

if (valueRanks[k] == val)

return k;

  

// should not happen

return 0;

}

/* ---------------------- run Card compareTo() test ------------------

should all be 0:

0

0

0

should all be < 0 :

-3

-1

-1

should all be > 0 :

1

1

1

Some random cards:

K of Diamonds 6 of Diamonds Q of Hearts 5 of Clubs A of Clubs 9 of Spades

5 of Spades J of Clubs K of Clubs Q of Spades T of Clubs K of Spades 2 of

Clubs Q of Clubs 7 of Spades 3 of Clubs T of Hearts K of Spades 5 of Diamo

nds A of Diamonds 4 of Spades 9 of Spades 3 of Clubs A of Clubs 9 of Diamo

nds 6 of Diamonds 5 of Clubs 9 of Spades A of Clubs Q of Diamonds 2 of Spa

des 7 of Diamonds K of Hearts 8 of Spades 3 of Diamonds 8 of Spades 6 of S

pades 7 of Hearts 6 of Clubs 6 of Diamonds Q of Diamonds A of Diamonds Q o

f Clubs 2 of Hearts 6 of Diamonds 9 of Diamonds 5 of Clubs 6 of Spades 7 o

f Diamonds Q of Clubs

Press any key to continue . . .

--------------------------------- */

That's it.

Thank you for your help. It is much appreciated

Explanation / Answer

Comments added .. and make bold what I modified in given code

#include <iostream>
#include <string>
#include <ctime>
#include <list>
using namespace std;

class Card
{
public:
enum Suit { clubs, diamonds, hearts, spades };
static char DEFAULT_VAL;
static Suit DEFAULT_SUIT;
static const int NUM_CARD_VALS = 13;
static const int NUM_CARD_SUITS = 4;
  
private:
char value;
Suit suit;
bool errorFlag;
  
public:
Card(char value = DEFAULT_VAL, Suit suit = DEFAULT_SUIT);
string toString();
bool set(char value = DEFAULT_VAL, Suit suit = DEFAULT_SUIT);
  
char getVal() { return value; }
Suit getSuit() { return suit; }
bool getErrorFlag() { return errorFlag; }
bool equals(Card card);
  
// comparison members and methods
const static char valueRanks[NUM_CARD_VALS];
const static Suit suitRanks[NUM_CARD_SUITS];
  
int compareTo(Card &other);
static int getSuitRank(Suit st);
static int getValueRank(char val);
  
// helpers
private:
bool isValid(char value, Suit suit);
};

// global scope method prototypes
typedef list<Card> CardList;
void showList(CardList &myList);
void insert(CardList &myList, Card &x);
bool remove(CardList &myList, Card &x);
bool removeAll(CardList &myList, Card &x);


//showing list
void showList(CardList &myList){
//iterating cards
for (iter=mylist.begin(); iter!=mylist.end(); ++iter)
cout<<" "<<*iter.value<<" "<<*iter.suit;
}
}
//method returns true if card 'x' presents in myList
bool removeAll(CardList &myList, Card &x){
//iterating cards
for (iter=mylist.begin(); iter!=mylist.end(); ++iter)
if(*iter.compareTo(x) == 0){ //cards are comparing .. using compareTo
return true;
}
}
//if card is not present in deck of cards
return false;
}

//adding new card to list
void insert(CardList &myList, Card &x){
myList.push_back(x);
//showing list
showList(myList);
}

bool remove(CardList &myList, Card &x){
//iterating cards
for (iter=mylist.begin(); iter!=mylist.end(); ++iter)
if(*iter.compareTo(x) == 0){ //cards are comparing .. using compareTo
myList.erase(x);
}
}

showList(myList);
//if all cards removed then returns true .. or return false
return removeAll(myList,x))
}

// for easy comparisons
int operator==(Card first, Card other)
{ return first.compareTo(other) == 0; }

// for client Card generation
Card generateRandomCard();

char Card::DEFAULT_VAL = 'A';
Card::Suit Card::DEFAULT_SUIT = Card::spades;

// for comparisons -- ordering values and ranks
const char Card::valueRanks[NUM_CARD_VALS] // const forces correct # initializers
= { '2', '3', '4', '5', '6', '7', '8', '9', 'T',
'J', 'Q', 'K', 'A'};
const Card::Suit Card::suitRanks[NUM_CARD_SUITS] = {Card::clubs, Card::diamonds,
Card::hearts, Card::spades};

// main to test comparison mechanism and random card generation
int main()
{
int k;
Card card1, card2;
  
srand(time(NULL)); // or not, if you want repetition
  
cout << "should all be 0: ";
card1.set('A', Card::spades); card2.set('A', Card::spades);
cout << card1.compareTo( card2 ) << endl;
card1.set('4', Card::hearts); card2.set('4', Card::hearts);
cout << card1.compareTo( card2 ) << endl;
card1.set('T', Card::clubs); card2.set('T', Card::clubs);
cout << card1.compareTo( card2 ) << endl;
  
cout << "should all be < 0 : ";
card1.set('A', Card::clubs); card2.set('A', Card::spades);
cout << card1.compareTo( card2 ) << endl;;
card1.set('4', Card::hearts); card2.set('5', Card::hearts);
cout << card1.compareTo( card2 ) << endl;
card1.set('9', Card::hearts); card2.set('T', Card::clubs);
cout << card1.compareTo( card2 ) << endl;
  
cout << "should all be > 0 : ";
card1.set('A', Card::clubs); card2.set('K', Card::clubs);
cout << card1.compareTo( card2 ) << endl;
card1.set('6', Card::hearts); card2.set('5', Card::spades);
cout << card1.compareTo( card2 ) << endl;
card1.set('K', Card::diamonds); card2.set('K', Card::clubs);
cout << card1.compareTo( card2 ) << endl;
  
cout << " Some random cards: ";
for ( k = 0; k < 50; k++ )
{
cout << generateRandomCard().toString() << " ";
}
cout << endl << endl;
  
return 0;
}

// new global scope method -------------------------------------------
Card generateRandomCard()
{
Card::Suit suit;
char val;
  
suit = (Card::Suit) ( rand() % Card::NUM_CARD_SUITS );
val = Card::valueRanks[ rand() % Card::NUM_CARD_VALS ];
  
return Card(val, suit);
}

// Card comparison method definitions -------------------------------------------
int Card::compareTo(Card &other)
{
if (this->value == other.value)
return ( getSuitRank(this->suit) - getSuitRank(other.suit) );
// else
return getValueRank(this->value) - getValueRank(other.value) ;
}

int Card::getSuitRank(Suit st)
{
int k;
  
for (k = 0; k < NUM_CARD_SUITS; k++)
if (suitRanks[k] == st)
return k;
  
// should not happen
return 0;
}

int Card::getValueRank(char val)
{
int k;
  
for (k = 0; k < NUM_CARD_VALS; k++)
if (valueRanks[k] == val)
return k;
  
// should not happen
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