-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty_math.c
125 lines (114 loc) · 2.93 KB
/
monty_math.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
#include "monty.h"
/**
* _sub - sub top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _sub(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;
int sub = 0, i = 0;
if (tmp == NULL)
{
fprintf(stderr, "L%d: can't sub, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
while (tmp)
{
tmp = tmp->next;
i++;
}
if (stack == NULL || (*stack)->next == NULL || i <= 1)
{
fprintf(stderr, "L%d: can't sub, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
sub = (*stack)->next->n - (*stack)->n;
_pop(stack, line_number);
(*stack)->n = sub;
}
/**
* _mul - mul top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _mul(stack_t **stack, unsigned int line_number)
{
int aux;
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%d: can't mul, stack too short\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
else
{
aux = (*stack)->n;
_pop(stack, line_number);
(*stack)->n *= aux;
}
}
/**
* _div - div top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _div(stack_t **stack, unsigned int line_number)
{
int div = 0;
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't div, stack too short\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
else if ((*stack)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
else
{
div = (*stack)->n;
_pop(stack, line_number);
(*stack)->n /= div;
}
}
/**
* _mod - mod top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _mod(stack_t **stack, unsigned int line_number)
{
int mod = 0;
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't mod, stack too short\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
else if ((*stack)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
else
{
mod = (*stack)->n;
_pop(stack, line_number);
(*stack)->n %= mod;
}
}