-
Notifications
You must be signed in to change notification settings - Fork 3
/
4-4.c
182 lines (158 loc) · 4.54 KB
/
4-4.c
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Command to print the top element of the stack
#define TOP_STACK_ELEMENT 'P'
// Comand to swap the top two elements
#define SWAP_ELEMENTS 'S'
// Command to duplicate the top element of the stack
#define DUPLICATE 'D'
// Command to clear the stack
#define CLEAR_STACK 'C'
#define MAX_OPERATORS 100
#define NUMBER '0'
#define MAX_STACK_SIZE 100
#define BUFFER_SIZE 100
int stack_index = 0;
double stack[MAX_STACK_SIZE];
char buffer[BUFFER_SIZE];
int buffer_index = 0;
// Function for pushing into stack
void push(double new_stack_number) {
if (stack_index < MAX_STACK_SIZE)
stack[stack_index++] = new_stack_number;
else
printf("Error : Stack full\n");
}
// Function for poping from stack
double pop() {
if (stack_index > 0)
return stack[--stack_index];
else {
printf("Error : Stack empty\n");
return 0;
}
}
// Function for getting input characters
int get_character() {
if (buffer_index > 0)
return buffer[--buffer_index];
else
return getchar();
}
// Function to "undo" get_character()
void unget_character(int character) {
if (buffer_index >= BUFFER_SIZE)
printf("Ungetch : Too many characters\n");
else
buffer[buffer_index++] = character;
}
// Function to get the digits of a number
void get_digits(char string[]) {
int string_index = 0;
do {
string[++string_index] = get_character();
} while (isdigit(string[string_index]) || string[string_index] == '.');
if (string[string_index] != EOF)
unget_character(string[string_index]);
string[string_index] = '\0';
}
// Function to get the type of input : Number / Operators
int get_operators(char input_string[]) {
int character = get_character();
// Ignore the spaces beetween characters
while (isspace(character) && character != '\n')
character = get_character();
// Return the operator, except in case of '-'
if (!isdigit(character) && character != '.' && character != '-')
return character;
if (character != '-') {
input_string[0] = character;
get_digits(input_string);
return NUMBER;
} else if (stack_index < 2) {
input_string[0] = '-';
get_digits(input_string);
return NUMBER;
} else {
return '-';
}
}
double top() {
if (stack_index > 0) {
return stack[stack_index - 1];
}
else {
printf("Error : Stack empty\n");
return 0;
}
}
void clear_stack() {
stack_index = 0;
}
int main() {
int type;
do {
double first_operand;
double second_operand;
char input_string[MAX_OPERATORS];
type = get_operators(input_string);
switch (type) {
// Push the number into the stack
case NUMBER:
push(atof(input_string));
break;
// Do the requested operations
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
second_operand = pop();
first_operand = pop();
push(first_operand - second_operand);
break;
case '/':
second_operand = pop();
first_operand = pop();
if (second_operand != 0)
push(first_operand / second_operand);
else
printf("Error : 0 divisor\n");
break;
case '%':
second_operand = pop();
first_operand = pop();
if (second_operand != 0)
push((int)first_operand % (int)second_operand);
else
printf("Error : 0 divisor\n");
break;
case 'P':
printf("\t%g\n", top());
break;
case 'D':
push(top());
break;
case 'S':
first_operand = pop();
second_operand = pop();
push(first_operand);
push(second_operand);
break;
case 'C' :
clear_stack();
break;
// Print output
case '\n':
printf("\t%g\n", pop());
break;
default:
printf("Error : Unknown command %s\n", input_string);
break;
}
} while (type != EOF);
return 0;
}