So this is a bit of homework I have. I have to create a calculator application that asks for user input then calculates it. The input must be in an equation format. For example: " x = 3 + 8 ", " x = 6 - 3 " or x = " 6 - 3 * 9 ". My approach to this problem is to first break down the string user input and store it into an array of char:
private char[] userInput;
string input = Console.ReadLine();
input = input.Replace(" " ,"");
userInput = input.ToCharArray();
At this point, userInput will contain all char from input. Next, I look for the variable of equation by looping through the array, this should give me the first alphabet character it found:
char var = 'x';
for (int i = 0; i < userInput.Length; i++)
{
char c = userInput[i];
if (Char.IsLetter(c)){
var = c;
break;
}
}
Next, I will break the equation up with variable one side and all of the number and operator in the other side, separated by '=', then add all number and operator to a new char array:
//get '=' position
int equalPos = 0;
for (int i = 0; i < userInput.Length; i++)
{
char c = userInput[i];
if (Char.IsSymbol(c))
{
if (c.Equals('='))
{
equalPos = i;
break;
}
}
}
//add equation to new array
rightSide = new char[userInput.Length-equalPos];
int a = 0;
for (int i = equalPos + 1; i < userInput.Length; i++)
{
char c = userInput[i];
rightSide[a] = c;
a++;
}
At this point, the rightSide array will contain all of the number and operator as character. I can calculate this part by using System.Data.DataTable().Compute()
. However, if I am not allowed to use any library, how could I implement this? The equation should only contain 1 variable(always appear on the left side of the equation), four basic operators (+-/*) and no parenthesis.
If you first split the string by the =
operator you will get the right and left hand side. So on the right hand side of the equation, if the equation is 'x = 6 * 2 + 1', we have '6 * 2 + 1', so we can compute that and follow standard BIDMAS rules using a loop and switch:
i have removed all error checking, this solution is for when a user inputs a perfect equation in the form 'x = {equation}' or '{equation} = x'
Also to note, a string
is a char[]
//get user input
Console.Write("Enter equation:");
string input = Console.ReadLine();
string[] splitInput = input.Split('=');
int index = char.IsLetter(splitInput[0].Replace(" ", "")[0]) ? 1 : 0;
string sideWithEquation = splitInput[index];
//Compute right hand side
string[] equation = sideWithEquation.Split(' ');
Using BIDMAS, and ignoring brackets and indices, we compute divison and multiplication first.
//compute for * and /
for (int i = 1; i < equation.Length - 1; i++)
{
string item = equation[i];
int num = 0;
switch (item)
{
case "*":
num = Convert.ToInt32(equation[i - 1]) * Convert.ToInt32(equation[i + 1]);
break;
case "/":
num = Convert.ToInt32(equation[i - 1]) / Convert.ToInt32(equation[i + 1]);
break;
}
if (num > 0)
{
equation[i - 1] = "";
equation[i] = "";
equation[i + 1] = num.ToString();
}
}
And then we comoute for addition and subtraction
//Now compute for + and -
equation = string.Join(" ", equation).Split(' ');
for (int i = 1; i < equation.Length - 1; i++)
{
string item = equation[i];
int num = 0;
switch (item)
{
case "+":
num = Convert.ToInt32(equation[i - 1]) + Convert.ToInt32(equation[i + 1]);
break;
case "-":
num = Convert.ToInt32(equation[i - 1]) - Convert.ToInt32(equation[i + 1]);
break;
}
if (num > 0)
{
equation[i - 1] = "";
equation[i] = "";
equation[i + 1] = num.ToString();
}
}
and then to display the value of x to the user again
string total = string.Join("", equation);
//display what x is
Console.WriteLine($"x = {int.Parse(total)}" );
Your answer can be divided into two parts First How to convert char array to a string type Second How to convert a string to a executable code block For the first part use this method:
char[] chars;
string s = new string(chars);
For the Second part IT IS TOO DIFFiCULT to find a way without any pre-written code to do so then you must use Microsoft.CSharp.CSharpCodeProvider to compile code on-the-fly. In particular, search for CompileAssemblyFromFile .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.