Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. int main(){
  7. while(1){
  8. printf("> ");
  9. printf("%d\n",calc_3());
  10. while(getchar()!='\n');
  11. }
  12. return 0;
  13. }
  14.  
  15. int calc_3(){
  16. int result=calc_2();
  17. while(1){
  18. char op=getchar();
  19. switch(op){
  20. case '+':
  21. result+=calc_2();
  22. break;
  23. default:
  24. ungetc(op,stdin);
  25. return result;
  26. }
  27. }
  28. }
  29.  
  30. int calc_2(){
  31. int result=calc_1();
  32. while(1){
  33. char op=getchar();
  34. switch(op){
  35. case '*':
  36. result*=calc_1();
  37. break;
  38. default:
  39. ungetc(op,stdin);
  40. return result;
  41. }
  42. }
  43. }
  44.  
  45. int calc_1(){
  46. char top=getchar();
  47. if(top=='('){
  48. int result=calc_3();
  49. getchar();
  50. return result;
  51. }else if(isdigit(top)){
  52. int i;
  53. char buf[64];
  54. buf[0]=top;
  55. for(i=1;i<64;i++){
  56. buf[i]=getchar();
  57. if(!isdigit(buf[i])){
  58. ungetc(buf[i],stdin);
  59. buf[i]='\0';
  60. break;
  61. }
  62. }
  63. return atoi(buf);
  64. }
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement