Skip to content

Instantly share code, notes, and snippets.

@matsud224
Created August 17, 2017 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsud224/ddb44128bf5d761ecfc4a1e22580a94e to your computer and use it in GitHub Desktop.
Save matsud224/ddb44128bf5d761ecfc4a1e22580a94e to your computer and use it in GitHub Desktop.
#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