小明交换牌
//小明和牌:名字 人:左手和右手 牌:花色和数字
// 交换:动词 动作:把左手牌给右手,把右手的牌给左手
交换前:左手有一张牌、右手有一张牌(抓牌)
C语言中可以把名词封装成结构体、动词封装成函数
代码:
#include <stdio.h>
//创建一个牌的结构体
typedef struct Poker{
//花色和数字
int color; //花色
int number; //牌的数字
}Poker;
//创建一个手的结构体
typedef struct Hands{
//手里有牌
Poker poker;
}Hands;
//创建一个人的类型
typedef struct Person{
//拥有左手和右手
Hands leftHand; //人的左手
Hands rightHand; //人的右手
}Person;
//左手拿牌
//Poker leftHandTakePoker(Poker poker){
//
// return poker;
//}
//交换牌
void changePoker(Poker* poker1 , Poker* poker2){
//交换牌
Poker temp = *poker1;
*poker1 = *poker2;
*poker2 = temp;
}
//牌花色的枚举
enum PokerColor{
HEI_TAO,
HONG_TAO,
MEO_HUA,
FANG_KUAI
};
//牌数字的枚举
enum PokerNumber{
A = 1,
J = 11,
Q,
K
};
//牌的所有花色
char* colors[4] = {"♠︎","♡","♣︎","♢"};
//牌的所有数字
char* numbers[] = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
void showPoker(Poker poker1, Poker poker2){
printf("左手:%s%s 右手:%s%s\n",colors[poker1.color],numbers[poker1.number], colors[poker2.color],numbers[poker2.number]);
}
int main(int argc, const char * argv[]) {
//创建两张牌分别是红桃K和黑桃A
Poker poker1 = {HONG_TAO,K}; //创建了红桃K
Poker poker2 = {HEI_TAO,A};
//创建两只手
Hands leftHand;
Hands rightHand;
//创建小明
Person xiaoming;
//小明有两只手
xiaoming.leftHand = leftHand;
xiaoming.rightHand = rightHand;
//用小明的手去抓牌
xiaoming.leftHand.poker = poker1;
xiaoming.rightHand.poker = poker2;
printf("======交换前=====\n");
showPoker(xiaoming.leftHand.poker, xiaoming.rightHand.poker);
//交换小明两只手里的牌
changePoker(&xiaoming.leftHand.poker, &xiaoming.rightHand.poker);
printf("======交换后======\n");
showPoker(xiaoming.leftHand.poker, xiaoming.rightHand.poker);
return 0;
}