-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_op.c
executable file
·44 lines (35 loc) · 1.11 KB
/
stack_op.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
#include "monty.h"
/**
* mul_nodes - Adds 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 mul_nodes(stack_t **stack, unsigned int line_number)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "mul");
(*stack) = (*stack)->next;
sum = (*stack)->n * (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}
/**
* mod_nodes - Adds 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 mod_nodes(stack_t **stack, unsigned int line_number)
{
int sum;
if (stack == NULL || *stack == NULL || (*stack)->next == NULL)
more_err(8, line_number, "mod");
if ((*stack)->n == 0)
more_err(9, line_number);
(*stack) = (*stack)->next;
sum = (*stack)->n % (*stack)->prev->n;
(*stack)->n = sum;
free((*stack)->prev);
(*stack)->prev = NULL;
}