开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (31)
标签 (24)
master
release-16.01
release-13.12
release-xx.yy
xml_compiler
release-12.11
codeblocks_console
sqrat
wxpropgrid_debugger
wxsmith_addons
backup_of_trunk_before_switch_to_codecompletion_refactrong
codecompletion_refactoring
scintilla
release-10.05
wxfnb_to_wxaui
sc
release-8.02
rick_s_playground
New_wxSmith
cf
wxpropgrid_debugger_merge_candidate
before_merge_codecompletion_refactoring
10.05
wiley1
8.02
head_merged_to_yt
VERSION_1_0_MERGE_TO_HEAD
SNAPSHOT_AUG_17_2005
SNAPSHOT_AUG_11_2005
SNAPSHOT_UNICODE
SNAPSHOT_AUG_04_2005
CODEBLOCKS_1_0_RC1_1
CODEBLOCKS_1_0_RC1
SNAPSHOT_24_6_05
SNAPSHOT_28052005
CODEBLOCKS_1_0_FINALBETA
SNAPSHOT_5_5_05
CODEBLOCKS_1_0_BETA7
CODEBLOCKS_1_0_BETA6
CODEBLOCKS_1_0_BETA5
master
分支 (31)
标签 (24)
master
release-16.01
release-13.12
release-xx.yy
xml_compiler
release-12.11
codeblocks_console
sqrat
wxpropgrid_debugger
wxsmith_addons
backup_of_trunk_before_switch_to_codecompletion_refactrong
codecompletion_refactoring
scintilla
release-10.05
wxfnb_to_wxaui
sc
release-8.02
rick_s_playground
New_wxSmith
cf
wxpropgrid_debugger_merge_candidate
before_merge_codecompletion_refactoring
10.05
wiley1
8.02
head_merged_to_yt
VERSION_1_0_MERGE_TO_HEAD
SNAPSHOT_AUG_17_2005
SNAPSHOT_AUG_11_2005
SNAPSHOT_UNICODE
SNAPSHOT_AUG_04_2005
CODEBLOCKS_1_0_RC1_1
CODEBLOCKS_1_0_RC1
SNAPSHOT_24_6_05
SNAPSHOT_28052005
CODEBLOCKS_1_0_FINALBETA
SNAPSHOT_5_5_05
CODEBLOCKS_1_0_BETA7
CODEBLOCKS_1_0_BETA6
CODEBLOCKS_1_0_BETA5
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (31)
标签 (24)
master
release-16.01
release-13.12
release-xx.yy
xml_compiler
release-12.11
codeblocks_console
sqrat
wxpropgrid_debugger
wxsmith_addons
backup_of_trunk_before_switch_to_codecompletion_refactrong
codecompletion_refactoring
scintilla
release-10.05
wxfnb_to_wxaui
sc
release-8.02
rick_s_playground
New_wxSmith
cf
wxpropgrid_debugger_merge_candidate
before_merge_codecompletion_refactoring
10.05
wiley1
8.02
head_merged_to_yt
VERSION_1_0_MERGE_TO_HEAD
SNAPSHOT_AUG_17_2005
SNAPSHOT_AUG_11_2005
SNAPSHOT_UNICODE
SNAPSHOT_AUG_04_2005
CODEBLOCKS_1_0_RC1_1
CODEBLOCKS_1_0_RC1
SNAPSHOT_24_6_05
SNAPSHOT_28052005
CODEBLOCKS_1_0_FINALBETA
SNAPSHOT_5_5_05
CODEBLOCKS_1_0_BETA7
CODEBLOCKS_1_0_BETA6
CODEBLOCKS_1_0_BETA5
expression.cpp 17.19 KB
一键复制 编辑 原始数据 按行查看 历史
ollydbg 提交于 2015年03月10日 10:50 +08:00 . - CC: fix duplicated else if condition reported by cppcheck.
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
* http://www.gnu.org/licenses/gpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include <sdk.h>
#include <stack>
#ifndef CB_PRECOMP
#include <wx/wx.h>
#endif
#include <logmanager.h>
#include <manager.h>
#include "cclogger.h"
#include "expression.h"
#include "token.h"
#define CC_EXPRESSION_DEBUG_OUTPUT 0
#if defined(CC_GLOBAL_DEBUG_OUTPUT)
#if CC_GLOBAL_DEBUG_OUTPUT == 1
#undef CC_EXPRESSION_DEBUG_OUTPUT
#define CC_EXPRESSION_DEBUG_OUTPUT 1
#elif CC_GLOBAL_DEBUG_OUTPUT == 2
#undef CC_EXPRESSION_DEBUG_OUTPUT
#define CC_EXPRESSION_DEBUG_OUTPUT 2
#endif
#endif
#ifdef CC_PARSER_TEST
#define TRACE(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#define TRACE2(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#else
#if CC_EXPRESSION_DEBUG_OUTPUT == 1
#define TRACE(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#define TRACE2(format, args...)
#elif CC_EXPRESSION_DEBUG_OUTPUT == 2
#define TRACE(format, args...) \
do \
{ \
if (g_EnableDebugTrace) \
CCLogger::Get()->DebugLog(F(format, ##args)); \
} \
while (false)
#define TRACE2(format, args...) \
CCLogger::Get()->DebugLog(F(format, ##args))
#else
#define TRACE(format, args...)
#define TRACE2(format, args...)
#endif
#endif
namespace ExpressionConsts
{
const wxString Plus (_T("+"));
const wxString Subtract (_T("-"));
const wxString Multiply (_T("*"));
const wxString Divide (_T("/"));
const wxString LParenthesis (_T("("));
const wxString RParenthesis (_T(")"));
const wxString Mod (_T("%"));
const wxString Power (_T("^"));
const wxString BitwiseAnd (_T("&"));
const wxString BitwiseOr (_T("|"));
const wxString And (_T("&&"));
const wxString Or (_T("||"));
const wxString Not (_T("!"));
const wxString Equal (_T("=="));
const wxString Unequal (_T("!="));
const wxString GT (_T(">"));
const wxString LT (_T("<"));
const wxString GTOrEqual (_T(">="));
const wxString LTOrEqual (_T("<="));
const wxString LShift (_T("<<"));
const wxString RShift (_T(">>"));
}
ExpressionNode::ExpressionNode()
{
Initialize(wxEmptyString);
}
void ExpressionNode::Initialize(wxString token)
{
m_UnaryOperator = false;
m_Token = token;
m_Type = ParseNodeType(m_Token);
m_Priority = GetNodeTypePriority(m_Type);
}
ExpressionNode::ExpressionNodeType ExpressionNode::ParseNodeType(wxString token)
{
if (token.IsEmpty()) return ExpressionNode::Unknown;
else if (token == ExpressionConsts::Plus) return ExpressionNode::Plus;
else if (token == ExpressionConsts::Subtract) return ExpressionNode::Subtract;
else if (token == ExpressionConsts::Multiply) return ExpressionNode::Multiply;
else if (token == ExpressionConsts::Divide) return ExpressionNode::Divide;
else if (token == ExpressionConsts::Mod) return ExpressionNode::Mod;
else if (token == ExpressionConsts::Power) return ExpressionNode::Power;
else if (token == ExpressionConsts::LParenthesis) return ExpressionNode::LParenthesis;
else if (token == ExpressionConsts::RParenthesis) return ExpressionNode::RParenthesis;
else if (token == ExpressionConsts::BitwiseAnd) return ExpressionNode::BitwiseAnd;
else if (token == ExpressionConsts::BitwiseOr) return ExpressionNode::BitwiseOr;
else if (token == ExpressionConsts::And) return ExpressionNode::And;
else if (token == ExpressionConsts::Or) return ExpressionNode::Or;
else if (token == ExpressionConsts::Not) return ExpressionNode::Not;
else if (token == ExpressionConsts::Equal) return ExpressionNode::Equal;
else if (token == ExpressionConsts::Unequal) return ExpressionNode::Unequal;
else if (token == ExpressionConsts::GT) return ExpressionNode::GT;
else if (token == ExpressionConsts::LT) return ExpressionNode::LT;
else if (token == ExpressionConsts::GTOrEqual) return ExpressionNode::GTOrEqual;
else if (token == ExpressionConsts::LTOrEqual) return ExpressionNode::LTOrEqual;
else if (token == ExpressionConsts::LShift) return ExpressionNode::LShift;
else if (token == ExpressionConsts::RShift) return ExpressionNode::RShift;
else
{
if (wxIsdigit(token[0])) return ExpressionNode::Numeric;
else return ExpressionNode::Unknown;
}
}
long ExpressionNode::GetNodeTypePriority(ExpressionNodeType type)
{
switch (type)
{
case LParenthesis:
case RParenthesis: return 10;
case Not: return 9;
case Mod: return 8;
case Multiply:
case Divide:
case Power: return 7;
case Plus:
case Subtract: return 6;
case LShift:
case RShift: return 5;
case BitwiseAnd:
case BitwiseOr: return 4;
case Equal:
case Unequal:
case GT:
case LT:
case GTOrEqual:
case LTOrEqual: return 3;
case And: return 2;
case Or: return 1;
case Numeric:
case Unknown:
default: return 0;
}
}
bool ExpressionNode::IsUnaryNode(ExpressionNodeType type)
{
switch (type)
{
case ExpressionNode::Plus:
case ExpressionNode::Subtract:
case ExpressionNode::Not:
return true;
case ExpressionNode::Unknown:
case ExpressionNode::Multiply:
case ExpressionNode::Divide:
case ExpressionNode::LParenthesis:
case ExpressionNode::RParenthesis:
case ExpressionNode::Mod:
case ExpressionNode::Power:
case ExpressionNode::BitwiseAnd:
case ExpressionNode::BitwiseOr:
case ExpressionNode::And:
case ExpressionNode::Or:
case ExpressionNode::Equal:
case ExpressionNode::Unequal:
case ExpressionNode::GT:
case ExpressionNode::LT:
case ExpressionNode::GTOrEqual:
case ExpressionNode::LTOrEqual:
case ExpressionNode::LShift:
case ExpressionNode::RShift:
case ExpressionNode::Numeric:
default:
return false;
}
}
bool ExpressionNode::IsBinaryOperator(wxString first, wxString second)
{
switch ((wxChar)first.GetChar(0))
{
case _T('&'):
case _T('|'):
case _T('='):
case _T('!'):
case _T('>'):
case _T('<'):
{
wxString newOperator(first + second);
if (newOperator == ExpressionConsts::And ||
newOperator == ExpressionConsts::Or ||
newOperator == ExpressionConsts::Equal ||
newOperator == ExpressionConsts::Unequal ||
newOperator == ExpressionConsts::GTOrEqual ||
newOperator == ExpressionConsts::LTOrEqual ||
newOperator == ExpressionConsts::LShift ||
newOperator == ExpressionConsts::RShift)
return true;
else
return false;
}
default:
return false;
}
}
void Expression::Clear()
{
m_InfixExpression.clear();
m_PostfixExpression.clear();
}
void Expression::AddToInfixExpression(wxString token)
{
if (token.IsEmpty())
return;
if (!m_InfixExpression.empty())
{
wxString& lastToken = m_InfixExpression[m_InfixExpression.size() - 1];
if (ExpressionNode::IsBinaryOperator(lastToken, token))
{
lastToken += token;
return;
}
}
m_InfixExpression.push_back(token);
}
void Expression::ConvertInfixToPostfix()
{
if (!m_PostfixExpression.empty() || m_InfixExpression.empty())
return;
m_Result = true;
m_Status = true;
std::stack<ExpressionNode> stackOperator;
ExpressionNode::ExpressionNodeType lastType = ExpressionNode::Unknown;
for (PostfixVector::size_type i = 0; i < m_InfixExpression.size(); ++i)
{
ExpressionNode expNode;
expNode.Initialize(m_InfixExpression[i]);
const ExpressionNode::ExpressionNodeType type = expNode.GetType();
if (type == ExpressionNode::Numeric)
{
// Operand, add to postfix expression
m_PostfixExpression.push_back(expNode);
while (!stackOperator.empty() && stackOperator.top().IsUnaryOperator())
{
m_PostfixExpression.push_back(stackOperator.top());
stackOperator.pop();
}
}
else if (type == ExpressionNode::LParenthesis)
{
// Left Parentheses, add to stack
stackOperator.push(expNode);
}
else if (type == ExpressionNode::RParenthesis)
{
// Right Parentheses, reverse search the Left Parentheses, add all operator of the middle
ExpressionNode node;
while (!stackOperator.empty())
{
node = stackOperator.top();
stackOperator.pop();
if (node.GetType() == ExpressionNode::LParenthesis)
{
while (!stackOperator.empty() && stackOperator.top().IsUnaryOperator())
{
m_PostfixExpression.push_back(stackOperator.top());
stackOperator.pop();
}
break;
}
else
m_PostfixExpression.push_back(node);
}
// The lastest node must be Left Parentheses
if (node.GetType() != ExpressionNode::LParenthesis)
{
m_Status = false;
}
}
else
{
if (ExpressionNode::IsUnaryNode(type) && (m_PostfixExpression.empty() ||
(lastType != ExpressionNode::Unknown &&
lastType != ExpressionNode::RParenthesis &&
lastType != ExpressionNode::Numeric)))
{
expNode.SetUnaryOperator();
stackOperator.push(expNode);
}
else if (stackOperator.empty())
{
stackOperator.push(expNode);
}
else
{
ExpressionNode beforeExpNode = stackOperator.top();
if (beforeExpNode.GetType() != ExpressionNode::LParenthesis &&
beforeExpNode.GetPriority() >= expNode.GetPriority())
{
m_PostfixExpression.push_back(beforeExpNode);
stackOperator.pop();
}
stackOperator.push(expNode);
}
}
lastType = type;
}
while (!stackOperator.empty())
{
ExpressionNode beforeExpNode = stackOperator.top();
if (beforeExpNode.GetType() == ExpressionNode::LParenthesis)
{
m_Status = false;
}
m_PostfixExpression.push_back(beforeExpNode);
stackOperator.pop();
}
#ifdef CC_PARSER_TEST
wxString infix, postfix;
for (InfixVector::size_type i = 0; i < m_InfixExpression.size(); ++i)
infix += m_InfixExpression[i] + _T(" ");
for (PostfixVector::size_type i = 0; i < m_PostfixExpression.size(); ++i)
postfix += m_PostfixExpression[i].GetToken() + _T(" ");
TRACE(_T("ConvertInfixToPostfix() : InfixExpression : %s"), infix.wx_str());
TRACE(_T("ConvertInfixToPostfix() : PostfixExpression : %s"), postfix.wx_str());
#endif
}
bool Expression::CalcPostfix()
{
std::pair<long, long> pair;
std::stack<long> stack;
int cntNumeric = 0;
for (PostfixVector::size_type i = 0; i < m_PostfixExpression.size(); ++i)
{
const ExpressionNode& node = m_PostfixExpression[i];
const ExpressionNode::ExpressionNodeType type = node.GetType();
if (type == ExpressionNode::Numeric)
{
++cntNumeric;
if (cntNumeric == 1)
pair.first = node.GetTokenValue();
else if (cntNumeric == 2)
pair.second = node.GetTokenValue();
else if (cntNumeric == 3)
{
--cntNumeric;
stack.push(pair.first);
TRACE(_T("CalcPostfix() : stack.push(pair.first) : %ld"), pair.first);
pair.first = pair.second;
pair.second = node.GetTokenValue();
}
}
else
{
if (node.IsUnaryOperator())
{
if (cntNumeric == 1)
pair.first = CalculateUnary(type, pair.first);
else if (cntNumeric == 2)
pair.second = CalculateUnary(type, pair.second);
}
else
{
if (cntNumeric == 2)
{
--cntNumeric;
pair.first = Calculate(type, pair.first, pair.second);
}
else if (cntNumeric == 1)
{
if (stack.empty())
{
m_Status = false;
return false;
}
pair.second = pair.first;
pair.first = stack.top();
TRACE(_T("CalcPostfix() : stack.pop() : %ld"), pair.first);
stack.pop();
pair.first = Calculate(type, pair.first, pair.second);
}
}
}
TRACE(_T("CalcPostfix() : pair.first : %ld, pair.second : %ld"), pair.first, pair.second);
if (!m_Status)
return false;
}
if (!stack.empty())
m_Status = false;
if (m_Status)
m_Result = pair.first; // get the actual value
return true;
}
long Expression::Calculate(ExpressionNode::ExpressionNodeType type, long first, long second)
{
switch (type)
{
case ExpressionNode::Plus:
return first + second;
case ExpressionNode::Subtract:
return first - second;
case ExpressionNode::Multiply:
return first * second;
case ExpressionNode::Divide:
if (second == 0) { m_Status = false; return 0; }
else return first / second;
case ExpressionNode::Mod:
if (second == 0) { m_Status = false; return 0; }
else return first / second;
case ExpressionNode::BitwiseAnd:
return first & second;
case ExpressionNode::BitwiseOr:
return first | second;
case ExpressionNode::And:
return first && second;
case ExpressionNode::Or:
return first || second;
case ExpressionNode::Equal:
return first == second;
case ExpressionNode::Unequal:
return first != second;
case ExpressionNode::GT:
return first > second;
case ExpressionNode::LT:
return first < second;
case ExpressionNode::GTOrEqual:
return first >= second;
case ExpressionNode::LTOrEqual:
return first <= second;
case ExpressionNode::LShift:
return first << second;
case ExpressionNode::RShift:
return first >> second;
case ExpressionNode::Unknown:
case ExpressionNode::LParenthesis:
case ExpressionNode::RParenthesis:
case ExpressionNode::Power:
case ExpressionNode::Not:
case ExpressionNode::Numeric:
default:
return 0;
}
}
long Expression::CalculateUnary(ExpressionNode::ExpressionNodeType type, long value)
{
switch (type)
{
case ExpressionNode::Plus:
return value;
case ExpressionNode::Subtract:
return 0 - value;
case ExpressionNode::Not:
return !value;
case ExpressionNode::Unknown:
case ExpressionNode::Multiply:
case ExpressionNode::Divide:
case ExpressionNode::LParenthesis:
case ExpressionNode::RParenthesis:
case ExpressionNode::Mod:
case ExpressionNode::Power:
case ExpressionNode::BitwiseAnd:
case ExpressionNode::BitwiseOr:
case ExpressionNode::And:
case ExpressionNode::Or:
case ExpressionNode::Equal:
case ExpressionNode::Unequal:
case ExpressionNode::GT:
case ExpressionNode::LT:
case ExpressionNode::GTOrEqual:
case ExpressionNode::LTOrEqual:
case ExpressionNode::LShift:
case ExpressionNode::RShift:
case ExpressionNode::Numeric:
default:
return 0;
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Git and svn mirror of the Code::Blocks svn-repo at http://svn.code.sf.net/p/codeblocks/code
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yyxtyk/codeblocks.git
git@gitee.com:yyxtyk/codeblocks.git
yyxtyk
codeblocks
codeblocks
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /