栈栈的应用队列优先队列内容摘要:
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。栈栈的应用队列优先队列
本资源仅提供20页预览,下载后可查看全文
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。
相关推荐
校园是我们成长的摇篮
生命的活物,温润可人 意醉情迷,物我两忘 ,主客观合一 , 更突出对康桥的依恋。 二、修辞 两个拟人 “招摇”:常含贬义,用“逍遥”写水草,显现出无拘无束,自由自在的情态。 这种“自在逍遥”的安闲,本是写人之词,用来写水草,又更显生动、形象。 “油油”是光润的样子,水流的样子,和悦的样子。 用它修饰“招摇”,不但增强了水草的自在感,而且还使我们想起了水草得以“招摇”的河水:微风轻拂,水波轻涌
校园网学生创新实践中心介绍
中心数据服务接口 短信与邮件平台接口 软件开发平台 开源软件 … 2 ④ 指导培训与资金支持 设立 “智慧校园”创新实践论坛 ,组织前沿讲座及技术培训 设立 “校园网学生创新实践基金” ,支持团队开展活动 聘请指导老师,对学生创新实践活动提供指导咨询 3 ④ 应用推广 校内应用推广 安徽省教育科研网网络中心 国内高校宣讲推广 • 教育网年会 •
标题:蒙昧中的觉醒课时:1教学目标:了解古代希腊智者学
名言及主张。 美德即知识 认识你自己 尊崇理性,追求思想自由 苏格拉底的哲学观点一: 美德即知识 “ 美德即知识 ” 是苏格拉底对 道德问题的思考 ,体现 反对智者忽视道德,追求功利 的主张。 《 熊与隐士 》 熊和隐士成了好朋友。 熊看到有苍蝇来打扰隐士睡觉,很生气。 它抱起一块大石头砸向落在隐士额角上的苍蝇。 苍蝇飞跑了,隐士的脑袋也成了两半。 熊品德好不好。 做了什么 “ 蠢事 ”