static bool checkSyntax(const char *ptr) { // std::cout << __func__ << " (" << __FILE__ << ") - line: " << __LINE__ << std::endl; //debug int i = 0; if (isOp(*ptr) && *ptr != '(') return false; while (*ptr) { if ( (!isdigit(*ptr) && !isOp(*ptr)) || (isOp(*ptr) == 1 && isOp(*(ptr + 1) == 1)) ) return false; if (*ptr == '(') i++; else if (*ptr == ')') i--; ptr++; } if ((isOp(*(ptr - 1)) && *(ptr - 1) != ')') || i ) return false; return true; }
vector<string> convertToRPN(vector<string> &expression) { vector<string> rpnList; stack<string> opStack; for(int i = 0; i < expression.size(); i++){ string now = expression[i]; if(now.compare("(") == 0){ opStack.push(now); } else if(now.compare(")") == 0){ while(opStack.top().compare("(") != 0){ rpnList.push_back(opStack.top()); opStack.pop(); } opStack.pop(); } else if(isOp(now)){ if(!opStack.empty() && isOp(opStack.top()) && compareOrder(opStack.top(), now) >= 0){ // pop out those ops with higher or equal order rpnList.push_back(opStack.top()); opStack.pop(); } opStack.push(now); } else { rpnList.push_back(now); } } while(!opStack.empty()) { rpnList.push_back(opStack.top()); opStack.pop(); } return rpnList; }
// checkFormat: Checks the format of the input and prints an // error and exits if one is found. int checkFormat(char s[MAXS][MAXLEN]) { int state; int ops = 0, nums = 0; int exp_nums = 0; if(si == 0) { badFormat(); } if(si == 1) { if(isNumber(s[0]) == 0) { return 0; } else { badFormat(); } } // Start by counting the number of operators state = OPSTATE; for(int i = si - 1; i >= 0; i--) { if(isNumber(s[i]) != 0 && isOp(s[i]) != 0) { badFormat(); } switch(state) { case OPSTATE: if(isOp(s[i]) == 0) { ops++; state = OPSTATE; } else { exp_nums = ops + 1; nums++; state = NUMSTATE; } break; case NUMSTATE: if(isNumber(s[i]) == 0) { nums++; state = NUMSTATE; } else { badFormat(); } break; default: badFormat(); } } if(nums != exp_nums) { badFormat(); } return 0; }
static string addBrackets(string s) { int i = 0; while (i<s.length()) { if (i<s.length()-1 && isOp(s[i]) && isOp(s[i+1])) { string f = s.substr(0,i+1); return f + "(" + addBrackets(s.substr(i+1,s.length()-i-1)) + ")"; } i++; } return s; }
bool PermissibleBase::hasPermission(const std::string &inName) { std::string name = SMUtil::toLower(inName); if (isPermissionSet(name)) return permissions[name]->getValue(); Permission *perm = ServerManager::getPluginManager()->getPermission(name); if (perm) return Permission::getDefaultValue(perm->getDefault(), isOp()); return Permission::getDefaultValue(Permission::DEFAULT_PERMISSION, isOp()); }
void Client::updateCounts(bool aRemove) { // We always remove the count and then add the correct one if requested... if (m_countType != COUNT_UNCOUNTED) { --counts[m_countType]; m_countType = COUNT_UNCOUNTED; } if (!aRemove) { if (isOp()) { m_countType = COUNT_OP; } else if (isRegistered()) { m_countType = COUNT_REGISTERED; } else { m_countType = COUNT_NORMAL; } ++counts[m_countType]; } }
bool Client::allowChatMessagefromUser(const ChatMessage& message, const string& p_nick) { if (!message.m_from) { if (!p_nick.empty() && UserManager::isInIgnoreList(p_nick)) // http://code.google.com/p/flylinkdc/issues/detail?id=1432 { return false; } else { return true; } } else if (isMe(message.m_from)) { return true; } else if (message.thirdPerson && BOOLSETTING(NSL_IGNORE_ME)) { return false; } if (BOOLSETTING(SUPPRESS_MAIN_CHAT) && !isOp()) { return false; } else if (UserManager::isInIgnoreList(message.m_from->getIdentity().getNick())) // !SMT!-S { return false; } else { return true; } }
Expression BoolQuery::toRPN(Expression exp) { stack<string> tmpStack; Expression rpn; for (string token : exp) { if (token == "(") { tmpStack.push(token); } else if (token == ")") { while (tmpStack.top() != "(") { rpn.push_back(tmpStack.top()); tmpStack.pop(); } tmpStack.pop(); } else if (isOp(token)) { while (!tmpStack.empty() && priority(token) <= priority(tmpStack.top())) { rpn.push_back(tmpStack.top()); tmpStack.pop(); } tmpStack.push(token); } else { // token is a term rpn.push_back(token); } } while (!tmpStack.empty()) { rpn.push_back(tmpStack.top()); tmpStack.pop(); } return rpn; }
// returns 1 if * or / and returns 0 if + or -... *,/ > +,- int InfixCalculator::precedence(char op){ if (isOp(op)){ if (op == '*' || op == '/') return 1; if (op == '-' || op == '+') return 0; } return -1; }
// returns a string of the infix expression in postfix form std::string InfixCalculator::toPostfix(std::string infix){ std::string p = ""; for (unsigned int i = 0; i < infix.size(); i++){ if (isVal(infix[i])){ p += infix[i]; } if (isOp(infix[i])){ while (!opStack.isEmpty() && precedence(infix[i]) <= precedence(opStack.peek())){ p += opStack.peek(); opStack.pop(); } opStack.push(infix[i]); } if (infix[i] == '('){ opStack.push(infix[i]); } if (infix[i] == ')'){ while (opStack.peek() != '('){ p += opStack.peek(); opStack.pop(); } opStack.pop(); } } while (!opStack.isEmpty()){ p += opStack.peek(); opStack.pop(); } return p; }
// make infix expression into postfix expression // based off algorithm from book int InfixCalculator::evaluate(std::string infix){ int result; for (unsigned int i = 0; i < infix.size(); i++){ if (isVal(infix[i])){ valStack.push(infix[i] - 48); } if (isOp(infix[i])){ if (opStack.isEmpty() || precedence(infix[i]) > precedence(opStack.peek())){ opStack.push(infix[i]); } else { while (!opStack.isEmpty() && precedence(infix[i]) <= precedence(opStack.peek())){ execute(); } opStack.push(infix[i]); } } if (infix[i] == '('){ opStack.push(infix[i]); } if (infix[i] == ')'){ while (opStack.peek() != '('){ execute(); } opStack.pop(); } } while (!opStack.isEmpty()){ execute(); } result = valStack.peek(); return result; }
QString ChannelNick::tooltip() const { QString strTooltip; QTextStream tooltip( &strTooltip, QIODevice::WriteOnly ); tooltip << "<qt>"; tooltip << "<table cellspacing=\"5\" cellpadding=\"0\">"; m_nickInfo->tooltipTableData(tooltip); QStringList modes; if(isOp()) modes << i18n("Operator"); if(isAdmin()) modes << i18n("Admin"); if(isOwner()) modes << i18n("Owner"); if(isHalfOp()) modes << i18n("Half-operator"); if(hasVoice()) modes << i18n("Has voice"); //Don't show anything if the user is just a normal user //if(modes.empty()) modes << i18n("A normal user"); if(!modes.empty()) { tooltip << "<tr><td><b>" << i18n("Mode") << ":</b></td><td>" << modes.join(QStringLiteral(", ")) << "</td></tr>"; } tooltip << "</table></qt>"; //qDebug() << strTooltip ; //if(!dirty) return QString(); return strTooltip; }
int evalRPN(vector<string> &tokens) { stack<int> stack; for (auto &token : tokens) { if (!isOp(token)) stack.push(atoi(token.c_str())); else if (token == "+") { int rhs = stack.top(); stack.pop(); stack.top() += rhs; } else if (token == "-") { int rhs = stack.top(); stack.pop(); stack.top() -= rhs; } else if (token == "*") { int rhs = stack.top(); stack.pop(); stack.top() *= rhs; } else if (token == "/") { int rhs = stack.top(); stack.pop(); stack.top() /= rhs; } } return stack.top(); }
bool PermissibleBase::hasPermission(Permission *perm) { std::string name = SMUtil::toLower(perm->getName()); if (isPermissionSet(name)) return permissions[name]->getValue(); return Permission::getDefaultValue(perm->getDefault(), isOp()); }
void calc(char *inf) { if (isDigit (inf[0]) || inf[0] == '(') { int i = 0; char c = '\0'; bool hasBracket = false; Stack * head = createStack(); while ((inf[i] != '\0')) { if (isOp(inf[i])) { printf(" "); if (!isEmpty(head)) { c = pop(head); if (c == '(') //again, check if bracket (it's second, first is down there) push(head, c); else if (isHigher(inf[i], c)) //check if operand has higher priority push(head, c); else { while (!isEmpty(head)) { printf("%c ", c); c = pop(head); } printf("%c ", c); } } push(head, inf[i]); } else if (isDigit(inf[i])) // check if number printf("%c", inf[i]); else //check if bracket if (inf[i] == '(') push(head, inf[i]); else //push all out till ( if (inf[i] == ')') { c = pop(head); while ((c != '(') && (!isEmpty(head))) { printf("%c ", c); c = pop(head); } } i++; } while (!isEmpty(head)) printf("%c ", pop(head)); } else printf("error"); }
int evalRPN(vector<string> &tokens) { stack<int> result; if (tokens.size() == 0) return 0; for (int i=0; i<tokens.size(); i++) { if (isOp(tokens[i])) { int right = result.top(); result.pop(); int left = result.top(); result.pop(); result.push(op(tokens[i], left, right)); } else { result.push(atoi(tokens[i].c_str())); } } return result.top(); }
void CompilationEngine::readOp() { if (jt.tokenType() == TokenType::kSYMBOL) { char sym = jt.symbol(); if (isOp(sym)) { writeTerminal(jt.getTokenWithTags()); return; } } errorMsgExpected("op", jt.getToken()); // ????????????????????????????? }
void CompilationEngine::compileExpression() { /* term (op term)* */ tagNonTerminal("expression"); compileTerm(); while (jt.tokenType() == TokenType::kSYMBOL && isOp(jt.symbol())) { readOp(); nextToken(); compileTerm(); } untagNonTerminal("expression"); }
int evalRPN(vector<string>& tokens) { stack<int> s; for (int i = 0; i < tokens.size(); i++){ if (isOp(tokens[i])){ int y = s.top(); s.pop(); int x = s.top(); s.pop(); s.push(evaluate(x, y, tokens[i])); } else { s.push(stoi(tokens[i], nullptr, 10)); } } return s.top(); }
void PermissibleBase::recalculatePermissions() { clearPermissions(); std::vector<Permission *> defaults = ServerManager::getPluginManager()->getDefaultPermissions(isOp()); ServerManager::getPluginManager()->subscribeToDefaultPerms(isOp(), parent); for (Permission *perm : defaults) { std::string name = SMUtil::toLower(perm->getName()); permissions[name] = new PermissionAttachmentInfo(parent, name, NULL, true); ServerManager::getPluginManager()->subscribeToPermission(name, parent); calculateChildPermissions(perm->getChildren(), false, NULL); } for (size_t i = 0; i < attachments.size(); ++i) calculateChildPermissions(attachments[i]->getPermissions(), false, attachments[i]); }
//time complexity: O(n) //space complexity: O(n) //1A, some bugs fixed int calculate(string s) { int lastOpPos = -1; int pos; stack<char> op; stack<int> opr; for (pos = 0; pos < s.size(); pos++) { if (isOp(s[pos])) { string oprStr = s.substr(lastOpPos+1, pos - lastOpPos -1); opr.push(atoi(oprStr.c_str())); while ((!op.empty()) && (level(op.top()) >= level(s[pos]))) { int opr2 = opr.top(); opr.pop(); int opr1 = opr.top(); opr.pop(); char ope = op.top(); op.pop(); opr.push(cal(ope, opr1, opr2)); } op.push(s[pos]); lastOpPos = pos; } } string oprStr = s.substr(lastOpPos+1, pos - lastOpPos -1); opr.push(atoi(oprStr.c_str())); while (!op.empty()) { char ope = op.top(); op.pop(); int opr2 = opr.top(); opr.pop(); int opr1 = opr.top(); opr.pop(); opr.push(cal(ope, opr1, opr2)); } return opr.top(); }
void treeMoveMinus(Node **node) { int minusCnt = 0; if (*node == NULL) return; if ((*node)->_varOp == '*' || (*node)->_varOp == '/') { treeCountMinus(node, &minusCnt); if (minusCnt & 1) *node = treeMakeMinus(node); } else if (isOp((*node)->_varOp)) { treeMoveMinus(&(*node)->_left); treeMoveMinus(&(*node)->_right); } }
int calculateRPN(vector<string> &expression) { stack<int> s; int number = 0; for(int i = 0; i < expression.size(); i++){ string now = expression[i]; if(isOp(now)){ int b = s.top(); s.pop(); int a = s.top(); s.pop(); number = calculate(a, b, now); s.push(number ); } else { number = stoi(now); s.push(number); } } return s.top(); }
void treeBuild(Node **node, Stack *st) { Token token; if (stackEmpty(st)) return; token = stackTop(st); stackPop(st); (*node) = treeNodeCreate(); (*node)->_varOp = token._varOp; (*node)->_num = token._num; if (isOp((*node)->_varOp)) { treeBuild(&(*node)->_right, st); treeBuild(&(*node)->_left, st); } }
void Builder::parse( const char* content ) { NodeManager* mgr = NodeManager::getSingleton(); const char* last = content; int width; while( *content ) { if( (width = isBlank(content)) > 0 ) { content += width; } else if( (width = isOp( content )) > 0 ) { mgr->createOperator(content, width); mgr->createOperand(content, content-last); last = content; content += width; } else if( (width = isBrace( content )) > 0 ) { mgr->createBrace(content, width); last = content; content += width; } content ++; } }
void initSVec(string& s, std::vector<Data>& vec) { int i = 0; while (i < s.size()) { char ch = s[i]; if (isKuoHao(ch)){ Data data; data.value.ch = s[i]; data.type = KuoHao; vec.push_back(data); } else if (isNumber(ch)){ int j = i + 1; while (j < s.size()) { if (isNumber(s[j]) == false) break; j = j + 1; } std::string n = s.substr(i, j - i); i = j - 1; int number = atoi(n.c_str()); Data data; data.value.number = number; data.type = INT; vec.push_back(data); } else if (isOp(ch)){ Data data; data.value.ch = s[i]; data.type = OP; vec.push_back(data); } else if (isBlank(ch)){ } i = i + 1; } }
ResultSet BoolQuery::calcRPN(Expression exp) { stack<ReverseTable> tableStack; ReverseTable opA, opB; for (string token : exp) { if (isOp(token)) { opA = tableStack.top(); tableStack.pop(); if (token != "NOT") { opB = tableStack.top(); tableStack.pop(); } tableStack.push(doCalc(token, opA, opB)); } else { ReverseTable tmp = getReverseTable(token); tableStack.push(tmp); } } return tableStack.top(); }
/* expression -> var "=" expression | simple-expression * var -> ID ["["expression"]"]*/ static TreeNode * expression (void) { TreeNode * t = newExpNode(IdK); TreeNode * p = NULL; TreeNode * newNode = NULL; #define CALL 1 #define ARRAY 2 int factorType = 0; while (token == COMMENT) unexpectedTokenHandling(); if (token == ID) { if (t != NULL) t->attr.name = copyString(tokenString); match (ID); /* var -> ID "["expression"]" */ if (token == SLPAREN) { factorType = ARRAY; match (SLPAREN); t->child[0] = expression(); match (SRPAREN); /* ID "["expression"]" "=" expression */ if (token == ASSIGN) { match (ASSIGN); newNode = newStmtNode(AssignK); if (t != NULL) { newNode->child[0] = t; t= newNode; t->child[1] = expression(); if (token != SEMI) unexpectedTokenHandling(); } } } /* simple-expression => call => ID "("args")" */ else if (token == LPAREN) { factorType = CALL; match (LPAREN); if (t != NULL) { t->nodekind = ExpK; t->kind.exp = CallK; t->child[0] = args(); } match (RPAREN); } /* var | call is followed by assignment, operation, * semi-colon or comma. */ while (token == COMMENT) unexpectedTokenHandling(); if (token == ASSIGN) { if ((factorType == CALL) || (factorType == ARRAY)) unexpectedTokenHandling(); /* ID "=" expression */ else { match (ASSIGN); newNode = newStmtNode(AssignK); if (t != NULL) { newNode->child[0] = t; t= newNode; t->child[1] = expression(); if (token != SEMI) unexpectedTokenHandling(); } } } /* ID [("("args")"|"["expression"]")] * op additivie-expression */ else if (isOp (token)) { newNode = newExpNode (OpK); if (newNode != NULL) { newNode->child[0] = t; newNode->attr.op = token; t = newNode; } match (token); if (t != NULL) t->child[1] = additive_expression(); } else if ((token != SEMI) && (token != COMMA) && (token != RPAREN)) unexpectedTokenHandling(); } else if ((token == LPAREN) || (token == NUM)) t = simple_expression(); else unexpectedTokenHandling(); return t; }
void postOrder(const char *str, Stack *st) { int i = 0, isBracket = 0; char tmpCh; Token tk; Stack stOp; stackCreate(&stOp); tk._varOp = '\0'; tk._num = 0; while (str[i] != '\0') { if (isLetter(str[i])) { tk._varOp = str[i]; stackPush(st, tk); } else if (isNumber(str[i])) { tk._varOp = '\0'; tk._num = tk._num * 10 + str[i] - '0'; if (str[i + 1] != '.' && !isNumber(str[i + 1])) { stackPush(st, tk); tk._num = 0; } } else if (isOp(str[i])) { tk._varOp = str[i]; if (str[i] == ')') isBracket = 1; else if (str[i] == '-' && (i == 0 || str[i - 1] == '(')) { tmpCh = tk._varOp; tk._varOp = '\0'; tk._num = 0; stackPush(st, tk); tk._varOp = tmpCh; } while (!stackEmpty(&stOp) && (isOpHigh(stackTop(&stOp)._varOp, str[i]) || isBracket)) { if (stackTop(&stOp)._varOp == '(') isBracket = 0; else stackPush(st, stackTop(&stOp)); stackPop(&stOp); } if (str[i] != ')') stackPush(&stOp, tk); } i++; } while (!stackEmpty(&stOp)) { stackPush(st, stackTop(&stOp)); stackPop(&stOp); } stackDestroy(&stOp); }
std::string Token::toString() const { std::string tokData = isOp() ? "operator " + op().getName() : "data \"" + data + "\""; return "Token " + type.toString() + ", " + tokData + ", " + trace.toString(); }