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 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Token
{
friend class MathFunctions;
private:
string TokenName;
int priority;
bool Operator;
bool Number;
bool Symbol;
public:
int GetPriority(string ch){return priority;};
bool GetAssociative(string ch);
bool IsOperator(string ch);
bool IsNumber(string ch);
bool IsVariable(string ch);
bool IsFunction(string ch);
bool IsSymbol(string ch);
Token(){};
Token(string Name,bool Ope, bool Num, bool Sym){TokenName=Name,Operator=Ope,Number=Num,Symbol=Sym;};
};
bool Token::IsNumber(string ch)
{
string numbers="0123456789";
for(int i=0; i<10; i++)
{
if(ch[i]==numbers[i]) // <--
return true;
}
return false;
}
bool Token::IsOperator(string ch)
{
string operators="+-*/";
for(int i=0; i<4; i++)
{
if(ch[i]==operators[i]) // <--
return true;
}
return false;
}
class MathFunctions
{
private:
vector<Token> ToRPN(vector <Token>);
public:
MathFunctions(); // <--
int Parse(string exp);
bool Tokenization(string exp);
bool Calculator();
};
bool MathFunctions::Tokenization(string exp)
{
int j;
string tmp;
vector <Token> tokens;
for(int i=0; i<exp.size(); i++)
{
if(isdigit(exp[i]))
{
tmp = exp[i];
j=i+1;
while(isdigit(exp[j]))
{
tmp += exp[j];
j++;
}
cout<<tmp<<endl;
Token tok(tmp,false,true,false);
tokens.push_back(tok);
i = j-1;
}
else if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')
{
cout<<exp[i]<<endl;
tmp = exp[i];
Token tok(tmp,true,false,false);
tokens.push_back(tok);
}
else if(exp[i]=='('||exp[i]==')')
{
tmp = exp[i];
Token tok(tmp,false,false,true);
tokens.push_back(tok);
}
}
for (vector<Token>::iterator it = tokens.begin() ; it != tokens.end(); ++it)
cout <<" "<< (*it).TokenName;
return false; // <-- or whatever
}
MathFunctions::MathFunctions()
{}
vector<Token> MathFunctions::ToRPN(vector <Token> tok)
{
vector <Token> RPN;
vector <Token> Stack;
for(vector<Token>::iterator it = tok.begin(); it != tok.end(); ++it)
{
if((*it).IsNumber((*it).TokenName))
{
}
}
return RPN; // <-- OR WHATEVER
}
int main(int argc, char * argv[])
{
string exp;
getline(cin,exp);
return 0;
}
|