Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. #include <math.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define BUFFSIZE 1
  7. #define MAXOP 1
  8. #define MAXVAL 2
  9. #define NUMBER 0
  10. #define FUNCTION 255
  11. void isfunction(char s[]);
  12. void print(void);
  13. void dupe(void);
  14. void swap(void);
  15. void clear(void);
  16. int getch(void);
  17. int getop(char s[]);
  18. double pop(void);
  19. void push(double);
  20. void ungetch(int c);
  21. char buf[BUFFSIZE];
  22. int bufp=0;
  23. int sp=0;
  24. double val[MAXVAL];
  25. double sto[26];
  26. int last=0;
  27. int main(void)
  28. {
  29.     int type;
  30.     double op2;
  31.     char s[MAXOP];
  32.     while((type=getop(s))!=EOF)
  33.     {
  34.         last=type;
  35.         switch(type)
  36.         {
  37.             case FUNCTION:
  38.             {
  39.                 isfunction(s);
  40.                 break;
  41.             }
  42.             case NUMBER:
  43.             {
  44.                 push(atof(s));
  45.                 break;
  46.             }
  47.             case'$':
  48.             {
  49.                 print();
  50.                 break;
  51.             }
  52.             case'%':
  53.             {
  54.                 op2=pop();
  55.                 push((int)pop()%(int)op2);
  56.                 break;
  57.             }
  58.             case'+':
  59.             {
  60.                 push(pop()+pop());
  61.                 break;
  62.             }
  63.             case'*':
  64.             {
  65.                 push(pop()*pop());
  66.                 break;
  67.             }
  68.             case'-':
  69.             {
  70.                 op2=pop();
  71.                 push(pop()-op2);
  72.                 break;
  73.             }
  74.             case'/':
  75.             {
  76.                 op2=pop();
  77.                 if(op2!=0.0)
  78.                 {
  79.                     push(pop()/op2);
  80.                 }
  81.                 else
  82.                 {
  83.                     printf("Error: zero divisor.\n");
  84.                 }
  85.                 break;
  86.             }
  87.             case'\n':
  88.             {
  89.                 printf("\t%.8g\n",pop());
  90.                 break;
  91.             }
  92.             default:
  93.             {
  94.                 if(type>64&&type<91||type>96&&type<123)
  95.                 {
  96.                     if(type>64&&type<91)
  97.                     {
  98.                         push(sto[(type-65)]);
  99.                     }
  100.                     else if(type>96&&type<123)
  101.                     {
  102.                         push(sto[(type-97)]);
  103.                     }
  104.                 }
  105.                 else
  106.                 {
  107.                     printf("Error: unknown command %s.\n",s);
  108.                 }
  109.                 break;
  110.             }
  111.         }
  112.     }
  113.     return 0;
  114. }
  115. void isfunction(char s[])
  116. {
  117.     double temp = 0;
  118.     if(strcmp(s,"print")==0)
  119.     {
  120.         print();
  121.     }
  122.     else if(strcmp(s,"dupe")==0)
  123.     {
  124.         dupe();
  125.     }
  126.     else if(strcmp(s,"swap")==0)
  127.     {
  128.         swap();
  129.     }
  130.     else if(strcmp(s,"clear")==0)
  131.     {
  132.         clear();
  133.     }
  134.     else if(strcmp(s,"sin")==0)
  135.     {
  136.         push(sin(pop()));
  137.     }
  138.     else if(strcmp(s,"cos")==0)
  139.     {
  140.         push(cos(pop()));
  141.     }
  142.     else if(strcmp(s,"tan")==0)
  143.     {
  144.         push(tan(pop()));
  145.     }
  146.     else if(strcmp(s,"asin")==0)
  147.     {
  148.         push(asin(pop()));
  149.     }
  150.     else if(strcmp(s,"acos")==0)
  151.     {
  152.         push(acos(pop()));
  153.     }
  154.     else if(strcmp(s,"atan")==0)
  155.     {
  156.         push(atan(pop()));
  157.     }
  158.     else if(strcmp(s,"sinh")==0)
  159.     {
  160.         push(sinh(pop()));
  161.     }
  162.     else if(strcmp(s,"cosh")==0)
  163.     {
  164.         push(cosh(pop()));
  165.     }
  166.     else if(strcmp(s,"tanh")==0)
  167.     {
  168.         push(tanh(pop()));
  169.     }
  170.     else if(strcmp(s,"exp")==0)
  171.     {
  172.         push(exp(pop()));
  173.     }
  174.     else if(strcmp(s,"ln")==0)
  175.     {
  176.         push(log(pop()));
  177.     }
  178.     else if(strcmp(s,"log")==0)
  179.     {
  180.         push(log10(pop()));
  181.     }
  182.     else if(strcmp(s,"pow")==0)
  183.     {
  184.         temp=pop();
  185.         push(pow(pop(),temp));
  186.     }
  187.     else if(strcmp(s,"sqrt")==0)
  188.     {
  189.         push(sqrt(pop()));
  190.     }
  191.     else if(strcmp(s,"ceil")==0)
  192.     {
  193.         push(ceil(pop()));
  194.     }
  195.     else if(strcmp(s,"floor")==0)
  196.     {
  197.         push(floor(pop()));
  198.     }
  199.     else if(strcmp(s,"fabs")==0)
  200.     {
  201.         push(fabs(pop()));
  202.     }
  203.     else if(strcmp(s,"ldexp")==0)
  204.     {
  205.         temp=pop();
  206.         push(ldexp(pop(),temp));
  207.     }
  208.     else if(strcmp(s,"fmod")==0)
  209.     {
  210.         temp=pop();
  211.         push(fmod(pop(),temp));
  212.     }
  213.     else
  214.     {
  215.         printf("isfunction: Error: unknown function.");
  216.     }
  217. }
  218. void print(void)
  219. {
  220.     double retval;
  221.     if(sp>0)
  222.     {
  223.         retval= val[sp];
  224.         printf("%lf\n",retval);
  225.     }
  226.     else
  227.     {
  228.         printf("Error: the stack is empty\n");
  229.     }
  230. }
  231. void dupe(void)
  232. {
  233.     double c;
  234.     if(sp>0&&sp<MAXVAL)
  235.     {
  236.         c=val[(sp-1)];
  237.         val[sp]=c;
  238.         val[++sp]='\0';
  239.     }
  240.     else if(sp==0)
  241.     {
  242.         printf("Error: the is stack empty.\n");
  243.     }
  244.     else if(sp==MAXVAL)
  245.     {
  246.         printf("Error: the stack is full, cannot duplicate.\n");
  247.     }
  248. }
  249. void swap(void)
  250. {
  251.     double c;
  252.     if(sp>1)
  253.     {
  254.         c=val[(sp-2)];
  255.         val[(sp-2)]=val[(sp-1)];
  256.         val[(sp-1)]=c;
  257.        
  258.     }
  259.     else
  260.     {
  261.         printf("Error: the stack is empty\n");
  262.     }
  263. }
  264. void clear(void)
  265. {
  266.     sp=0;
  267. }
  268.  
  269. int getch(void)
  270. {
  271.     return (bufp>0)?buf[--bufp]:getchar();
  272. }
  273. int getop(char s[])
  274. {
  275.     int i,c;
  276.     while((s[0]=c=getch())==' '||c=='\t')
  277.     {
  278.         ;
  279.     }
  280.     s[1]='\0';
  281.     i=0;
  282.     if(isalpha(c))
  283.     {
  284.         while(isalpha(s[++i]=c=getch()))
  285.         {
  286.             ;
  287.         }
  288.         s[i]='\0';
  289.         if(c!=EOF)
  290.         {
  291.             ungetch(c);
  292.         }
  293.         if(strlen(s)>1)
  294.         {
  295.             return FUNCTION;
  296.         }
  297.         else
  298.         {
  299.             return c;
  300.         }
  301.     }
  302.     if(!isdigit(c)&&c!='.')
  303.     {
  304.         return c;
  305.     }
  306.     if(isdigit(c))
  307.     {
  308.         while(isdigit(s[++i]=c=getch()))
  309.         {
  310.             ;
  311.         }
  312.     }
  313.     if(c=='.')
  314.     {
  315.         while(isdigit(s[++i]=c=getch()))
  316.         {
  317.             ;
  318.         }
  319.     }
  320.     s[i]='\0';
  321.     if(c!=EOF)
  322.     {
  323.         ungetch(c);
  324.     }
  325.     return NUMBER;
  326. }
  327. double pop(void)
  328. {
  329.     if(sp>0)
  330.     {
  331.         return val[--sp];
  332.     }
  333.     else
  334.     {
  335.         printf("Error: the stack is empty. pop() returns:\n");
  336.         return 0.;
  337.     }
  338. }
  339. void push(double f)
  340. {
  341.     if(sp<MAXVAL)
  342.     {
  343.         val[sp++]=f;
  344.     }
  345.     else
  346.     {
  347.         printf("Error: the stack is full, cannot push %g.\n",f);
  348.     }
  349. }
  350. void ungetch(int c)
  351. {
  352.     if(bufp>=BUFFSIZE)
  353.     {
  354.         printf("ungetch: Too many characters.\n");
  355.     }
  356.     else
  357.     {
  358.         buf[bufp++]=c;
  359.     }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement