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;
}
|