栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;	
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691496.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号