请问用C语言怎么算24 尽量给我原代码 谢谢啊!!

来源:百度知道 编辑:UC知道 时间:2024/05/26 17:32:26

主要是使用逆波兰表达式的求值。欢迎测试和指正!

[code]
/* cal24.c
* given N integer numbers
* find one expression which produces value T using all
* the N numbers and {+,-,*,/} operators
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 4 /* number of cards */
#define T 24 /* target solution */
#define M 13 /* maximum card value */
#define STACK_SIZE (N+N-1)
#define EPS 1e-6
#define ADD M+1
#define SUB M+2
#define MUL M+3
#define DIV M+4

/* evaluate an expression in reverse Polish notation (RPN) */
double eval(int expr[])
{
double oprnds[N],oprnd1,oprnd2;
int top = -1,i,op;
for(i = 0;i<STACK_SIZE;++i){
op = expr[i];
if(op<=M){
oprnds[++top] = (double)op;
continue;
}
oprnd1 = oprnds[top--];
oprnd2 = oprnds[top-