CH3 中缀表达式(C++类模板)

CH3 中缀表达式(C++类模板),第1张

CH3 中缀表达式(C++类模板)

SeqStack.hpp

#include
using namespace std;

template 
class SeqStack
{
	T data[Maxsize];//存放栈元素的数组
	int top;//栈顶指针,指示栈顶元素在数组中的下标public:
public:
	SeqStack();//构造函数
	void Push(T x);//入栈
	int Stacktop();//返回栈顶元素
	T Pop();//出栈
	T Top();//取栈顶元素(元素并不出栈)
	bool Empty();//判断栈是否为空
	bool Full();
	int Length();
};

template
SeqStack::SeqStack()
{
	top = -1;
}

template
void SeqStack::Push(T x)
{
	if (top == Maxsize - 1)
	{
		cout << "上溢" << endl;
		exit(1);
	}
	data[++top] = x;
}

template
T SeqStack::Pop()
{
	if (top == -1)
	{
		cout << "下溢" << endl;
		exit(1);
	}
	top--;
	return data[top + 1];
}

template
T SeqStack::Top()
{
	return data[top];
}

template
int SeqStack::Stacktop()
{
	return top;
}

template
bool SeqStack::Full()
{
	if (top == Maxsize - 1)
		return true;
}

template
bool SeqStack::Empty()
{
	if (top == -1)
		return true;
}

template
int SeqStack::Length()
{
	return top + 1;
}

test.h

#include
#include"SeqStack.hpp"
using namespace std;

const char Oper[7][7] = { '>','>','<','<','<','>','>',
					   '>','>','<','<','<','>','>',
					   '>','>','>','>','<','>','>',
					   '>','>','>','>','<','>','>',
					   '<','<','<','<','<','=',' ',
					   '>','>','>','>',' ','>','>',
					   '<','<','<','<','<',' ','=' };
int OperOcca(char oper)
{
	switch (oper)
	{
	case '+':
		return 0;
	case '-':
		return 1;
	case '*':
		return 2;
	case '/':
		return 3;
	case '(':
		return 4;
	case ')':
		return 5;
	case 'n':
		return 6;
	}
}
char Precede(char a, char b)
{
	int m = OperOcca(a);
	int n = OperOcca(b);
	return Oper[m][n];
}
double Operate(double left, char a, double right)
{
	switch (a)
	{
	case '+':return left + right;
	case '-':return left - right;
	case '*':return left * right;
	case '/':return left / right;
	}
}
bool isCharacter(char ch)
{
	if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == 'n')
		return true;
	else
		return false;
}

class Calculate
{
	SeqStackOPTR; // *** 作符栈
	SeqStackOPND; // *** 作数栈
public:
	double Cal(string str);
};

double Calculate::Cal(string str)
{
	SeqStack OPTR;     // *** 作符栈
	SeqStack OPND;   //数栈
	OPTR.Push('n');
	cout << "请输入一个算术表达式:";
	char ch = getchar();
	bool flag = 0;
	while (ch != 'n' || OPTR.Top() != 'n')
	{
		if ((ch >= '0' && ch <= '9') || ch == '.')
		{
			double sum = 1;
			double num = 0;
			while ((ch >= '0' && ch <= '9') || ch == '.')
			{
				if (ch == '.')
				{
					flag = 1;
					ch = getchar();
					continue;
				}
				if (flag)
				{
					sum /= 10;
					num += sum * ((double)ch - '0');

				}
				else
					num = num * 10 + ch - '0';
				ch = getchar();
			}
			OPND.Push(num);
			flag = 0;
		}
		else if (isCharacter(ch))
		{
			char pre_op = OPTR.Top();
			switch (Precede(pre_op, ch))
			{
			case '<':
				OPTR.Push(ch);
				ch = getchar();
				break;
			case '=':
				OPTR.Pop();
				ch = getchar();
				break;
			case '>':
				double right;
				double left;
				right = OPND.Pop();
				left = OPND.Pop();
				char pre_op = OPTR.Pop();
				OPND.Push(Operate(left, pre_op, right));
				break;
			}
		}
		else
		{
			cout << "表达式有非法字符" << endl;
			exit(1);
		}
	}
	cout << "结果为:" << OPND.Top() << endl;
}

main.cpp

#include
#include"SeqStack.hpp"
#include"test.h"
using namespace std;

int main()
{
	string experssion;
	cout << "请输入一个算术表达式:" << endl;
	cin >> experssion;
	Calculate cal;
	cal.Cal(experssion);
	return 0;	
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存