Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BUFFSIZE 1
- #define MAXOP 1
- #define MAXVAL 2
- #define NUMBER 0
- #define FUNCTION 255
- void isfunction(char s[]);
- void print(void);
- void dupe(void);
- void swap(void);
- void clear(void);
- int getch(void);
- int getop(char s[]);
- double pop(void);
- void push(double);
- void ungetch(int c);
- char buf[BUFFSIZE];
- int bufp=0;
- int sp=0;
- double val[MAXVAL];
- double sto[26];
- int last=0;
- int main(void)
- {
- int type;
- double op2;
- char s[MAXOP];
- while((type=getop(s))!=EOF)
- {
- last=type;
- switch(type)
- {
- case FUNCTION:
- {
- isfunction(s);
- break;
- }
- case NUMBER:
- {
- push(atof(s));
- break;
- }
- case'$':
- {
- print();
- break;
- }
- case'%':
- {
- op2=pop();
- push((int)pop()%(int)op2);
- break;
- }
- case'+':
- {
- push(pop()+pop());
- break;
- }
- case'*':
- {
- push(pop()*pop());
- break;
- }
- case'-':
- {
- op2=pop();
- push(pop()-op2);
- break;
- }
- case'/':
- {
- op2=pop();
- if(op2!=0.0)
- {
- push(pop()/op2);
- }
- else
- {
- printf("Error: zero divisor.\n");
- }
- break;
- }
- case'\n':
- {
- printf("\t%.8g\n",pop());
- break;
- }
- default:
- {
- if(type>64&&type<91||type>96&&type<123)
- {
- if(type>64&&type<91)
- {
- push(sto[(type-65)]);
- }
- else if(type>96&&type<123)
- {
- push(sto[(type-97)]);
- }
- }
- else
- {
- printf("Error: unknown command %s.\n",s);
- }
- break;
- }
- }
- }
- return 0;
- }
- void isfunction(char s[])
- {
- double temp = 0;
- if(strcmp(s,"print")==0)
- {
- print();
- }
- else if(strcmp(s,"dupe")==0)
- {
- dupe();
- }
- else if(strcmp(s,"swap")==0)
- {
- swap();
- }
- else if(strcmp(s,"clear")==0)
- {
- clear();
- }
- else if(strcmp(s,"sin")==0)
- {
- push(sin(pop()));
- }
- else if(strcmp(s,"cos")==0)
- {
- push(cos(pop()));
- }
- else if(strcmp(s,"tan")==0)
- {
- push(tan(pop()));
- }
- else if(strcmp(s,"asin")==0)
- {
- push(asin(pop()));
- }
- else if(strcmp(s,"acos")==0)
- {
- push(acos(pop()));
- }
- else if(strcmp(s,"atan")==0)
- {
- push(atan(pop()));
- }
- else if(strcmp(s,"sinh")==0)
- {
- push(sinh(pop()));
- }
- else if(strcmp(s,"cosh")==0)
- {
- push(cosh(pop()));
- }
- else if(strcmp(s,"tanh")==0)
- {
- push(tanh(pop()));
- }
- else if(strcmp(s,"exp")==0)
- {
- push(exp(pop()));
- }
- else if(strcmp(s,"ln")==0)
- {
- push(log(pop()));
- }
- else if(strcmp(s,"log")==0)
- {
- push(log10(pop()));
- }
- else if(strcmp(s,"pow")==0)
- {
- temp=pop();
- push(pow(pop(),temp));
- }
- else if(strcmp(s,"sqrt")==0)
- {
- push(sqrt(pop()));
- }
- else if(strcmp(s,"ceil")==0)
- {
- push(ceil(pop()));
- }
- else if(strcmp(s,"floor")==0)
- {
- push(floor(pop()));
- }
- else if(strcmp(s,"fabs")==0)
- {
- push(fabs(pop()));
- }
- else if(strcmp(s,"ldexp")==0)
- {
- temp=pop();
- push(ldexp(pop(),temp));
- }
- else if(strcmp(s,"fmod")==0)
- {
- temp=pop();
- push(fmod(pop(),temp));
- }
- else
- {
- printf("isfunction: Error: unknown function.");
- }
- }
- void print(void)
- {
- double retval;
- if(sp>0)
- {
- retval= val[sp];
- printf("%lf\n",retval);
- }
- else
- {
- printf("Error: the stack is empty\n");
- }
- }
- void dupe(void)
- {
- double c;
- if(sp>0&&sp<MAXVAL)
- {
- c=val[(sp-1)];
- val[sp]=c;
- val[++sp]='\0';
- }
- else if(sp==0)
- {
- printf("Error: the is stack empty.\n");
- }
- else if(sp==MAXVAL)
- {
- printf("Error: the stack is full, cannot duplicate.\n");
- }
- }
- void swap(void)
- {
- double c;
- if(sp>1)
- {
- c=val[(sp-2)];
- val[(sp-2)]=val[(sp-1)];
- val[(sp-1)]=c;
- }
- else
- {
- printf("Error: the stack is empty\n");
- }
- }
- void clear(void)
- {
- sp=0;
- }
- int getch(void)
- {
- return (bufp>0)?buf[--bufp]:getchar();
- }
- int getop(char s[])
- {
- int i,c;
- while((s[0]=c=getch())==' '||c=='\t')
- {
- ;
- }
- s[1]='\0';
- i=0;
- if(isalpha(c))
- {
- while(isalpha(s[++i]=c=getch()))
- {
- ;
- }
- s[i]='\0';
- if(c!=EOF)
- {
- ungetch(c);
- }
- if(strlen(s)>1)
- {
- return FUNCTION;
- }
- else
- {
- return c;
- }
- }
- if(!isdigit(c)&&c!='.')
- {
- return c;
- }
- if(isdigit(c))
- {
- while(isdigit(s[++i]=c=getch()))
- {
- ;
- }
- }
- if(c=='.')
- {
- while(isdigit(s[++i]=c=getch()))
- {
- ;
- }
- }
- s[i]='\0';
- if(c!=EOF)
- {
- ungetch(c);
- }
- return NUMBER;
- }
- double pop(void)
- {
- if(sp>0)
- {
- return val[--sp];
- }
- else
- {
- printf("Error: the stack is empty. pop() returns:\n");
- return 0.;
- }
- }
- void push(double f)
- {
- if(sp<MAXVAL)
- {
- val[sp++]=f;
- }
- else
- {
- printf("Error: the stack is full, cannot push %g.\n",f);
- }
- }
- void ungetch(int c)
- {
- if(bufp>=BUFFSIZE)
- {
- printf("ungetch: Too many characters.\n");
- }
- else
- {
- buf[bufp++]=c;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement