-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_3.c
74 lines (71 loc) · 1.76 KB
/
stack_3.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
#include "monty.h"
/**
* _add - Function that adds the top two elements of the stack
* @top: element at the top of the stack (head)
* @line_number: constant int value in the structure
* Return: void
*/
void _add(stack_t **top, unsigned int line_number)
{
stack_t *tmp;
if (*top == NULL || (*top)->next == NULL)
{
fprintf(stderr, "L%u: can't add, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
tmp = (*top)->next;
tmp->n += (*top)->n;
pop_stack(top, line_number);
}
/**
* _nop - Function that doesnt do anything
* @top: element at the top of the stack (head)
* @line_number: constant int value in the structure
* Return: void
**/
void _nop(stack_t **top, unsigned int line_number)
{
(void) top;
(void) line_number;
}
/**
* _sub - Function that adds the top two elements of the stack
* @top: element at the top of the stack (head)
* @line_number: constant int value in the structure
* Return: void
*/
void _sub(stack_t **top, unsigned int line_number)
{
stack_t *tmp;
if (*top == NULL || (*top)->next == NULL)
{
fprintf(stderr, "L%u: can't sub, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*top)->n == 0)
{
fprintf(stderr, "L%u: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
tmp = (*top)->next;
tmp->n -= (*top)->n;
pop_stack(top, line_number);
}
/**
* _mul - Function that multiplies the top two elements of the stack
* @top: element at the top of the stack (head)
* @line_number: constant int value in the structure
* Return: void
*/
void _mul(stack_t **top, unsigned int line_number)
{
stack_t *tmp;
if (*top == NULL || (*top)->next == NULL)
{
fprintf(stderr, "L%u: can't mul, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
tmp = (*top)->next;
tmp->n *= (*top)->n;
pop_stack(top, line_number);
}