#include
#include
using namespace std;
The class Token
{
Private:
A double value.
Char kind;
Public:
Void the set (double v)//set num token
{
(* this). Value=https://bbs.csdn.net/topics/v;
This - & gt;='n' kind;
}
Void the set (char c)//set char token
{
The switch (c)
{
Case: '(' case') 'case' ^ ': case:' * 'case'/' :
Case: '+' case '-' : case 'q' : case '; ':
{
Kind=c;
Value=https://bbs.csdn.net/topics/0;
break;
}
Default:
cout <"Operator \ '" & lt;
}
}
Double getvalue () const//for functions provides that do not modify this
{
The return value.
}
Const char getkind ()
{
Return kind;
}
Friend ostream& Operator<(ostream& Out, const Token& T)
{
If (t.k ind=='n')
The out & lt;
The out & lt;
}
};
Token gettoken ()//this function reads one Token at a time
{
Token t;
A double v.
char c;
Cin & gt;> C;
The switch (c)
{
Case: '(' case') 'case' ^ ': case:' * 'case'/' :
Case: '+' case '-' : case 'q' : case '; ':
{
T.s et (c);
return t;
}
Case 'case' 0 ': case' 1 ': case' 2 ': case' 3 ':
Case: '4' case '5' : case '6' : case '7' : case '8' :
Case '9' :
{
Cin. Putback (c);//put it back into the out stream
Cin & gt;> v;
T.s et (v);
return t;
}
Default:
cout <"Invalid character: \ '" & lt;
}
}
Double the evaluate (vector
{
//regularization of -
For (size_t I=beg; i
If (expr [I] getkind ()=='-')
{
If (I==beg)
{
Expr [I]. Set (expr - [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I + 1);
}
Else if (expr [I - 1]. Getkind ()!='n')
{
Expr [I]. Set (expr - [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I + 1);
}
}
}
For (size_t I=end - 1; I & gt; Beg; I -)
{
If (expr [I] getkind ()=='^')
{
Expr [I - 1]. The set (pow (expr [I - 1]. The getvalue (), expr [I + 1] getvalue ()));
Expr. Erase (expr. The begin () + I);//removes ^
Expr. Erase (expr. The begin () + I);//removes right term
End -=2;
}
}
//handle the mul and div
For (size_t I=beg; i
If (expr [I] getkind ()=='*')
{
Expr [I - 1]. The set (expr [I - 1]. The getvalue () * expr [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I);//removes *
Expr. Erase (expr. The begin () + I);//removes right term
I -;
End -=2;
}
Else if (expr [I] getkind ()=='/')
{
Expr [I - 1]. The set (expr [I - 1]. The getvalue ()/expr [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I);//removes/
Expr. Erase (expr. The begin () + I);//removes right term
I -;
End -=2;
}
}
//handle the add and sub
For (size_t I=beg; i
If (expr [I] getkind ()=='+')
{
Expr [I - 1]. The set (expr [I - 1]. The getvalue () + expr [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I);//removes +
Expr. Erase (expr. The begin () + I);//removes right term
I -;
End -=2;
}
Else if (expr [I] getkind ()=='-')
{
Expr [I - 1]. The set (expr [I - 1]. The getvalue () - expr [I + 1] getvalue ());
Expr. Erase (expr. The begin () + I);//removes -
Expr. Erase (expr. The begin () + I);//removes right term
I -;
End -=2;
}
}
Return expr) [r]. Beg getvalue ();
}
Double the evaluate (vector
{
While (expr. The size () & gt; 1)
{
For (size_t I=expr. The size () - 1; I & gt; 1; I -)
{
If (expr [I] getkind ()==') ')
{
Size_t rparenpos=I;
For (size_t I=0; i
If (expr [I] getkind ()=='(')
{
Size_t lparenpos=I;
Evaluate (expr, lparenpos + 1, rparenpos);
Expr. Erase (expr. The begin () + lparenpos);
Expr. Erase (expr. The begin () + lparenpos + 1);
I -;
}
The else
{
cout <"Invalid expression: parentheses not matched";
return 0;
}
}
}
The else
{
Evaluate (expr, 0, expr. The size ());
}
}
}
Return expr [0]. Getvalue ();
}
Int main ()
{
Vector
Token t;
Double exprval;
cout <"Enter an algebraic expression to the as (1 + 12.5) * 4 ^ 2 \ n"
<"For now we can handle only: +, -, *,/. \ n", "
<"Finish expression with character \ '; \ '"
{
T=gettoken ();
If (t.g etkind ()=='q')
break;
If (t.g etkind ()=='; ')
{
For (size_t I=0; i
cout <"=" & lt;