栈实现的简单计算器

这是一个使用栈结构实现的简单计算器,能够将输入的中缀表达式转换为后缀表达式,并计算后缀表达式的值。代码包括初始化栈、压栈、出栈等操作,以及中缀转后缀和后缀表达式计算的函数。
/*****************************************************************
                             栈实现的简单计算器
*****************************************************************/

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef char StackData;
typedef int StackData_cal;
typedef struct node                       //字符结点
{
    StackData data;	                      //结点数据	
    struct node * next;	                  //结点链指针
} StackNode;

typedef struct node_cal                  //整数结点
{
    StackData_cal data;	                 //结点数据	
    struct node_cal * next;	             //结点链指针
} StackNode_cal;
//**********************************************************
StackNode * InitStack(void) 
{
    StackNode *top;
    top=NULL;
	return (top);
}

int Push ( StackNode **top, StackData x ) 
{
    StackNode *p =(StackNode*)malloc(sizeof(StackNode));
    if(top==NULL)
    {
        *top=p;
		p->data = x;  
		p->next=NULL;
    }
	else
	{
		p->data = x;  
		p->next = *top;
		*top = p;  
	}
	return 1;
}
int StackEmpty (StackNode *top)
{
    return (top==NULL);
}

int Pop(StackNode **top,StackData * x) 
{
    if ( StackEmpty (*top) ) return 0;
    StackNode * p = (*top);
    *top = p->next;
    (*x) = p->data;
    free (p);
    return 1;
}     
StackData GetTop (StackNode *top) 
{
    StackData x ;
    if ( StackEmpty (top) ) return 0;
    x = top->data;
    return x;
}

//**********************************************************

//**********************************************************
StackNode_cal * InitStack_cal(void) 
{
    StackNode_cal *top;
    top=NULL;
	return (top);
}

int Push_cal ( StackNode_cal **top, StackData_cal x ) 
{
    StackNode_cal *p =(StackNode_cal*)malloc(sizeof(StackNode_cal));
    if(top==NULL)
    {
        *top=p;
		p->data = x;  
		p->next=NULL;
    }
	else
	{
		p->data = x;  
		p->next = *top;
		*top = p;  
	}
	return 1;
}
int StackEmpty_cal (StackNode_cal *top)
{
    return (top==NULL);
}

int Pop_cal(StackNode_cal **top,StackData_cal * x) 
{
    if ( StackEmpty_cal (*top) ) return 0;
    StackNode_cal * p = (*top);
    *top = p->next;
    (*x) = p->data;
    free (p);
    return 1;
}     
StackData_cal GetTop_cal (StackNode_cal *top) 
{
    StackData_cal x ;
    if ( StackEmpty_cal (top) ) return 0;
    x = top->data;
    return x;
}


/************************************************
         中缀表达式转为后缀表达式
************************************************/
char *RPN(char *ch)
{
    StackData x;
	StackNode *top;
	top=InitStack();
    char *tmp=(char *)malloc(100*sizeof(char));
    char *first;
	first=tmp;	
    while(*ch != '\0')
    {
        if(*ch>='0' && *ch<='9')
        {
            while(*ch>='0' && *ch<='9')
            {
				*tmp=*ch;
				tmp++;
				ch++;			
            }
			*tmp=' ';
			tmp++;
        }
		else
		{
		    if(*ch == '(')
		    {
				Push(&top,*ch);
				ch++;
		    }
			else
			{
			    if(StackEmpty (top))
			    {
					Push(&top,*ch);
					ch++;
			    }
				else
				{
				    if(*ch == ')')
				    {
						Pop(&top,&x);
						if(x!='(')
						{
							*tmp=x;
							tmp++;
						}
						else
						{
						    ch++;
						}
				    }
					else
					{
						while(GetTop(top)=='*' || GetTop(top)=='/'||
							((GetTop(top)=='+'||GetTop(top)=='-')&&(*ch=='+'||*ch=='-')))
					    {
					        Pop(&top,&x);
						    *tmp=x;
						    tmp++;
						    *tmp=' ';
						    tmp++;
					    }
						Push(&top,*ch);
						ch++;
					}
				}
			}
		}
    }
	while(!StackEmpty (top))
	{
		Pop(&top,&x);
		*tmp=x;
		tmp++;
	}
    *tmp='\0';
	return first;
}


/************************************************
                  后缀表达式计算结果
************************************************/
int calculate(char *rpn)
{
    StackData_cal tmp=0;
    StackData_cal tmp1=0,tmp2=0;
	StackNode_cal *top;
	top=InitStack_cal();
    while(*rpn!= '\0')
    {
        if(*rpn>='0' && *rpn<='9')
        {
            tmp=tmp*10+(*rpn-'0');
			rpn++;
			if(*rpn==' ')
			{ 
				Push_cal(&top,tmp);
				tmp=0;
				rpn++;
			}
        }
		else
		{
			if(*rpn==' ')
			{ 
				rpn++;
			}
			else
			{
			    switch(*rpn)
			    	{
			    	    case('+'):
							{
								Pop_cal(&top,&tmp2);
								Pop_cal(&top,&tmp1);
								tmp1=tmp1+tmp2;
								Push_cal(&top,tmp1);
								break;
							}
			    	    case('-'):
							{
								Pop_cal(&top,&tmp2);
								Pop_cal(&top,&tmp1);
								tmp1=tmp1-tmp2;
								Push_cal(&top,tmp1);
								break;
							}
			    	    case('*'):
							{
								Pop_cal(&top,&tmp2);
								Pop_cal(&top,&tmp1);
								tmp1=tmp1*tmp2;
								Push_cal(&top,tmp1);
								break;
							}
			    	    case('/'):
							{
								Pop_cal(&top,&tmp2);
								Pop_cal(&top,&tmp1);
								tmp1=tmp1/tmp2;
								Push_cal(&top,tmp1);
								break;
							}
						default:
							{
								break;
							}
			    	}
				rpn++;
			}
		}
    }

	Pop_cal(&top,&tmp);
	return tmp;
}

int main()
{
    char * ch=(char *)malloc(100*sizeof(char));
    char * rpn=(char *)malloc(100*sizeof(char));
    char c;
	int cal;
	while(1)
	{
		printf("INPUT:");
		scanf("%s",ch);
		rpn=RPN(ch);
		printf("%s\n",rpn);
		cal = calculate(rpn);
		printf("%s=%d\n",ch,cal);
	}
	return 0;	
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值