Expression::Expression(char* s) { strcpy(expression, s); expressionIndex = 0; tokenize(); toPostfix(); }
int main() { char *test; /*To determines if we've hit EOF*/ char infix[EXPRLEN + 1]; /*Gets input*/ char *postfix; /*Banner*/ printf("Assignment 4, Question 1. 074.216, L01. Trevor Bekolay, 6796723\n"); printf("Please enter formulas. Spaces are required after all numbers! But aside from that go nuts. Ctrl+d (or EOF) will end the porgram.\n"); /*Get the first line of input*/ test = fgets(infix, EXPRLEN, stdin); /*Continue getting input until EOF*/ while(test != NULL) { printf("Infix equation: %s\n",infix); /*Changes infix to postfix*/ postfix = toPostfix(infix); printf("Postfix equation: %s\n",postfix); /*Evaluate and print result*/ printf("Evaluates to: %d\n\n",evalPostfix(postfix)); /*Read in another expression*/ test = fgets(infix, EXPRLEN, stdin); } printf("~End of processing~"); return 0; }
int main(int argc,char** argv) { if(strcmp(argv[1],"--help")==0||strcmp(argv[1],"-h")==0){ printf("Command:\n-h --help print command\n-calc use calc and exit\n"); } else if(strcmp(argv[1],"-calc")==0){ char virogenie[SIZE]; Node *stack; initALL(&stack); Node *postFix; initALL(&postFix); Simvol result; fgets(virogenie,SIZE,stdin); toPostfix(&postFix,virogenie,&stack); dellList(&stack); //gogoPostfix(&postFix,&stack); chekTop(&result,stack); dellList(&stack); dellList(&postFix); printf("%.3f\n",result.numb); } return 0; }
std::string Antecedent::toPostfix(const Expression* node) const { if (not isLoaded()) { throw fl::Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); } if (not node) node = this->_expression; if (dynamic_cast<const Proposition*> (node)) { return node->toString(); } std::stringstream ss; if (const Operator * fuzzyOperator = dynamic_cast<const Operator*> (node)) { ss << toPostfix(fuzzyOperator->left) << " " << toPostfix(fuzzyOperator->right) << " " << fuzzyOperator->toString() << " "; } else { ss << "[antecedent error] unknown class of Expression <" << node->toString() << ">"; } return ss.str(); }
Function::Node* Function::parse(const std::string& formula) { if (formula.empty()) throw fl::Exception("[function error] formula is empty", FL_AT); std::string postfix = toPostfix(formula); std::stack<Node*> stack; std::istringstream tokenizer(postfix); std::string token; FunctionFactory* factory = fl::FactoryManager::instance()->function(); while (tokenizer >> token) { Element* element = factory->getObject(token); bool isOperand = not element and token != "(" and token != ")" and token != ","; if (element) { if (element->arity > (int) stack.size()) { std::ostringstream ss; ss << "[function error] " << (element->isOperator() ? "operator" : "function") << " <" << element->name << "> has arity <" << element->arity << ">, " "but found <" << stack.size() << "> element" << (stack.size() == 1 ? "" : "s"); throw fl::Exception(ss.str(), FL_AT); } Node* node = new Node(element->clone()); node->left.reset(stack.top()); stack.pop(); if (element->arity == 2) { node->right.reset(stack.top()); stack.pop(); } stack.push(node); } else if (isOperand) { Node* node; try { scalar value = fl::Op::toScalar(token); node = new Node(value); } catch (std::exception& ex) { (void) ex; node = new Node(token); } stack.push(node); } } if (stack.size() != 1) throw fl::Exception("[function error] ill-formed formula <" + formula + ">", FL_AT); return stack.top(); }
double ExprCalculator::calculateExpr(const QString &expr) { return calculatePostfixExpr(toPostfix(expr)); }