逆波兰计算器

逆波兰计算器

所谓的逆波兰计算器,就是将我们常见的中缀表达式,转换为后缀表达式,来供计算机运算的计算器。
适合栈刚入门做练习

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;

const int MaxSize = 30;
const int Stack_Init_Size = 30;
const int StackIncrement = 10;

typedef char ElemType_c;
typedef double ElemType_d;
typedef struct{
	ElemType_c *top;
	ElemType_c *base;
	int StackSize;
}sqStack_c;
typedef struct{
	ElemType_d *top;
	ElemType_d *base;
	int StackSize;
}sqStack_d;

bool InitStack_c(sqStack_c *s);
bool InitStack_d(sqStack_d *s);
void Push_c(sqStack_c *s,ElemType_c e);
void Push_d(sqStack_d *s,ElemType_d e);
void Pop_c(sqStack_c *s,ElemType_c *e);
void Pop_d(sqStack_d *s,ElemType_d *e);
int LenStack_c(sqStack_c s);
int LenStack_d(sqStack_d s);

int main()
{
	sqStack_c s;
	InitStack_c(&s);
	ElemType_c c,e;
	ElemType_c arr[MaxSize];
	int i = 0;
	t1:
	cout << "Please enter the infix expression(# to stop): ";
	cin >> c;
	while('#' != c){
		bool flag = true;
		while(isdigit(c) || '.' == c){
			arr[i++] = c;
			arr[i] = '\0';
			cin >> c;
			if(!(isdigit(c) || '.' == c))
			{
				arr[i++] = ' ';
				arr[i] ='\0';
			}
		}
		if(')' == c)
		{
			Pop_c(&s,&e);
			while('(' != e){
				arr[i++] = e;
				arr[i++] = ' ';
				arr[i] = '\0';
				Pop_c(&s,&e);
				if(i > LenStack_c(s)){
					flag = false;
					break;
				}
			}
		}
		else if('+' == c || '-' == c)
		{
			if(!LenStack_c(s))
				Push_c(&s,c);
			else
			{
				do{
					Pop_c(&s,&e);
					if('(' == e)
						Push_c(&s,e);
					else
					{
						arr[i++] = e;
						arr[i++] = ' ';
						arr[i] = '\0';
					}
				}while('(' != e && LenStack_c(s));
				Push_c(&s,c);
			}
		}
		else if('*' == c || '/' == c || '(' == c)
		{
			Push_c(&s,c);
		}
		else if('#' == c)
		{
			break;
		}
		else
		{
			cerr << "Enter Error.\n";
			break;
		}
		if(!flag){
			memset(arr, 0, sizeof(arr));
			s.top = s.base;
			while( c != '#'){
				cin >> c;   // 吃掉尾部的#
			}
			goto t1;    // 重新来过
		}
		cin >> c;
	}
	while(LenStack_c(s)){
		Pop_c(&s,&e);
		arr[i++] = e;
		arr[i++] = ' ';
		arr[i] = '\0';
	}
	arr[i-1] = '#';

	// calc
	sqStack_d calc;
	InitStack_d(&calc);
	ElemType_d d,f;
	int j = 0, k = 0;
	char str[MaxSize];
	char oop = arr[j++];
	while('#' != oop){
		while(isdigit(oop) || '.' == oop){
			str[k++] = oop;
			str[k] = '\0';
			if(k >= MaxSize)
			{
				cerr << "Runout.\n";
				return -1;
			}
			oop = arr[j++];
			if(' ' == oop)
			{
				d = atof(str);
				Push_d(&calc,d);
				k = 0;
				break;
			}
		}
		switch(oop){
			case '+':	Pop_d(&calc,&d);
						Pop_d(&calc,&f);
						Push_d(&calc,f+d); break;
			case '-':	Pop_d(&calc,&d);
						Pop_d(&calc,&f);
						Push_d(&calc,f-d); break;
			case '*':	Pop_d(&calc,&d);
						Pop_d(&calc,&f);
						Push_d(&calc,f*d); break;
			case '/':	Pop_d(&calc,&d);
						Pop_d(&calc,&f);
						Push_d(&calc,f/d); break;
		}
		oop = arr[j++];
	}
	Pop_d(&calc,&d);
	cout.setf(ios_base::fixed);
	cout.setf(ios::showpoint);
	cout.precision(4);
	cout << "The final result is: " << d;
	return 0;
}

bool InitStack_c(sqStack_c *s)
{
	s->base = (ElemType_c *)malloc(Stack_Init_Size * sizeof(ElemType_c));
	if(!s->base)
		return false;
	s->top = s->base;
	s->StackSize = Stack_Init_Size;
	return true;
}

bool InitStack_d(sqStack_d *s)
{
	s->base = (ElemType_d *)malloc(Stack_Init_Size * sizeof(ElemType_d));
	if(!s->base)
		return false;
	s->top = s->base;
	s->StackSize = Stack_Init_Size;
	return true;
}

void Push_c(sqStack_c *s,ElemType_c e)
{
	if(s->top - s->base >= s->StackSize)
	{
		s->base = (ElemType_c *)realloc(s->base,(s->StackSize + StackIncrement) * sizeof(ElemType_c));
		if(!s->base)
			exit(EXIT_FAILURE);
		s->top = s->base + s->StackSize;
		s->StackSize = s->StackSize +StackIncrement;
	}
	*(s->top) = e;
	s->top++;
}

void Push_d(sqStack_d *s,ElemType_d e)
{
	if(s->top - s->base >= s->StackSize)
	{
		s->base = (ElemType_d *)realloc(s->base,(s->StackSize + StackIncrement) * sizeof(ElemType_d));
		if(!s->base)
			exit(EXIT_FAILURE);
		s->top = s->base + s->StackSize;
		s->StackSize = s->StackSize + StackIncrement;
	}
	*(s->top) = e;
	s->top++;
}

void Pop_c(sqStack_c *s,ElemType_c *e)
{
	if(s->top == s->base)
		return;
	*e = *--(s->top);
}

void Pop_d(sqStack_d *s,ElemType_d *e)
{
	if(s->top == s->base)
		return;
	*e = *--(s->top);
}

int LenStack_c(sqStack_c s)
{
	if(s.top == s.base)
		return 0;
	return s.top - s.base;
}

int LenStack_d(sqStack_d s)
{
	if(s.top == s.base)
		return 0;
	return s.top - s.base;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值