状态机学习(五)解析四则运算式
将四则运算拆分成一个个数字和符号后
就进行运算分析
使用以下语法规则:(摘录自《自编编程语言》)
expression:
term:
primary expression:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #pragma once #include <string> #include <deque> #include <iostream> enum TokenType { BAD_TOKEN, NUM_TOKEN, ADD_TOKEN, SUB_TOKEN, MUL_TOKEN, DIV_TOKEN, LPAREN_TOKEN, RPAREN_TOKEN, ENDFILE_TOKEN }; class Token { private : TokenType type_; std::string valueStr_; public : Token() : type_(BAD_TOKEN) {} Token(TokenType t,std::string s) : type_(t),valueStr_(s) {} bool operator ==( const Token& t) { if (type_ == t.type_ && valueStr_ == t.valueStr_) { return true ; } return false ; } Token( const Token& t) { if (* this != t) { type_ = t.type_; valueStr_ = t.valueStr_; } } bool operator != ( const Token& t) { return !(* this == t); } Token& operator=( const Token& t) { if (* this != t) { type_ = t.type_; valueStr_ = t.valueStr_; } return * this ; } /*void SetType(TokenType type) { type_ = type; } void SetValue(double value) { value_ = value; } void SetValStr(std::string valueStr) { valueStr_ = valueStr; }*/ TokenType GetType() { return type_; } std::string GetValStr() { return valueStr_; } }; class Expression2Tokens { std::string contentStr_; Expression2Tokens& operator=( const Expression2Tokens&) {}; Expression2Tokens( const Expression2Tokens&); enum Status { INIT_STATUS, NUM_STATUS, OPERATOR_STATUS, LPAREN_STATUS, RPAREN_STATUS, END_STATUS, ERROR_STATUS }; public : std::deque<Token> tokenDeque_; Expression2Tokens(std::string s):contentStr_(s){} bool Analyzer(); void PrintTokens() { for (std::deque<Token>::iterator it = tokenDeque_.begin(); it != tokenDeque_.end(); it++) { std::cout << "type: " << (*it).GetType() << ", string: " << (*it).GetValStr() << std::endl; } std::cout << std::endl; } }; class ParseExpression { Expression2Tokens express2token_; ParseExpression& operator=( const ParseExpression&) {}; ParseExpression( const ParseExpression&); double result_; double ParseTerm(); double ParsePrimaryExpression(); public : ParseExpression(std::string s) : express2token_(s), result_(0.0){} bool StartParse(); double GetResult() { return result_; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | #include "token.h" #include <iostream> bool Expression2Tokens::Analyzer() { bool bRet = false ; size_t index = 0, valueBeg = 0, valueEnd = 0; Status status = INIT_STATUS; for (;index < contentStr_.size();index++) { if ( isdigit (contentStr_[index])) { if (status != NUM_STATUS) { status = NUM_STATUS; valueBeg = index; } continue ; } //对于 非数字进行判断 if (NUM_STATUS == status ) { std::string s(contentStr_.substr(valueBeg, index - valueBeg)); Token t(NUM_TOKEN,s); tokenDeque_.push_back(t); valueBeg = 0; } if (contentStr_[index] == '+' ) { Token t(ADD_TOKEN, "+" ); tokenDeque_.push_back(t); status = OPERATOR_STATUS; continue ; } else if (contentStr_[index] == '-' ) { Token t(SUB_TOKEN, "-" ); tokenDeque_.push_back(t); status = OPERATOR_STATUS; continue ; } else if (contentStr_[index] == '*' ) { Token t(MUL_TOKEN, "*" ); tokenDeque_.push_back(t); status = OPERATOR_STATUS; continue ; } else if (contentStr_[index] == '/' ) { Token t(DIV_TOKEN, "/" ); tokenDeque_.push_back(t); status = OPERATOR_STATUS; continue ; } else if (contentStr_[index] == ';' ) { Token t(ENDFILE_TOKEN, ";" ); tokenDeque_.push_back(t); status = END_STATUS; bRet = true ; return bRet; } else { std::cerr << "analyzer error (" << contentStr_[index] << ")" << std::endl; status = ERROR_STATUS; return false ; } } return bRet; } double ParseExpression::ParsePrimaryExpression() { Token token; token = express2token_.tokenDeque_.front(); if (token.GetType() == NUM_TOKEN) { //return atof(token.GetValStr().c_str()); express2token_.tokenDeque_.pop_front(); return stod(token.GetValStr()); } std::cerr << "syntax error." << std::endl; exit (1); return 0.0; } double ParseExpression::ParseTerm() { double dRet; double v; Token token; dRet = ParsePrimaryExpression(); for (;;) { token = express2token_.tokenDeque_.front(); if (token.GetType() != MUL_TOKEN && token.GetType() != DIV_TOKEN) { break ; } express2token_.tokenDeque_.pop_front(); v = ParsePrimaryExpression(); if (token.GetType() == MUL_TOKEN) { dRet *= v; } else if (token.GetType() == DIV_TOKEN) { dRet /= v; } } return dRet; } bool ParseExpression::StartParse() { bool bRet = false ; if (!express2token_.Analyzer()) return bRet; express2token_.PrintTokens(); double v; Token token; result_ = ParseTerm(); for (;;) { token = express2token_.tokenDeque_.front(); if (token.GetType() != ADD_TOKEN && token.GetType() != SUB_TOKEN) { break ; } express2token_.tokenDeque_.pop_front(); v = ParseTerm(); if (token.GetType() == ADD_TOKEN) { result_ += v; } else if (token.GetType() == SUB_TOKEN) { result_ -= v; } } bRet = true ; return bRet; } |
测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // MyParse.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "token.h" #include <iostream> int main() { Expression2Tokens e( "1+2/3*45-67-89+999;" ); if (e.Analyzer()) { e.PrintTokens(); } ParseExpression p( "132+243/7*4455-6-89+34;" ); if (p.StartParse()) std::cout << "result: " << p.GetResult() << std::endl; return 0; } |
type: 1, string: 1
type: 2, string: +
type: 1, string: 2
type: 5, string: /
type: 1, string: 3
type: 4, string: *
type: 1, string: 45
type: 3, string: -
type: 1, string: 67
type: 3, string: -
type: 1, string: 89
type: 2, string: +
type: 1, string: 999
type: 8, string: ;
type: 1, string: 132
type: 2, string: +
type: 1, string: 243
type: 5, string: /
type: 1, string: 7
type: 4, string: *
type: 1, string: 4455
type: 3, string: -
type: 1, string: 6
type: 3, string: -
type: 1, string: 89
type: 2, string: +
type: 1, string: 34
type: 8, string: ;
result: 154723
请按任意键继续. . .
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话