Advertisement
Vladimir76

calculator

Jan 27th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace CalculationExpression
  10. {
  11.     public class Calculator
  12.     {
  13.  
  14.         public static Stack<char> bracket = new Stack<char>();
  15.         public static Stack<string> st = new Stack<string>();
  16.  
  17.         public static Queue<object> q = new Queue<object>();
  18.  
  19.         public static List<string> list = new List<string>();
  20.         public static List<object> myList = new List<object>();
  21.         public static List<int> indexFunction = new List<int>();
  22.         public static List<string> litleExpression = new List<string>();
  23.  
  24.         static void Calculation(List<object> listNumber, int position)
  25.         {
  26.             double rezultFun = 0;
  27.  
  28.             for (int k = 0; k < listNumber.Count; k++)
  29.             {
  30.                 double rezult = 0;
  31.                 if (listNumber[k].Equals("*"))
  32.                 {
  33.                     rezult = (double)listNumber[k - 2] * (double)listNumber[k - 1];
  34.                     listNumber[k] = rezult;
  35.                     listNumber.RemoveRange(k - 2, 2);
  36.                     k = -1;
  37.                     continue;
  38.                 }
  39.                 else if (listNumber[k].Equals("/"))
  40.                 {
  41.                     rezult = (double)listNumber[k - 2] / (double)listNumber[k - 1];
  42.                     listNumber[k] = rezult;
  43.                     listNumber.RemoveRange(k - 2, 2);
  44.                     k = -1;
  45.                     continue;
  46.                 }
  47.                 else if (listNumber[k].Equals("+"))
  48.                 {
  49.                     rezult = (double)listNumber[k - 2] + (double)listNumber[k - 1];
  50.                     listNumber[k] = rezult;
  51.                     listNumber.RemoveRange(k - 2, 2);
  52.                     k = -1;
  53.                     continue;
  54.                 }
  55.                 else if (listNumber[k].Equals("-"))
  56.                 {
  57.                     rezult = (double)listNumber[k - 2] - (double)listNumber[k - 1];
  58.                     listNumber[k] = rezult;
  59.                     listNumber.RemoveRange(k - 2, 2);
  60.                     k = -1;
  61.                     continue;
  62.                 }
  63.             }
  64.             if (list[position].Equals("ln"))
  65.             {
  66.                 rezultFun = Math.Log((double)listNumber[0]);
  67.                 listNumber.Clear();
  68.             }
  69.             if (list[position].Equals("cos"))
  70.             {
  71.                 rezultFun = Math.Cos((double)listNumber[0] * Math.PI / 180);
  72.                 listNumber.Clear();
  73.             }
  74.             if (list[position].Equals("sin"))
  75.             {
  76.                 rezultFun = Math.Sin((double)listNumber[0] * Math.PI / 180);
  77.                 listNumber.Clear();
  78.             }
  79.             myList.Add(rezultFun);
  80.         }
  81.  
  82.         static void Opz(List<string> str, int indexNum)
  83.         {
  84.             for (int index = 0; index < str.Count; index++)
  85.             {
  86.                 double number;
  87.                 bool checkNumber = double.TryParse(str[index], out number);
  88.                 if (checkNumber)
  89.                 {
  90.                     q.Enqueue(number);
  91.                 }
  92.                 else if (checkNumber == false)
  93.                 {
  94.                     if (str[index] == "(")
  95.                     {
  96.                         st.Push(str[index]);
  97.                     }
  98.                     else if (str[index] == ")")
  99.                     {
  100.                         if (st.Peek() == "(")
  101.                         {
  102.                             st.Pop();
  103.                             continue;
  104.                         }
  105.                         do
  106.                         {
  107.                             q.Enqueue(st.Pop());
  108.                         } while (st.Peek() != "(");
  109.                         st.Pop();
  110.                     }
  111.                     else if (str[index] == "+")
  112.                     {
  113.                         if (st.Contains("*") || (st.Contains("/")))
  114.                         {
  115.                             if (st.Peek() != "(")
  116.                             {
  117.                                 do
  118.                                 {
  119.                                     q.Enqueue(st.Pop());
  120.  
  121.                                 } while ((st.Count != 0) && (((st.Peek() != "*") ||
  122.                                     (st.Peek() != "/")) && (st.Peek() != "(")));
  123.                             }
  124.  
  125.                         }
  126.                         st.Push(str[index]);
  127.                     }
  128.                     else if (str[index] == "-")
  129.                     {
  130.                         if (st.Contains("*") || (st.Contains("/")))
  131.                         {
  132.                             if (st.Peek() != "(")
  133.                             {
  134.                                 do
  135.                                 {
  136.                                     q.Enqueue(st.Pop());
  137.  
  138.                                 } while ((st.Count != 0) && (((st.Peek() != "*") ||
  139.                                     (st.Peek() != "/")) && (st.Peek() != "(")));
  140.                             }
  141.                         }
  142.                         st.Push(str[index]);
  143.                     }
  144.                     else if (str[index] == "*")
  145.                     {
  146.                         st.Push(str[index]);
  147.                     }
  148.                     else if (str[index] == "/")
  149.                     {
  150.                         st.Push(str[index]);
  151.                     }
  152.  
  153.                 }
  154.  
  155.             }
  156.             while (st.Count != 0)
  157.             {
  158.                 q.Enqueue(st.Pop());
  159.             }
  160.  
  161.             while (q.Count != 0)
  162.             {
  163.                 myList.Add(q.Dequeue());
  164.             }
  165.             Calculation(myList,indexNum);
  166.         }
  167.  
  168.  
  169.         static string ValidationBracket(string input)
  170.         {
  171.             bool correct = true;
  172.             for (int i = 0; i < input.Length; i++)
  173.             {
  174.                 if (input[i] == '(')
  175.                 {
  176.                     bracket.Push(input[i]);
  177.                 }
  178.                 else if (input[i] == ')')
  179.                 {
  180.                     if (bracket.Count == 0)
  181.                     {
  182.                         correct = false;
  183.                         break;
  184.                     }
  185.                     bracket.Pop();
  186.                 }
  187.             }
  188.             if (bracket.Count != 0)
  189.             {
  190.                 correct = false;
  191.             }
  192.             if (correct == false)
  193.             {
  194.                 Console.WriteLine("Invalid expression. Not correct bracket!");
  195.                 Console.Write("Enter expression: ");
  196.                 EnterExpression();
  197.             }
  198.             return input;
  199.         }
  200.         public static string EnterExpression()
  201.         {
  202.             string expression = Console.ReadLine();
  203.  
  204.             string validExpr = ValidationBracket(expression);
  205.             return validExpr;
  206.         }
  207.  
  208.         static void Main()
  209.         {
  210.            
  211.             string validExpression = EnterExpression();
  212.             Stopwatch sw = new Stopwatch();
  213.             sw.Start();
  214.             StringBuilder str = new StringBuilder();
  215.  
  216.             for (int i = 0; i < validExpression.Length; i++)
  217.             {
  218.                 if (validExpression[i] != ' ')
  219.                 {
  220.                     str.Append(validExpression[i]);
  221.                 }
  222.             }
  223.             string trimExpression = str.ToString();
  224.             int nextIndex;
  225.  
  226.             for (int j = 0; j < trimExpression.Length; j++)
  227.             {
  228.                 if (trimExpression[j]=='(')
  229.                 {
  230.                     list.Add("(");
  231.                 }
  232.                 else if (trimExpression[j]==')')
  233.                 {
  234.                     list.Add(")");
  235.                 }
  236.                 else if (trimExpression[j]=='+')
  237.                 {
  238.                     list.Add("+");
  239.                 }
  240.                 else if (trimExpression[j]=='-')
  241.                 {
  242.                     list.Add("-");
  243.                 }
  244.                 else if (trimExpression[j]=='*')
  245.                 {
  246.                     list.Add("*");
  247.                 }
  248.                 else if (trimExpression[j]=='/')
  249.                 {
  250.                     list.Add("/");
  251.                 }
  252.                 else if (char.IsDigit(trimExpression[j]))
  253.                 {
  254.                     nextIndex = 0;
  255.                     StringBuilder nextNumber = new StringBuilder();
  256.                     while ((char.IsDigit(trimExpression[j + nextIndex]) ||
  257.                         (trimExpression[j + nextIndex] == '.')))
  258.                     {
  259.                         nextNumber.Append(trimExpression[j + nextIndex]);
  260.                         nextIndex++;
  261.  
  262.                         if ((j + nextIndex) == trimExpression.Length)
  263.                         {
  264.                             break;
  265.                         }                      
  266.                     }
  267.                     j += (nextIndex-1);
  268.                     list.Add(nextNumber.ToString());
  269.                 }
  270.                 else if (char.IsLetter(trimExpression[j]))
  271.                 {
  272.                     nextIndex = 0;
  273.                     StringBuilder nextLetter = new StringBuilder();
  274.                     while (char.IsLetter(trimExpression[j + nextIndex]))
  275.                     {
  276.                         nextLetter.Append(trimExpression[j + nextIndex]);
  277.                         nextIndex++;
  278.  
  279.                         if ((j + nextIndex) == trimExpression.Length)
  280.                         {
  281.                             break;
  282.                         }
  283.                     }
  284.                     j += (nextIndex - 1);
  285.                     list.Add(nextLetter.ToString().ToLower());
  286.                 }
  287.             }
  288.  
  289.             if ((list.Contains("ln"))||
  290.                 (list.Contains("cos"))||
  291.                 (list.Contains("sin")))
  292.             {
  293.                 string[] function = { "ln", "cos", "sin" };
  294.                 for (int m = 0; m < function.Length; m++)
  295.                 {
  296.                     for (int n = 0; n < list.Count; n++)
  297.                     {
  298.                         if (function[m].Equals(list[n]))
  299.                         {
  300.                             indexFunction.Add(n);
  301.                         }
  302.                     }
  303.                 }
  304.                 indexFunction.Sort();
  305.                 bool checkBrackets = false;
  306.                 int saveIndex = 0; ;
  307.                 for (int p = indexFunction.Count - 1; p >= 0; p--)
  308.                 {
  309.                     for (int h = indexFunction[p] + 1; h < list.Count; h++)
  310.                     {
  311.                         if (list[h].Equals(")"))
  312.                         {
  313.                             saveIndex = h;
  314.                             litleExpression.Add(list[h]);
  315.                             checkBrackets = true;
  316.                             break;
  317.                         }
  318.                         litleExpression.Add(list[h]);
  319.                     }
  320.  
  321.                     checkBrackets = false;
  322.  
  323.                     Opz(litleExpression, indexFunction[p]);
  324.  
  325.                     list[indexFunction[p]] = myList[0].ToString();
  326.                     int count = saveIndex - indexFunction[p];
  327.                     list.RemoveRange((indexFunction[p] + 1), count);
  328.                     saveIndex = 0;
  329.                     litleExpression.Clear();
  330.                     myList.Clear();
  331.                 }
  332.             }
  333.             int nullIndex = 0;
  334.             Opz(list,nullIndex);
  335.            
  336.             Console.WriteLine("{0} = {1:0.0000}", trimExpression, myList[0]);
  337.             Console.WriteLine("{0} s.",sw.Elapsed.TotalSeconds);
  338.         }
  339.     }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement