Question
Can someone add in-line comments to this C code? I'm not 100% sure how it works. #include int eval (char operator, int x, int y)
Can someone add in-line comments to this C code? I'm not 100% sure how it works.
#include
int eval (char operator, int x, int y)
{
switch (operator)
{
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
}
return 0;
}
int
main ()
{
char exp[20];
printf (\"Enter the expression: \");
scanf (\"%s\", exp);
int result, operand = 0;
char *expr = exp, operator;
char ch = *expr;
result = ch - '0';
expr++;
while (*expr != '\\0')
{
char ch = *expr;
switch (ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': result = eval (operator, result, ch - '0');
break;
case '+': operator = '+';
break;
case '-': operator = '-';
break;
case '*': operator = '*';
break;
case '/': operator = '/';
break;
}
expr++;
}
printf (\"The result of strict L->R evaluation is: %d \", result);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started