Advertisement
TitanChase

Untitled

May 1st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Calculator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             char[] operatorOrder = { '*', '/', '+', '-' };
  10.             string function = Console.ReadLine();
  11.             string[] operators = { };
  12.             string[] numbers = { };
  13.             do
  14.             {
  15.                 Console.WriteLine(function);
  16.                 numbers = function.Split(operatorOrder);
  17.                 operators = function.Split(numbers, StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.                 for (int i = 0; i < operatorOrder.Length; i++)
  20.                 {
  21.                     for (int x = 0; x < operators.Length; x++)
  22.                     {
  23.                         if (operators[x][0] == operatorOrder[i])
  24.                         {
  25.                             double number1 = double.Parse(numbers[x]);
  26.                             double number2 = double.Parse(numbers[x + 1]);
  27.  
  28.                             double total = handleOperator(operatorOrder[i], number1, number2);
  29.                             string toReplace = number1.ToString() + operatorOrder[i].ToString() + number2.ToString();
  30.                             function = function.Replace(toReplace, total.ToString());
  31.                             break;
  32.                         }
  33.                     }
  34.                 }
  35.             } while (function.Split(numbers, StringSplitOptions.RemoveEmptyEntries).Length != 0);
  36.  
  37.             Main(null);
  38.            
  39.         }
  40.  
  41.         public static double handleOperator(char op, double number1, double number2)
  42.         {
  43.             switch (op)
  44.             {
  45.                 case '+':
  46.                     return number1 + number2;
  47.                 case '-':
  48.                     return number1 - number2;
  49.                 case '*':
  50.                     return number1 * number2;
  51.                 case '/':
  52.                     return number1 / number2;
  53.                 default:
  54.                     throw new Exception("Operator not handled");
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement