资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C+之父表达式计算例子Analyse.h#include#include#includeusing namespace std;enum Token_valueNAME, NUMBER, END,PLUS=+, MINUS=-, MUL=*, DIV=/,PRINT=;, ASSIGN=, LP=(,RP=);Token_value curr_tok = PRINT;double number_value;string string_value;maptable;/-error_status-int no_of_errors;double error(const string& s)no_of_errors+;cerrnumber_value;return curr_tok = NUMBER;default:if(isalpha(ch)string_value = ch;while(cin.get(ch) & isalnum(ch)string_value.push_back(ch);cin.putback(ch);return curr_tok = NAME;error(非法变量名);return curr_tok = PRINT;/-analyse-double prim(bool get);double term(bool get)double left = prim(get);for(;)switch(curr_tok)case MUL: left *= prim(true);break;case DIV:if(double d = prim(true)left /= d;break;return error(除以 0 溢出);default:return left;double expr(bool get)double left = term(get);for(;)switch(curr_tok)case PLUS:left += term(true);break;case MINUS:left -= term(true);break;default:return left;double prim(bool get)if(get)get_token();switch(curr_tok)case NUMBER:double v = number_value;get_token(); return v;case NAME: double& v = tablestring_value;if(get_token() = ASSIGN)v = expr(true);return v;case MINUS: return -prim(true);case LP: double e = expr(true);if(curr_tok != RP)return error(没有匹配右括号 );get_token();return e;default:return error(初等项异常);main.cpp#includeAnalyse.hint main()tablepi = 3.1415926535897932385;tablee = 2.7182818284590452354;while(cin)get_token();if(curr_tok = END)break;if(curr_tok = PRINT)continue;cout 4. #include 5. #include 6. #include 7. #include 8. using namespace std;9.10. bool isLetter(char c);11. bool isOperator(char c);12. bool isBracket(char c);13. int getToken(const string& expr, string& token, size_t& idx);14. int getOperatorPriority(const string& op);15. int infix_to_postfix(const string& infix, string& postfix);16. int compute_postfix(const string& postfix_expr, double& result);17.18. int main()19. 20. ifstream cin(input.txt); / input 21. ofstream cout(output.txt); / output 22. string infix_expr, postfix_expr;23. double result = 0.0;24.25. while (getline(cin, infix_expr) 26. if (infix_to_postfix(infix_expr, postfix_expr) 27. if (compute_postfix(postfix_expr, result) 28. cout stk;43. int balance = 0; / use to check the brackets balance. 44.45. postfix_expr.clear();46. while (getToken(infix_expr, token, idx) 47. switch (token0) 48. /* If we see +,-,*,/,%,( , then we pop entries from49. * the stack until we find an entry of lower priority50. * One exception is that we never remove a ( from the stack except when processing a ).*/51. case +:52. case -:53. case *:54. case /:55. case %:56. case (:57. if (token0 = ()58. +balance;59. while (!stk.empty() & getOperatorPriority(stk.top() = getOperatorPriority(token) & stk.top() != () 60. postfix_expr += stk.top();61. stk.pop();62. postfix_expr += ;63. 64. stk.push(token);65. break;66. /* right association, handle it specifically! */67. case :68. while (!stk.empty() & getOperatorPriority(stk.top() getOperatorPriority(token) & stk.top() != () 69. postfix_expr += stk.top();70. stk.pop();71. postfix_expr += ;72. 73. stk.push(token); / later come, early out.(right association) 74. break;75. /* If we see a ), then we pop the stack, writing symbols until we encounter76. * a (corresponding) left parenthesis, which is poped but not output. */77. case ):78. -balance;79. while (!stk.empty() & stk.top() != () 80. postfix_expr += stk.top();81. stk.pop();82. postfix_expr += ;83. 84. stk.pop();85. break;86. default:87. postfix_expr += token;88. postfix_expr += ;89. break;90. 91. 92. while (!stk.empty() 93. postfix_expr += ;94. postfix_expr += stk.top();95. stk.pop();96. 97. if (balance != 0) 98. return 0;99. 100.101. return 1;102. 103.104. int compute_postfix(const string& postfix_expr, double& result)105. 106. stack stk;107. string token;108. size_t idx = 0;109. double operand1 = 0.0, operand2 = 0.0;110. char buf128 = 0;111. int resStatus = 0;112.113. while (getToken(postfix_expr, token, idx) 114. memset(buf, 0, 128 * sizeof(char);115. switch (token0) 116. case +:117. case -:118. case *:119. case /:120. case %:121. case :122. / firstly, get two operands to compute. 123. operand1 = atof(stk.top().c_str();124. stk.pop();125. operand2 = atof(stk.top().c_str();126. stk.pop();127. switch (token0) 128. case +:129. sprintf(buf, %f, operand1 + operand2);130. break;131. case -:132. sprintf(buf, %f, operand2 - operand1);133. break;134. case *:135. sprintf(buf, %f, operand1 * operand2);136. break;137. case /:138. if (!operand1) 139. resStatus = 0;140. goto Exit;141. 142. sprintf(buf, %f, operand2 / operand1);143. break;144. case %:145. / if operand is a float number, then error! 146. if (int
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号