-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction_math.c
80 lines (70 loc) · 2.05 KB
/
instruction_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
#include "monty.h"
/**
* add_op - Adds the top two elements
* and stored the result in the second top element of the stack.
* @list: Head of the list
* @line_num: Line number
*/
void add_op(stack_t **list, unsigned int line_num)
{
if ((*list) == NULL || (*list)->next == NULL)
syntax_error(6, list, line_num);
(*list)->next->n = ((*list)->n) + ((*list)->next->n);
pop_op(list, line_num);
}
/**
* sub_op - Subtracts the top element of the stack from the second top element
* result is stored in the second top element of the stack.
* @list: Head of the list
* @line_num: Line number
*/
void sub_op(stack_t **list, unsigned int line_num)
{
if ((*list) == NULL || (*list)->next == NULL)
syntax_error(7, list, line_num);
(*list)->next->n = ((*list)->next->n) - ((*list)->n);
pop_op(list, line_num);
}
/**
* mul_op - Multiplies the second top element of
* the stack with the top element of the stack.
* @list: Head of the list
* @line_num: Line number
*/
void mul_op(stack_t **list, unsigned int line_num)
{
if ((*list) == NULL || (*list)->next == NULL)
syntax_error(9, list, line_num);
(*list)->next->n = ((*list)->next->n) * ((*list)->n);
pop_op(list, line_num);
}
/**
* div_op - Divides the second top element of the stack
* by the top element of the stack.
* @list: Head of the list
* @line_num: Line number
*/
void div_op(stack_t **list, unsigned int line_num)
{
if ((*list) == NULL || (*list)->next == NULL)
syntax_error(8, list, line_num);
else if ((*list)->n == 0)
syntax_error(2, list, line_num);
(*list)->next->n = ((*list)->next->n) / ((*list)->n);
pop_op(list, line_num);
}
/**
* mod_op - Computes the rest of the division of the second
* top element of the stack by the top element of the stack.
* @list: Head of the list
* @line_num: Line number
*/
void mod_op(stack_t **list, unsigned int line_num)
{
if ((*list) == NULL || (*list)->next == NULL)
syntax_error(10, list, line_num);
else if ((*list)->n == 0)
syntax_error(2, list, line_num);
(*list)->next->n = ((*list)->next->n) % ((*list)->n);
pop_op(list, line_num);
}