int error1(const struct command_t* command){ /*if((isNum(command->token[0]) && isNum(command->token[2]) && !(isOperation(command->token[1])) && strcmp(command->token[3], "") == 0) || (isNum(command->token[1]) && !(isOperation(command->token[0])) && strcmp(command->token[2], "") == 0))*/ if(isNum(command->token[0])) if(isNum(command->token[2])) if(!(isOperation(command->token[1]))) if(strcmp(command->token[3], "") == 0) return 1; if(isNum(command->token[1])) if(!(isOperation(command->token[0]))) if(strcmp(command->token[2], "") == 0) return 1; return 0; }
//------------------------------------------------------------------------------ std::streampos ISerializable::tellg(void) const { if (isOperation(OP_READ)) return istr_->tellg(); return 0; }
short ArithmeticTree::findMinPriorityIndex(string exp) const { short tmp, priority_max = 0; string i_max; bool p = true; int q = 0; short corect = 0, *ptr; ptr = &corect; exp = eraseBrackets(exp, ptr); if ((exp.find('(') != string::npos && exp.find(')') == string::npos) || (exp.find(')') != string::npos && exp.find('(') == string::npos)) p = true; else if (exp.find('(') != string::npos && exp.find(')') != string::npos) p = false; for (int i = 0; i < exp.size(); i++) { if (exp[i] == '(') q++; if (exp[i] == ')') q--; if (isOperation(exp[i])) { if (p) tmp = priority(exp[i]); else { if (q > 0) tmp = -1; else tmp = priority(exp[i]); } if (tmp >= priority_max) { priority_max = tmp; i_max += i; } } } return (short)i_max[i_max.size() / 2] + corect; }
Calculator::ActionType Calculator::getLastOperation() { for (auto op = m_actions.rbegin(); op != m_actions.rend(); ++op) { if (isOperation(op->actionType)) return op->actionType; } return ActionType::None; }
unsigned CExpression::operatorCount(sintElem* op) { unsigned k = 1; signed j = 0; while(k)//проверку на достижение начала массива ставить не нужно, если мы предпологаем нотацию разрешаемой {//Ну пошутил. . . Если это оператор, добавить 1 если он бинарный, если не оператор (число) вычесть 1. k += isOperation(op[-++j].lexeme) ? !isUnary(op[-j].lexeme) : -1;//по моему это приведёт нас ко второму операнду } return j; }
/* returns number of bytes consumed */ int QtTelnetPrivate::parseIAC(const QByteArray &data) { if (data.isEmpty()) return 0; Q_ASSERT(uchar(data.at(0)) == Common::IAC); if (data.size() >= 3 && isOperation(data[1])) { // IAC, Operation, Option const uchar operation = data[1]; const uchar option = data[2]; if (operation == Common::WONT && option == Common::Logout) { q->close(); return 3; } if (operation == Common::DONT && option == Common::Authentication) { if (loginp.isEmpty() && passp.isEmpty()) emit q->loggedIn(); nullauth = true; } if (replyNeeded(operation, option)) { bool allowed = allowOption(operation, option); sendCommand(opposite(operation, allowed), option); setMode(operation, option); } return 3; } if (data.size() >= 2 && isCommand(data[1])) { // IAC Command return 2; } QByteArray suboption = getSubOption(data); if (suboption.isEmpty()) return 0; // IAC SB Operation SubOption [...] IAC SE switch (suboption[0]) { case Common::Authentication: parseSubAuth(suboption); break; case Common::TerminalType: parseSubTT(suboption); break; case Common::NAWS: parseSubNAWS(data); break; default: qWarning("QtTelnetPrivate::parseIAC: unknown suboption %d", quint8(suboption.at(0))); break; } return suboption.size() + 4; }
//------------------------------------------------------------------------------ void Terrain::serializeObject(void) { serialize<int8_t>(Enabled); serialize<int8_t>(Random); serialize(Name, getNameSize()); serialize(Name2, getNameSize()); if (getGameVersion() >= genie::GV_AoEB) serialize<int32_t>(SLP); serialize<int32_t>(Unknown3); serialize<int32_t>(SoundID); if (getGameVersion() >= genie::GV_AoKB) { serialize<int32_t>(BlendPriority); serialize<int32_t>(BlendType); } serialize<uint8_t>(Colors, 3); serializePair<uint8_t>(CliffColors); serialize<int8_t>(PassableTerrain); serialize<int8_t>(ImpassableTerrain); serialize<int8_t>(IsAnimated); serialize<int16_t>(AnimationFrames); serialize<int16_t>(PauseFames); serialize<float>(Interval); serialize<float>(PauseBetweenLoops); serialize<int16_t>(Frame); serialize<int16_t>(DrawFrame); serialize<float>(AnimateLast); serialize<int8_t>(FrameChanged); serialize<int8_t>(Drawn); serializeSub<FrameData>(ElevationGraphics, TILE_TYPE_COUNT); serialize<int16_t>(TerrainToDraw); serializePair<int16_t>(TerrainDimensions); if (isOperation(OP_READ)) serialize<int16_t>(Borders, getTerrainCount(getGameVersion())); else serialize<int16_t>(Borders, Borders.size()); serialize<int16_t>(TerrainUnitID, TERRAIN_UNITS_SIZE); serialize<int16_t>(TerrainUnitDensity, TERRAIN_UNITS_SIZE); serialize<int8_t>(TerrainUnitPriority, TERRAIN_UNITS_SIZE); serialize<int16_t>(NumberOfTerrainUnitsUsed); if (getGameVersion() < genie::GV_SWGB) serialize<int16_t>(Unknown1); }
double evalRPN(std::queue<std::string> &queue) { std::stack<double> evalStack; while (!queue.empty()) { auto item = queue.front(); queue.pop(); if (isOperation(item)) { double a, b; b = evalStack.top(); evalStack.pop(); a = evalStack.top(); evalStack.pop(); switch(item[0]) { case '+': evalStack.push(a+b); break; case '-': evalStack.push(a-b); break; case '*': evalStack.push(a*b); break; case '/': evalStack.push(a/b); break; case '^': evalStack.push(pow(a, b)); break; default: exit(-1); } } else { double d = std::stod(item); evalStack.push(d); } } double result = evalStack.top(); return result; }
int error4(const struct command_t* command){ if(!(isNum(command->token[0]) && isOperation(command->token[1]) && isNum(command->token[2]) && strcmp(command->token[3], "") == 0) && !(isNum(command->token[1]) && isOperation(command->token[0]) && strcmp(command->token[2], "") == 0)) return 1; return 0; }
// Logic: // - If it is a number then just add it to the actions vector (but adding two // times a number is not allowed as "3 4 6 =" is not a correct syntax. "3 + 6 =" would be correct.) // - If it is an expression-operation (like "3+4-") then do the operation with the operation before that // ("3+4" in the example) and add it to "left expression" (m_leftExpression). // - If it is a term-operation (like 3+4x) add the left number (3) to m_leftExpression and // the right handside number (4) of the expression operation to m_leftTerm. // - "=" and "None" as an operation means that a totally new calculation started from there, // meaning that the first number after them are just assigned to "left expression" (m_leftExpression). // After "=" and "None" the first temporary results (untill the next expression operator) // go to m_leftTerm and m_leftExpression remains zero. // return value: Returns true if input was valid otherwise false bool Calculator::addInput(const Action& input) { const Calculator::Action lastInput = getLastInput(); if (input.actionType == ActionType::Number) { // adding a number after a number would be an error -> that entry is ignored if (lastInput.actionType != ActionType::Number) m_actions.push_back(input); } else if (isOperation(input.actionType)) { if (lastInput.actionType == ActionType::Number) { ActionType lastOperation = getLastOperation(); switch (lastOperation) { case ActionType::Plus: if (isExpression(input.actionType) || input.actionType == ActionType::Equals) { // "3 + 4 -", "3 + 4 =" m_leftExpression.add(lastInput.value); m_leftTerm.reset(); } else if (isTerm(input.actionType)) { // "3 + 4 x", m_leftTerm.set(lastInput.value); } break; case ActionType::Minus: if (isExpression(input.actionType) || input.actionType == ActionType::Equals) { // "3 - 4 -", "3 - 4 =" m_leftExpression.add(-lastInput.value); m_leftTerm.reset(); } else if (isTerm(input.actionType)) { // "3 - 4 x", m_leftTerm.set(-lastInput.value); } break; case ActionType::Multiply: if (isExpression(input.actionType) || input.actionType == ActionType::Equals) { // "3 x 4 +", "3 x 4 =" m_leftExpression.add(m_leftTerm.getValue() * lastInput.value); m_leftTerm.reset(); } else if (isTerm(input.actionType)) // "3 x 4 x" m_leftTerm.multiplyBy(lastInput.value); break; case ActionType::Divide: if (isExpression(input.actionType) || input.actionType == ActionType::Equals) { if (lastInput.value == 0.0) { CalculatorException divByZeroException("Error: Cannot Divide By Zero", CalculatorException::ExceptionType::DividedByZero); throw divByZeroException; } else { // "3 / 4 +", "3 / 4 =" m_leftExpression.add(m_leftTerm.getValue() / lastInput.value); m_leftTerm.reset(); } } else if (isTerm(input.actionType)) // "3 / 4 x" m_leftTerm.multiplyBy(1.0 / lastInput.value); break; case ActionType::Equals: // "=" is the start of a new beginnning, see (h: *) if (isTerm(input.actionType)) { // "= 3 x " m_leftExpression.reset(); m_leftTerm.set(lastInput.value); } else if (isExpression(input.actionType)) { // "= 3 + " m_leftExpression.set(lastInput.value); m_leftTerm.reset(); } break; case ActionType::None: // "None" is the start of a new beginnning, see (h: *) if (isTerm(input.actionType)) { // "3 x " m_leftExpression.reset(); m_leftTerm.set(lastInput.value); } else if (isExpression(input.actionType)) { // "3 + " m_leftExpression.set(lastInput.value); m_leftTerm.reset(); } break; } m_actions.push_back(input); return true; } } return false; }
void Graphic::serializeObject(void) { /* * Workaround for Name strings, because in RoR, SWGB and TC there are * Unknown values after the \0 on some strings. In RoR the size should be * ok, but it may not be right in >= SWGB */ if (isOperation(OP_WRITE)) { if (CstrName == 0 || Name.compare(CstrName) != 0) serialize<std::string>(Name, getNameSize()); else serialize<char>(&CstrName, getNameSize()); if (CstrName2 == 0 || Name2.compare(CstrName2) != 0) serialize<std::string>(Name2, getName2Size()); else serialize<char>(&CstrName2, getName2Size()); } else { serialize<char>(&CstrName, getNameSize()); serialize<char>(&CstrName2, getName2Size()); Name = std::string(CstrName); Name2 = std::string(CstrName2); } serialize<int32_t>(SLP); serialize<int8_t>(Unknown1); serialize<int8_t>(Unknown2); /// TODO: priority? serialize<int8_t>(Layer); serialize<int16_t>(PlayerColor); serialize<int8_t>(Replay); serialize<int16_t, COORDINATES_SIZE>(Coordinates); serializeSize<uint16_t>(DeltaCount, Deltas.size()); serialize<int16_t>(SoundID); serialize<int8_t>(AttackSoundUsed); serialize<uint16_t>(FrameCount); serialize<uint16_t>(AngleCount); serialize<float>(NewSpeed); serialize<float>(FrameRate); serialize<float>(ReplayDelay); serialize<int8_t>(SequenceType); serialize<int16_t>(ID); if (getGameVersion() >= genie::GV_AoK) serialize<int16_t>(MirroringMode); else { int8_t tmp = MirroringMode; serialize<int8_t>(tmp); MirroringMode = tmp; } serializeSub<GraphicDelta>(Deltas, DeltaCount); if (AttackSoundUsed != 0) { if (isOperation(OP_WRITE) && AttackSounds.size() > AngleCount) std::cerr << "Warning: There'are more GraphicAttackSounds than angles!" << std::endl; serializeSub<GraphicAttackSound>(AttackSounds, AngleCount); } }