How can i make a calculator with string that uses multiple operations and brackets? C#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My calculator works almost perfect without brackets (it has some bugs). This calculator can do multiple operations (ex. 12+345*2-100 //602).
I want the brackets and the string inside them to be replaced with the result in and then continue with the calculations.
How can i fix the method for the brackets so it works as intended?
Steps for calculations:
- Finds an symbol
- Finds the numbers around the symbol
- Makes calculations and replaces numbers and symbol with the result in the string
- Checks for any symbols and starts again if there are any
Gives the result of the mathematical problem
//Finds numbers around an symbol
static void FindNumbers(string equation, int start, char symbol)
number1 = 0;
number2 = 0;
number1String = string.Empty;
number2String = string.Empty;
if (equation[start] == symbol)
for (int j = start - 1; j >= 0; j--)//Finds left number around the symbol
if (char.IsDigit(equation[j]))
number1String = equation[j] + number1String;
else
break;
for (int j = start + 1; j < equation.Length; j++)//Finds right number around the symbol
if (char.IsDigit(equation[j]))
number2String += equation[j];
else
break;
number1 = int.Parse(number1String);
number2 = int.Parse(number2String);
return;
//Devision and Multiplication
static void Priority1(string equation, int start)
for (int i = start; i < equation.Length; i++)//Multiplication
if (equation[i] == '*')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 * number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '/')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 / number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
Priority2(equation, 0);
//Addition and Devision
static void Priority2(string equation, int start)
for (int i = start; i < equation.Length; i++)//Addition
if (equation[i] == '+')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 + number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '-')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 - number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Checks if there are more symbols in the string
if (char.IsSymbol(equation[i]))
Priority1(equation, 0);
tempresult = equation;
Console.WriteLine("Result : " + equation);
//Brackets
static void Brackets(string equation, int index)
for (int i = index; index < equation.Length; index++)
if (equation[index] == '(')
index += 1;
Brackets(equation, index);
for (int j = index; j < equation.Length; j++)
if (equation[j] == ')')
tempresult = temp;
Console.WriteLine("..." + tempresult);
break;
temp += equation[j];
Priority1(tempresult, index);
equation = equation.Replace('(' + temp.ToString() + ')', tempresult.ToString());
Console.WriteLine("." + equation);
Priority1(equation, 0);
static void Main(string args)
equation = Console.ReadLine();
Brackets(equation, 0);
Console.ReadKey();
c# string calculator
add a comment |
My calculator works almost perfect without brackets (it has some bugs). This calculator can do multiple operations (ex. 12+345*2-100 //602).
I want the brackets and the string inside them to be replaced with the result in and then continue with the calculations.
How can i fix the method for the brackets so it works as intended?
Steps for calculations:
- Finds an symbol
- Finds the numbers around the symbol
- Makes calculations and replaces numbers and symbol with the result in the string
- Checks for any symbols and starts again if there are any
Gives the result of the mathematical problem
//Finds numbers around an symbol
static void FindNumbers(string equation, int start, char symbol)
number1 = 0;
number2 = 0;
number1String = string.Empty;
number2String = string.Empty;
if (equation[start] == symbol)
for (int j = start - 1; j >= 0; j--)//Finds left number around the symbol
if (char.IsDigit(equation[j]))
number1String = equation[j] + number1String;
else
break;
for (int j = start + 1; j < equation.Length; j++)//Finds right number around the symbol
if (char.IsDigit(equation[j]))
number2String += equation[j];
else
break;
number1 = int.Parse(number1String);
number2 = int.Parse(number2String);
return;
//Devision and Multiplication
static void Priority1(string equation, int start)
for (int i = start; i < equation.Length; i++)//Multiplication
if (equation[i] == '*')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 * number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '/')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 / number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
Priority2(equation, 0);
//Addition and Devision
static void Priority2(string equation, int start)
for (int i = start; i < equation.Length; i++)//Addition
if (equation[i] == '+')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 + number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '-')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 - number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Checks if there are more symbols in the string
if (char.IsSymbol(equation[i]))
Priority1(equation, 0);
tempresult = equation;
Console.WriteLine("Result : " + equation);
//Brackets
static void Brackets(string equation, int index)
for (int i = index; index < equation.Length; index++)
if (equation[index] == '(')
index += 1;
Brackets(equation, index);
for (int j = index; j < equation.Length; j++)
if (equation[j] == ')')
tempresult = temp;
Console.WriteLine("..." + tempresult);
break;
temp += equation[j];
Priority1(tempresult, index);
equation = equation.Replace('(' + temp.ToString() + ')', tempresult.ToString());
Console.WriteLine("." + equation);
Priority1(equation, 0);
static void Main(string args)
equation = Console.ReadLine();
Brackets(equation, 0);
Console.ReadKey();
c# string calculator
2
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
My calculator works almost perfect without brackets (it has some bugs). This calculator can do multiple operations (ex. 12+345*2-100 //602).
I want the brackets and the string inside them to be replaced with the result in and then continue with the calculations.
How can i fix the method for the brackets so it works as intended?
Steps for calculations:
- Finds an symbol
- Finds the numbers around the symbol
- Makes calculations and replaces numbers and symbol with the result in the string
- Checks for any symbols and starts again if there are any
Gives the result of the mathematical problem
//Finds numbers around an symbol
static void FindNumbers(string equation, int start, char symbol)
number1 = 0;
number2 = 0;
number1String = string.Empty;
number2String = string.Empty;
if (equation[start] == symbol)
for (int j = start - 1; j >= 0; j--)//Finds left number around the symbol
if (char.IsDigit(equation[j]))
number1String = equation[j] + number1String;
else
break;
for (int j = start + 1; j < equation.Length; j++)//Finds right number around the symbol
if (char.IsDigit(equation[j]))
number2String += equation[j];
else
break;
number1 = int.Parse(number1String);
number2 = int.Parse(number2String);
return;
//Devision and Multiplication
static void Priority1(string equation, int start)
for (int i = start; i < equation.Length; i++)//Multiplication
if (equation[i] == '*')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 * number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '/')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 / number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
Priority2(equation, 0);
//Addition and Devision
static void Priority2(string equation, int start)
for (int i = start; i < equation.Length; i++)//Addition
if (equation[i] == '+')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 + number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '-')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 - number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Checks if there are more symbols in the string
if (char.IsSymbol(equation[i]))
Priority1(equation, 0);
tempresult = equation;
Console.WriteLine("Result : " + equation);
//Brackets
static void Brackets(string equation, int index)
for (int i = index; index < equation.Length; index++)
if (equation[index] == '(')
index += 1;
Brackets(equation, index);
for (int j = index; j < equation.Length; j++)
if (equation[j] == ')')
tempresult = temp;
Console.WriteLine("..." + tempresult);
break;
temp += equation[j];
Priority1(tempresult, index);
equation = equation.Replace('(' + temp.ToString() + ')', tempresult.ToString());
Console.WriteLine("." + equation);
Priority1(equation, 0);
static void Main(string args)
equation = Console.ReadLine();
Brackets(equation, 0);
Console.ReadKey();
c# string calculator
My calculator works almost perfect without brackets (it has some bugs). This calculator can do multiple operations (ex. 12+345*2-100 //602).
I want the brackets and the string inside them to be replaced with the result in and then continue with the calculations.
How can i fix the method for the brackets so it works as intended?
Steps for calculations:
- Finds an symbol
- Finds the numbers around the symbol
- Makes calculations and replaces numbers and symbol with the result in the string
- Checks for any symbols and starts again if there are any
Gives the result of the mathematical problem
//Finds numbers around an symbol
static void FindNumbers(string equation, int start, char symbol)
number1 = 0;
number2 = 0;
number1String = string.Empty;
number2String = string.Empty;
if (equation[start] == symbol)
for (int j = start - 1; j >= 0; j--)//Finds left number around the symbol
if (char.IsDigit(equation[j]))
number1String = equation[j] + number1String;
else
break;
for (int j = start + 1; j < equation.Length; j++)//Finds right number around the symbol
if (char.IsDigit(equation[j]))
number2String += equation[j];
else
break;
number1 = int.Parse(number1String);
number2 = int.Parse(number2String);
return;
//Devision and Multiplication
static void Priority1(string equation, int start)
for (int i = start; i < equation.Length; i++)//Multiplication
if (equation[i] == '*')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 * number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '/')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 / number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
Priority2(equation, 0);
//Addition and Devision
static void Priority2(string equation, int start)
for (int i = start; i < equation.Length; i++)//Addition
if (equation[i] == '+')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 + number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Devision
if (equation[i] == '-')
symbol = equation[i];
FindNumbers(equation, i, symbol);
currentresult = number1 - number2;
equation = equation.Replace(number1.ToString() + symbol + number2.ToString(),
currentresult.ToString());
for (int i = 0; i < equation.Length; i++)//Checks if there are more symbols in the string
if (char.IsSymbol(equation[i]))
Priority1(equation, 0);
tempresult = equation;
Console.WriteLine("Result : " + equation);
//Brackets
static void Brackets(string equation, int index)
for (int i = index; index < equation.Length; index++)
if (equation[index] == '(')
index += 1;
Brackets(equation, index);
for (int j = index; j < equation.Length; j++)
if (equation[j] == ')')
tempresult = temp;
Console.WriteLine("..." + tempresult);
break;
temp += equation[j];
Priority1(tempresult, index);
equation = equation.Replace('(' + temp.ToString() + ')', tempresult.ToString());
Console.WriteLine("." + equation);
Priority1(equation, 0);
static void Main(string args)
equation = Console.ReadLine();
Brackets(equation, 0);
Console.ReadKey();
c# string calculator
c# string calculator
asked Nov 13 '18 at 20:58
Pr0 G4M3r127Pr0 G4M3r127
43
43
2
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
2
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
2
2
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
2 Answers
2
active
oldest
votes
Your code right now theoretically knows how to parse and evaluate correct simple mathematical expressions. By simple expressions I’m referring to expressions made up only of known unary and binary operators and numbers (no parenthesizes)
The expression you are now attempting to parse is either:
- A simple expression
- A parenthesized expression
- A complex expression made up of simple expressions and parenthesized expressions
If its case #1, then you already know how to handle it; parse it and you are finished.
If it’s case #2 the simply remove the parenthesis and parse the expression within.
If it’s case #3 then you take the expressions inside the top level parenthesizes and parse them; you will either be in step 1 or 2 or 3 and hey! Guess what? You already know how to handle those.
Eventually you’ll end up breaking down your expression into only simple expressions. Then you simply need to backtrack evaluating on the way back.
In plain English, you need a recursive parser; a parser that can call itself and parse and evaluate nested expressions.
Consider for example:
e0: 1 + (2 * (3 - 2))
- e0: You parse
1 + p0
- p0: You parse
2 * p1
- p1: You parse
3 - 2
- You’re done parsing
- p1: evaluates to
1
- p0: evaluates to
2
- e0: evaluates to
3
- You’re finished.
Cool thing about this is that the parser can handle, theoretically, infinite nested expressions... the problem is that your computer probably can’t.
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
You may want to look into breaking the String equation into separate tokens and use something like the Shunting-Yard-Algorithm to parse/calculate the result.
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289408%2fhow-can-i-make-a-calculator-with-string-that-uses-multiple-operations-and-bracke%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code right now theoretically knows how to parse and evaluate correct simple mathematical expressions. By simple expressions I’m referring to expressions made up only of known unary and binary operators and numbers (no parenthesizes)
The expression you are now attempting to parse is either:
- A simple expression
- A parenthesized expression
- A complex expression made up of simple expressions and parenthesized expressions
If its case #1, then you already know how to handle it; parse it and you are finished.
If it’s case #2 the simply remove the parenthesis and parse the expression within.
If it’s case #3 then you take the expressions inside the top level parenthesizes and parse them; you will either be in step 1 or 2 or 3 and hey! Guess what? You already know how to handle those.
Eventually you’ll end up breaking down your expression into only simple expressions. Then you simply need to backtrack evaluating on the way back.
In plain English, you need a recursive parser; a parser that can call itself and parse and evaluate nested expressions.
Consider for example:
e0: 1 + (2 * (3 - 2))
- e0: You parse
1 + p0
- p0: You parse
2 * p1
- p1: You parse
3 - 2
- You’re done parsing
- p1: evaluates to
1
- p0: evaluates to
2
- e0: evaluates to
3
- You’re finished.
Cool thing about this is that the parser can handle, theoretically, infinite nested expressions... the problem is that your computer probably can’t.
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
Your code right now theoretically knows how to parse and evaluate correct simple mathematical expressions. By simple expressions I’m referring to expressions made up only of known unary and binary operators and numbers (no parenthesizes)
The expression you are now attempting to parse is either:
- A simple expression
- A parenthesized expression
- A complex expression made up of simple expressions and parenthesized expressions
If its case #1, then you already know how to handle it; parse it and you are finished.
If it’s case #2 the simply remove the parenthesis and parse the expression within.
If it’s case #3 then you take the expressions inside the top level parenthesizes and parse them; you will either be in step 1 or 2 or 3 and hey! Guess what? You already know how to handle those.
Eventually you’ll end up breaking down your expression into only simple expressions. Then you simply need to backtrack evaluating on the way back.
In plain English, you need a recursive parser; a parser that can call itself and parse and evaluate nested expressions.
Consider for example:
e0: 1 + (2 * (3 - 2))
- e0: You parse
1 + p0
- p0: You parse
2 * p1
- p1: You parse
3 - 2
- You’re done parsing
- p1: evaluates to
1
- p0: evaluates to
2
- e0: evaluates to
3
- You’re finished.
Cool thing about this is that the parser can handle, theoretically, infinite nested expressions... the problem is that your computer probably can’t.
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
Your code right now theoretically knows how to parse and evaluate correct simple mathematical expressions. By simple expressions I’m referring to expressions made up only of known unary and binary operators and numbers (no parenthesizes)
The expression you are now attempting to parse is either:
- A simple expression
- A parenthesized expression
- A complex expression made up of simple expressions and parenthesized expressions
If its case #1, then you already know how to handle it; parse it and you are finished.
If it’s case #2 the simply remove the parenthesis and parse the expression within.
If it’s case #3 then you take the expressions inside the top level parenthesizes and parse them; you will either be in step 1 or 2 or 3 and hey! Guess what? You already know how to handle those.
Eventually you’ll end up breaking down your expression into only simple expressions. Then you simply need to backtrack evaluating on the way back.
In plain English, you need a recursive parser; a parser that can call itself and parse and evaluate nested expressions.
Consider for example:
e0: 1 + (2 * (3 - 2))
- e0: You parse
1 + p0
- p0: You parse
2 * p1
- p1: You parse
3 - 2
- You’re done parsing
- p1: evaluates to
1
- p0: evaluates to
2
- e0: evaluates to
3
- You’re finished.
Cool thing about this is that the parser can handle, theoretically, infinite nested expressions... the problem is that your computer probably can’t.
Your code right now theoretically knows how to parse and evaluate correct simple mathematical expressions. By simple expressions I’m referring to expressions made up only of known unary and binary operators and numbers (no parenthesizes)
The expression you are now attempting to parse is either:
- A simple expression
- A parenthesized expression
- A complex expression made up of simple expressions and parenthesized expressions
If its case #1, then you already know how to handle it; parse it and you are finished.
If it’s case #2 the simply remove the parenthesis and parse the expression within.
If it’s case #3 then you take the expressions inside the top level parenthesizes and parse them; you will either be in step 1 or 2 or 3 and hey! Guess what? You already know how to handle those.
Eventually you’ll end up breaking down your expression into only simple expressions. Then you simply need to backtrack evaluating on the way back.
In plain English, you need a recursive parser; a parser that can call itself and parse and evaluate nested expressions.
Consider for example:
e0: 1 + (2 * (3 - 2))
- e0: You parse
1 + p0
- p0: You parse
2 * p1
- p1: You parse
3 - 2
- You’re done parsing
- p1: evaluates to
1
- p0: evaluates to
2
- e0: evaluates to
3
- You’re finished.
Cool thing about this is that the parser can handle, theoretically, infinite nested expressions... the problem is that your computer probably can’t.
edited Nov 13 '18 at 22:07
answered Nov 13 '18 at 21:17
InBetweenInBetween
25.5k34068
25.5k34068
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
You don't really need a recursive-descent parser for this type of thing. It's a valid approach, but algorithms like Shunting-Yard are simpler and faster.
– 3Dave
Nov 13 '18 at 21:45
1
1
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
@3Dave I know, just pointing out alternative approaches. It’s very instructive to write your own parser.
– InBetween
Nov 13 '18 at 22:05
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
Thanks for your tips.
– Pr0 G4M3r127
Nov 18 '18 at 17:28
add a comment |
You may want to look into breaking the String equation into separate tokens and use something like the Shunting-Yard-Algorithm to parse/calculate the result.
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
add a comment |
You may want to look into breaking the String equation into separate tokens and use something like the Shunting-Yard-Algorithm to parse/calculate the result.
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
add a comment |
You may want to look into breaking the String equation into separate tokens and use something like the Shunting-Yard-Algorithm to parse/calculate the result.
You may want to look into breaking the String equation into separate tokens and use something like the Shunting-Yard-Algorithm to parse/calculate the result.
answered Nov 13 '18 at 21:20
LoPabloLoPablo
7412
7412
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
add a comment |
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
– jhpratt
Nov 27 '18 at 7:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289408%2fhow-can-i-make-a-calculator-with-string-that-uses-multiple-operations-and-bracke%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Step 0: Define your expression syntax
– TaW
Nov 13 '18 at 21:02
this smells like homework! And also something I would attempt as a TDD Kata
– Daniel Loudon
Nov 13 '18 at 21:45
Actualy this is not homework, but i found something in a book and this came to my mind, so i decided to try to make it.
– Pr0 G4M3r127
Nov 18 '18 at 17:28