Reputation: 61
I am trying to achieve a program that takes in an equation from the user (in 1 line) and outputs the result. So far I have I'm using indexof to find the + sign, and then Im trying to find the value of the number to the left and right of the + sign -
string input = "5+4+6";
while (input.Contains('+'))
{
Console.WriteLine(input.IndexOf("+"));
string position1 = input.Substring(0, input.IndexOf("+"));
int number1 = Convert.ToInt32(position1);
String position2 = input.Substring(2, input.IndexOf("+"));
int number2 = Convert.ToInt32(position2);
int sum = (number1 + number2);
The problem with my code is that I am stating the number to the left and converting it to a int whereas in real life the numbers in the equation will be unkown , can be 2+4, or 3+5+6+4, for simplicity for now Im trying to do + and - , then / and *.
Could anyone suggest an improvement on the code or any help? I know there are other ways to calculate formula such as ncalc, but I would like to stick to this approach.
Thank you
Upvotes: 3
Views: 120
Reputation: 48177
I use same precedence as Excel. Work for + - * /
.net Fiddle
This work only with integer numbers in the formula. Even when return double to store the division.
If you want accept double in the formula try double.Parse
instead of int.Parse
=1+2*3/4-5+6*7/8-9
will get -6.75.
First operations are 3/4 and 7/8
then 2 * 0.75 and 6 * 0.875
then 1.5 - 5 and 5.25 - 9
finally 1 + (-3.5) + (-3.75)
=1+2+3+4+5+6+7+8+9
is 45
=1-2-3-4
is -8
=1*2*3*4
is 24
=1/2/3/4
is 0.416667
public static void Main()
{
int n;
bool isNumeric;
string input = "1+2*3/4-5+6*7/8-9";
string[] addSplit = input.Split('+');
double addTotal = 0;
foreach (string addCode in addSplit)
{
isNumeric = int.TryParse(addCode, out n);
if (isNumeric)
{
addTotal += n;
}
else
{
string[] addMinus = addCode.Split('-');
double minusTotal = 0;
bool firstMinus = true;
foreach (string minusCode in addMinus)
{
isNumeric = int.TryParse(minusCode, out n);
if (isNumeric)
{
if (firstMinus) {
minusTotal = n;
firstMinus = false;
}
else {
minusTotal -= n;
}
}
else
{
string[] multySplit = minusCode.Split('*');
double multyTotal = 1;
foreach (string multyCode in multySplit)
{
isNumeric = int.TryParse(multyCode, out n);
if (isNumeric)
{
multyTotal *= n;
}
else
{
string[] divSplit = multyCode.Split('/');
int.TryParse(divSplit[0], out n);
double divTotal = n;
for( int i = 1; i < divSplit.Length ; i++ ) {
int.TryParse(divSplit[i], out n);
divTotal /= n;
}
multyTotal *= divTotal;
}
}
if (firstMinus) {
minusTotal = multyTotal;
firstMinus = false;
}
else {
minusTotal -= multyTotal;
}
}
}
addTotal += minusTotal;
}
}
Console.WriteLine("addTotal: " + addTotal);
}
Upvotes: 0
Reputation: 4658
Well, for study purposes and for this particular case you can use your approach with "manual" parsing.
But for real life if you you want to parse formulas, I recommend you to read about Reverse Polish Notation and Shunting-yard algorithm. It's the way how you can parse any formulas without any limits for operations (you can implement this for "+" and then add "-", "*", "/" and even "sin", "cos" operations easily).
Upvotes: 2
Reputation: 793
To handle addition, subtraction, multiplication, and division as well as taking order of operations into account...I would look up the Shunting-Yard Algorithm. This will take the input expression which is given as infix notation and convert it to reverse-Polish notation. There are plenty of .NET implementations of the Shunting-Yard Algorithm that you can look at.
As everyone else has said, if the input only contains one operation type, say addition, then you can use .Split('+')
and then parse out each integer and add them together.
Upvotes: 0
Reputation: 150108
If the only operator you need to worry about is +, you can just use string.Split() to create an array of strings, then convert them to integers and sum them up. You can even further simplify some of that with Linq.
string[] numbers = input.Split('+');
int sum = input.Select(n => int.Parse(n)).Sum();
You could even put that together into one statement
int sum = input.Split('+').Select(n => int.Parse(n)).Sum();
Upvotes: 2
Reputation: 6908
From what you've given, if this is the expected string
input, you could do something like this:
string input = "5+4+6";
int result = 0;
while (input.Contains('+'))
{
var numbers = input.Split('+');
foreach(var num in numbers)
{
result += Convert.ToInt32(num);
}
}
Something similar to this should work, and you can substitute the +
for any of the operators.
Upvotes: 3