c++ - Infix to postfix calculator/ reading input -
i need implement infix postfix calculator using stacks , queues in c++. know algorithm follow i'm having problem starting. programme should go following:
enter valid infix expression: 3 + 4 * 5 / 6 resulting postfi look is: 3 4 5 * / 6 + result is: 6.3333
so need read input command line , that's i'm having problem. code far:
using namespace std; #include <iostream> #include <stdlib.h> #include <stack> #include <queue> int main() { stack <string> convert; stack <string> evaluate; queue <string> store; string data; float num; cout << "enter valid infix expression: "; while (getline(cin, data)) { store.push(data); } homecoming 0; }
my problem here how stop loop , how can numbers string input can force them queue print them later on? code pushing entire string the first slot in queue. hope can help.
thank you
use class tools written me
tools.h
static class tools { public: static char* topostfix(char* source); static double calculatepostfix(char* source); static bool contain(char* source,char character); };
tools.cpp
#include "tools.h" #include <stack> #include <iostream> #include <string> using namespace std; char* tools::topostfix(char* source) { stack<char> symbol; string postfix = ""; int = 0; char variables[] = { "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" }; bool find = false; while (*source != '\0') { switch (*source) { case '+': case '-': case '*': case '/': symbol.push(*source); break; case ')': postfix += symbol.top(); symbol.pop(); default: find = tools::contain(variables, *source); if (find) { postfix += *source; find = false; } } source++; } // attach other operator in stack(pop all) while (!symbol.empty()) { postfix += symbol.top(); symbol.pop(); } char* p = new char(postfix.length()); const char* o = postfix.c_str(); (int = 0; < postfix.length(); i++) p[i] = o[i]; homecoming p; } double tools::calculatepostfix(char* source) { char numbers[] = { "0123456789" }; stack<double> number; (int = 0; < strlen(source); i++) { char character = source[i]; if (tools::contain(numbers, character)) { number.push(atof(&character)); } else { double number1, number2; switch (character) { case '+': number2 = number.top(); number.pop(); number1 = number.top(); number.pop(); number.push(number1 + number2); break; case '-': number2 = number.top(); number.pop(); number1 = number.top(); number.pop(); number.push(number1 - number2); break; case '*': number2 = number.top(); number.pop(); number1 = number.top(); number.pop(); number.push(number1 * number2); break; case '/': number2 = number.top(); number.pop(); number1 = number.top(); number.pop(); number.push(number1 / number2); break; } } } homecoming number.top(); } bool tools::contain(char* source, char character) { int size = strlen(source); (int = 0; < size ; i++) { if (source[i] == character) homecoming true; } homecoming false; }
usage :
std::cout << "postfix : " << tools::topostfix("a+(b*c+t)") << std::endl; std::cout << "answer : " << tools::calculatepostfix("24*95+-") << std::endl;
c++ stack queue calculator
No comments:
Post a Comment