Recursive Binary Postfix

Hello everyone,

I'm trying to evaluate binary postfix recursively.

For simplification purposes, it only needs to work for single digits. I have no idea how to do the recursive call. It would be greatly appreciated if someone could give me some insight.

Here is what I have:

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
#include <iostream>
#include <fstream>
#include <stack>

//had trouble with the recursive call in the rdtdp function

using namespace std;
//----------------------------------------------------------------
//----------------------------------------------------------------
class Binary_Postfix_Parser{
public:
    Binary_Postfix_Parser();
    void Get_Input();
    bool Is_Opr(char);
    void Validity_chk(string);
    int rdtdp(string::iterator,string::iterator);
    string::iterator Partition(string::iterator,string::iterator);
private:
    int Count;
    stack<char>op;
};
//----------------------------------------------------------------
//----------------------------------------------------------------
Binary_Postfix_Parser::Binary_Postfix_Parser(){
    Count=0;
}
//----------------------------------------------------------------
void Binary_Postfix_Parser::Get_Input(){
    string line;
    ifstream txtfile("C:/blech.txt");
    if (txtfile.is_open()){
        while(getline(txtfile,line)){
            cout<<"Parsing: "<<line<<endl;
            Validity_chk(line);
        }
        txtfile.close();
    }
}
//----------------------------------------------------------------
bool Binary_Postfix_Parser::Is_Opr(char token){
    bool chk=false;
    switch(token){
        case '+':chk=true;break;
        case '-':chk=true;break;
        case '*':chk=true;break;
        default:chk=false;break;
    }
    return chk;
}
//----------------------------------------------------------------
string::iterator Binary_Postfix_Parser::Partition(string::iterator l,string::iterator r){
    int c=0;
    string::iterator mid;
    for(string::iterator it=r;it!=l;it--){ //go backwards through string
        //cout<<c<<" on "<<*it<<endl<<endl;
        if(Is_Opr(*it)==true){c++;}               //if its an operator, add one
        if(isdigit(*it)==true){c--;}              //if its a digit, subtract one
        if(c==0&&it!=r){mid=it-1; break;}       //break on 0 for split
    }
    return mid;
}
//----------------------------------------------------------------
int Binary_Postfix_Parser::rdtdp(string::iterator leftaddr,string::iterator rightaddr){
    if(leftaddr==rightaddr){
        return *leftaddr;
    }
    else{
        string::iterator mid=Partition(leftaddr,rightaddr);
        string leftsub,rightsub;
        for(string::iterator i=leftaddr;i<mid;++i){leftsub+=*i;}
        for(string::iterator i=mid+1;i<rightaddr;++i){rightsub+=*i;}
        cout<<"Left substring: "<<leftsub<<"  Mid: "<<*mid<<"   Right substring: "<<rightsub<<endl; //works fine up until here
//        switch(*mid){
//            case '+':return rdtdp(leftsub.begin(),leftsub.end())+rdtdp(rightsub.begin(),rightsub.end());break;
//            case '-':return rdtdp(leftsub.begin(),leftsub.end())-rdtdp(rightsub.begin(),rightsub.end());break;
//            case '*':return rdtdp(leftsub.begin(),leftsub.end())*rdtdp(rightsub.begin(),rightsub.end());break;
//        }
      }
}

//----------------------------------------------------------------
void Binary_Postfix_Parser::Validity_chk(string in){
    for(char i : in){             //for each symbol in the string
        if (isdigit(i)==true){    //if the symbol is a digit
            Count++;              //increase the count
        }
        else if(Is_Opr(i)==true){//if the symbol is an operator
            Count--;             //decrease the count
            if(Count==0){
                break;           //break if it gets to 0
            }
        }
        cout<<Count;
    }

    if(Count!=1){
        cout<<endl<<"The string is invalid. "<<endl<<endl;
        }
    else if(Count==1){
        cout<<endl<<"The string is valid. "<<endl<<endl;
        cout<<"Answer: "<<rdtdp(in.begin(),in.end())<<endl<<endl;//pass the beginning and end addresses of string
        }
    Count=0;
}
//----------------------------------------------------------------

int main()
{
    Binary_Postfix_Parser start;
    start.Get_Input();
    return 0;
}


I would never do this recursively if I had a choice. It's for a homework assignment. I just don't know where i'm going wrong with the recursive call.

Here's the output:
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
Parsing: 51-62+3+524*-*-
121232323454321
The string is valid.
Left substring: 51  Mid: -   Right substring: 62+3+524*-*-
Answer: 45


Parsing: 32+4*53+-5+*62+
12121232121
The string is invalid.


Parsing: 347-49-*-
123234321
The string is valid.
Left substring:   Mid: 3   Right substring: 47-49-*-
Answer: 51


Parsing: 334-27-9*+*78*+
123234343212321
The string is valid.
Left substring: 334-27-9*+  Mid: *   Right substring: 78*+
Answer: 42


Parsing: 567*-67+2*33++
12321232323432
The string is invalid.


Process returned 0 (0x0)   execution time : 0.029 s
Press any key to continue.

Last edited on
Topic archived. No new replies allowed.