Sei sulla pagina 1di 13

----------------------- Page 1----------------------CS172 Pre-Lab1

1.

The program validate.ccp first ask user to enter a date as the format of mm dd yyyy, and then the program will judge whether the input is valid or not. If the format is qualified, then the input will be display as the form of mm/dd/yyyy.

2.

enum MonthsOfYear { JANUARY = 1, FEBRUARY = 2, MARCH

= 3,

APRIL

= 4, MAY

= 5, JUNE

= 6,

JULY

= 7, AUGUST

= 8, SEPTEMBER = 9,

OCTOBER = 10, NOVEMBER = 11, DECEMBER = 12

}; 3. 4. The name of the new type is MonthsOfYear The program will print out 4.

5.

The MonthOfYear will not function properly, because the elements in the enum type are not defined.

III.

switch (month) {

case JANUARY: case MARCH : case MAY : case JULY:

case AUGUST : case OCTOBER: case DECEMBER:

daysInMonth = 31;

break ;

case APRIL : case JUNE: case SEPTEMBER:

case NOVEMBER :

daysInMonth = 30;

break ;

case FEBRUARY:

daysInMonth = daysInFebruary;

break ;

default:

cout << "Invalid month: " << month << endl;

exit(1) ;

6. 7. 8. 9.

If the input month is not valid, the output will be Invalid month: The break works as the end of the switch. The output shows that Invalid day of month: 31 enum CardSuit {SPADES, HEARTS, CLUBS, DIAMONDS} ;

enum CardRank {ACE} ;

10enum CardRank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK,

QUEEN, KING} ;

11. void translateCard(int card, CardSuit &suit, CardRank &rank)

----------------------- Page 2----------------------{

int suit_number, rank_number;

suit_number = card / 12;

rank_number = card % 13;

switch(suit_number)

case 0 : suit = SPADES;

break ;

case 1 : suit = HEARTS ;

break ;

case 2 : suit = CLUBS ;

break ;

case 3 : suit = DIAMONDS ;

break ;

switch(rank_number)

case 0 : rank = ACE ;

break ;

case 1 : rank = TWO ;

break ;

case 2 : rank = THREE ;

break ;

case 3 : rank = FOUR ;

break ;

case 4 : rank = FIVE ;

break ;

case 5 : rank = SIX;

break ;

case 6 : rank = SEVEN;

break ;

case 7 : rank = EIGHT ;

break ;

case 8 : rank = NINE ;

break ;

case 9 : rank = TEN ;

break ;

case 10 : rank = JACK ;

break ;

case 11 : rank = QUEEN;

break ;

----------------------- Page 3----------------------case 12 : rank = KING ;

break ;

12. void printCard(CardSuit suit, CardRank rank)

string RANK;

string SUIT;

switch(rank)

case 0 : RANK = "ACE";

break ;

case 1 : RANK = "TWO";

break ;

case 2 : RANK = "THREE";

break ;

case 3 : RANK = "FOUR";

break ;

case 4 : RANK = "FIVE";

break ;

case 5 : RANK = "SIX";

break ;

case 6 : RANK = "SEVEN";

break ;

case 7 : RANK = "EIGHT";

break ;

case 8 : RANK = "NINE";

break ;

case 9 : RANK = "TEN";

break ;

case 10 : RANK = "JACK";

break ;

case 11 : RANK = "QUEEN";

break ;

case 12 : RANK = "KING";

break ;

switch(suit)

case 0 : SUIT = "SPADES";

break ;

----------------------- Page 4----------------------case 1 : SUIT = "HEARTS";

break ;

case 2 : SUIT = "CLUBS";

break ;

case 3 : SUIT = "DIAMONDS";

break ;

cout << RANK << " of " << SUIT << endl ;

Potrebbero piacerti anche