infix to postfix reading from Text File

So i solved my earlier problem
but now how do i make it read from the text file that i have and output the postfix notations? Because right now it is only outputting what is currently in the text file for example

6+9
2+7*8
(1+5)*3
5+(6-2)*((2-8)

Header file is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef STACKS_H
#define STACKS_H
#include <iostream>
using namespace std;



const int STACKS_CAPACITY = 128;
typedef int StacksElement;

class Stacks

{
	private:

		StacksElement myArray[STACKS_CAPACITY];
		int myTop;

	public:
		
		Stacks();
		void push(const StacksElement & value);
		void pop();
		bool empty() const;
		void display(ostream & out) const;
		StacksElement top() const;



};

inline Stacks::Stacks()
{
	myTop = -1;
}



inline bool Stacks::empty() const
{
	return (myTop == -1);
}




inline void Stacks::pop()
{
	 if(myTop >= 0)
		myTop--;
	 else    
		 cerr << "*** Stack is empty -- "            "can't remove a value ***\n";

}


void Stacks::push(const StacksElement & value)
{
	if (myTop < STACKS_CAPACITY - 1)
	{			
		++myTop;
		myArray[myTop] = value;
	}
	else   cerr <<     "*** Stack full -- can't add new value ***\n"    "Must increase value of STACK_CAPACITY in Stack.h\n";
}


inline void Stacks::display(ostream & out) const
{  for (int i = myTop; i >= 0; i--) 
	    out << myArray[i] << endl;
} 


StacksElement Stacks::top() const
{
  if (myTop >= 0)   
  return (myArray[myTop]);
  else  
  {    cerr << "*** Stack is empty "            " -- returning garbage ***\n";   
  return myArray[STACKS_CAPACITY-1];  
  }
} 
#endif 



test client cpp is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
using namespace std;
#include "Stacks.h"
#include <fstream>
#include <string>
#include <cassert>
#include <cctype>




string postfix(string exp);

int main()
	
{
	string infixExp;

cout << "The current infix expressions are: " << endl;

 string getcontent;
    ifstream openfile ("infix.txt");
    if(openfile.is_open())
    {
        while(! openfile.eof())
        {
            openfile >> getcontent;
            cout << getcontent << endl;
        }
    }

cout << "Now to convert into post-fix expression and compute the result: " << endl;




cin.get();
cin.get();
return 0;

}

string postfix(string exp)
////////////////////////////////////////////////////////////////////////////////////
// Converts infix to postfix expression
{
	char token; 
	char topToken;
	Stacks opStack;
	string postfixExp;
	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;
					   postfixExp.append(BLANK + topToken);
				   }
				   ;
				   break;
		case '+' : case '-' :
		case '*' : case '/' : case '%' :
			for (;;)
			{
				if (opStack.empty() ||
					opStack.top() == '(' ||
					(token == '*' || token == '/' || token == '%') &&
					(opStack.top() == '+' || opStack.top() == '-'))
				{
					opStack.push(token);
					break;
				}
				else
				{
					topToken = opStack.top();
					opStack.pop();
					postfixExp.append(BLANK + topToken);
				}
			}
			break;
		default : // operand
			postfixExp.append(BLANK +token);
			for(;;)
			{
				if (!isalnum(exp[i+1])) break; // end of identifier
				i++;
				token = exp[i];
				postfixExp.append(1, token);
			}
		}
	}


	// pop remaining operators on the stack
	for (;;)
	{
		if (opStack.empty()) break;
		topToken = opStack.top();
		opStack.pop();
		if (topToken != '(')
		{
			postfixExp.append(BLANK + topToken);
		}
		else
		{
			cout << " *** Error in infix expression *** \n " ;
			break;
		}
	}
	return postfixExp;
}
Last edited on
anybody?
You kind of double posted..http://www.cplusplus.com/forum/general/122886/#msg670277

Anyways not sure how many lines you have of infix notations but if you have more than one I would suggest a vector to store them.

If you only have one though you should put something like

infixExp = getcontent;

then
1
2
3
postfixExp = postfix( infixExp );
std::cout << "Infix: " << infixExp << std::endl;
std::cout << "Postfix: " <<  postfixExp << std::endl;


mm i tried it even with 1 and it doesn't work
bump
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>

using std::string ;

string postfix( string exp ) ;

int main()
{
    const char* const input_file_name = "infix.txt" ;
    const char* const output_file_name = "postfix.txt" ;

    std::ifstream in_file(input_file_name) ;
    if( !in_file )
    {
        std::cerr << "failed to open input file\n" ;
        return 1 ;
    }

    std::ofstream out_file(output_file_name) ;
    if( !out_file )
    {
        std::cerr << "failed to open output file\n" ;
        return 1 ;
    }

    string infix ;
    while( std::getline( in_file, infix ) ) // for each line in the input file
        out_file << postfix(infix) << '\n' ; // write out the postfix to the output file

    // final sanity check
    if( in_file.eof() && out_file.good() ) ; // things are fine, keep quiet

    else
    {
        std::cerr << "there was an i/o error\n" ;
        return 1 ;
    }
}
hm still doesn't work, try compiling my code with that
bump
If you cannot be bothered to tell us why you believe the code supplied doesn't work, please don't bother bumping the thread.
just trying to get some help, im not great at coding ok. dont need to be a douche about it, not everybody knows everything there is to coding.
Last edited on
not everybody knows everything there is to coding.

I don't know some people around here seem to know a lot...JLBorges being one of those people and he tried to help you.
*edit misread last post was thinking it said nobody knows everything about coding :P

What I think Cire was trying to say was.. "It doesn't work" isn't very much information. What exactly does not work?
Last edited on
Topic archived. No new replies allowed.