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;








-1















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:



  1. Finds an symbol

  2. Finds the numbers around the symbol

  3. Makes calculations and replaces numbers and symbol with the result in the string

  4. Checks for any symbols and starts again if there are any


  5. 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();











share|improve this question

















  • 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

















-1















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:



  1. Finds an symbol

  2. Finds the numbers around the symbol

  3. Makes calculations and replaces numbers and symbol with the result in the string

  4. Checks for any symbols and starts again if there are any


  5. 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();











share|improve this question

















  • 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













-1












-1








-1


1






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:



  1. Finds an symbol

  2. Finds the numbers around the symbol

  3. Makes calculations and replaces numbers and symbol with the result in the string

  4. Checks for any symbols and starts again if there are any


  5. 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();











share|improve this question














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:



  1. Finds an symbol

  2. Finds the numbers around the symbol

  3. Makes calculations and replaces numbers and symbol with the result in the string

  4. Checks for any symbols and starts again if there are any


  5. 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












2 Answers
2






active

oldest

votes


















3














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:



  1. A simple expression

  2. A parenthesized expression

  3. 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))


  1. e0: You parse 1 + p0

  2. p0: You parse 2 * p1

  3. p1: You parse 3 - 2

  4. You’re done parsing

  5. p1: evaluates to 1

  6. p0: evaluates to 2

  7. e0: evaluates to 3

  8. 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.






share|improve this answer

























  • 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


















4














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.






share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









3














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:



  1. A simple expression

  2. A parenthesized expression

  3. 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))


  1. e0: You parse 1 + p0

  2. p0: You parse 2 * p1

  3. p1: You parse 3 - 2

  4. You’re done parsing

  5. p1: evaluates to 1

  6. p0: evaluates to 2

  7. e0: evaluates to 3

  8. 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.






share|improve this answer

























  • 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















3














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:



  1. A simple expression

  2. A parenthesized expression

  3. 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))


  1. e0: You parse 1 + p0

  2. p0: You parse 2 * p1

  3. p1: You parse 3 - 2

  4. You’re done parsing

  5. p1: evaluates to 1

  6. p0: evaluates to 2

  7. e0: evaluates to 3

  8. 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.






share|improve this answer

























  • 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













3












3








3







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:



  1. A simple expression

  2. A parenthesized expression

  3. 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))


  1. e0: You parse 1 + p0

  2. p0: You parse 2 * p1

  3. p1: You parse 3 - 2

  4. You’re done parsing

  5. p1: evaluates to 1

  6. p0: evaluates to 2

  7. e0: evaluates to 3

  8. 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.






share|improve this answer















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:



  1. A simple expression

  2. A parenthesized expression

  3. 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))


  1. e0: You parse 1 + p0

  2. p0: You parse 2 * p1

  3. p1: You parse 3 - 2

  4. You’re done parsing

  5. p1: evaluates to 1

  6. p0: evaluates to 2

  7. e0: evaluates to 3

  8. 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.







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













4














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.






share|improve this answer























  • 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















4














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.






share|improve this answer























  • 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













4












4








4







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)