数据结构之C之父表达式计算例子

上传人:宝路 文档编号:23508797 上传时间:2017-12-01 格式:DOCX 页数:13 大小:26.72KB
返回 下载 相关 举报
数据结构之C之父表达式计算例子_第1页
第1页 / 共13页
数据结构之C之父表达式计算例子_第2页
第2页 / 共13页
数据结构之C之父表达式计算例子_第3页
第3页 / 共13页
数据结构之C之父表达式计算例子_第4页
第4页 / 共13页
数据结构之C之父表达式计算例子_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《数据结构之C之父表达式计算例子》由会员分享,可在线阅读,更多相关《数据结构之C之父表达式计算例子(13页珍藏版)》请在金锄头文库上搜索。

1、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

2、_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

3、);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:retu

4、rn 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 er

5、ror(没有匹配右括号 );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 na

6、mespace 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 s

7、tring& 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

8、_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 pri

9、ority50. * 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

10、. 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.po

11、p();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.em

12、pty() & 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 (balan

13、ce != 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_e

14、xpr, 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()

15、;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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号