using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApp
{
class Calculator
{
private string s;
private bool isoper(char s)
{
if (s == '(' || s == ')' || s == '+' || s == '-')
return true;
if (s == 's' || s == 'c' || s == 'l' || s == '^')
return true;
if (s == '*' || s == '/')
return true;
return false;
}
private bool isUnar(char s)
{
if (s == 'l' || s == 'c' || s == 's')
return true;
return false;
}
private double calc1(double x, char s)
{
if (s == 'l')
return Math.Log(x);
if (s == 'c')
return Math.Cos(x);
if (s == 's')
return Math.Sin(x);
return -1;
}
private double calc2(double x1, double x2, char s)
{
if (s == '+')
return x1+x2;
if (s == '-')
return x2-x1;
if (s == '^')
return Math.Pow(x2, x1);
if (s == '*')
return x1 * x2;
if (s == '/')
return x2 / x1;
return -1;
}
public Calculator(string s)
{
this.s = s;
}
public double F(double x)
{
double x1,x2,res = 0;
string ts;
List<double> stack = new List<double>();
for (int i = 0; i < s.Length; ++i)
{
if (s[i] == ' ')
continue;
if (isoper(s[i]))
{
if (isUnar(s[i]))
{
x1 = stack[stack.Count - 1];
stack.RemoveAt(stack.Count - 1);
res = calc1(x1, s[i]);
stack.Add(res);
continue;
}
x1 = stack[stack.Count - 1];
x2 = stack[stack.Count - 2];
stack.RemoveAt(stack.Count - 1);
stack.RemoveAt(stack.Count - 1);
res = calc2(x1, x2, s[i]);
stack.Add(res);
}
else
{
if (s[i] == 'x')
{
stack.Add(x);
continue;
}
if (s[i] == 'e')
{
stack.Add(Math.E);
continue;
}
ts = "";
for (int j = 0; j + i < s.Length; ++j)
{
if (!(s[i + j] >= '0' && s[i + j] <= '9'))
{
i += (j - 1);
break;
}
ts += s[i + j];
}
stack.Add(Convert.ToDouble(ts));
}
}
res = stack[0];
return res;
}
}
}