放置在结构中的一副牌

Deck of Cards placed in structs

本文关键字:一副 结构      更新时间:2023-10-16

我想创建一个结构,里面有一副牌,但我不知道该怎么做。由于我试图使两个面都适合int值,因此当调用例如face = 3suit = 1时,它会给我:3 of Diamonds。

#include <iostream>
#include <cmath>
using namespace std;
struct cards {
    char suit[];
};

int main() {
     cards type = {
    '0','1','2','3','4'
 };
  cout << type.suit << endl;

}

我知道这是错误的,我不知道该怎么做。。。

来自您的原始代码:

const std::string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
const std::string facevalue[] = {
    "Two", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
};
struct card
{
    card(int index)
    : suit(index % 4)
    , rank(index % 13)
    {}
    int suit;
    int rank;
};
std::ostream& operator<<(std::ostream& os, const card& c)
{
    return os << facevalue[c.rank] + " of " + suit[c.suit];
}