Advertisement
AvengersAssemble

Simplistic-Calculator-v0.6 annotated

Sep 9th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace QuadraticEquationSolver
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             while (true) //loops whole program
  13.             {
  14.                 int i = 0; //index of strNumber, used to give value to strNumbers (based on toCalc breakdown)
  15.                 int n = 0; //index of strNumber, used to give value to numbers (double array)
  16.                 int partNum = 0; //index of operation, used to save toCalc's operation in order
  17.                 int digit = 0; //index of toCalc, used to break toCalc down into chars which will, in turn, become part of strings in strNumbers
  18.                 int k = 0; //index of numbers & operation, used to find operations of multiplications and division
  19.                 int m = 0; //index of multiDivision, used to give priority to multiplication and division
  20.                 int h = 0; //index of numbers & operation, used to calculate ans
  21.                 double ans = 0; //used in calculations, keeps answer value
  22.                 Console.WriteLine("Please enter query:");
  23.                 string toCalc = Console.ReadLine();
  24.                 string[] strNumbers = new string[toCalc.Length]; //separates numbers from toCalc
  25.                 string[] operation = new string[toCalc.Length]; //separates operations from toCalc
  26.                 double[] numbers = new double[toCalc.Length]; //converts numbers separated from toCalc by strNumbers to double type
  27.                 double[] multiDivision = new double[toCalc.Length]; //runs multiplication and division calculations with priority, used in ans calculations later
  28.                 bool isMapped = false; //used to exit toCalc mapping loop
  29.                 while (isMapped == false) //loop to map toCalc, runs throu its chars
  30.                 {
  31.                     if (toCalc[digit] == '+' || toCalc[digit] == '-' || toCalc[digit] == '*' || toCalc[digit] == '/') //checks if specific toCalc char is an operation
  32.                     {
  33.                         operation[partNum] = Convert.ToString(toCalc[digit]); //saves operation
  34.                         partNum++;
  35.                         digit++; //used so that strNumbers[i] won't keep the operator
  36.                         i++; //changes number of object in numbers array in order to seperate the numbers
  37.                     }
  38.                     strNumbers[i] += toCalc[digit];
  39.                     if (digit == toCalc.Length - 1) //checks if all toCalc chars have been mapped
  40.                     {
  41.                         isMapped = true;
  42.                     }
  43.                     digit++;
  44.                 }
  45.                 if (i == 0) //checks if toCalc only contains 1 number, if so, prints it
  46.                 {
  47.                     Console.WriteLine(strNumbers[0]);
  48.                     continue; //skips rest of current iteration
  49.                 }
  50.                 while (n <= i) //converts separated numbers from string type to double type
  51.                 {
  52.                     numbers[n] = Convert.ToDouble(strNumbers[n]);
  53.                     n++;
  54.                 }
  55.                 for (k = 1; k < n - 1; k++) //calculates all multiplications and divisions in advance
  56.                 {
  57.                     if (operation[k] == "*")
  58.                     {
  59.                         multiDivision[m] = numbers[k] * numbers[k + 1];
  60.                         m++;
  61.                     }
  62.                     if (operation[k] == "/")
  63.                     {
  64.                         multiDivision[m] = numbers[k] / numbers[k + 1];
  65.                         m++;
  66.                     }
  67.                 }
  68.                 m = 0;
  69.                 for (h = 0; h < n - 1; h++) //runs actual calculations
  70.                 {
  71.                     if (h == 0 && operation[h + 1] != "*" && operation[h + 1] != "/") //gives ans its initial value if operation[h+1] is of the same priority
  72.                     {
  73.                         if (operation[h] == "+")
  74.                             ans += numbers[h] + numbers[h + 1];
  75.                         if (operation[h] == "-")
  76.                             ans += numbers[h] - numbers[h + 1];
  77.                         if (operation[h] == "/")
  78.                             ans += numbers[h] / numbers[h + 1];
  79.                         if (operation[h] == "*")
  80.                             ans += numbers[h] * numbers[h + 1];
  81.                     }
  82.                     else if (h == 0 && (operation[h + 1] == "*" || operation[h + 1] == "/")) //gives ans its initial value if operation[h+1] is of higher priority
  83.                     {
  84.                         if (operation[h] == "+")
  85.                             ans += numbers[h] + multiDivision[m];
  86.                         if (operation[h] == "-")
  87.                             ans += numbers[h] - multiDivision[m];
  88.                         if (operation[h] == "/")
  89.                             ans += numbers[h] / multiDivision[m];
  90.                         if (operation[h] == "*")
  91.                             ans += numbers[h] * multiDivision[m];
  92.                         h++;
  93.                         m++;
  94.                     }
  95.                     else if (h != 0)
  96.                     {
  97.                         if (operation[h] == "+" || operation[h] == "-") //checks operation
  98.                         {
  99.                             if (operation[h + 1] != "*" && operation[h + 1] != "/") //runs if there is no need to give priority to multiDivision
  100.                             {
  101.                                 if (operation[h] == "+")
  102.                                     ans += numbers[h + 1];
  103.                                 if (operation[h] == "-")
  104.                                     ans -= numbers[h + 1];
  105.                             }
  106.                             if (operation[h + 1] == "*" || operation[h + 1] == "/") //runs if there is a need to give priority to multiDivision
  107.                             {
  108.                                 if (operation[h] == "+")
  109.                                     ans += multiDivision[m];
  110.                                 if (operation[h] == "-")
  111.                                     ans -= multiDivision[m];
  112.                                 h++;
  113.                                 m++;
  114.                             }
  115.                         }
  116.                     }
  117.                 }
  118.                   Console.WriteLine(ans); //prints result
  119.             }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement