Evaluating Arithmetic Expression Using Recursion in C
up vote
0
down vote
favorite
Here's an algorithm for evaluating an arithmetic expression using recursion:
- Find operand1
- t1 = Eval(operand1)
- Find operand2
- t2 = Eval(operand2)
- Apply operator on t1 and t2
Assumptions:
- each operand is between two operators
there are only binary operations
for each operation, there are parentheses (including outermost parentheses)
Input:
array of tokens representing an arithmetic expression
num_tokens is the number of tokens
Sample array of tokens: "(", "9", "+", "(", "50", "-", "25", ")", ")"
I tried to implement the algorithm but my program doesn't run (exit status -1 is the only message I get). Why is that happening?
int apply(char op, int a, int b)
if (op == '+')
printf("%d %c %dn", a,op,b);
return a + b;
else if (op == '-')
printf("%d %c %dn", a,op,b);
return a - b;
else if(op == '/')
printf("%d %c %dn", a,op,b);
return a / b;
else if(op == '*')
printf("%d %c %dn", a,op,b);
return a * b;
int eval_tokens(char** expression, int num_tokens)
// implement me
int index;
int opIndex = find_operator(expression, num_tokens); //find index of operator
int count1=0,count2=0,term1,term2,i,j;
if(*expression[0] == '(')
i = 1;
else
i = 0;
while(i <= opIndex)
i++;
count1++;
term1 = eval_tokens(expression+1,count1);
j = opIndex+1;
while(j < num_tokens)
count2++;
j++;
term2 = eval_tokens(expression+opIndex+1,count2); //expression+opIndex+1 points to index after opIndex
return apply(*expression[opIndex], term1, term2);
int main(void)
char*expression = "(", "(", "5", "+", "3", ")", "-", "(", "2", "+", "1", ")", ")";
printf("result = %dn", eval_tokens(expression, 13));
return 0;
c recursion calculator
|
show 2 more comments
up vote
0
down vote
favorite
Here's an algorithm for evaluating an arithmetic expression using recursion:
- Find operand1
- t1 = Eval(operand1)
- Find operand2
- t2 = Eval(operand2)
- Apply operator on t1 and t2
Assumptions:
- each operand is between two operators
there are only binary operations
for each operation, there are parentheses (including outermost parentheses)
Input:
array of tokens representing an arithmetic expression
num_tokens is the number of tokens
Sample array of tokens: "(", "9", "+", "(", "50", "-", "25", ")", ")"
I tried to implement the algorithm but my program doesn't run (exit status -1 is the only message I get). Why is that happening?
int apply(char op, int a, int b)
if (op == '+')
printf("%d %c %dn", a,op,b);
return a + b;
else if (op == '-')
printf("%d %c %dn", a,op,b);
return a - b;
else if(op == '/')
printf("%d %c %dn", a,op,b);
return a / b;
else if(op == '*')
printf("%d %c %dn", a,op,b);
return a * b;
int eval_tokens(char** expression, int num_tokens)
// implement me
int index;
int opIndex = find_operator(expression, num_tokens); //find index of operator
int count1=0,count2=0,term1,term2,i,j;
if(*expression[0] == '(')
i = 1;
else
i = 0;
while(i <= opIndex)
i++;
count1++;
term1 = eval_tokens(expression+1,count1);
j = opIndex+1;
while(j < num_tokens)
count2++;
j++;
term2 = eval_tokens(expression+opIndex+1,count2); //expression+opIndex+1 points to index after opIndex
return apply(*expression[opIndex], term1, term2);
int main(void)
char*expression = "(", "(", "5", "+", "3", ")", "-", "(", "2", "+", "1", ")", ")";
printf("result = %dn", eval_tokens(expression, 13));
return 0;
c recursion calculator
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.
– Stephan Lechner
Nov 11 at 20:02
Well, the main problem with your program is that it's missing amain
function.
– melpomene
Nov 11 at 20:04
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner I tried usingterm1 = infix_eval_tokens(expression+1,count1);
to take the left operand andterm2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.
– vxssx
Nov 11 at 20:17
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Here's an algorithm for evaluating an arithmetic expression using recursion:
- Find operand1
- t1 = Eval(operand1)
- Find operand2
- t2 = Eval(operand2)
- Apply operator on t1 and t2
Assumptions:
- each operand is between two operators
there are only binary operations
for each operation, there are parentheses (including outermost parentheses)
Input:
array of tokens representing an arithmetic expression
num_tokens is the number of tokens
Sample array of tokens: "(", "9", "+", "(", "50", "-", "25", ")", ")"
I tried to implement the algorithm but my program doesn't run (exit status -1 is the only message I get). Why is that happening?
int apply(char op, int a, int b)
if (op == '+')
printf("%d %c %dn", a,op,b);
return a + b;
else if (op == '-')
printf("%d %c %dn", a,op,b);
return a - b;
else if(op == '/')
printf("%d %c %dn", a,op,b);
return a / b;
else if(op == '*')
printf("%d %c %dn", a,op,b);
return a * b;
int eval_tokens(char** expression, int num_tokens)
// implement me
int index;
int opIndex = find_operator(expression, num_tokens); //find index of operator
int count1=0,count2=0,term1,term2,i,j;
if(*expression[0] == '(')
i = 1;
else
i = 0;
while(i <= opIndex)
i++;
count1++;
term1 = eval_tokens(expression+1,count1);
j = opIndex+1;
while(j < num_tokens)
count2++;
j++;
term2 = eval_tokens(expression+opIndex+1,count2); //expression+opIndex+1 points to index after opIndex
return apply(*expression[opIndex], term1, term2);
int main(void)
char*expression = "(", "(", "5", "+", "3", ")", "-", "(", "2", "+", "1", ")", ")";
printf("result = %dn", eval_tokens(expression, 13));
return 0;
c recursion calculator
Here's an algorithm for evaluating an arithmetic expression using recursion:
- Find operand1
- t1 = Eval(operand1)
- Find operand2
- t2 = Eval(operand2)
- Apply operator on t1 and t2
Assumptions:
- each operand is between two operators
there are only binary operations
for each operation, there are parentheses (including outermost parentheses)
Input:
array of tokens representing an arithmetic expression
num_tokens is the number of tokens
Sample array of tokens: "(", "9", "+", "(", "50", "-", "25", ")", ")"
I tried to implement the algorithm but my program doesn't run (exit status -1 is the only message I get). Why is that happening?
int apply(char op, int a, int b)
if (op == '+')
printf("%d %c %dn", a,op,b);
return a + b;
else if (op == '-')
printf("%d %c %dn", a,op,b);
return a - b;
else if(op == '/')
printf("%d %c %dn", a,op,b);
return a / b;
else if(op == '*')
printf("%d %c %dn", a,op,b);
return a * b;
int eval_tokens(char** expression, int num_tokens)
// implement me
int index;
int opIndex = find_operator(expression, num_tokens); //find index of operator
int count1=0,count2=0,term1,term2,i,j;
if(*expression[0] == '(')
i = 1;
else
i = 0;
while(i <= opIndex)
i++;
count1++;
term1 = eval_tokens(expression+1,count1);
j = opIndex+1;
while(j < num_tokens)
count2++;
j++;
term2 = eval_tokens(expression+opIndex+1,count2); //expression+opIndex+1 points to index after opIndex
return apply(*expression[opIndex], term1, term2);
int main(void)
char*expression = "(", "(", "5", "+", "3", ")", "-", "(", "2", "+", "1", ")", ")";
printf("result = %dn", eval_tokens(expression, 13));
return 0;
c recursion calculator
c recursion calculator
edited Nov 15 at 0:39
asked Nov 11 at 19:53
vxssx
134
134
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.
– Stephan Lechner
Nov 11 at 20:02
Well, the main problem with your program is that it's missing amain
function.
– melpomene
Nov 11 at 20:04
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner I tried usingterm1 = infix_eval_tokens(expression+1,count1);
to take the left operand andterm2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.
– vxssx
Nov 11 at 20:17
|
show 2 more comments
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.
– Stephan Lechner
Nov 11 at 20:02
Well, the main problem with your program is that it's missing amain
function.
– melpomene
Nov 11 at 20:04
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner I tried usingterm1 = infix_eval_tokens(expression+1,count1);
to take the left operand andterm2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.
– vxssx
Nov 11 at 20:17
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.– Stephan Lechner
Nov 11 at 20:02
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.– Stephan Lechner
Nov 11 at 20:02
Well, the main problem with your program is that it's missing a
main
function.– melpomene
Nov 11 at 20:04
Well, the main problem with your program is that it's missing a
main
function.– melpomene
Nov 11 at 20:04
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner I tried using
term1 = infix_eval_tokens(expression+1,count1);
to take the left operand and term2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.– vxssx
Nov 11 at 20:17
@StephanLechner I tried using
term1 = infix_eval_tokens(expression+1,count1);
to take the left operand and term2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.– vxssx
Nov 11 at 20:17
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
To use str
(or expression
) as stack from which you can take items off, I'd make these arguments "modifyable" in the recursive function. Therefore, you could introduce a second function int eval_tokens_recursive(char*** expression, int *num_tokens)
, which has one more level of indirection and may actually "take items from the stack" by altering the arguments' values.
The code could look as follows. Hope it helps.
int eval_tokens_recursive(char*** expression, int *num_tokens)
char *token = **expression;
if (*num_tokens == 0)
printf("expecting more tokens.n");
exit(1);
if (*token == '(') // begin of expression?
(*expression)++; // skip opening brace
(*num_tokens)--;
// lhs
int lhs = eval_tokens_recursive(expression, num_tokens);
// operand
char operand = ***expression;
(*expression)++;
(*num_tokens)--;
// rhs
int rhs = eval_tokens_recursive(expression, num_tokens);
(*expression)++; // skip closing brace
(*num_tokens)--;
switch (operand)
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
default:
return 0;
else // not an expression; must be a numeric token
int operand;
if (sscanf(token, "%2d", &operand) != 1)
printf("expecting numeric value; cannot parse %s.n", token);
exit(1);
(*expression)++;
(*num_tokens)--;
return operand;
int eval_tokens(char** expression, int num_tokens)
return eval_tokens_recursive(&expression, &num_tokens);
int main()
char *expressions = "(", "9", "+", "(", "50", "-", "25", ")", ")";
int result = eval_tokens(expressions, 9);
printf("result: %dn", result);
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
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',
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%2f53252610%2fevaluating-arithmetic-expression-using-recursion-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
To use str
(or expression
) as stack from which you can take items off, I'd make these arguments "modifyable" in the recursive function. Therefore, you could introduce a second function int eval_tokens_recursive(char*** expression, int *num_tokens)
, which has one more level of indirection and may actually "take items from the stack" by altering the arguments' values.
The code could look as follows. Hope it helps.
int eval_tokens_recursive(char*** expression, int *num_tokens)
char *token = **expression;
if (*num_tokens == 0)
printf("expecting more tokens.n");
exit(1);
if (*token == '(') // begin of expression?
(*expression)++; // skip opening brace
(*num_tokens)--;
// lhs
int lhs = eval_tokens_recursive(expression, num_tokens);
// operand
char operand = ***expression;
(*expression)++;
(*num_tokens)--;
// rhs
int rhs = eval_tokens_recursive(expression, num_tokens);
(*expression)++; // skip closing brace
(*num_tokens)--;
switch (operand)
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
default:
return 0;
else // not an expression; must be a numeric token
int operand;
if (sscanf(token, "%2d", &operand) != 1)
printf("expecting numeric value; cannot parse %s.n", token);
exit(1);
(*expression)++;
(*num_tokens)--;
return operand;
int eval_tokens(char** expression, int num_tokens)
return eval_tokens_recursive(&expression, &num_tokens);
int main()
char *expressions = "(", "9", "+", "(", "50", "-", "25", ")", ")";
int result = eval_tokens(expressions, 9);
printf("result: %dn", result);
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
add a comment |
up vote
1
down vote
accepted
To use str
(or expression
) as stack from which you can take items off, I'd make these arguments "modifyable" in the recursive function. Therefore, you could introduce a second function int eval_tokens_recursive(char*** expression, int *num_tokens)
, which has one more level of indirection and may actually "take items from the stack" by altering the arguments' values.
The code could look as follows. Hope it helps.
int eval_tokens_recursive(char*** expression, int *num_tokens)
char *token = **expression;
if (*num_tokens == 0)
printf("expecting more tokens.n");
exit(1);
if (*token == '(') // begin of expression?
(*expression)++; // skip opening brace
(*num_tokens)--;
// lhs
int lhs = eval_tokens_recursive(expression, num_tokens);
// operand
char operand = ***expression;
(*expression)++;
(*num_tokens)--;
// rhs
int rhs = eval_tokens_recursive(expression, num_tokens);
(*expression)++; // skip closing brace
(*num_tokens)--;
switch (operand)
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
default:
return 0;
else // not an expression; must be a numeric token
int operand;
if (sscanf(token, "%2d", &operand) != 1)
printf("expecting numeric value; cannot parse %s.n", token);
exit(1);
(*expression)++;
(*num_tokens)--;
return operand;
int eval_tokens(char** expression, int num_tokens)
return eval_tokens_recursive(&expression, &num_tokens);
int main()
char *expressions = "(", "9", "+", "(", "50", "-", "25", ")", ")";
int result = eval_tokens(expressions, 9);
printf("result: %dn", result);
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
To use str
(or expression
) as stack from which you can take items off, I'd make these arguments "modifyable" in the recursive function. Therefore, you could introduce a second function int eval_tokens_recursive(char*** expression, int *num_tokens)
, which has one more level of indirection and may actually "take items from the stack" by altering the arguments' values.
The code could look as follows. Hope it helps.
int eval_tokens_recursive(char*** expression, int *num_tokens)
char *token = **expression;
if (*num_tokens == 0)
printf("expecting more tokens.n");
exit(1);
if (*token == '(') // begin of expression?
(*expression)++; // skip opening brace
(*num_tokens)--;
// lhs
int lhs = eval_tokens_recursive(expression, num_tokens);
// operand
char operand = ***expression;
(*expression)++;
(*num_tokens)--;
// rhs
int rhs = eval_tokens_recursive(expression, num_tokens);
(*expression)++; // skip closing brace
(*num_tokens)--;
switch (operand)
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
default:
return 0;
else // not an expression; must be a numeric token
int operand;
if (sscanf(token, "%2d", &operand) != 1)
printf("expecting numeric value; cannot parse %s.n", token);
exit(1);
(*expression)++;
(*num_tokens)--;
return operand;
int eval_tokens(char** expression, int num_tokens)
return eval_tokens_recursive(&expression, &num_tokens);
int main()
char *expressions = "(", "9", "+", "(", "50", "-", "25", ")", ")";
int result = eval_tokens(expressions, 9);
printf("result: %dn", result);
To use str
(or expression
) as stack from which you can take items off, I'd make these arguments "modifyable" in the recursive function. Therefore, you could introduce a second function int eval_tokens_recursive(char*** expression, int *num_tokens)
, which has one more level of indirection and may actually "take items from the stack" by altering the arguments' values.
The code could look as follows. Hope it helps.
int eval_tokens_recursive(char*** expression, int *num_tokens)
char *token = **expression;
if (*num_tokens == 0)
printf("expecting more tokens.n");
exit(1);
if (*token == '(') // begin of expression?
(*expression)++; // skip opening brace
(*num_tokens)--;
// lhs
int lhs = eval_tokens_recursive(expression, num_tokens);
// operand
char operand = ***expression;
(*expression)++;
(*num_tokens)--;
// rhs
int rhs = eval_tokens_recursive(expression, num_tokens);
(*expression)++; // skip closing brace
(*num_tokens)--;
switch (operand)
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
default:
return 0;
else // not an expression; must be a numeric token
int operand;
if (sscanf(token, "%2d", &operand) != 1)
printf("expecting numeric value; cannot parse %s.n", token);
exit(1);
(*expression)++;
(*num_tokens)--;
return operand;
int eval_tokens(char** expression, int num_tokens)
return eval_tokens_recursive(&expression, &num_tokens);
int main()
char *expressions = "(", "9", "+", "(", "50", "-", "25", ")", ")";
int result = eval_tokens(expressions, 9);
printf("result: %dn", result);
edited Nov 14 at 10:05
vxssx
134
134
answered Nov 11 at 20:30
Stephan Lechner
25.3k21839
25.3k21839
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
add a comment |
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
Just out of curiosity, is it possible for the same thing to be accomplished without a helper function? If so, would there be any significant differences in code length or the complexity of the statements within the function?
– vxssx
Nov 11 at 21:25
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
I don't think so, because I see no way of how to pass back how many items had been processed. Concerning code length/complexity, introducing one helper function will not count in any significant way.
– Stephan Lechner
Nov 11 at 22:44
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53252610%2fevaluating-arithmetic-expression-using-recursion-in-c%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
**expression
serves as stack (queue actually). If it is infix on solely binary operators, the order is.. (1) take LHS operand from expression "stack" using a recursive call, (2) take the operand from stack, (3) take RHS operand from stack and evaluate then.– Stephan Lechner
Nov 11 at 20:02
Well, the main problem with your program is that it's missing a
main
function.– melpomene
Nov 11 at 20:04
@melpomene I did have a main function while testing. I'll add it to my question.
– vxssx
Nov 11 at 20:05
@StephanLechner How do I "take" from the expression "stack"?
– vxssx
Nov 11 at 20:10
@StephanLechner I tried using
term1 = infix_eval_tokens(expression+1,count1);
to take the left operand andterm2 = infix_eval_tokens(expression+opIndex+1,count2);
to take the right operand but it doesn't seem to work.– vxssx
Nov 11 at 20:17