8588 表达式求值 (C++)使用stack()模板

8588 表达式求值 (C++)使用stack()模板,第1张

题目:
利用栈编写表达式求值程序:输入含有“+”、“-”、“*”、“/”四则运算的表达式,其中负数要用(0-正数)表示,并以=结束。要求输出表达式的值
输入格式
第一行:一个算术表达式
输出格式
第一行:算术表达式的值
输入样例
3*(9-7)=
输出样例
6

使用C++的stack模板进行优化,方便准备期末上机考试。

详解请见注释,有问题轻喷。

#include//表达式求值    
#include
#include
#include
using namespace std;

stack opter;    //运算符栈
stack opval;  // *** 作数栈

int Index(char theta)   //获取theta所对应的索引
{
	int index = 0;
	switch (theta)
	{
	case '+':
		index = 0;
		break;
	case '-':
		index = 1;
		break;
	case '*':
		index = 2;
		break;
	case '/':
		index = 3;
		break;
	case '(':
		index = 4;
		break;
	case ')':
		index = 5;
		break;
	case '=':
		index = 6;
	default:break;
	}
	return index;
}

char getPriority(char theta1, char theta2)   //获取theta1与theta2之间的优先级
{
	const char priority[][7] =     //算符间的优先级关系
	{
		{ '>','>','<','<','<','>','>' },/*     +   -   *   /   (   )   #      */
		{ '>','>','<','<','<','>','>' },/*     +   -   *   /   (   )   #      */
		{ '>','>','>','>','<','>','>' },/*     +   -   *   /   (   )   #      */
		{ '>','>','>','>','<','>','>' },/*     +   -   *   /   (   )   #      */
		{ '<','<','<','<','<','=','0' },/*     +   -   *   /   (   )   #      */
		{ '>','>','>','>','0','>','>' },/*     +   -   *   /   (   )   #      */
		{ '<','<','<','<','<','0','=' },/*     +   -   *   /   (   )   #      */
	};

	int index1 = Index(theta1);
	int index2 = Index(theta2);
	return priority[index1][index2];
}

double calculate(double b, char theta, double a)   //计算b 符号 a
{
	switch (theta)
	{
	case '+':
		return b + a;
	case '-':
		return b - a;
	case '*':
		return b * a;
	case '/':
		return b / a;
	default:
		break;
	}
}

double Answer()   //表达式求值
{
	opter.push('=');      //首先将'='入栈opter
	int counter = 0;      //添加变量counter表示有多少个数字相继入栈,实现多位数的四则运算
	char c = getchar();
	while (c != '=' || opter.top() != '=')   //终止条件
	{
		if (isdigit(c))   //如果c在'0'~'9'之间,isdigit判断是否是数字,直接调用C++函数
		{
			if (counter == 1)//counter==1表示上一字符也是数字,所以要合并,比如11*11,要算11,而不是单独的1和1
			{
				double t = opval.top();
				opval.pop();
				opval.push(t * 10 + (c - '0'));
				counter = 1;
			}
			else
			{
				opval.push(c - '0'); //将c对应的数值入栈opval
				counter++;
			}
			c = getchar();
		}
		else
		{
			counter = 0;   //counter置零
			switch (getPriority(opter.top(), c))   //获取运算符栈opter栈顶元素与c之间的优先级,用'>','<','='表示
			{
			case '<':               //<则将c入栈opter
				opter.push(c);
				c = getchar();
				break;
			case '=':/*   =将opter栈顶元素弹出,
					用于括号的处理  */
				opter.pop();
				c = getchar();
				break;
			case '>':               //>则计算
				char theta = opter.top();
				opter.pop();//符号
				double a = opval.top();
				opval.pop();//数字a
				double b = opval.top();
				opval.pop();//数字b
				opval.push(calculate(b, theta, a));//推入计算结果
			}
		}
	}
	return opval.top();   //返回opval栈顶元素的值
}

int main()
{
	double ans = Answer();
	cout << ans;
	return 0;
}

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

原文地址: https://www.outofmemory.cn/langs/1499209.html

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

发表评论

登录后才能评论

评论列表(0条)

保存