Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CalcExpression
  7. {
  8.     class CalcExpression
  9.     {
  10.         public static List<string> rawExpression = new List<string>();
  11.  
  12.         public static void ReadExpression()
  13.         {
  14.             string input = Console.ReadLine();
  15.  
  16.            
  17.             string tmp = null;
  18.             for (int i = 0; i < input.Length; i++)
  19.             {
  20.                
  21.                 if(input[i]=='.' || ('0'<=input[i] && input[i]<='9'))
  22.                 {
  23.                     tmp+=input[i];
  24.                 }
  25.                 else
  26.                 {
  27.                     if (tmp != null)
  28.                     {
  29.                         rawExpression.Insert(rawExpression.Count, tmp);
  30.                         tmp = null;
  31.                     }
  32.                     if (input[i] == '(' || input[i] == ')')
  33.                         rawExpression.Insert(rawExpression.Count, input.Substring(i, 1));
  34.                     else
  35.                         if (input[i] == 'p')
  36.                             rawExpression.Insert(rawExpression.Count, input.Substring(i, 3));
  37.                         else
  38.                             if (input[i] == 's')
  39.                                 rawExpression.Insert(rawExpression.Count, input.Substring(i, 4));
  40.                             else
  41.                                 if (input[i] == 'l')
  42.                                     rawExpression.Insert(rawExpression.Count, input.Substring(i, 2));
  43.                                 else
  44.                                     if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/')
  45.                                         rawExpression.Insert(rawExpression.Count, input.Substring(i, 1));
  46.                 }
  47.             }
  48.             if (tmp != null)
  49.                 rawExpression.Insert(rawExpression.Count, tmp);
  50.         }
  51.         public static double CalcPow(double b, double power)
  52.         {
  53.             return Math.Pow(b, power);
  54.         }
  55.         public static double CalcSqrt(double num)
  56.         {
  57.             return Math.Sqrt(num);
  58.         }
  59.         public static double CalcLn(double num)
  60.         {
  61.             return Math.Log(num);
  62.         }
  63.  
  64.         public static Queue<double> q = new Queue<double>();
  65.         public static Stack<double> st = new Stack<double>();
  66.  
  67.         public static double CalcExp(List<string> str)
  68.         {
  69.  
  70.             for (int i = 0; i < str.Count; i++)
  71.             {
  72.                 switch (str[i])
  73.                 {
  74.                     case "sqrt":
  75.                         q.Enqueue(CalcSqrt(double.Parse(str[i+2])));
  76.                         i+=3;
  77.                         break;
  78.                     case "pow":
  79.                         q.Enqueue(CalcPow(double.Parse(str[i + 2]), double.Parse(str[i + 3])));
  80.                         i += 4;
  81.                         break;
  82.                     case "ln":
  83.                         q.Enqueue(CalcLn(double.Parse(str[i + 2])));
  84.                         i += 3;
  85.                         break;
  86.                     case "(":
  87.                         int lenght = 0;
  88.                         while (str[i + lenght] != ")")
  89.                             lenght++;
  90.  
  91.  
  92.                         break;
  93.                 }
  94.             }
  95.             return 0;
  96.         }
  97.  
  98.         static void Main()
  99.         {
  100.             ReadExpression();
  101.  
  102.  
  103.  
  104.             for (int i = 0; i < rawExpression.Count; i++)
  105.             {
  106.                 Console.WriteLine(rawExpression[i]);
  107.             }
  108.         }
  109.     }
  110. }
Add Comment
Please, Sign In to add comment