예제 #1
0
        public static string CalculateSubtract(string screen)
        {
            active = false; //resets active to false

            int spot;       //spot of sign

            for (spot = 0; spot < screen.Length; spot++)
            {
                if (screen[spot] == '-' && spot > 0) //if spot == 0 and result = - then issue
                {
                    if (screen[spot - 1] != '*' && screen[spot - 1] != '/' && screen[spot - 1] != '+' && screen[spot - 1] != '-')
                    {
                        active = true;
                        break;
                    }
                }
            }

            if (active == true)
            {
                string num1 = screen.Remove(spot);
                string num2 = screen.Substring(++spot);

                double num1Num;
                double num2Num;

                double result;
                string resultString;

                for (int i = num1.Length - 1; i >= 0; i--) //reduces everything behind num1
                {
                    if (num1[i] == '*' || num1[i] == '/' || num1[i] == '+')
                    {
                        num1 = num1.Substring(i + 1);
                        break;
                    }
                    else if (num1[i] == '-')
                    {
                        if (i > 0 && (num1[i - 1] == '*' || num1[i - 1] == '/' || num1[i - 1] == '+' || num1[i - 1] == '-'))
                        {
                            num1 = num1.Substring(i);
                            break;
                        }
                        else
                        {
                            if (i == 0)
                            {
                                break;
                            }
                            else
                            {
                                num1 = num1.Substring(i + 1);
                                break;
                            }
                        }
                    }
                }

                for (int i = 0; i < num2.Length; i++) //reduces everythng after num2
                {
                    if (i > 0 && (num2[i] == '*' || num2[i] == '/' || num2[i] == '+' || num2[i] == '-'))
                    {
                        num2 = num2.Remove(i);

                        break;
                    }
                }

                num1Num = Convert.ToDouble(num1);
                num2Num = Convert.ToDouble(num2);

                result       = CalcMath.CalcSubtract(num1Num, num2Num);
                result       = Math.Round(result, 9);
                resultString = Convert.ToString(result);

                screen = screen.Replace(num1 + '-' + num2, resultString);
            }

            return(screen);
        }
예제 #2
0
        public static string CalculateExponents(string screen) //include */+- ...
        {
            active = false;

            int startNum       = 0;
            int startPower     = 0;
            int endParenthesis = 0;

            string num          = "";
            string power1       = "";
            string power2       = "1";
            string miniScreen   = ""; //holds the num and exponent
            string resultString = ""; //result after everything as string

            double numNum    = 0;
            double power1Num = 0;
            double power2Num = 1;
            double result    = 0;                                          //result after everything

            for (startPower = 0; startPower < screen.Length; startPower++) //records ^ of power
            {
                if (screen[startPower] == '^' && screen[startPower + 1] == '(')
                {
                    active = true;
                    break;
                }
            }

            if (active == true)
            {
                for (endParenthesis = startPower; endParenthesis < screen.Length; endParenthesis++) //records end parenthesis for power
                {
                    if (screen[endParenthesis] == ')')
                    {
                        break;
                    }
                }

                for (startNum = startPower - 1; startNum >= 0; startNum--) //records start of num
                {
                    if (startNum == 0 || screen[startNum] == '*' || screen[startNum] == '/' || screen[startNum] == '+')
                    {
                        break;
                    }
                    else if (screen[startNum] == '-' && (startNum == 0 || startNum - 1 == '*' || startNum - 1 == '/' || startNum - 1 == '+' || startNum - 1 == '-'))
                    {
                    }
                }

                num    = screen.Remove(startPower);
                num    = num.Substring(startNum);
                numNum = Convert.ToDouble(num);

                power1 = screen.Remove(endParenthesis);
                power1 = power1.Substring(startPower + 2); //really just the base power for now

                miniScreen = num + "^(" + power1 + ")";

                for (int i = 0; i < power1.Length; i++)
                {
                    if (power1[i] == '/')
                    {
                        power2 = power1.Substring(i + 1);
                        power1 = power1.Remove(i);
                        break;
                    }
                }

                power1Num = Convert.ToDouble(power1);
                power2Num = Convert.ToDouble(power2);

                result       = CalcMath.CalcToPower(numNum, power1Num, power2Num);
                result       = Math.Round(result, 9);
                resultString = Convert.ToString(result);

                screen = screen.Replace(miniScreen, resultString);
            }

            return(screen);
        }