- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在对后缀表示法进行中缀。我的程序可以编译,尽管出于某种原因它不会接受任何中缀表达式,只接受后缀表达式。这与我想做的相反。这是我的程序:
#include <iostream>
#include <string>
#include <sstream>
#include "stack"
using namespace std;
string infixexpr (istream& in)
{
//Holds value in computation
stack<string> postfixstack;
//used to to read in characters from the expression
char ch;
// used to read in numbers from expression
int num;
// Used to remove infix expressions from stack
string lexpr, rexpr;
ch = in.peek();
while ( ch != EOF)
{
//If we have a whitespace character skip it and continue with
// the end of the loop.
if(isspace(ch))
{
ch = in.get();
ch =in.peek();
continue;
}
//nonspace character is next to input stream
// if the next character is a number read it and convert it
// to string then put the string onto the postfix stack
if (isdigit (ch))
{
in >> num;
// use to convert string
ostringstream numberstr;
// convert to number using sstream
numberstr << num;
// Push the representing string onto stack0
postfixstack.push(numberstr.str());
ch = in.peek();
continue;
}
// if operator pop the two postfix expressions
// stored on the stack, put the operator after
postfixstack.pop();
lexpr = postfixstack.top();
postfixstack.pop();
if (ch == '+' || ch == '-' || + ch == '*' || ch == '/' || ch == '%')
postfixstack.push(rexpr + " " + lexpr + " " + ch);
else
{
cout << "Error in input expression" << endl;
exit(1);
}
ch = in.get();
ch = in.peek();
}
return postfixstack.top();
}
int main()
{
string input;
cout << "Enter a infix expression to convert to postfix,"
<< " \nor a blank line to quit the program:";
getline(cin,input);
while (input.size() != 0 )
{
//convert string to a string stream
istringstream inputExpr(input);
cout << "the infix equavilent is: "
<< infixexpr(inputExpr) << endl;
cout << "Enter a infix Expression to evaluate: ";
getline(cin,input);
}
return 0;
}
例如程序运行是这样的:
如果您想查看我的堆栈类和 header ,如果这是问题所在,请告诉我。我可以在回复下发帖。
最佳答案
哦,天哪,从哪里开始。 你应该把这个带到Code Review Stack Exchange相反,但让我们开始吧:
关于c++ - 将中缀转换为后缀表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307452/
前缀表达式 前缀表达式又称波兰式,前缀表达式的运算符位于操作数之前。 例如: ( 3 + 4 ) × 5 − 6 (3+4)×5-6(3+4)×5−6 对应的前缀表达式就是 - × + 3 4 5 6
众所周知: ((.).(.)) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c 我可以像这样使用这个复合运算符前缀样式: ((.).(.)) f g 但看起
换句话说,我可以使用什么语法(如果有)来代替 XXX在过滤器的以下实现中: filter' :: (a -> Bool) -> [a] -> [a] filter' _ [] = [] fil
在 R 中,每当两个包定义相同的函数时,很容易指定使用哪个包 pkg::foo .但是当冲突的函数是中缀运算符时你怎么办,即使用 %% 定义? 例如,ggplot2和 crayon定义 %+% .有没
关于代码的简短介绍:我必须创建一个类来计算前缀、后缀或中缀表达式。它必须判断是否是pre/post/infix并将其转换为后缀,例如从'/x7'转换的代码中的prefixTOpostfix()(其他已
前缀表达式(波兰表达式) 前缀表达式又称波兰表达式,前缀表达式的运算符位于操作符之前,如(3+4)*5-6对应的前缀表达式就是- * + 3 4 5 6 前缀表达式的计算机求
我需要使用 VBA 对数学表达式进行标记。我有一个可行的解决方案,但正在寻找一种更有效的方法(可能是 RegExp)。 我当前的解决方案: Function TokeniseTheString(str
我正在编写一个包含如下函数的包: "%IN%" 0 当我 Build & Reload 包时(我使用 RStudio),这个函数不可用,与包中定义的所有其他函数相反。 我如何使它工作? 最佳答案 解
我一直在用 Java 开发表达式求值器,出于沮丧,我也来这里询问。到目前为止,我至少重写了 15 次,但每次都无济于事。 基本上我需要在前缀、中缀或后缀中获取一个字符串并将其计算为整数。该表达式可以使
C++ 中的运算符通常被认为是函数/方法的替代语法,尤其是在重载的上下文中。如果是这样,下面的两个表达式应该是同义词: std::cout & __out, char __c) operator& _
我是一名优秀的程序员,十分优秀!