Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.Push(2) Q 2 W.Push(+) W + Q.Push(2) W.Push(*). W + *. + , * . Q.Push(3) W.Push(-)
– *, , *. 2 AB.
B <- Q.pop()
A <- Q.pop()
Q.push(A - B)
, . Q = {8, 4} W={-}
, , . Q = {4} W={}
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
Q.push(2)
Q = {2}
W = { } W.push(+)
Q = {2}
W = {+} Q.push(1)
Q = {2, 1}
W = {+} W.push(-)
+ – , +
Q = {3}
W = {-} Q.push(6)
Q = {3, 6}
W = {-} W.push( / )
Q = {3, 6}
W = {-, /} W.push( ( )
,
Q = {3, 6}
W = {-, /, (} Q.push(1)
Q = {3, 6, 1}
W = {-, /, (} W.push(+)
. .
Q = {3, 6, 1}
W = {-, /, (, +} Q.push(2)
Q = {3, 6, 1, 2}
W = {-, /, (, +} W.push( ) )
( )
Q = {3, 6, 3}
W = {-, /} ,
Q = {3, 2, 3}
W = {-, /} Q = {1}
W = {}
. 1 – , .
public static double Calc( string s)
{
s = '(' + s + ')' ;
Stack< double > Operands = new Stack< double >();
Stack< char > Functions = new Stack< char >();
int pos = 0;
object token;
object prevToken = '' ;
do
{
token = getToken(s, ref pos);
// + -
if (token is char && prevToken is char &&
// (char)prevToken != ')' &&
// (2 * -5) (2 - -4)
// , 2 + -+-+-+2 :)
( char )prevToken == '(' &&
(( char )token == '+' || ( char )token == '-' ))
Operands.Push(0); //
if (token is double ) //
{
Operands.Push(( double )token); //
}
// . , .. ..
else if (token is char ) //
{
if (( char )token == ')' )
{
// - .
while (Functions.Count > 0 && Functions.Peek() != '(' )
popFunction(Operands, Functions);
Functions.Pop(); // "("
}
else
{
while (canPop(( char )token, Functions)) //
popFunction(Operands, Functions); //
Functions.Push(( char )token); //
}
}
prevToken = token;
}
while (token != null );
if (Operands.Count > 1 || Functions.Count > 0)
throw new Exception( " " );
return Operands.Pop();
}
private static void popFunction(Stack< double > Operands, Stack< char > Functions)
{
double B = Operands.Pop();
double A = Operands.Pop();
switch (Functions.Pop())
{
case '+' : Operands.Push(A + B);
break ;
case '-' : Operands.Push(A - B);
break ;
case '*' : Operands.Push(A * B);
break ;
case '/' : Operands.Push(A / B);
break ;
}
}
private static bool canPop( char op1, Stack< char > Functions)
{
if (Functions.Count == 0)
return false ;
int p1 = getPriority(op1);
int p2 = getPriority(Functions.Peek());
return p1 >= 0 && p2 >= 0 && p1 >= p2;
}
private static int getPriority( char op)
{
switch (op)
{
case '(' :
return -1; //
case '*' : case '/' :
return 1;
case '+' : case '-' :
return 2;
default :
throw new Exception( " " );
}
}
private static object getToken( string s, ref int pos)
{
readWhiteSpace(s, ref pos);
if (pos == s.Length) //
return null ;
if ( char .IsDigit(s[pos]))
return Convert .ToDouble(readDouble(s, ref pos));
else
return readFunction(s, ref pos);
}
private static char readFunction( string s, ref int pos)
{
//
// == && || mod div
return s[pos++];
}
private static string readDouble( string s, ref int pos)
{
string res = "" ;
while (pos < s.Length && ( char .IsDigit(s[pos]) || s[pos] == '.' ))
res += s[pos++];
return res;
}
// .
private static void readWhiteSpace( string s, ref int pos)
{
while (pos < s.Length && char .IsWhiteSpace(s[pos]))
pos++;
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/50196/
All Articles