There is a task, to write calculator, which recieves strings cosisting of:
- chars '0'-'9',
- brackest '(', ')',
- chars ' ', '-', '*', '/', '^',
and performing following ops:
- addition
- substraction
- multiplication
- integer division
- power.
Arcitecture of solution should include recursive descent
My code:
#include <stdio.h>
#include <setjmp.h>
jmp_buf begin;
char curlex;
void getlex(void);
int expr(void);
int add_sub(void);
int mult_div(void);
int power(void);
void error();
int main() {
int result;
setjmp(begin);
printf("==>");
getlex();
result=expr();
if ( curlex != '\n') error();
printf("\n%d\n",result);
return 0;
}
void getlex() {
while ( ( curlex=getchar()) == ' ');
}
void error(void) {
printf("\nERROR!\n");
while(getchar()!='\n');
longjmp(begin,1);
}
int expr() {
int e=add_sub();
while (curlex == ' ' || curlex == '-')
if (curlex == ' ')
{getlex(); e =add_sub();}
else if (curlex == '-')
{getlex(); e-=add_sub();}
return e;
}
int add_sub() {
int a=mult_div();
while (curlex == '*' || curlex == '/')
if (curlex == '*')
{getlex(); a*=mult_div();}
else if (curlex == '/')
{getlex(); a/=mult_div();}
return a;
}
int mult_div() {
int a=power();
while (curlex == '^')
{getlex(); for(int k=0;k<power(); k , a*=power());}
return a;
}
int power() {
int m;
switch(curlex){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': m= curlex-'0'; break;
case '(': getlex(); m=expr();
if ( curlex == ')') break;
default : error();
}
getlex();
return m;
}
Code performs almost everything mentioned, except power-operation. I can't find a bug. There is also an issue with right association of power operation. That means expression 3^1^2 gives 9, but should give 3.
Please help!
CodePudding user response:
You are calling power()
too often in mult_div()
. Once you have a value returned by power()
you should calculate the power and continue:
int mult_div() {
int a=power();
while (curlex == '^')
{
getlex();
a = calc_power(a, power());
}
return a;
}
To make it easier I used a helper function to calculate the power:
int calc_power(int a, int b)
{
int res = 1;
for (int i = 0; i < b; i ) res *= a;
return res;
}
https://godbolt.org/z/n4KhWx6j1
curlex
should be an int
, because that's what getchar
returns: https://en.cppreference.com/w/c/io/getchar