| | |
Mathematics In A Stack
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 3
Reputation:
Solved Threads: 0
I have read in problems "INFIX" AND CHANGED THEM TO "POSTFIX" or RPN notation, however i need to evaluate the problems and i can't get this to work. I am not sure where to go from the infix to postifix i have. Please help. My Stack.h is just a basic Stack defined. I am not sure what to do next. Any ideas?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <cassert> #include <stdlib.h> #include <cstring> using namespace std; #include "Stack.h" struct temp1 { char line[30]; }; string RPN(string exp); void main() { string exp; int count= 0; int result; temp1 temp[128]; ifstream File; File.open("a2.txt"); if (!File.is_open()) { cout << "File not found!!!!"; exit(1); } while(File.peek() != EOF) { cout << "\nThe Infix Expression is: \n" << endl; File.getline(temp[count].line, 30); cout << temp[count].line << endl; for (;;) { cout << "\nThe Postfix Expression is: \n"; exp = temp[count].line; cout << RPN(exp) << endl; break; } cout << "\nThe Evaluated Expression is:\n"; cout << RPN(exp) << endl; cout << "\nThe Answer is:\n" << endl; // result = -exp; // cout << result << "\n"; count++; } } string RPN(string exp) { char token, topToken; Stack opStack; string RPNexp; const string BLANK = " "; for (int i= 0; i < exp.length(); i++) { token = exp[i]; switch(token) { case ' ' : break; case '(' : opStack.push(token); break; case ')' : for (;;) { assert (!opStack.empty()); topToken = opStack.top(); opStack.pop(); if (topToken == '(') break; RPNexp.append(BLANK + topToken); } break; case '+' : case '-' : case '*' : case '/' : for (;;) { if (opStack.empty() || opStack.top() == '(' || (token == '*' || token == '/') && (opStack.top() == '+' || opStack.top() == '-') ) { opStack.push(token); break; } else { topToken = opStack.top(); opStack.pop(); RPNexp.append(BLANK + topToken); } } break; default : RPNexp.append(BLANK + token); } } for(;;) { if (opStack.empty()) break; topToken = opStack.top(); opStack.pop(); if (topToken != '(') { RPNexp.append(BLANK + topToken); } else { cout << " Error in Infix Expressions\n"; break; } } return RPNexp; } void Stack::push(const StackElement & value) { if (myTop < STACK_CAPACITY - 1) { ++myTop; myArray[myTop] = value; } } void Stack::display(ostream & out) const { for (int i = myTop; i >= 0; i--) out << myArray[i] << endl; } StackElement Stack::top() const { if (myTop >= 0) return myArray[myTop]; cout << "--- Stack is Empty----\n"; } void Stack::pop() { if (myTop >= 0) myTop--; else cout << "Stack is empty can't remove a value\n"; }
Last edited by alc6379; Feb 23rd, 2005 at 6:22 pm. Reason: added [code] tags
•
•
Join Date: Feb 2005
Posts: 31
Reputation:
Solved Threads: 2
Take a look at http://www.calculator.org/rpn.html, I think this will help you fill in the gaps. Good luck!
![]() |
Similar Threads
- relation of mathematics with computer science (Computer Science)
- October 2004 - Mathematics: In Depth (Software Development Job Offers)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: question
- Next Thread: Beginning: Need Help on an Issue.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






Thanks for everything so far