-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path4-3.c
141 lines (123 loc) · 3.66 KB
/
4-3.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#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 '-';
}
}
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;
// 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;
}