实验案例--发纸牌C++(模板类实现)
作者:
AmbitionX
,
2022-05-06 09:31:59
,
所有人可见
,
阅读 224
#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;
int sign[4][13] = {0};
string str1[4] = {"梅花", "黑桃", "红桃", "方块"};
string str2[13] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
class PlayCard
{
public:
PlayCard();
void SendCards(int n);
void PrintCards();
private:
string card[13];
int num;
};
PlayCard :: PlayCard()
{
num = 0;
}
void PlayCard :: SendCards(int n)
{
int i, j, k;
num = n;
memset(sign, 0, n);
srand(time(nullptr));
for (int k = 0; k < n; )
{
i = rand() % 4;
j = rand() % 13;
if (sign[i][j] == 1) continue;
else
{
card[k] = str1[i] + str2[j];
sign[i][j] = 1;
k ++;
}
}
}
void PlayCard :: PrintCards()
{
for (int k = 0; k < num; k++) cout << card[k] << "\t";
cout << endl;
}
int main()
{
int n;
PlayCard P{ };
cout << "输入要发牌的张数: ";
cin >> n;
P.SendCards(n);
P.PrintCards();
return 0;
}