请帮助(评估后缀表达式) [英] Pls need help in (evaluating postfix expressions)

查看:58
本文介绍了请帮助(评估后缀表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这个程序>>我想添加这两个运算符(^和%)但我不知道如何

这是程序它的工作当我不写字符串post_exp(%或^)但我想写它们请帮帮我



我尝试过的事情:



hello i have this program >>i want to add this two Operator (^ and %) but i dont know how
this is the program its work when i dont write in the string post_exp (% or ^) but i want to write them pls help me

What I have tried:

// CPP Program to convert postfix to prefix
#include <iostream>
#include <stack>
using namespace std;

// function to check if character is operator or not
bool isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
case '^':
case '%':
	return true;
}
return false;
}

// Convert postfix to Prefix expression
string postToPre(string post_exp) {
stack<string> s;

// length of expression
int length = post_exp.size();

// reading from right to left
for (int i = 0; i < length; i++) {

	// check if symbol is operator
	if (isOperator(post_exp[i])) {

	// pop two operands from stack
	string op1 = s.top();
	s.pop();
	string op2 = s.top();
	s.pop();
	

	// concat the operands and operator
	string temp = post_exp[i] + op2 + op1;

	// Push string temp back to stack
	s.push(temp);
	}

	// if symbol is an operand
	else {

	// push the operand to the stack
	s.push(string(1, post_exp[i]));
	}
}

// stack[0] contains the Prefix expression
return s.top();
}

// Driver Code
int main() {
string post_exp = "A%BC/-AK/L-*^";
cout << "Prefix : " << postToPre(post_exp);
return 0;
}

推荐答案

C ++运营商 - Google搜索 [ ^ ]


这篇关于请帮助(评估后缀表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆