已知一副扑克有52张牌,分为4种花色(suit):黑桃(Spades)、红桃(Hearts)、草花(Clubs)、方块(Diamonds)。每种花色又有13张牌面(face):A,2,3,4,5,6,7,8,9,10,Jack,Queen,King。请用结构体定义扑克牌类型,用结构体数组card表示52张牌,每张牌包括花色和牌面两个字符型数组类型的数据成员。 请使用下面的结构体编程: typedef struct card { char suit[10]; char face[10]; } CARD; 输出格式: "%9s%9s\n"
时间: 2023-10-08 11:06:27 浏览: 353
超漂亮水晶扑克花色,红桃,黑桃方块,梅花
以下是基于所给结构体的扑克牌定义和初始化代码:
```c
#include <stdio.h>
#include <string.h>
typedef struct card {
char suit[10];
char face[10];
} CARD;
int main() {
// 定义四种花色和13种牌面
char* suits[4] = {"Spades", "Hearts", "Clubs", "Diamonds"};
char* faces[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
CARD card[52]; // 定义52张牌的结构体数组
// 初始化52张牌
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 13; j++) {
strcpy(card[i*13+j].suit, suits[i]); // 将花色赋值给suit成员
strcpy(card[i*13+j].face, faces[j]); // 将牌面赋值给face成员
}
}
// 输出52张牌
for (i = 0; i < 52; i++) {
printf("%9s%9s\n", card[i].suit, card[i].face);
}
return 0;
}
```
输出结果为:
```
Spades A
Spades 2
Spades 3
Spades 4
Spades 5
Spades 6
Spades 7
Spades 8
Spades 9
Spades 10
Spades Jack
Spades Queen
Spades King
Hearts A
Hearts 2
Hearts 3
Hearts 4
Hearts 5
Hearts 6
Hearts 7
Hearts 8
Hearts 9
Hearts 10
Hearts Jack
Hearts Queen
Hearts King
Clubs A
Clubs 2
Clubs 3
Clubs 4
Clubs 5
Clubs 6
Clubs 7
Clubs 8
Clubs 9
Clubs 10
Clubs Jack
Clubs Queen
Clubs King
Diamonds A
Diamonds 2
Diamonds 3
Diamonds 4
Diamonds 5
Diamonds 6
Diamonds 7
Diamonds 8
Diamonds 9
Diamonds 10
Diamonds Jack
Diamonds Queen
Diamonds King
```
阅读全文