Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.10 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace CoolCalculator
  10. {
  11.     class MathValue
  12.     {
  13.         private ArrayList args = new ArrayList();
  14.         public MathValue(string input)
  15.         {
  16.             Console.WriteLine("Parsing " + input);
  17.             int paranthesisLevel = 0;
  18.             int paranthesisStart = -1;
  19.             int numberStart = -1;
  20.             int functionNameStart = -1;
  21.             for(int i = 0; i < input.Length; i++)
  22.             {
  23.                 char currChar = input[i];
  24.                 if(currChar == '(')
  25.                 {
  26.                     if(paranthesisLevel==0)
  27.                     {
  28.                         paranthesisStart = i;
  29.                         if (numberStart != -1)
  30.                         {
  31.                             args.Add(Double.Parse(input.Substring(numberStart, i - numberStart)));
  32.                             numberStart = -1;
  33.                         }
  34.                         else if(functionNameStart != -1)
  35.                         {
  36.                             args.Add(input.Substring(functionNameStart, i - functionNameStart));
  37.                             functionNameStart = -1;
  38.                         }
  39.                     }
  40.                     paranthesisLevel++;
  41.                 }
  42.                 else if(currChar == ')')
  43.                 {
  44.                     paranthesisLevel--;
  45.                     if(paranthesisLevel==0)
  46.                     {
  47.                         args.Add(new MathValue(input.Substring(paranthesisStart+1, i-paranthesisStart-1)));
  48.                         paranthesisStart = 0;
  49.                     }
  50.                 }
  51.                 else if(paranthesisLevel!=0)
  52.                 {
  53.                     //do nothing, we let other math values do that parsing...
  54.                 }
  55.                 else if(isAllowedOperator(currChar))
  56.                 {
  57.                     if(numberStart!=-1)
  58.                     {
  59.                         args.Add(Double.Parse(input.Substring(numberStart, i - numberStart)));
  60.                         numberStart = -1;
  61.                     }
  62.                     args.Add(currChar);
  63.                 }
  64.                 else if(isNumeric(currChar) || currChar == Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator))
  65.                 {
  66.                     if(functionNameStart != -1)
  67.                     {
  68.                         throw new Exception("Fuck you no loose numbers next to a function you wanker");
  69.                     }
  70.                     if(numberStart==-1)
  71.                     {
  72.                         numberStart = i;
  73.                     }
  74.                 }
  75.                 else
  76.                 {
  77.                     //function call
  78.                     if(functionNameStart == -1)
  79.                     {
  80.                         functionNameStart = i;
  81.                     }
  82.                 }
  83.             }
  84.             if(numberStart != -1)
  85.             {
  86.                 //Parse remaining numbers
  87.                 args.Add(Double.Parse(input.Substring(numberStart, input.Length - numberStart)));
  88.             }
  89.             Console.WriteLine("Done parsing!");
  90.         }
  91.         private bool isAllowedOperator(char a)
  92.         {
  93.             return a == '*' || a == '/' || a == '+' || a == '-';
  94.         }
  95.         private bool isNumeric(char a)
  96.         {
  97.             return a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || a == '6' || a == '7' || a == '8' || a == '9' || a == '0';
  98.         }
  99.         private double doFunction(string funcName, double arg)
  100.         {
  101.             switch(funcName)
  102.             {
  103.                 case "sqrt":
  104.                     return Math.Sqrt(arg);
  105.             }
  106.             throw new Exception("Unknown function");
  107.             return -1;
  108.         }
  109.         public double solve()
  110.         {
  111.             //First iteration, handle * and /
  112.             for (int i = 0; i < args.Count; i++)
  113.             {
  114.                 if (args[i].GetType() == typeof(char) && ((char)args[i] == '/' || (char)args[i] == '*'))
  115.                 {
  116.                     double leftSideVal = 0.0;
  117.                     double rightSideVal = 0.0;
  118.                     //Get left and right side
  119.                     if (args[i - 1].GetType() == typeof(double))
  120.                     {
  121.                         leftSideVal = (double)args[i - 1];
  122.                     }
  123.                     else if(args[i-1].GetType() == typeof(MathValue))
  124.                     {
  125.                         leftSideVal = ((MathValue)args[i - 1]).solve();
  126.                     }
  127.                     //Left side can't have a function string, that would be weird
  128.  
  129.                     if (args[i + 1].GetType() == typeof(double))
  130.                     {
  131.                         rightSideVal = (double)args[i + 1];
  132.                     }
  133.                     else if (args[i + 1].GetType() == typeof(MathValue))
  134.                     {
  135.                         rightSideVal = ((MathValue)args[i + 1]).solve();
  136.                     }
  137.                     else
  138.                     {
  139.                         //Right side can have a function string
  140.                         rightSideVal = doFunction((string)args[i + 1], ((MathValue)args[i + 1]).solve());
  141.                     }
  142.                     //Calculate and set value
  143.                     if((char)args[i] == '/')
  144.                     {
  145.                         args[i - 1] = leftSideVal / rightSideVal;
  146.                     }
  147.                     else if((char)args[i] == '*')
  148.                     {
  149.                         args[i - 1] = leftSideVal * rightSideVal;
  150.                     }
  151.                     //Remove stuff we have calculated
  152.                     args.RemoveAt(i);
  153.                     args.RemoveAt(i);
  154.                     //Jump back a bit
  155.                     i -= 2;
  156.                 }
  157.             }
  158.             //We have divided and multiplied everything, sum it up
  159.             double sum = 0;
  160.             char opcode = '+';
  161.             for (int i = 0; i < args.Count; i++)
  162.             {
  163.                 if(args[i].GetType() == typeof(double))
  164.                 {
  165.                     if(opcode == '+')
  166.                     {
  167.                         sum += (double)args[i];
  168.                     }
  169.                     else if(opcode == '-')
  170.                     {
  171.                         sum -= (double)args[i];
  172.                     }
  173.                     else
  174.                     {
  175.                         throw new Exception("Illegal opcode");
  176.                     }
  177.                     opcode = ' '; //Swallow the opcode
  178.                 }
  179.                 else if(args[i].GetType() == typeof(MathValue))
  180.                 {
  181.                     if (opcode == '+')
  182.                     {
  183.                         sum += ((MathValue)args[i]).solve();
  184.                     }
  185.                     else if (opcode == '-')
  186.                     {
  187.                         sum -= ((MathValue)args[i]).solve();
  188.                     }
  189.                     else
  190.                     {
  191.                         throw new Exception("Illegal opcode");
  192.                     }
  193.                     opcode = ' '; //Swallow the opcode
  194.                 }
  195.                 else if(args[i].GetType() == typeof(string))
  196.                 {
  197.                     if (opcode == '+')
  198.                     {
  199.                         sum += doFunction((string)args[i], ((MathValue)args[i + 1]).solve());
  200.                         i++;
  201.                     }
  202.                     else if (opcode == '-')
  203.                     {
  204.                         sum -= doFunction((string)args[i], ((MathValue)args[i + 1]).solve());
  205.                         i++;
  206.                     }
  207.                     else
  208.                     {
  209.                         throw new Exception("Illegal opcode");
  210.                     }
  211.                     opcode = ' '; //Swallow the opcode
  212.                 }
  213.                 else
  214.                 {
  215.                     opcode = (char)args[i];
  216.                 }
  217.             }
  218.             return sum;
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement