-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_div_mul_mod.c
More file actions
94 lines (90 loc) · 2.29 KB
/
Copy pathsub_div_mul_mod.c
File metadata and controls
94 lines (90 loc) · 2.29 KB
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
#include "monty.h"
/**
* _sub - subtracts the top element of the stack
* @stack: pointer to a stack_t list
* @line_number: line count
*
* Return: void has no return
*/
void _sub(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || (*stack == NULL) || ((*stack)->next == NULL))
{
fprintf(stderr, "L%u: can't sub, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
(*stack)->next->n = ((*stack)->next->n) - ((*stack)->n);
_pop(stack, line_number);
}
/**
* _div - divides the second top element of the stack
* @stack: pointer to a stack_t
* @line_number: line count
*
* Return: void has no return
*/
void _div(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || (*stack == NULL) || ((*stack)->next == NULL))
{
fprintf(stderr, "L%u: can't div, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
fprintf(stderr, "L%u: division by zero\n", line_number);
free_stack(stack);
exit(EXIT_FAILURE);
}
(*stack)->next->n = ((*stack)->next->n) / ((*stack)->n);
_pop(stack, line_number);
}
/**
* _mul - multiplies the second top element of the stack
* @stack: pointer to stack_t
* @line_number: line count
*
* Return: void has no return
*/
void _mul(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || (*stack == NULL) || ((*stack)->next == NULL))
{
fprintf(stderr, "L%u: can't mul, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
(*stack)->next->n = ((*stack)->next->n) * ((*stack)->n);
_pop(stack, line_number);
}
/**
* _mod - computes the rest of the division of the second top element
* of the stack by the top element of the stack
* @stack: pointer to a stack_t
* @line_number: line count
*
* Return: void has no return
*/
void _mod(stack_t **stack, unsigned int line_number)
{
if (stack == NULL || (*stack == NULL) || ((*stack)->next == NULL))
{
fprintf(stderr, "L%u: can't mod, stack too short\n", line_number);
if (*stack)
free_stack(stack);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
fprintf(stderr, "L%u: division by zero\n", line_number);
free_stack(stack);
exit(EXIT_FAILURE);
}
(*stack)->next->n = ((*stack)->next->n) % ((*stack)->n);
_pop(stack, line_number);
}