-
Notifications
You must be signed in to change notification settings - Fork 2
/
opcode_init3.c
149 lines (128 loc) · 2.59 KB
/
opcode_init3.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
#include "monty.h"
/**
* _div - divides the second element by the top element of the stack
*
* @doubly: head of the linked list
* @cline: line number;
* Return: no return
*/
void _div(stack_t **doubly, unsigned int cline)
{
int m = 0;
stack_t *aux = NULL;
aux = *doubly;
for (; aux != NULL; aux = aux->next, m++)
;
if (m < 2)
{
dprintf(2, "L%u: can't div, stack too short\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
if ((*doubly)->n == 0)
{
dprintf(2, "L%u: division by zero\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
aux = (*doubly)->next;
aux->n /= (*doubly)->n;
_pop(doubly, cline);
}
/**
* _mul - multiplies the top element to the second top element of the stack
*
* @doubly: head of the linked list
* @cline: line number;
* Return: no return
*/
void _mul(stack_t **doubly, unsigned int cline)
{
int m = 0;
stack_t *aux = NULL;
aux = *doubly;
for (; aux != NULL; aux = aux->next, m++)
;
if (m < 2)
{
dprintf(2, "L%u: can't mul, stack too short\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
aux = (*doubly)->next;
aux->n *= (*doubly)->n;
_pop(doubly, cline);
}
/**
* _mod - computes the rest of the division of the second element
* by the top element of the stack
*
* @doubly: head of the linked list
* @cline: line number;
* Return: no return
*/
void _mod(stack_t **doubly, unsigned int cline)
{
int m = 0;
stack_t *aux = NULL;
aux = *doubly;
for (; aux != NULL; aux = aux->next, m++)
;
if (m < 2)
{
dprintf(2, "L%u: can't mod, stack too short\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
if ((*doubly)->n == 0)
{
dprintf(2, "L%u: division by zero\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
aux = (*doubly)->next;
aux->n %= (*doubly)->n;
_pop(doubly, cline);
}
/**
* _pchar - print the char value of the first element
*
* @doubly: head of the linked list
* @cline: line number;
* Return: no return
*/
void _pchar(stack_t **doubly, unsigned int cline)
{
if (doubly == NULL || *doubly == NULL)
{
dprintf(2, "L%u: can't pchar, stack empty\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
if ((*doubly)->n < 0 || (*doubly)->n >= 128)
{
dprintf(2, "L%u: can't pchar, value out of range\n", cline);
free_vglo();
exit(EXIT_FAILURE);
}
printf("%c\n", (*doubly)->n);
}
/**
* _pstr - prints the string of the stack
*
* @doubly: head of the linked list
* @cline: line number;
* Return: no return
*/
void _pstr(stack_t **doubly, unsigned int cline)
{
stack_t *aux;
(void)cline;
aux = *doubly;
while (aux && aux->n > 0 && aux->n < 128)
{
printf("%c", aux->n);
aux = aux->next;
}
printf("\n");
}