1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
Using command line arguments read in the file called Deck.txt
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
//Enum types for Card objects
enum CardSuit { CLUBS, DIAMONDS, HEARTS, SPADES };
enum CardRank {ACE = 1, TWO, THREE ,FOUR, FIVE ,SIX, SEVEN , EIGHT, NINE, TEN, JACK , QUEEN, KING };
class Card
{
public:
CardSuit suit;
CardRank rank;
};
//Create two string arrays to use for overloaded operator << function
string ranks[] = { "", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string suits[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
ostream& operator << (ostream & os, Card const& c) {
os << ranks[c.rank] << " of " << suits[c.suit];
return os;
}
void print(vector<Card> const& v)
{
for (size_t i = 0; i < v.size(); ++i)
{
cout << (i + 1) << "." << v[i] << endl;
}
cout << endl;
}
istream& operator >> (istream& is, Card& c)
{
if (!is)
return is;
string rank, garbage, suit;
is >> rank >> garbage >> suit;
for (unsigned i = 1; i < (sizeof(ranks) / sizeof(ranks[1])); ++i)
{
if (rank == ranks[i])
c.rank = CardRank(i);
//cout << c.rank << endl;
}
for (unsigned i = 0; i < (sizeof(suits) / sizeof(suits[0])); ++i)
{
if (suit == suits[i])
c.suit = CardSuit(i);
}
return is;
}
int main(int argc, char* argv[])
{
ifstream infile(argv[1]);
//Test for success
if (!infile)
{
cerr << "Input file opening failed.\n";
return EXIT_FAILURE;
}
//Load a vector with the Card data
vector<Card> deck;
Card c;
while (infile >> c)
{
deck.push_back(c);
}
print(deck);
// Remove every 3rd card from the deck. How many cards are left in the deck ?
cout << "removing every 3rd card from the deck" << endl;
vector<Card>::iterator it5 = deck.begin();
for (int unsigned i = 0; i < deck.size(); ++i)
{
if (i % 3 == 2)
{
it5 = deck.erase(it5 + (i), it5 + (i)+1 );
it5 = deck.begin();
//++it5;
}
/*
else
{
++i;
}
*/
}
cout << "deck after removing every third card " << endl;
print(deck);
cout << "deck size after removing every third card " << deck.size() << endl;
system("pause");
}
|