-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_func2.c
114 lines (99 loc) · 2.47 KB
/
stack_func2.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "monty.h"
/**
* _swap - swap top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _swap(stack_t **stack, unsigned int line_number)
{
stack_t *runner;
int tmp;
runner = *stack;
if (runner == NULL || runner->next == NULL)
{
fprintf(stderr, "L%d: can't swap, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
tmp = runner->n;
runner->n = runner->next->n;
runner->next->n = tmp;
}
/**
* _add - add top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _add(stack_t **stack, unsigned int line_number)
{
stack_t *tmp = *stack;
int sum = 0, i = 0;
if (tmp == NULL)
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
while (tmp)
{
tmp = tmp->next;
i++;
}
if (stack == NULL || (*stack)->next == NULL || i <= 1)
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
sum = (*stack)->next->n + (*stack)->n;
_pop(stack, line_number);
(*stack)->n = sum;
}
/**
* _nop - nop top of stack y second top stack
* @stack: pointer to lists for monty stack
* @line_number: number of line opcode occurs on
*/
void _nop(__attribute__((unused)) stack_t **stack,
__attribute__((unused)) unsigned int line_number)
{
;
}
/**
* _pchar - prints the ASCII value of a number
* @stack: pointer to the top of the stack
* @line_number: the index of the current line
*
*/
void _pchar(stack_t **stack, unsigned int line_number)
{
int val;
if (stack == NULL || *stack == NULL)
{
fprintf(stderr, "L%d: can't pchar, stack empty\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
val = (*stack)->n;
if (val > 127 || val < 0)
{
fprintf(stderr, "L%d: can't pchar, value out of range\n", line_number);
free(var_global.buffer);
fclose(var_global.file);
free_dlistint(*stack);
exit(EXIT_FAILURE);
}
putchar(val);
putchar('\n');
}
/**
* _isalpha - checks if int is in alphabet
* @c: int
* Return: 1 if yes, 0 if no
*/
int _isalpha(int c)
{
if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90))
return (1);
else
return (0);
}