表达式求值

status StackInit(SqSTACK* stack)
{
	stack->base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(int));
	if(!stack->base)
	{
		return ERROR;
	}
	stack->top = stack->base;
	stack->stackSize = STACK_INIT_SIZE;
	return OK;
}
status Push(SqSTACK* stack,ElemType e)
{
	if(stack->top - stack->base >= stack->stackSize)
	{
		ElemType* preBase = stack->base;
		printf("over flow! top = %p memory reallocing ...\n",stack->top);
		stack->base = (ElemType*)realloc(stack->base,(stack->stackSize + STACK_INCREMENT)*sizeof(ElemType));

		if(!stack->base)
		{
			perror("realloc");
			return ERROR;
		}
		stack->stackSize += STACK_INCREMENT;
		stack->top = stack->base + (stack->top - preBase);
	}
	
	*(stack->top) = e;
	stack->top +=1;
	return OK;
}
ElemType Pop(SqSTACK* stack)
{
	ElemType pop;
	if(stack->base == stack->top)
	{
		printf("error:stack empty!\n");
		return (ERROR);
	}
	
	pop = * -- stack->top;
	return pop;
}
status StackDestroy(SqSTACK* stack)
{
	free(stack->base);
	return 0;
}
status StackIsEmpty(SqSTACK* stack)
{
	if(stack->base == stack->top)
	{
		return TRUE;
	}
	return FALSE;
}

ElemType GetTop(SqSTACK* stack)
{
	if(stack->top == stack->base)
	{
		return ERROR;
	}
	return *(stack->top-1);
}

status IsIn(ElemType e)
{
	switch(e)
	{
		case '+':
		case '-': 
		case '*':
		case '/': 
		case '=':
		case '#':
		case '(':
		case ')':
			return TRUE;
			break;
		default:
			break;
	}
	return FALSE;
}

int GetIndex(ElemType x)
{
	int ret = -1;
	switch(x)
	{
		case '+':ret = 0;break;
		case '-':ret = 1;break;
		case '*':ret = 2;break;
		case '/':ret = 3;break;
		case '(':ret = 4;break;
		case ')':ret = 5;break;
		case '#':ret = 6;break;
		default:
			break;
	}
	return ret;
}

ElemType Precede(ElemType a,ElemType b)
{
	ElemType priority[7][7] = {
			{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},
			{'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},
			{'<','<','<','<','<','=','/'},{'>','>','>','>','/','>','>'},
			{'<','<','<','<','<','/','='}
	};
	ElemType oprant[] = {'+','-','*','/','(',')','#'};
	int ia = 0,ib = 0;
	ia = GetIndex(a);
	ib = GetIndex(b);

	return priority[ia][ib];
}

ElemType OperationSwitch(ElemType a1,ElemType op,ElemType a2)
{
	ElemType ret=0;
	switch(op)
	{
		case '+':ret= a1+a2;	break;
		case '-':ret= a1-a2;	break;
		case '*':ret= a1*a2;	break;
		case '/':ret= a1/a2;	break;
		default:
			break;
	}
	return ret;
}
ElemType EvaluateExpression()
{
	SqSTACK stack_oprand,stack_operator;
	ElemType ch[100],*pch,toprand,toperator1,toperator2,ret;
	StackInit(&stack_oprand);
	StackInit(&stack_operator);
	memset(ch,0,sizeof(ch));
	fgets(ch,100,stdin);
	ch[strlen(ch)-1] = '\0';
	pch = ch;
	Push(&stack_oprand,'#');

	while(1)
	{
		//printf("%c",*pch);
		if( !IsIn(*pch))
		{
			*pch = *pch - '0';
			//printf("=%d ",*pch);
			Push(&stack_operator,*pch); 

		}//operator
		else//operand
		{
			switch(Precede(GetTop(&stack_oprand),*pch))
			{
				case '<':
					Push(&stack_oprand,*pch);	
					break;
				case '=':
					Pop(&stack_oprand);
					break;
				case '>': 
					toprand = Pop(&stack_oprand);
					toperator1 = Pop(&stack_operator);
					toperator2 = Pop(&stack_operator);
					ret = OperationSwitch(toperator1,toprand,toperator2);
					Push(&stack_operator,ret);
					if(*pch != '#' && *pch != ')'){
						Push(&stack_oprand,*pch);
					}
					break;
				case '/':
					Pop(&stack_oprand);
					break;
			}
		}
		if(*pch != '#')	{pch++;	}
		else if(GetTop(&stack_oprand) == '#'){break;}
	}

	ret = GetTop(&stack_operator);
	return ret;
}


<pre name="code" class="cpp">int DecNotationExchange(int dec,int N)
{
	int ox = 1;
	SqSTACK stack;
	if(dec <1)
	{
		return 0;
	}
	StackInit(&stack);
	
	while(dec)
	{
		Push(&stack,dec%N);
		dec = dec/N;
	}
	if (!StackIsEmpty(&stack))
	{
		ox = Pop(&stack);
	}
	while(!StackIsEmpty(&stack))
	{
		ox = ox *10 + Pop(&stack);
	}
	StackDestroy(&stack);	
	return ox;
}
void StackTest(){while(1){ //printf("Dec %d to .. is:%d\n",10,DecNotationExchange(10,2)); ElemType ret = EvaluateExpression(); printf("\n\nret = %d\n",ret);}}
 


int DecNotationExchange(int dec,int N)
{
	int ox = 1;
	SqSTACK stack;
	if(dec <1)
	{
		return 0;
	}
	StackInit(&stack);
	
	while(dec)
	{
		Push(&stack,dec%N);
		dec = dec/N;
	}
	if (!StackIsEmpty(&stack))
	{
		ox = Pop(&stack);
	}
	while(!StackIsEmpty(&stack))
	{
		ox = ox *10 + Pop(&stack);
	}
	StackDestroy(&stack);	
	return ox;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZenZenZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值