Question: The task is to read in card structs from standard input and place such in to a frequency histogram (in C++). Input is to occur

The task is to read in card structs from standard input and place such in to a frequency histogram (in C++).

Input is to occur until EOF (end-of-file) or an input error occurs. After reading in all input, for each entry in the frequency histogram's map, output the following (always on its own line): occurs time(s) where is the card and is the number of times it occurs in the histogram. (The output for card is the same as how p7.cxx's card type is output.)

Tips To output the histogram, use iterators to iterate through the map (or a range-for loop).

NOTE: Use prefix ++ instead of postfix ++. Why? Prefix ++ and -- are more efficient --only use the postfix forms when they are absolutely needed.

Each element in the map is a std::pair and one can access the Key via .first or ->first and Value via .second or ->second.

Most of the code for this assignment is from the p7.cxx --but with the code handling cards removed and the code inside main() removed.

Essentially this assignment is writing code in main() to compute a frequency histogram of cards since the rest of the code has been provided. p7.cxx

#include #include #include #include using namespace std;

//------

struct card { enum class suit { club, spade, diamond, heart }; enum { ace=1, jack=10, queen=11, king=12 }; using number = int;

number num_; suit suit_; };

bool operator ==(card const& a, card const& b) { return a.num_ == b.num_ && a.suit_ == b.suit_; }

bool operator <(card const& a, card const& b) { return a.num_ < b.num_; }

istream& operator >>(istream& is, card& c) { // number followed by the suit (CSHD)... is >> c.num_;

char ch; if (is >> ch) { switch (ch) { case 'C': c.suit_ = card::suit::club; break; case 'S': c.suit_ = card::suit::spade; break; case 'H': c.suit_ = card::suit::heart; break; case 'D': c.suit_ = card::suit::diamond; break; default: is.setstate(ios::failbit); break; } } else is.setstate(ios::badbit); return is; }

ostream& operator <<(ostream& os, card const& c) { os << c.num_;

switch (c.suit_) { // card::suit allows us to access suit // suit::club allows us to access club // thus, card::suit::club lets us access club case card::suit::club: os << 'C'; break;

case card::suit::spade: os << 'S'; break;

case card::suit::diamond: os << 'D'; break;

case card::suit::heart: os << 'H'; break; }

return os; }

//------

FILL IN THE MAIN

PROGRAM INPUT:

$ cat input.dat

10D3S4H9C10D8H2D12S10D6H2D4H

$

PROGRAM OUTPUT:

$ ./a.out < input.dat

2D occurs 2 times

3S occurs 1 times

4H occurs 2 times

6H occurs 1 times

8H occurs 1 times

9C occurs 1 times

10D occurs 3 times

12S occurs 1 times

$

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!