Advertisement
dimipan80

Telerik Exam. Enigmanation

Jun 24th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. namespace _3.Enigmanation
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Enigmanation
  8.     {        
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 string mathExpression = Console.ReadLine();
  14.                 List<double> numsList = new List<double>();
  15.                 List<double> bracketNums = new List<double>();
  16.                 List<char> operatorsList = new List<char>();
  17.                 List<char> bracketOperats = new List<char>();
  18.                 int indexLeftBracket = 0;                
  19.                 double bracketResult = 0;
  20.                 for (int i = 0; i < mathExpression.Length; i++)
  21.                 {
  22.                     char symb = mathExpression[i];
  23.                     if (symb == '=')
  24.                     {
  25.                         break;
  26.                     }
  27.  
  28.                     if (char.IsDigit(symb))
  29.                     {                        
  30.                         numsList.Add(double.Parse(symb.ToString()));
  31.                     }
  32.  
  33.                     if (symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '%')
  34.                     {
  35.                         operatorsList.Add(symb);
  36.                     }
  37.  
  38.                     if (symb == '(')
  39.                     {
  40.                         indexLeftBracket = i;
  41.                         bracketNums.Clear();
  42.                         bracketOperats.Clear();                        
  43.                         while (mathExpression[indexLeftBracket] != ')')
  44.                         {
  45.                             symb = mathExpression[indexLeftBracket];
  46.                             if (char.IsDigit(symb))
  47.                             {                                
  48.                                 bracketNums.Add(double.Parse(symb.ToString()));
  49.                             }
  50.  
  51.                             if (symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '%')
  52.                             {
  53.                                 bracketOperats.Add(symb);
  54.                             }
  55.  
  56.                             indexLeftBracket++;
  57.                         }
  58.  
  59.                         bracketResult = bracketNums[0];
  60.                         for (int j = 0; j < bracketOperats.Count; j++)
  61.                         {
  62.                             bracketResult = CalculateResultFromMathOperator(bracketNums, bracketOperats, bracketResult, j);          
  63.                         }
  64.  
  65.                         numsList.Add(bracketResult);
  66.                         i = indexLeftBracket;
  67.                     }
  68.                 }
  69.  
  70.                 double totalResult = numsList[0];
  71.                 for (int k = 0; k < operatorsList.Count; k++)
  72.                 {
  73.                     totalResult = CalculateResultFromMathOperator(numsList, operatorsList, totalResult, k);
  74.                 }
  75.  
  76.                 Console.WriteLine("{0:f3}", totalResult);
  77.             }
  78.         }
  79.  
  80.         private static double CalculateResultFromMathOperator(List<double> numList, List<char> operatorList, double result, int j)
  81.         {
  82.             checked
  83.             {
  84.                 switch (operatorList[j])
  85.                 {
  86.                     case '+':
  87.                         result += numList[j + 1];
  88.                         break;
  89.                     case '-':
  90.                         result -= numList[j + 1];
  91.                         break;
  92.                     case '*':
  93.                         result *= numList[j + 1];
  94.                         break;
  95.                     case '/':
  96.                         result /= numList[j + 1];
  97.                         break;
  98.                     case '%':
  99.                         result %= numList[j + 1];
  100.                         break;
  101.                 }
  102.  
  103.                 return result;
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement