REWRITE THE FOLOWWING CODE; SO THAT NO GLOBAL VARIABLES ARE USED. Please CHANGE
ID: 664294 • Letter: R
Question
REWRITE THE FOLOWWING CODE; SO THAT NO GLOBAL VARIABLES ARE USED. Please CHANGE the CODE based on RECURSSION too please. I know how to decalre variables LOCALLY BUT i don't know how to change the eval code using RECURSSION.
#include <stdio.h>
char *a; int i;
int eval(){
int x = 0;
while (a[i] == ' ') i++;
if (a[i] == '+')
{ i++; return eval() + eval(); }
if (a[i] == '*')
{ i++; return eval() * eval(); }
if (a[i] == '-')
{ i++; return eval() - eval(); }
if (a[i] == '/')
{ i++; return eval() / eval(); }
while ((a[i] >= '0') && (a[i] <= '9'))
x = 10*x + (a[i++]-'0');
return x;
}
int main(){
char buff[128];
while(1) {
printf("enter a prefix expression to evaluate: ");
fgets(buff, 127, stdin);
a = buff;
i = 0;
printf(" > %i ", eval());
}
}
Explanation / Answer
recursion is already there in this code.
you are already recalling eval function again and again.This is nothing but recusrion.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.