栈栈的应用队列优先队列内容摘要:

Missing operand! endl。 return False。 } opnd1 = ( )。 // fetch righthand operand if (()) { cerr Missing operand! endl。 return False。 } opnd2 = ( )。 // fetch lefthand operand return True。 } void Calculator::Compute(char op) { Boolean result。 double operand1, operand2。 result = GetTwoOperands(operand1, operand2)。 if (result == True) switch(op) { case 39。 +39。 : (operand2+operand1)。 break。 case 39。 39。 : (operand2operand1)。 break。 case 39。 *39。 : (operand2*operand1)。 break。 case 39。 /39。 : if (operand1 == ) { cerr Divide by 0! endl。 ( )。 } else (operand2/operand1)。 break。 case 39。 ^39。 : (pow(operand2,operand1))。 break。 } else ( )。 // error! clear calculator } Calculator::Calculator(void) {} void Calculator::Run(void) { char c。 double newoperand。 while(cin c, c != 39。 =39。 ) // read until 39。 =39。 (Quit) { switch(c) {case 39。 +39。 : case 39。 39。 : case 39。 *39。 : case 39。 /39。 : case 39。 ^39。 : Compute(c)。 break。 default: // not operator, must be operand。 put char back (c)。 // read the operand and store it on the stack cin newoperand。 Enter(newoperand)。 break。 } } if (!( )) cout ( ) endl。 } void Calculator::Clear(void) { ( )。 } include void main(void) { Calculator CALC。 ( )。 } /*Run 1 8 8 * 6 6 * + .5 ^ = 10 Run 2 3 4 + * Missing operand! 3 4 + 8 * = 56 Run 3 1 0 / = Divide by 0! */ 中缀表达式求值 定义表达式 等级 为表达式中 每个元素 赋一个 等级 表达式的 累计等级 必须为 0或 1 整个表达式的等级为 1 项 等级 数 1 正负 + 0 运算 + * / 1 左右括号( ) 0 5+3*6 读到负号时等级累计 1,出错 5+3 最后等级 0,出错 中缀表达式求值法 用 两个栈 :一个存放 操作数 一个存放 运算符 2+34*5= 3 2 + 操作数 读入“ ” 弹出两个 优先级与 +相同 操作数运算得到结 运算符 弹出 + 果 6入栈 6 操作数 运算符 “ ”入栈 5 4 * 操作数4入栈 5入栈 “*”优先级高于“ ” 运算符“ *”入栈 20 操作数 弹出“ *” 20入栈 弹出 5 4运算 运算符 6 操作数 弹出“ ” 14入栈 弹出 20 6运算 运算符 14 操作数 弹出结果 14 运算符定义运算符优先级 icp 栈外优先级 in ing priority isp 栈内优先级 in stack priority 运算符 icp isp 等级 + 1 1 1 * / 2 2 1 ( 3 1 0 ) 0 0 0 ifndef INFIX_MATH_OPERATIONS define INFIX_MATH_OPERATIONS // list of constants specifying specific error messages const int OperatorExpected = 0, OperandExpected = 1, MissingLeftParenthesis = 2, MissingRightParenthesis = 3, InvalidInput = 4。 // labels designating the parentheses characters const char leftparenthesis = 39。 (39。 , rightparenthesis = 39。 )39。 // a class that handles operators on the operator stack class MathOperator { char op。 int icp, isp。 public: MathOperator(void)。 MathOperator(char ch)。 int operator= (MathOperator a) const。 void Evaluate (Stackfloat amp。 OperandStack)。 char GetOp(void)。 }。 // default constructor MathOperator::MathOperator(void) {} MathOperator::MathOperator(char ch) { op = ch。 // assign operator switch(op) { case „+‟: case „‟: icp = 1。 isp = 1。 break。 case „*‟: case „/‟: icp = 2。 isp = 2。 break。 case „(‟: icp = 3。 isp = 1。 break。 case 39。 )39。 : icp = 0。 isp = 0。 break。 } } int MathOperator::operator= (MathOperator a) const { return isp =。 } void MathOperator::Evaluate ( Stackfloat amp。 OperandStack) { float operand1 = ( )。 float operand2 = ( )。 switch (op) // select operation { case 39。 +39。 : (operand2 + operand1)。 break。 case 39。 39。 : (operand2 operand1)。 break。 case 39。 *39。 : (operand2 * operand1)。 break。 case 39。 /39。 : (operand2 / operand1)。 break。 } } // return operator associated //with current object char MathOperator::GetOp(void) { return op。 } endif // INFIX_MATH_OPERATIONS include include include // used for function 39。 isdigit39。 pragma hdrstop include // includ。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。