-
Notifications
You must be signed in to change notification settings - Fork 1
/
opcodes2.c
157 lines (142 loc) · 2.85 KB
/
opcodes2.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
#include "monty.h"
/**
* _add - add top of stack and second
* element of stack
*
* @stack: head node
* @line_number: line number
*/
void _add(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int sum, *p_id;
(void)(line_number);
if (*stack == NULL || dlistint_len(*stack) < 2)
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
temp = *stack;
sum = temp->n + (temp->next)->n;
(temp->next)->n = sum;
_pop(stack, line_number);
}
/**
* _sub - sub top of stack from second
* element of stack
*
* @stack: head node
* @line_number: line number
*/
void _sub(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int sub, *p_id;
(void)(line_number);
if (*stack == NULL || dlistint_len(*stack) < 2)
{
fprintf(stderr, "L%d: can't sub, stack too short\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
temp = *stack;
sub = (temp->next)->n - temp->n;
(temp->next)->n = sub;
_pop(stack, line_number);
}
/**
* _div - div second element of stack
* by top of stack
*
* @stack: head node
* @line_number: line number
*/
void _div(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int div, *p_id;
(void)(line_number);
if (*stack == NULL || dlistint_len(*stack) < 2)
{
fprintf(stderr, "L%d: can't div, stack too short\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
temp = *stack;
if (temp->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
div = (temp->next)->n / temp->n;
(temp->next)->n = div;
_pop(stack, line_number);
}
/**
* _mul - mul top of stack and second
* element of stack
*
* @stack: head node
* @line_number: line number
*/
void _mul(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int mul, *p_id;
(void)(line_number);
if (*stack == NULL || dlistint_len(*stack) < 2)
{
fprintf(stderr, "L%d: can't mul, stack too short\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
temp = *stack;
mul = (temp->next)->n * temp->n;
(temp->next)->n = mul;
_pop(stack, line_number);
}
/**
* _mod - mod second element of stack
* by top of stack
*
* @stack: head node
* @line_number: line number
*/
void _mod(stack_t **stack, unsigned int line_number)
{
stack_t *temp;
int mod, *p_id;
(void)(line_number);
if (*stack == NULL || dlistint_len(*stack) < 2)
{
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
temp = *stack;
if (temp->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
free_dlistint(stack);
p_id = &id;
*p_id = -1;
return;
}
mod = (temp->next)->n % temp->n;
(temp->next)->n = mod;
_pop(stack, line_number);
}