24点游戏

game.h

/*
 * game.h
 *
 *  Created on: 2011-9-27
 *      Author: brunie
 */

#ifndef GAME_H_
#define GAME_H_

#define MAX 13
#define N	4
#define RES	24
#define E	(1E-6)

#include  	<stdlib.h>
#include	<stdio.h>
#include	<math.h>
#include 	<time.h>

typedef struct Node
{
	double res;
	struct Node *left;
	struct Node *right;
	char op;
}Node, *pNode;

int calculate(Node **list, int n);
int operate(double left, double right, double *res, int i, char *op);
void print_item(Node *p);
int play_game();

#endif /* GAME_H_ */


game.c

/*
 * game.c
 *
 *  Created on: 2011-9-27
 *      Author: brunie
 */

#include "game.h"

int num[N] = {8, 10 , 7 , 8};
pNode list[N];
int success = 0;

int operate(double left, double right, double *res, int i, char *op)
{
	switch(i)
	{
	case 0:
		*res = left + right;
		*op = '+';
		break;
	case 1:
		*res = left - right;
		*op = '-';
		break;
	case 2:
		*res = left * right;
		*op = '*';
		break;
	case 3:
		if(right == 0)
			return 0;
		else
			*res = left / right;
		*op = '/';
		break;
	}
	return 1;
}

int calculate(Node **list, int n)
{
	int i,j,k,t = 1;
	if(n == 1)
	{
		if(fabs(list[0]->res - RES) < E)
		{
			print_item(list[0]);
			printf("\n");
			success = 1;
		}
	}
	else
	{
		Node **next = (Node **)malloc((n - 1) * sizeof(pNode));
		pNode p = (pNode)malloc(sizeof(Node));
		next[0] = p;
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (j != i)
				{
					for ( k = 0; k < n; k++)
					{
						if (k != i && k != j)
							next[t++] = list[k];
					}
					for ( k = 0; k < 4; k++)
					{
						operate(list[i]->res, list[j]->res, &(p->res), k, &(p->op));
						p->left = list[i];
						p->right = list[j];
						calculate(next, n - 1);
					}
				}
			}// for j
		}// for i
	}//else
	return 1;
}

int priority(char op)
#include "game.h"
{
	if (op == 0)
		return 3;
	else if (op == '+' || op == '-')
		return 1;
	else
		return 2;
}

void print_item(Node *p)
{
	if (p->left == 0 && p->right == 0)
		printf("%d",(int)(p->res));
	else
	{
		if (priority(p->op) > priority(p->left->op))
		{
			printf("(");
			print_item(p->left);
			printf(")");
		}
		else
			print_item(p->left);

		printf(" %c ",p->op);
		if (priority(p->op) > priority(p->right->op))
		{
			printf("(");
			print_item(p->right);
			printf(")");
		}
		else
			print_item(p->right);
	}
}

void init()
{
	int i;
	for (i = 0; i < N; i++)
	{
		list[i] = (pNode)malloc(sizeof(Node));
		list[i]->left = 0;
		list[i]->right = 0;
		list[i]->res = num[i];
		list[i]->op = 0;
	}
}

int play_game()
{
	init();
	calculate(list, N);
	return success;
}

main.c

/*
 * main.c
 *
 *  Created on: 2011-9-27
 *      Author: brunie
 */

#include "game.h"

int main()
{
	setbuf(stdout,0);
	int status;
	status = play_game();

	if (status)
		printf("Successful!\n");
	else
		printf("Not exist!\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值