I want to create a method that allows me to firstly convert fractions to doubles, but also deal with exponentiation. For example, if I entered 2^(8/4) I want it to output 4. I already have methods that deal with converting fractions to doubles and exponentiation individually, but I can't seem to work out how I combine these methods to make the program work as I want it to.
This is the method that deals with the exponentiation:
public static double Coefficient()
{
while (true)
{
string input = Console.ReadLine();
string[] items = input.Split('^');
if (items.Length == 1)
{
if (double.TryParse(items[0], out double A))
return A;
}
else if (items.Length == 2)
{
if (double.TryParse(items[0], out double A) &&
double.TryParse(items[1], out double B))
return Math.Pow(A, B);
}
Console.WriteLine("\nPlease follow the specified input form.");
}
}
This is the method that converts a fraction to a double:
public static double FractionToDouble(string fraction)
{
double result;
if (double.TryParse(fraction, out result))
{
return result;
}
string[] split = fraction.Split(new char[] { ' ', '/' });
if (split.Length == 2 || split.Length == 3)
{
int a, b;
if (int.TryParse(split[0], out a) && int.TryParse(split[1], out b))
{
if (split.Length == 2)
{
return (double)a / b;
}
int c;
if (int.TryParse(split[2], out c))
{
return a + (double)b / c;
}
}
}
throw new FormatException("\nNot a valid fraction.");
}