欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

逆波兰计算器

程序员文章站 2022-04-01 18:44:34
逆波兰计算器所谓的逆波兰计算器,就是将我们常见的中缀表达式,转换为后缀表达式,来供计算机运算的计算器。适合栈刚入门做练习#include #include #include using namespace std;const int MaxSize = 30;const int Stack_Init_Size = 30;const int StackIncrement = 10;typedef...

逆波兰计算器

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

#include <iostream>
#include <cstdlib>
#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;
	cout << "Please enter the infix expression(# to stop): ";
	cin >> c;
	while('#' != c){
		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);
			}
		}
		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;
		}
		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;
}

本文地址:https://blog.csdn.net/weixin_45677702/article/details/108155696

相关标签: 数据结构 c++