-
October 11th, 2009, 02:38 PM
#1
need help > the typical infix statements to postfix
I got stuck in this program and dont know how to start to phase 2, i finish phase 1 quite good bt i dont understand connection phase 1 and phase 2. so i hope some one can help me pls, thank you very much.....
phase 2:Expand Phase 1 to translate the typical infix statements to postfix. The postfix should be printed out and then calculated. For example: (3+5)*9? should output 3 5 + 9 * = 72.
and i finish my phase 1
header.h
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Lexeme
{
private:
string Lex;
string type;
public:
Lexeme();
string getLex ();
string gettype ();
void setLex (string);
void settype (string);
};
main.cpp
Code:
#include "TheHeader.h"
#include <stack>
void ReadFile();
int main ()
{
ReadFile();
return 0;
}
void ReadFile()
{
char a;
string str = "";
Lexeme temp;
stack <Lexeme> name;
ifstream file ("sample.txt",ios::in);
if(!file)
{
cout<<"Unable to open the file!!!" <<endl;
exit(1);
}
file >> a;
while (file)
{
if (a == '/' ||a == '*' ||a == '+' ||a == '-' ||a == ';' ||a == '^'||a == '='||a == '('||a == ')')
{cout << a << " symbol" << endl ;
str = a;
temp.setLex(str);
temp.settype("SYMBOL");
name.push(temp);
file >> a;
}
else {str = a;file >> a;
while( a >= '0' && a <= '9' || a == '.')
{
str.append(1,a);
file >> a;
}
temp.setLex(str);
temp.settype("NUMBER");
name.push(temp);
cout << str << " number" <<endl ;
str = "";
}
}
file.close();
}
header.cpp
Code:
#include "TheHeader.h"
Lexeme::Lexeme()
{
Lex = "";
type ="";
}
string Lexeme::getLex ()
{
return Lex;
}
string Lexeme::gettype ()
{
return type;
}
void Lexeme::setLex(string b)
{
Lex = b;
}
void Lexeme::settype (string c)
{
type = c;
}
hope someone can guide me to right direction, i will spend all my time of this weekend to work on this problem only
thank you
-
October 12th, 2009, 05:28 AM
#2
Re: need help > the typical infix statements to postfix
ahh i just asked almost the same question, thought i could help give you some direction
Code:
toPostFix()
{
Lexeme temp;
List<Lexeme> your_List_Of_Infix;//use std::list or CList
List<Lexeme> temp;//temporary
Stack<Lexeme> theStack;
//here i omited the parentheses "(" and ")", you may add it in the
//switch statement after you get the normal equation withouth
// the parentheses right first
for(each item in your_List_Of_Infix)
{
switch(your_List_Of_Infix[i].getType)
{
case "NUMBER":
theStack.push(your_List_Of_Infix[i]);
break;
case "SYMBOL":
while(!theStack.isEmpty() && your_List_Of_Infix[i] has highprecedence that topOfStack)
{
temp.AddTail(theStack.GetTop());
theStack.pop()
}
push(your_List_Of_Infix[i]);
break;
};
}
//now get remaining items in stack
while(!theStack.isEmpty())
{
temp.AddTail(theStack.GetTop());
theStack.pop();
}
//now your "temp" list has the postfix expression,
//all you need to do now is to iterate through the list to
//and evaluate the expression to get the value,
//pseudokod should be easy here, just use the same stack
for (each item in temp list)
{
if type == NUMBER
push(the item)
else if type == SYMBOL
{
operand2 = top of stack;
pop stack;
operand1 = top of stack;
pop stack;
//operation here depand on the SYMBOL type
result = operand1 + operand2
push(result)
}
}
//after the loop above, you'll get the result from the top of the stack.
}
Come Join This Poll Where are we from? (Ultimate)
Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
An idle brain is the devil's workshop. - unknown
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|