-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackOp.c
51 lines (39 loc) · 1.09 KB
/
stackOp.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
#include "monty.h"
/**
* multiply_nodes - multiply the top two elements of the stack.
*
* @stack: Pointer to a pointer pointing to top node of the stack.
*
* @line_number: Interger representing the line number of of the opcode.
*/
void multiply_nodes(stack_t **stack, unsigned int line_number)
{
int m;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
AdvErrors(8, line_number, "mul");
(*stack) = (*stack)->next;
m = (*stack)->n * (*stack)->prev->n;
(*stack)->n = m;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* rem_nodes - Divide the top two elements of the stack.
*
* @stack: Pointer to a pointer pointing to top node of the stack.
*
* @line_number: Interger representing the line number of of the opcode.
*/
void rem_nodes(stack_t **stack, unsigned int line_number)
{
int d;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
AdvErrors(8, line_number, "mod");
if ((*stack)->n == 0)
AdvErrors(9, line_number);
(*stack) = (*stack)->next;
d = (*stack)->n % (*stack)->prev->n;
(*stack)->n = d;
free((*stack)->prev);
(*stack)->prev = NULL;
}