程序设计要求
- 设计一个Card类来表示单张扑克牌的逻辑模型。如下类图所示。
- 设计一个Deck类用来表示一副52张的扑克牌,公有接口必须包括生成扑克牌、显示扑克牌、洗牌等这样的成员函数。如下类图所示。
- 程序主函数如下:
int main() {
Deck deck;
deck.MakeDeck();
deck.ShowDeck();
return 0;
}
- 运行程序能输出一副扑克牌
-
View Code
#include <cstring> #include <iostream> #include <time.h> using namespace std; char Suit[4] = {5,4,3,6}; char Face[15][3] = {"0","0","2","3","4","5","6","7","8","9","10","J","Q","K","A"}; class Card { private: int face,suit; public: Card(); void Setsuit(int); void Showcard(); void Setface(int); int getsuit(); int getface(); }; Card::Card() { suit = NULL; face = NULL; } void Card::Setface(int x) { face = x; } void Card::Setsuit(int x) { suit = x; } int Card::getsuit() { return suit; } int Card::getface() { return face; } void Card::Showcard() { cout << Suit[suit]; cout << Face[face]; } class Deck { private: Card cards[52]; public: Deck(); void MakeDeck(); void ShowDeck(); void Shuffle(); }; Deck::Deck() { for(int i = 0; i < 52; i++) { cards[i].Setsuit(0); cards[i].Setface(0); } } void Deck::Shuffle() { //洗牌,随机化算法 } void Deck::ShowDeck() { for(int i = 1; i < 53; i++) { cards[i-1].Showcard(); if(i % 13 == 0 && i != 0) cout << endl; else cout << " "; } cout << endl; } void Deck::MakeDeck() { int k = 0; int j = 2; for(int i = 0; i < 52; i++) { if(j == 15) { k++; j = 2; } cards[i].Setsuit(k); cards[i].Setface(j++); } } int main() { Deck deck; deck.MakeDeck(); deck.ShowDeck(); return 0; }