/* Exercise 4.5 - other mathematical functions * I could add more functions, but you get the idea */ void parseCommand(char s[]) { double t; /* Commands for ex 4.4 - stack commands */ if (0 == strcmp(s,"view")) printtop(); /* view top element on stack */ else if (0 == strcmp(s, "duplicate")) duplicate(); /* duplicate top element on stack */ else if (0 == strcmp(s, "swap")) swap(); /* swap top two elements on stack */ else if (0 == strcmp(s, "clear")) clear(); /* clear stack */ /* Commands for 4.5 - maths functions */ else if (0 == strcmp(s,"sin")) push(sin(pop())); else if (0 == strcmp(s,"exp")) push(exp(pop())); else if (0 == strcmp(s,"pow")) { t = pop(); push(pow(pop(),t)); } /* misc functions */ else if (0 == strcmp(s, "stack")) viewstack(); else if (0 == strcmp(s, "vars")) viewvars(); else printf("error: not a valid command\n"); }
/* reverse Polish calculator */ main() { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '"': duptop(); break; case '\'': printtop(); break; case '!': swaptop2(); 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 '%': op2 = pop(); if (op2 != 0.0) { push(((int) pop()) % (int) op2); } else { printf("error: zero divisor\n"); } break; case '^': op2 = pop(); push(pow(pop(), op2)); break; case '#': push(exp(pop())); break; case '$': push(sin(pop())); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
/*perform a process switch*/ void handletimerinterrupt(short segment, short sp) { int i,cntr=79; /*save current process*/ process_table[current_process].segment=segment; process_table[current_process].sp=sp; /*print out the active processes at the top of the screen*/ for (i=MAXPROCESSES-1; i>=0; i--) { if (process_table[i].active>0) { printtop('P',cntr-5); printtop((char)(i+0x30),cntr-4); printtop(' ',cntr-3); if (process_table[i].active==1) printtop('A',cntr-2); else if (process_table[i].active==2) printtop('W',cntr-2); else if (process_table[i].active==3) printtop('K',cntr-2); printtop(' ',cntr-1); printtop(' ',cntr); cntr=cntr-6; } } printtop(' ',cntr); printtop(' ',cntr-1); printtop(':',cntr-2); printtop('s',cntr-3); printtop('e',cntr-4); printtop('s',cntr-5); printtop('s',cntr-6); printtop('e',cntr-7); printtop('c',cntr-8); printtop('o',cntr-9); printtop('r',cntr-10); printtop('P',cntr-11); /*find an active process round robin style*/ i=current_process; do { i++; if (i==MAXPROCESSES) i=0; } while(process_table[i].active!=1); current_process=i; /*restore that process*/ timer_restore(process_table[current_process].segment,process_table[current_process].sp); }
/* reverse Polish calculator */ int main() { int type; double op2; char s[MAXOP]; char varDetected = FALSE; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: if(varDetected == TRUE) { pushVarValue(atof(s)); varDetected = FALSE; printf("%c -> %g", varsNames[varPos-1], varsValues[varPos-1]); } else push(atof(s)); 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 '%': op2 = pop(); if (op2 != 0.0) push((int) pop() % (int) op2); else printf("error: zero divisor (modulus)\n"); break; case '=': // assign variable varDetected = TRUE; break; case 'S': // Compute sine push(sin(pop())); break; case 'E': // Compute exponential function push(exp(pop())); break; case '^': // Raise to power op2 = pop(); push(pow(pop(), op2)); break; case 'P': // print top element from the stack printf("Top element is: %g\n", printtop()); break; case 'D': // duplicate top element from the stack duplicate(); break; case '>': // swap top elements from the stack swap(); break; case 'C': //clear entire stack clear(); break; case '\n': //printf("\t%.8g\n", pop()); break; default: if(type >= 'a' && type <= 'z') pushVarName(type); else printf("error: unknown command %s\n", s); break; } } return 0; }
/* reverse Polish calculator */ main() { int type; double op2, last, vars[26]; char s[MAXOP], varname; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case '"': duptop(); break; case '\'': printtop(); break; case '!': swaptop2(); 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 '%': op2 = pop(); if (op2 != 0.0) { push(((int) pop()) % (int) op2); } else { printf("error: zero divisor\n"); } break; case '^': op2 = pop(); push(pow(pop(), op2)); break; case '#': push(exp(pop())); break; case '$': push(sin(pop())); break; case ASSIGNVAR: varname = s[0]; duptop(); vars[varname - 'a'] = pop(); break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': push(vars[type - 'a']); break; case '?': push(last); break; case '\n': last = pop(); printf("\t%.8g\n", last); break; default: printf("error: unknown command %s\n", s); break; } } return 0; }
int main(void) { /* reverse Polish calculator */ int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s) * sign); sign = 1; /* reset sign */ break; case SIN: push(sin(pop())); break; case COS: push(cos(pop())); break; case EXP: push(exp(pop())); break; case POW: op2 = pop(); push(pow(pop(), op2)); break; case SQRT: push(sqrt(pop())); 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 devision\n"); break; case '%': op2 = pop(); if (op2 != 0) push(fmod(pop(), op2)); else printf("error: zero devision\n"); break; case '\n': if (popallowed) printf("\t%.8g\n", pop()); else popallowed = 1; break; case '?': printtop(); break; case '&': dupetop(); break; case '~': swaptop(); break; case '!': clrstack(); break; default: printf("error: unknown command\n"); break; } } return 0; }