-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv_top_2.c
100 lines (85 loc) · 2.13 KB
/
div_top_2.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
#include "monty.h"
/**
* div_2 - divides the second top element by the first
* element on the stack
* @stack: points to the top of the stack
* @line_number: the line number in bytecode file
*
* Return: NOthing.
*/
void div_2(stack_t **stack, unsigned int line_number)
{
stack_t *div_node;
size_t i = 0;
if ((*stack) == NULL || (*stack)->next == NULL)
{
dprintf(2, "L%u: can't div, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
dprintf(2, "L%u: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
div_node = malloc(sizeof(stack_t));
div_node->n = (*stack)->next->n / (*stack)->n;
for (i = 0; i < 2; i++)
pop(stack, line_number);
div_node->next = *stack;
div_node->prev = NULL;
*stack = div_node;
}
/**
* mul - multiplies the second top element by the first
* element on the stack
* @stack: points to the top of the stack
* @line_number: the line number in bytecode file
*
* Return: Nothing.
*/
void mul(stack_t **stack, unsigned int line_number)
{
stack_t *mul_node = malloc(sizeof(stack_t));
size_t i = 0;
if ((*stack) == NULL || (*stack)->next == NULL)
{
dprintf(2, "L%u: can't mul, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
mul_node->n = (*stack)->next->n * (*stack)->n;
for (i = 0; i < 2; i++)
pop(stack, line_number);
mul_node->next = *stack;
mul_node->prev = NULL;
*stack = mul_node;
}
/**
* mod - computes the rest of the division of the second top
* element by the first element on the stack
* @stack: points to the top of the stack
* @line_number: the line number in bytecode file
*
* Return: NOthing.
*/
void mod(stack_t **stack, unsigned int line_number)
{
stack_t *mod_node;
size_t i = 0;
if ((*stack) == NULL || (*stack)->next == NULL)
{
dprintf(2, "L%u: can't mod, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
dprintf(2, "L%u: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
mod_node = malloc(sizeof(stack_t));
mod_node->n = (*stack)->next->n % (*stack)->n;
for (i = 0; i < 2; i++)
pop(stack, line_number);
mod_node->next = *stack;
mod_node->prev = NULL;
*stack = mod_node;
}