aboutsummaryrefslogtreecommitdiff
path: root/4.8.c
blob: 1bb3d9d6b3d413c12c1485d7acb3879912d99daf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXOP 100
#define NUMBER_SIG '0'
#define SPECIAL_SIG '1'
#define VARIABLE_SIG '2'
#define ALPHABET_LEN  26
#define ON 1 
#define OFF 0

double pop(void);
void push(double f);
char getop(char str[]);
void ptop(void);
void duplitop(void);
void swaptoptwo(void);
void clearstack(void);
void do_special(char str[]);
char getch(void);
void ungetch(char input);
double last_print;

int 
main(void)
{
  char str[MAXOP];
  char vars[ALPHABET_LEN];
  char vars_state[ALPHABET_LEN];
  char type;
  double op2;

  for (int i = 0; i < ALPHABET_LEN; i++)
    vars_state[i] = OFF;
  
  vars_state[ALPHABET_LEN-1] = '\0';

  while ((type = getop(str)) != EOF) {
    switch (type) {
      case NUMBER_SIG :
        push(atof(str));
        break;
      case SPECIAL_SIG :
        do_special(str);
        break;
      case VARIABLE_SIG :
        /* Assuming variabke can only be set once */
        if (vars_state[*str-'a'] == 1)
          push(vars[*str-'a']);
        else {
          vars_state[*str-'a'] = ON;
          push(vars[*str - 'a'] = pop());
        }
        break;
      case '+' :
        push(pop() + pop());
        break;
      case '*' :
        push(pop() * pop());
        break;
      case '-' :
        op2 = pop();
        push(pop() - op2);
        break;
      case '/' :
        if ((op2 = pop()) == 0.0)
          printf("Err: deviser cant be zero\n");
        else 
          push(pop() / op2);
        break;
      case '%' :
        op2 = pop();
        if (op2 != 0)
          push(fmod(pop(), op2));
        else 
          printf("Err: deviser cant be zero\n");
        break;
      case '^' :
        ptop();
        break;
      case '>' :
        duplitop();
        break;
      case '#' :
        swaptoptwo();
        break;
      case '<' :
        clearstack();
        break;
      case '\n' :
        printf("%.8g\n", (last_print = pop()));
        break;
      default :
        printf("Err: unknown command\n");
    }
  }

  return 0;
}

#define MAXVAL 100
double val[MAXVAL];
int sp = 0;

void 
do_special(char str[])
{
  double tempop;

  if (!strcmp(str, "sin"))
    push(sin(pop()));
  else if (!strcmp(str, "exp"))
    push(exp(pop()));
  else if (!strcmp(str, "pow")) {
    tempop = pop();
    push(pow(pop(), tempop));
  }
}

void
ptop(void)
{
  if (sp > 0)
    printf("Top of the stack: %.8g\n", (last_print = val[sp-1]));
  else 
    printf("Err: stack is empty\n");
}

void 
duplitop(void)
{
  val[sp] = val[sp-1];
  ++sp;
}

void 
swaptoptwo(void)
{
  double temp1 = pop();
  double temp2 = pop();

  push(temp1);
  push(temp2);
}

void
clearstack(void)
{
  sp = 0;
}

double 
pop(void)
{
  if (sp > 0)
    return val[--sp];
  else {
    printf("Err: stack empty\n");
    return 0.0;
  }
}

void
push(double f)
{
  if (sp < MAXVAL)
    val[sp++] = f;
  else
    printf("Err: stack is full\n");
}

char 
getop(char str[])
{
  char input;
  int i;

  /* ignore blanks */
  while ((input = str[0] = getch()) == ' ' || input == '\t');

  if (isalpha(input)) {
    i = 1;
    while(isalpha(str[i++] = input = getch()));
    str[--i] = '\0';
    ungetch(input);

    /* all single chars are reserved for variables*/
    if (i == 1)
      return VARIABLE_SIG;

    return SPECIAL_SIG;
  }
  
  /* return operator */
  if (!isdigit(input) && input != '.')
    return input;

  /* collect digits*/
  for (i = 1; i < MAXOP && (isdigit(input=getch()) || input == '.'); i++)
    str[i] = input;

  str[i] = '\0';

  if (input != EOF)
    ungetch(input);

  return NUMBER_SIG;
}

double buff = 0;

char
getch(void)
{
  int temp;
  if (buff) {
    temp = buff;
    buff = 0;
    return temp;
  }
  else 
    return getchar();
}

void
ungetch(char input)
{
  if (buff)
    printf("Err: buffer full\n");
  else 
    buff = input;
}