-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops3.c
164 lines (124 loc) · 2.55 KB
/
ops3.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "monty.h"
#include <stdio.h>
#include <stdlib.h>
/**
* mul - multiply the first node value with the second node value of the
* stack and delete the top node
* @top: top of the stack
* @line_number: current line of execution
* Return: Nothing
*/
void mul(stack_t **top, unsigned int line_number)
{
stack_t *next;
if (!top || !(*top) || !((*top)->next))
{
fprintf(stderr, "L%d: can't mul, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
next = (*top)->next;
next->n = next->n * (*top)->n;
free(*top);
*top = next;
}
/**
* mod - find the mod of the first node value from the second node value of the
* stack and delete the top node
* @top: top of the stack
* @line_number: current line of execution
* Return: Nothing
*/
void mod(stack_t **top, unsigned int line_number)
{
stack_t *next;
if (!top || !(*top) || !((*top)->next))
{
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*top)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
next = (*top)->next;
next->n = next->n % (*top)->n;
free(*top);
*top = next;
}
/**
* pchar - prints the value at the top of stack as char
* @top: top of the stack
* @line_number: current line of execution
* Return: Nothing
*/
void pchar(stack_t **top, unsigned int line_number)
{
int val;
if (!top || !(*top))
{
fprintf(stderr, "L%d: can't pchar, stack empty\n", line_number);
exit(EXIT_FAILURE);
}
val = (*top)->n;
if (val >= 0 && val <= 127)
{
putchar(val);
putchar('\n');
}
else
{
fprintf(stderr, "L%d: can't pchar, value out of range\n", line_number);
exit(EXIT_FAILURE);
}
}
/**
* pstr - prints the ascii value of all nodes in the stack
* @top: top of the stack
* @line_number: current line of execution
* Return: Nothing
*/
void pstr(stack_t **top, unsigned int line_number)
{
int val;
stack_t *curr;
(void)line_number;
if (!top || !(*top))
{
putchar('\n');
return;
}
curr = *top;
while (curr && curr->n)
{
val = curr->n;
if (val >= 0 && val <= 127)
putchar(val);
else
break;
curr = curr->next;
}
putchar('\n');
}
/**
* rotl - make the first node the last
* @top: top of the stack
* @line_number: current line of execution
* Return: Nothing
*/
void rotl(stack_t **top, unsigned int line_number)
{
stack_t *curr;
int val;
(void)line_number;
if (!top || !(*top) || !((*top)->next))
return;
curr = *top;
while (curr->next)
{
val = curr->n;
curr->n = curr->next->n;
curr->next->n = val;
curr = curr->next;
}
}