Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace CalculationExpression
- {
- public class Calculator
- {
- public static Stack<char> bracket = new Stack<char>();
- public static Stack<string> st = new Stack<string>();
- public static Queue<object> q = new Queue<object>();
- public static List<string> list = new List<string>();
- public static List<object> myList = new List<object>();
- public static List<int> indexFunction = new List<int>();
- public static List<string> litleExpression = new List<string>();
- static void Calculation(List<object> listNumber, int position)
- {
- double rezultFun = 0;
- for (int k = 0; k < listNumber.Count; k++)
- {
- double rezult = 0;
- if (listNumber[k].Equals("*"))
- {
- rezult = (double)listNumber[k - 2] * (double)listNumber[k - 1];
- listNumber[k] = rezult;
- listNumber.RemoveRange(k - 2, 2);
- k = -1;
- continue;
- }
- else if (listNumber[k].Equals("/"))
- {
- rezult = (double)listNumber[k - 2] / (double)listNumber[k - 1];
- listNumber[k] = rezult;
- listNumber.RemoveRange(k - 2, 2);
- k = -1;
- continue;
- }
- else if (listNumber[k].Equals("+"))
- {
- rezult = (double)listNumber[k - 2] + (double)listNumber[k - 1];
- listNumber[k] = rezult;
- listNumber.RemoveRange(k - 2, 2);
- k = -1;
- continue;
- }
- else if (listNumber[k].Equals("-"))
- {
- rezult = (double)listNumber[k - 2] - (double)listNumber[k - 1];
- listNumber[k] = rezult;
- listNumber.RemoveRange(k - 2, 2);
- k = -1;
- continue;
- }
- }
- if (list[position].Equals("ln"))
- {
- rezultFun = Math.Log((double)listNumber[0]);
- listNumber.Clear();
- }
- if (list[position].Equals("cos"))
- {
- rezultFun = Math.Cos((double)listNumber[0] * Math.PI / 180);
- listNumber.Clear();
- }
- if (list[position].Equals("sin"))
- {
- rezultFun = Math.Sin((double)listNumber[0] * Math.PI / 180);
- listNumber.Clear();
- }
- myList.Add(rezultFun);
- }
- static void Opz(List<string> str, int indexNum)
- {
- for (int index = 0; index < str.Count; index++)
- {
- double number;
- bool checkNumber = double.TryParse(str[index], out number);
- if (checkNumber)
- {
- q.Enqueue(number);
- }
- else if (checkNumber == false)
- {
- if (str[index] == "(")
- {
- st.Push(str[index]);
- }
- else if (str[index] == ")")
- {
- if (st.Peek() == "(")
- {
- st.Pop();
- continue;
- }
- do
- {
- q.Enqueue(st.Pop());
- } while (st.Peek() != "(");
- st.Pop();
- }
- else if (str[index] == "+")
- {
- if (st.Contains("*") || (st.Contains("/")))
- {
- if (st.Peek() != "(")
- {
- do
- {
- q.Enqueue(st.Pop());
- } while ((st.Count != 0) && (((st.Peek() != "*") ||
- (st.Peek() != "/")) && (st.Peek() != "(")));
- }
- }
- st.Push(str[index]);
- }
- else if (str[index] == "-")
- {
- if (st.Contains("*") || (st.Contains("/")))
- {
- if (st.Peek() != "(")
- {
- do
- {
- q.Enqueue(st.Pop());
- } while ((st.Count != 0) && (((st.Peek() != "*") ||
- (st.Peek() != "/")) && (st.Peek() != "(")));
- }
- }
- st.Push(str[index]);
- }
- else if (str[index] == "*")
- {
- st.Push(str[index]);
- }
- else if (str[index] == "/")
- {
- st.Push(str[index]);
- }
- }
- }
- while (st.Count != 0)
- {
- q.Enqueue(st.Pop());
- }
- while (q.Count != 0)
- {
- myList.Add(q.Dequeue());
- }
- Calculation(myList,indexNum);
- }
- static string ValidationBracket(string input)
- {
- bool correct = true;
- for (int i = 0; i < input.Length; i++)
- {
- if (input[i] == '(')
- {
- bracket.Push(input[i]);
- }
- else if (input[i] == ')')
- {
- if (bracket.Count == 0)
- {
- correct = false;
- break;
- }
- bracket.Pop();
- }
- }
- if (bracket.Count != 0)
- {
- correct = false;
- }
- if (correct == false)
- {
- Console.WriteLine("Invalid expression. Not correct bracket!");
- Console.Write("Enter expression: ");
- EnterExpression();
- }
- return input;
- }
- public static string EnterExpression()
- {
- string expression = Console.ReadLine();
- string validExpr = ValidationBracket(expression);
- return validExpr;
- }
- static void Main()
- {
- string validExpression = EnterExpression();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < validExpression.Length; i++)
- {
- if (validExpression[i] != ' ')
- {
- str.Append(validExpression[i]);
- }
- }
- string trimExpression = str.ToString();
- int nextIndex;
- for (int j = 0; j < trimExpression.Length; j++)
- {
- if (trimExpression[j]=='(')
- {
- list.Add("(");
- }
- else if (trimExpression[j]==')')
- {
- list.Add(")");
- }
- else if (trimExpression[j]=='+')
- {
- list.Add("+");
- }
- else if (trimExpression[j]=='-')
- {
- list.Add("-");
- }
- else if (trimExpression[j]=='*')
- {
- list.Add("*");
- }
- else if (trimExpression[j]=='/')
- {
- list.Add("/");
- }
- else if (char.IsDigit(trimExpression[j]))
- {
- nextIndex = 0;
- StringBuilder nextNumber = new StringBuilder();
- while ((char.IsDigit(trimExpression[j + nextIndex]) ||
- (trimExpression[j + nextIndex] == '.')))
- {
- nextNumber.Append(trimExpression[j + nextIndex]);
- nextIndex++;
- if ((j + nextIndex) == trimExpression.Length)
- {
- break;
- }
- }
- j += (nextIndex-1);
- list.Add(nextNumber.ToString());
- }
- else if (char.IsLetter(trimExpression[j]))
- {
- nextIndex = 0;
- StringBuilder nextLetter = new StringBuilder();
- while (char.IsLetter(trimExpression[j + nextIndex]))
- {
- nextLetter.Append(trimExpression[j + nextIndex]);
- nextIndex++;
- if ((j + nextIndex) == trimExpression.Length)
- {
- break;
- }
- }
- j += (nextIndex - 1);
- list.Add(nextLetter.ToString().ToLower());
- }
- }
- if ((list.Contains("ln"))||
- (list.Contains("cos"))||
- (list.Contains("sin")))
- {
- string[] function = { "ln", "cos", "sin" };
- for (int m = 0; m < function.Length; m++)
- {
- for (int n = 0; n < list.Count; n++)
- {
- if (function[m].Equals(list[n]))
- {
- indexFunction.Add(n);
- }
- }
- }
- indexFunction.Sort();
- bool checkBrackets = false;
- int saveIndex = 0; ;
- for (int p = indexFunction.Count - 1; p >= 0; p--)
- {
- for (int h = indexFunction[p] + 1; h < list.Count; h++)
- {
- if (list[h].Equals(")"))
- {
- saveIndex = h;
- litleExpression.Add(list[h]);
- checkBrackets = true;
- break;
- }
- litleExpression.Add(list[h]);
- }
- checkBrackets = false;
- Opz(litleExpression, indexFunction[p]);
- list[indexFunction[p]] = myList[0].ToString();
- int count = saveIndex - indexFunction[p];
- list.RemoveRange((indexFunction[p] + 1), count);
- saveIndex = 0;
- litleExpression.Clear();
- myList.Clear();
- }
- }
- int nullIndex = 0;
- Opz(list,nullIndex);
- Console.WriteLine("{0} = {1:0.0000}", trimExpression, myList[0]);
- Console.WriteLine("{0} s.",sw.Elapsed.TotalSeconds);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement