Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace QuadraticEquationSolver
- {
- class Program
- {
- static void Main(string[] args)
- {
- while (true) //loops whole program
- {
- int i = 0; //index of strNumber, used to give value to strNumbers (based on toCalc breakdown)
- int n = 0; //index of strNumber, used to give value to numbers (double array)
- int partNum = 0; //index of operation, used to save toCalc's operation in order
- int digit = 0; //index of toCalc, used to break toCalc down into chars which will, in turn, become part of strings in strNumbers
- int k = 0; //index of numbers & operation, used to find operations of multiplications and division
- int m = 0; //index of multiDivision, used to give priority to multiplication and division
- int h = 0; //index of numbers & operation, used to calculate ans
- double ans = 0; //used in calculations, keeps answer value
- Console.WriteLine("Please enter query:");
- string toCalc = Console.ReadLine();
- string[] strNumbers = new string[toCalc.Length]; //separates numbers from toCalc
- string[] operation = new string[toCalc.Length]; //separates operations from toCalc
- double[] numbers = new double[toCalc.Length]; //converts numbers separated from toCalc by strNumbers to double type
- double[] multiDivision = new double[toCalc.Length]; //runs multiplication and division calculations with priority, used in ans calculations later
- bool isMapped = false; //used to exit toCalc mapping loop
- while (isMapped == false) //loop to map toCalc, runs throu its chars
- {
- if (toCalc[digit] == '+' || toCalc[digit] == '-' || toCalc[digit] == '*' || toCalc[digit] == '/') //checks if specific toCalc char is an operation
- {
- operation[partNum] = Convert.ToString(toCalc[digit]); //saves operation
- partNum++;
- digit++; //used so that strNumbers[i] won't keep the operator
- i++; //changes number of object in numbers array in order to seperate the numbers
- }
- strNumbers[i] += toCalc[digit];
- if (digit == toCalc.Length - 1) //checks if all toCalc chars have been mapped
- {
- isMapped = true;
- }
- digit++;
- }
- if (i == 0) //checks if toCalc only contains 1 number, if so, prints it
- {
- Console.WriteLine(strNumbers[0]);
- continue; //skips rest of current iteration
- }
- while (n <= i) //converts separated numbers from string type to double type
- {
- numbers[n] = Convert.ToDouble(strNumbers[n]);
- n++;
- }
- for (k = 1; k < n - 1; k++) //calculates all multiplications and divisions in advance
- {
- if (operation[k] == "*")
- {
- multiDivision[m] = numbers[k] * numbers[k + 1];
- m++;
- }
- if (operation[k] == "/")
- {
- multiDivision[m] = numbers[k] / numbers[k + 1];
- m++;
- }
- }
- m = 0;
- for (h = 0; h < n - 1; h++) //runs actual calculations
- {
- if (h == 0 && operation[h + 1] != "*" && operation[h + 1] != "/") //gives ans its initial value if operation[h+1] is of the same priority
- {
- if (operation[h] == "+")
- ans += numbers[h] + numbers[h + 1];
- if (operation[h] == "-")
- ans += numbers[h] - numbers[h + 1];
- if (operation[h] == "/")
- ans += numbers[h] / numbers[h + 1];
- if (operation[h] == "*")
- ans += numbers[h] * numbers[h + 1];
- }
- else if (h == 0 && (operation[h + 1] == "*" || operation[h + 1] == "/")) //gives ans its initial value if operation[h+1] is of higher priority
- {
- if (operation[h] == "+")
- ans += numbers[h] + multiDivision[m];
- if (operation[h] == "-")
- ans += numbers[h] - multiDivision[m];
- if (operation[h] == "/")
- ans += numbers[h] / multiDivision[m];
- if (operation[h] == "*")
- ans += numbers[h] * multiDivision[m];
- h++;
- m++;
- }
- else if (h != 0)
- {
- if (operation[h] == "+" || operation[h] == "-") //checks operation
- {
- if (operation[h + 1] != "*" && operation[h + 1] != "/") //runs if there is no need to give priority to multiDivision
- {
- if (operation[h] == "+")
- ans += numbers[h + 1];
- if (operation[h] == "-")
- ans -= numbers[h + 1];
- }
- if (operation[h + 1] == "*" || operation[h + 1] == "/") //runs if there is a need to give priority to multiDivision
- {
- if (operation[h] == "+")
- ans += multiDivision[m];
- if (operation[h] == "-")
- ans -= multiDivision[m];
- h++;
- m++;
- }
- }
- }
- }
- Console.WriteLine(ans); //prints result
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement