1

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.");
}
Anders
  • 23
  • 4
  • Maybe something like this: https://github.com/GeorgDangl/Dangl.Calculator – Magnus May 07 '19 at 11:34
  • FYI: The result of `2^8/4` is not 4, it is **64**. –  May 07 '19 at 11:41
  • @elgonzo I meant 2^(8/4) sorry – Anders May 07 '19 at 11:42
  • Why do you use string as argument for your method? Why not just use three integer/long/float/double arguments (x,y,z) representing `x^(y/z)`. Now, don't tell me you don't want to make it easy for yourself... ;-) –  May 07 '19 at 11:45
  • @elgonzo I'm not sure how that would work hahah – Anders May 07 '19 at 11:54
  • Possible duplicate of [Evaluating string "3\*(4+2)" yield int 18](https://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18) – xdtTransform May 07 '19 at 11:55
  • related : https://stackoverflow.com/questions/2859111/c-sharp-math-calculator/2859130#2859130 – xdtTransform May 07 '19 at 11:56
  • The reason why it's difficult is because it's difficult for anyone to write a function that does lots of different things and works. It's not something we get better at - it's something we stop trying to do. In this case, I'd look at parsing the string into numbers and operations. Then have different functions for the different operations. (And re-use existing functions wherever possible.) – Scott Hannen May 07 '19 at 11:57
  • I know it's not a direct solution of "why my codes not working" but instead of having a code that break if someone use 2 space or `\n\r` in the input string or use a comma as decimal separator. In the process of building an Shunting yard algo able to withstand a user input, you will fall in all those pitfall Facebook math post failed into. – xdtTransform May 07 '19 at 12:09
  • double operator or negative number `1+-1`, missing operator '2(2)'. Don't focus too mutch on the math of those question not beeing the same math. As some question will show the same math but ar infact different question like https://stackoverflow.com/questions/234217. – xdtTransform May 07 '19 at 12:18

0 Answers0