|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> // for atof() |
| 4 | +#include <math.h> // for fmod() |
| 5 | +#include "calc.h" |
| 6 | + |
| 7 | +#define MAXOP 100 // max size of operand or operator |
| 8 | +#define MAX_VAR_LEN 26 // max size of array for variables |
| 9 | + |
| 10 | +void mathfunc(char[]); |
| 11 | +void setVar(double, double); |
| 12 | +void getVar(char[]); |
| 13 | + |
| 14 | +double variables[MAX_VAR_LEN]; |
| 15 | +int variableFlags[MAX_VAR_LEN]; // if [0] is 0, means 'a' not set, if [0] is 1, means 'a' set |
| 16 | + |
| 17 | +// compile with stack.c, getop.c, getch.c |
| 18 | +main() |
| 19 | +{ |
| 20 | + int type; |
| 21 | + double op2; |
| 22 | + char s[MAXOP]; |
| 23 | + |
| 24 | + int i; |
| 25 | + |
| 26 | + for (i = 0; i < MAX_VAR_LEN; i++) |
| 27 | + variableFlags[i] = 0; |
| 28 | + |
| 29 | + while ((type = getop(s)) != EOF) { |
| 30 | + switch(type) { |
| 31 | + case NUMBER: |
| 32 | + push(atof(s)); |
| 33 | + break; |
| 34 | + case VAR: |
| 35 | + getVar(s); |
| 36 | + break; |
| 37 | + case FUNC: |
| 38 | + mathfunc(s); |
| 39 | + break; |
| 40 | + case '+': |
| 41 | + push(pop() + pop()); |
| 42 | + break; |
| 43 | + case '*': |
| 44 | + push(pop() * pop()); |
| 45 | + break; |
| 46 | + case '-': |
| 47 | + op2 = pop(); |
| 48 | + push(pop() - op2); |
| 49 | + break; |
| 50 | + case '/': |
| 51 | + op2 = pop(); |
| 52 | + if (op2 != 0.0) |
| 53 | + push(pop() / op2); |
| 54 | + else |
| 55 | + printf("error: zero divisor\n"); |
| 56 | + break; |
| 57 | + case '%': |
| 58 | + op2 = pop(); |
| 59 | + if (op2 == 0.0) |
| 60 | + printf("error: can not mod 0\n"); |
| 61 | + else |
| 62 | + push(fmod(pop(), op2)); |
| 63 | + break; |
| 64 | + case '=': |
| 65 | + op2 = pop(); |
| 66 | + setVar(pop(), op2); |
| 67 | + break; |
| 68 | + case '\n': |
| 69 | + printf("\t%.8g\n", pop()); |
| 70 | + break; |
| 71 | + default: |
| 72 | + printf("error: unknown command %s\n", s); |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +void mathfunc(char s[]) |
| 80 | +{ |
| 81 | + int index; |
| 82 | + if (0 == strrindex(s, "sin")) { |
| 83 | + push(sin(pop())); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (0 == strrindex(s, "exp")) { |
| 88 | + push(exp(pop())); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (0 == strrindex(s, "pow")) { |
| 93 | + double ey = pop(); |
| 94 | + push(pow(pop(), ey)); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + printf("error: unknown func %s\n", s); |
| 99 | + return; |
| 100 | +} |
| 101 | + |
| 102 | +void setVar(double varIndex, double value) |
| 103 | +{ |
| 104 | + int index = (int)varIndex; |
| 105 | + if (index < 0 || index > MAX_VAR_LEN) { |
| 106 | + printf("error: cannot set variable, invalid variable: %c\n", index + 'a'); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + variables[index] = value; |
| 111 | + variableFlags[index] = 1; |
| 112 | + |
| 113 | + push(value); // to show result |
| 114 | + |
| 115 | + return; |
| 116 | +} |
| 117 | + |
| 118 | +void getVar(char s[]) |
| 119 | +{ |
| 120 | + if (strlen(s) != 1 && s[0] != 0) { |
| 121 | + // if variable is 'a', s[0] == 0, strlen(s) == 0 |
| 122 | + printf("error: cannot get variable, invalid variable: %s\n", s); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + double value = atof(s); |
| 127 | + int index = (int)value; |
| 128 | + if (variableFlags[index] == 0) |
| 129 | + push(value); |
| 130 | + else |
| 131 | + push(variables[index]); |
| 132 | + |
| 133 | + return; |
| 134 | +} |
0 commit comments