Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define STACK_DEPTH 100
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<ctype.h>
- #include<math.h>
- double stack[STACK_DEPTH];
- int sp=STACK_DEPTH;
- double pop(void)
- {
- if(sp<STACK_DEPTH){
- return stack[sp++];
- }else{
- return 0.0;
- }
- }
- void push(double f)
- {
- if(sp>0)
- stack[--sp]=f;
- }
- int getword(char* dst, const char* str, int limit)
- {
- int i, j, len = strlen(str);
- for(i=0; i<len && isspace(str[i]); i++);
- for(j=0; j<limit && (j+i)<len && !isspace(str[i+j]); j++)
- dst[j]=str[i+j];
- dst[j]='\0';
- return i+j;
- }
- int main(void)
- {
- printf("Podaj wyrażenie w rpn:\n");
- double op2;
- char line[100], tmp[100];
- int i, c;
- while(1){
- i = 0;
- fgets(line, 100, stdin);
- while((c=getword(tmp, &line[i], 100)) && strlen(tmp)){
- if(strcmp(tmp, "sin")==0)
- push(sin(pop()));
- else if(strcmp(tmp, "cos")==0)
- push(cos(pop()));
- else if(strcmp(tmp, "pow")==0){
- op2 = pop();
- push(pow(pop(),op2));
- }
- else if(strcmp(tmp, "+")==0)
- push(pop()+pop());
- else if(strcmp(tmp, "-")==0){
- op2 = pop();
- push(pop()-op2);}
- else if(strcmp(tmp, "*")==0)
- push(pop()*pop());
- else if(strcmp(tmp, "/")==0){
- op2 = pop();
- push(pop()/op2);}
- else if(strcmp(tmp, "=")==0)
- printf("%f\n", pop());
- else
- push(atof(tmp));
- i+=c;
- }
- if (line[0] == '\n') break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement