int main() { Expression* infix; Expression* postfix; std::string* evaluatedExpression; char newline[80]; char nextChar; std::fstream expressions; expressions.open("TestExpressions.txt"); infix = new Expression; infix->read(expressions); while(expressions.good()) { postfix = new Expression; evaluatedExpression = new std::string; infix->print(std::cout); convertToPostfix(*infix, *postfix); postfix->print(std::cout); evaluatePostfix(*postfix, *evaluatedExpression); std::cout << *evaluatedExpression << "\n"; delete infix; delete postfix; delete evaluatedExpression; expressions.getline(newline, 79, '\n'); // move past end of line nextChar = expressions.peek(); // peek next character to check if at end of file infix = new Expression; infix->read(expressions); } delete infix; expressions.close(); return 0; }
int main(void){ char str[100], input[100]; // Prints Intro printf("Simple Calculator\n"); // Prints the input prompt, along with goto label to jump back to input inputPrompt: printf(">>>"); fgets(str,sizeof(str),stdin); sscanf(str, "%[^\n]",input); // printf("The input is: %s\n",input); /* Checks to see if the user wants to quit */ if(strcmp(input, "q") == 0 || strcmp(input,"Q") == 0){ printf(" Goodbye!"); exit(0); } /* Checks to see if the user asked for help */ else if(strcmp(input,"h") == 0 || strcmp(input,"H") == 0){ printhelp(); } /* If the string entered has passed all pre-checks, then send off to functions dealing with arithmetic */ else{ /* Send the user input to the function that will standardise the whitespaces to use as delimiters * (make sure there is only ever one whitespace at most) */ standardiseWhitespaces(input); // printf("After White spaces: %s\n",input); /* Run through a bunch of checks, print an error message and go back to the input prompt if something is found */ if(operatorsIncorrect(input) == TRUE || tooManyPoints(input) == TRUE || invalidCharacters(input) == TRUE) { printf(" Error: Illegal input!\n"); goto inputPrompt; } if(divideByZero(input) == TRUE){ printf(" Error: Divide by zero!\n"); goto inputPrompt; } /* Converts any e's to scientific notation */ convertFromScientificNotation(input); // printf("After Scientific Notation: %s\n",input); /* Put string through function to turn it in to post-fix notation */ convertToPostfix(input); // printf("After postfix: %s\n",input); /* Puts the postfix notation string in to the arithmetic function, which then interprets the equation, solves it , then prints it */ printf(" %lf\n",arithmetic(input)); } /* Goes to the start of the program where it prompts for input */ goto inputPrompt; }
int main(int argc, char const *argv[]) { char infix[40]="(6+2)*5-8/4"; char postfix[40]=""; convertToPostfix(infix,postfix); printf("%s\n",postfix ); return 0; }
int compute(char* infix){ //allocate string (80 chars) char *postfix = malloc(80 * sizeof(char)); //convert to postfix convertToPostfix(infix, postfix); //compute answer return computePostfix(postfix); }
int main(void) { char infix[50] = {'(','3','-','1',')','*','8','+','2','\0'}; char infix2[50]; char postfix[50] = {'\0'}; int i = 0, result = 0; char cont = 'y'; while (toupper(cont) == 'Y') { //Clear infix2 for(i = 0; i < 50; i++) { infix2[i] = '\0'; } //Clear postfix for(i = 0; i < 50; i++) { postfix[i] = '\0'; } //Get expression from user _flushall(); printf("Enter an expression to convert to postfix (no spaces): "); gets(infix2); //Convert to postfix convertToPostfix(infix2, postfix); //Print postfix expression printf("\nPostfix: "); i = 0; while(postfix[i] != '\0') { printf("%c ", postfix[i]); i++; } //Calculate result using postfix expression result = evaluatePostfixExpression(postfix); //Print result printf("\nResult: %d\n", result); do{ _flushall(); printf("\nWould you like to continue? <Y or N>: "); scanf("%c", &cont); if(toupper(cont) != 'Y' && toupper(cont) != 'N') { printf("Invalid option, please choose Y or N\n"); } }while(toupper(cont) != 'Y' && toupper(cont) != 'N'); } return 0; }
int main(int argc, char const *argv[]) { printf("Enter the infix expression.\n"); char infix[MAXSIZE]; //defines char array to read input into scanf("%s",infix); //reads in infix string from user char postfix[MAXSIZE]; printf("The original infix expression is:\n%s\n",infix); convertToPostfix(infix,postfix); printf("The expression in postfix notation is:\n%s\n",postfix); return 0; }
// evaluate // Evaluates the entire expression // Parameters: // expression (input string) the expression to evaluate // Pre-condition: The expression is correctly formatted // Return: Value of the expression int evaluate(const char expression[]) { TokenList expr(expression), //Original expression postExpr, //Expression converted to postfix numberStack; //Stack of numbers to facilitate evaluation //Converts expr into postfix and store it into postExpr convertToPostfix(expr, postExpr); std::cout << "Original expression: " << expr << std::endl; std::cout << "Converted postfix expression: " << postExpr << std::endl; for (ListIterator iter = postExpr.begin(); iter != postExpr.end(); iter.advance()) { Token t = iter.token(); if (t.isInteger()) { numberStack.push_front(t); } else { //This is the huge bit that handles operators //val2 is created but uninitialized to handle binary operators and //unary operators ('~') easily in the same switch int value = numberStack.pop_front().integerValue(); int val2 = 0; switch (t.tokenChar()) { case '*': val2 = numberStack.pop_front().integerValue(); val2 *= value; numberStack.push_front(Token(val2)); break; case '/': val2 = numberStack.pop_front().integerValue(); val2 /= value; numberStack.push_front(Token(val2)); break; case '%': val2 = numberStack.pop_front().integerValue(); val2 %= value; numberStack.push_front(Token(val2)); break; case '+': val2 = numberStack.pop_front().integerValue(); val2 += value; numberStack.push_front(Token(val2)); break; case '-': val2 = numberStack.pop_front().integerValue(); val2 -= value; numberStack.push_front(Token(val2)); break; case '~': value = -value; numberStack.push_front(Token(value)); break; } } } //Value left on the top of the number stack is the final value of the expression return numberStack.pop_front().integerValue(); }