枚举,结构体、、、

#include <stdio.h>
typedef enum{
	SI,	//这个枚举表示单牌
	DO		//这个枚举表示对牌
}CardType;

typedef enum{
	GT,	//这个表示大于
	EQ,	//这个表示等于
	LT,	//这个表示小于
	IC		//这个表示不能比较
}ResultType;

typedef struct{
	CardType type;
	unsigned short point;
	ResultType (*pCompare)(const void * pFirstCard,const void * pSecondCard);
}Card;

//这个函数用来比较一个单牌和另外一副牌的大小
ResultType sCompare(const void * pFCard,const void* pSCard)
{
	Card * pFirstCard = (Card *)pFCard;
	Card * pSecondCard = (Card *)pSCard;

	if (SI != pFirstCard->type)
	{
		return IC;
	}
	if (SI == pSecondCard->type)
	{
		if (pFirstCard->point > pSecondCard->point)
		{
			return GT;
		}
		else if (pFirstCard->point == pSecondCard->point)
		{
			return EQ;
		}
		else
		{
			return LT;
		}
	}
	else
	{
		return IC;
	}
}

//这个函数用于完成副对牌和两外一副牌之间的大小比较
ResultType dCompare(const void * pFCard,const void * pSCard)
{
	Card * pFirstCard = (Card *)pFCard;
	Card * pSecondCard = (Card *)pSCard;
	if (DO != pFirstCard->type)
	{
		return IC;
	}

	if (SI == pSecondCard->type)
	{
		return IC;
	}
	else
	{
		if (pFirstCard->point > pSecondCard->point)
		{
			return GT;
		}
		else if (pFirstCard->point == pSecondCard->point)
		{
			return EQ;
		}
		else
		{
			return LT;
		}
	}


}
//这里曾经发生过一个错误,ResultType写成ResultTyep
ResultType compare(const void * pFCard,const void * pSCard)
{
	const Card * pFirstCard = (Card*)pFCard;
	const Card * pSecondCard = (Card*)pSCard;
	if (SI == pFirstCard->type)
	{
		if (SI == pSecondCard->type)
		{
			if (pFirstCard->point > pSecondCard->point)
			{
				return GT;
			}
			else if (pFirstCard->point == pSecondCard->point)
			{
				return EQ;
			}
			else
			{
				return LT;
			}
		}
		else
		{
			return IC;
		}
	}
	else
	{
		if (SI == pSecondCard->type)
		{
			return IC;
		}
		else
		{
			if (pFirstCard->type > pSecondCard->type)
			{
				return GT;
			}
			else if (pFirstCard->type == pSecondCard->type)
			{
				return EQ;
			}
			else
			{
				return LT;
			}
		}
	}
}

int main()
{
	ResultType result = 0;
	Card firstCard = {SI,8,sCompare};
	Card secondCard = {SI,1,sCompare};
	result = firstCard.pCompare(&firstCard,&secondCard);
	if (GT == result)
	{
		printf("结果是大于\n");
	}
	else if (EQ == result)
	{
		printf("结果是等于\n");
	}
	else if (LT == result)
	{
		printf("结果是小于\n");
	}
	else
	{
		printf("不能比较\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值