put any kind off equation in this:
Example:
x^2-2sinx^3
x^5 +x^4-4x^3
it also take trigonometry functions.
and exponential functions.
public class Evalute
{
public string getpower(string expression, string type)
{
if (!expression.Contains('x'))
{
return "0";
}
int length = expression.Length - 1;
string power = "";
if (type == "Sin" || type == "Cos" || type == "e")
{
length = (expression.IndexOf(type) - 1);
if (!expression.Substring(0, length).Contains('x'))
{ return "0"; }
}
for (int a = length; a > 0; a--)
{
char chr = expression[a];
if ((chr >= '0' && chr <= '9') || chr == '.')
{ power = chr + power; }
else if ((chr == 'x' || chr == '^') && power == "")
{
power = "1";
break;
}
else break;
}
return power;
}
}
public class Expression
{
Evalute evalute;
string[] operation; string[] trignometric;
string[] trigpower; string[] constants;
string[] conspower; string expression;
int parts, size; double total;
double output;
public Expression(int s, string i)
{
size = s;
expression = i;
operation = new string[size];
for (int a = 0; a < size; a++)
{
if (expression[a] == '+' || expression[a] == '-')
{
parts++;
operation[parts] += expression[a];
}
else
operation[parts] += expression[a];
}
trignometric = new string[parts + 1];
trigpower = new string[parts + 1];
conspower = new string[parts + 1];
constants = new string[parts + 1];
output = 1;
total = 0;
evalute = new Evalute();
}
public void evaluteexpression()
{
for (int i = 0; i <= parts; i++)
{
string input = operation[i];
bool trigno = false;
if (input.Contains("Sin")) { trignometric[i] = "Sin"; trigno = true; }
if (input.Contains("Cos")) { trignometric[i] = "Cos"; trigno = true; }
if (input.Contains("e")) { trignometric[i] = "e"; trigno = true; }
if (trigno == true)
{
trigpower[i] = evalute.getpower(input, "None");
}
conspower[i] = evalute.getpower(input, trignometric[i]);
for (int j = 0; j < input.Length; j++)
{
char chr = input[j];
if ((chr >= '0' && chr <= '9') || chr == '+' || chr == '-' || chr == '.')
{
constants[i] += chr;
}
else break;
}
if (constants[i] == null || constants[i] == "-" || constants[i] == "+")
{
constants[i] += "1";
}
}
}
public double funcx(double x)
{
total = 0;
for (int i = 0; i <= parts; i++)
{
double tpower = 1;
output *= Convert.ToDouble(constants[i]);
double temp = Convert.ToDouble(conspower[i]);
output *= Math.Pow(x, temp);
if (trigpower[i] != "0")
{
tpower = Convert.ToDouble(trigpower[i]);
if (trignometric[i] == "Sin")
{
output *= Math.Sin(Math.Pow(x, tpower));
}
if (trignometric[i] == "Cos")
{
output *= Math.Cos(Math.Pow(x, tpower));
}
if (trignometric[i] == "e")
{
output *= Math.Exp(Math.Pow(x, tpower));
}
}
total += output;
output = 1;
}
return total;
}
}
static void Main(string[] args)
{
string equation = "3x^2Sinx^3+4x^3Cosx^2-3x^6+400x^4";
int s = equation.Length;
Expression eq = new Expression(s, equation);
eq.evaluteexpression();
Console.WriteLine("the ans is {0} ", eq.funcx(1.5));
}
Example:
x^2-2sinx^3
x^5 +x^4-4x^3
it also take trigonometry functions.
and exponential functions.
public class Evalute
{
public string getpower(string expression, string type)
{
if (!expression.Contains('x'))
{
return "0";
}
int length = expression.Length - 1;
string power = "";
if (type == "Sin" || type == "Cos" || type == "e")
{
length = (expression.IndexOf(type) - 1);
if (!expression.Substring(0, length).Contains('x'))
{ return "0"; }
}
for (int a = length; a > 0; a--)
{
char chr = expression[a];
if ((chr >= '0' && chr <= '9') || chr == '.')
{ power = chr + power; }
else if ((chr == 'x' || chr == '^') && power == "")
{
power = "1";
break;
}
else break;
}
return power;
}
}
public class Expression
{
Evalute evalute;
string[] operation; string[] trignometric;
string[] trigpower; string[] constants;
string[] conspower; string expression;
int parts, size; double total;
double output;
public Expression(int s, string i)
{
size = s;
expression = i;
operation = new string[size];
for (int a = 0; a < size; a++)
{
if (expression[a] == '+' || expression[a] == '-')
{
parts++;
operation[parts] += expression[a];
}
else
operation[parts] += expression[a];
}
trignometric = new string[parts + 1];
trigpower = new string[parts + 1];
conspower = new string[parts + 1];
constants = new string[parts + 1];
output = 1;
total = 0;
evalute = new Evalute();
}
public void evaluteexpression()
{
for (int i = 0; i <= parts; i++)
{
string input = operation[i];
bool trigno = false;
if (input.Contains("Sin")) { trignometric[i] = "Sin"; trigno = true; }
if (input.Contains("Cos")) { trignometric[i] = "Cos"; trigno = true; }
if (input.Contains("e")) { trignometric[i] = "e"; trigno = true; }
if (trigno == true)
{
trigpower[i] = evalute.getpower(input, "None");
}
conspower[i] = evalute.getpower(input, trignometric[i]);
for (int j = 0; j < input.Length; j++)
{
char chr = input[j];
if ((chr >= '0' && chr <= '9') || chr == '+' || chr == '-' || chr == '.')
{
constants[i] += chr;
}
else break;
}
if (constants[i] == null || constants[i] == "-" || constants[i] == "+")
{
constants[i] += "1";
}
}
}
public double funcx(double x)
{
total = 0;
for (int i = 0; i <= parts; i++)
{
double tpower = 1;
output *= Convert.ToDouble(constants[i]);
double temp = Convert.ToDouble(conspower[i]);
output *= Math.Pow(x, temp);
if (trigpower[i] != "0")
{
tpower = Convert.ToDouble(trigpower[i]);
if (trignometric[i] == "Sin")
{
output *= Math.Sin(Math.Pow(x, tpower));
}
if (trignometric[i] == "Cos")
{
output *= Math.Cos(Math.Pow(x, tpower));
}
if (trignometric[i] == "e")
{
output *= Math.Exp(Math.Pow(x, tpower));
}
}
total += output;
output = 1;
}
return total;
}
}
static void Main(string[] args)
{
string equation = "3x^2Sinx^3+4x^3Cosx^2-3x^6+400x^4";
int s = equation.Length;
Expression eq = new Expression(s, equation);
eq.evaluteexpression();
Console.WriteLine("the ans is {0} ", eq.funcx(1.5));
}
No comments:
Post a Comment