-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.c
More file actions
76 lines (74 loc) · 1.69 KB
/
operations.c
File metadata and controls
76 lines (74 loc) · 1.69 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
#include "monty.h"
/**
* pchar - prints the top element of the stack in char
* @stack: the pointer to the top of stack
* @line_number: the line number in the file
* Return: void
*/
void pchar(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL)
{
dprintf(2, "L%d: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n >= 0 && (*stack)->n <= 127)
{
printf("%c\n", (*stack)->n);
}
else
{
dprintf(2, "L%d: can't pchar, value out of range\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
}
/**
* mul - multiplies the first 2 top elements
* @stack: the pointer to the stack
* @line_number: the line number in monty file
* Return: void
*/
void mul(stack_t **stack, unsigned int line_number)
{
stack_t *temp = NULL;
int result = 0;
if (*stack == NULL || (*stack)->next == NULL)
{
dprintf(2, "L%d: can't mul, stack too short\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
temp = (*stack)->next;
result = temp->n * (*stack)->n;
pop(stack, line_number);
temp->n = result;
}
/**
* mod - calculates modulus of the top 2 elements
* @stack: pointer to the top of stack
* @line_number: the line number in a file
* Return: void
*/
void mod(stack_t **stack, unsigned int line_number)
{
stack_t *temp = NULL;
int result = 0;
if (*stack == NULL || (*stack)->next == NULL)
{
dprintf(2, "L%d: can't mod, stack too short\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
dprintf(2, "L%d: division by zero\n", line_number);
free_stack(*stack);
exit(EXIT_FAILURE);
}
temp = (*stack)->next;
result = temp->n;
result %= (*stack)->n;
pop(stack, line_number);
temp->n = result;
}