-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_functions.c
94 lines (83 loc) · 1.79 KB
/
print_functions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "monty.h"
/**
* pall - prints the value of all items on the stack
* @stack: pointer to stack
* @line_number: line number of instruction
* Return: void
*/
void pall(stack_t **stack, unsigned int __attribute__((unused))line_number)
{
stack_t *current = *stack;
if (stack == NULL || *stack == NULL)
return;
while (current != NULL)
{
printf("%d\n", current->n);
current = current->next;
}
}
/**
* pint - prints value at the top of the stack, plus new line
* @stack: pointer to stack
* @line_number: line number of instruction
* Return: void
*/
void pint(stack_t **stack, unsigned int line_number)
{
int value;
if (stack == NULL || *stack == NULL)
{
printf("L%d: can't pint, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
value = (*stack)->n; /* head node's data */
printf("%d\n", value);
}
/**
* pchar - prints the char at the top of the stack
* @stack: pointer to stack
* @line_number: line number of instruction
* Return: void
*/
void pchar(stack_t **stack, unsigned int line_number)
{
int c;
if (stack == NULL || *stack == NULL)
{
printf("L%d: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
c = (*stack)->n; /* head node's data */
if (c < 0 || c > 127)
{
printf("L%d: can't pchar, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
putchar(c);
putchar('\n');
}
/**
* pstr - prints the string starting from the top of the stack
* @stack: pointer to stack
* @line_number: line number of instruction
* Return: void
*/
void pstr(stack_t **stack, unsigned int line_number)
{
int c;
stack_t *current;
(void)line_number;
current = *stack;
while (current != NULL)
{
c = current->n; /* current node's data */
if (c > 0 && c <= 127)
{
current = current->next;
putchar(c);
}
else
break;
}
putchar('\n');
}