Card.cpp: The result should look like b. Implement printCard to neatly print the
ID: 3821141 • Letter: C
Question
Card.cpp:The result should look like b. Implement printCard to neatly print the face and suit of one Card object, and then a newline character. See the sample results to know exactly how it should work. Did you study the global array named faces and the function named suit at the top of the program? You will find that printCard is very easy to do if you use these features properly. c. Implement printCards to neatly print the first n elements of the array cards, each on a separate line. Of course you should use printcard to simplify this part. d. Implement cmpCard to compare two Card objects. A return of value of true means that c1 is less than c2, first with regard to its face value, and in the case of equal face values then secondly by the alphabetical order of the two suits (C before D before H before S)
Explanation / Answer
Answer:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//The card structure
struct Card {
char suit;
int face;
};
//array to help printing face values
string faces[] = { "", "", "deuce", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king", "ace"
};
//function to help in printing suit names
string suit(char s)
{
if(s == 'C') return "Clubs";
if(s == 'H') return "Hearts";
if(s == 'D') return "Diamonds";
return "Spades";
}
//function protoypes
void printCard(Card c);
void printCards(Card cards[], int n);
bool cmpCard(Card c1, Card c2);
int main() {
//Create and initialize an array of five cards
Card cards[] = {{'S', 14}, {'H', 12}, {'D', 7}, {'C', 7}, {'S', 11}};
//Test printCard by itself
cout<<"printing third card: ";
printCard(cards[2]);
//Test printCards for first two cards, then whole array
cout<<" printing first two cards: ";
printCards(cards, 2);
cout<<" printing all cards, unsorted: ";
printCards(cards, 5);
//Depend on cmpCard to sort array, then print again
sort(cards, cards + 5, cmpCard);
cout<<" printing all cards, sorted: ";
printCards(cards, 5);
return 0;
}
void printCard(Card c)
{
string cardValue = faces[c.face];
string cardSuit = suit(c.suit);
cout<<cardValue<<" of "<<cardSuit<<endl;
}
void printCards(Card cards[], int n)
{
for(int i=0; i<n; i++)
{
printCard(cards[i]);
}
}
bool cmpCard(Card c1, Card c2)
{
char suitOrder[] = {'C', 'D', 'H', 'S'};
int card1SuitIndex, card2SuitIndex;
if(c1.face < c2.face)
return true;
else if(c2.face > c1.face)
return false;
else
{
for(int i=0; i<4; i++)
{
if(c1.suit == suitOrder[i])
card1SuitIndex = i;
if(c2.suit == suitOrder[i])
card2SuitIndex = i;
}
if(card1SuitIndex < card2SuitIndex)
return true;
else
return false;
}
}
-----------------------------------------------------------------------------------------------
OUTPUT:
printing third card:
seven of Diamonds
printing first two cards:
ace of Spades
queen of Hearts
printing all cards, unsorted:
ace of Spades
queen of Hearts
seven of Diamonds
seven of Clubs
jack of Spades
printing all cards, sorted:
seven of Clubs
seven of Diamonds
jack of Spades
queen of Hearts
ace of Spades