Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement