Created
August 17, 2017 07:44
-
-
Save matsud224/ddb44128bf5d761ecfc4a1e22580a94e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ctype.h> | |
#include <stdlib.h> | |
#include <math.h> | |
int main(){ | |
while(1){ | |
printf("> "); | |
printf("%d\n",calc_3()); | |
while(getchar()!='\n'); | |
} | |
return 0; | |
} | |
int calc_3(){ | |
int result=calc_2(); | |
while(1){ | |
char op=getchar(); | |
switch(op){ | |
case '+': | |
result+=calc_2(); | |
break; | |
default: | |
ungetc(op,stdin); | |
return result; | |
} | |
} | |
} | |
int calc_2(){ | |
int result=calc_1(); | |
while(1){ | |
char op=getchar(); | |
switch(op){ | |
case '*': | |
result*=calc_1(); | |
break; | |
default: | |
ungetc(op,stdin); | |
return result; | |
} | |
} | |
} | |
int calc_1(){ | |
char top=getchar(); | |
if(top=='('){ | |
int result=calc_3(); | |
getchar(); | |
return result; | |
}else if(isdigit(top)){ | |
int i; | |
char buf[64]; | |
buf[0]=top; | |
for(i=1;i<64;i++){ | |
buf[i]=getchar(); | |
if(!isdigit(buf[i])){ | |
ungetc(buf[i],stdin); | |
buf[i]='\0'; | |
break; | |
} | |
} | |
return atoi(buf); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment