-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith_funcs.c
166 lines (151 loc) · 3.08 KB
/
arith_funcs.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
165
166
#include "monty.h"
/**
* add_func - function to add topmost two values of the stack
* Afterwhich it removes the top node of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int add_func(stack_t **top, unsigned int line_number)
{
int num_nodes;
stack_t *ptr;
int a, b;
num_nodes = get_num_nodes(top);
if (num_nodes >= 2)
{
ptr = *top;
a = ptr->n;
b = ptr->next->n;
ptr->next->n = a + b;
pop_func(top, line_number);
}
else
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_number);
return (-1);
}
return (0);
}
/**
* sub_func - function to substract topmost two values of the stack
* Afterwhich it removes the top node of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int sub_func(stack_t **top, unsigned int line_number)
{
int num_nodes;
stack_t *ptr;
int a, b;
num_nodes = get_num_nodes(top);
if (num_nodes >= 2)
{
ptr = *top;
a = ptr->n;
b = ptr->next->n;
ptr->next->n = b - a;
pop_func(top, line_number);
}
else
{
fprintf(stderr, "L%d: can't sub, stack too short\n", line_number);
return (-1);
}
return (0);
}
/**
* mul_func - function to multiply topmost two values of the stack
* Afterwhich it removes the top node of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int mul_func(stack_t **top, unsigned int line_number)
{
int num_nodes;
stack_t *ptr;
int a, b;
num_nodes = get_num_nodes(top);
if (num_nodes >= 2)
{
ptr = *top;
a = ptr->n;
b = ptr->next->n;
ptr->next->n = b * a;
pop_func(top, line_number);
}
else
{
fprintf(stderr, "L%d: can't mul, stack too short\n", line_number);
return (-1);
}
return (0);
}
/**
* div_func - function to divide topmost two values of the stack
* Afterwhich it removes the top node of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int div_func(stack_t **top, unsigned int line_number)
{
int num_nodes;
stack_t *ptr;
int a, b;
num_nodes = get_num_nodes(top);
if (num_nodes >= 2)
{
ptr = *top;
a = ptr->n;
b = ptr->next->n;
if (a == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
return (-1);
}
ptr->next->n = b / a;
pop_func(top, line_number);
}
else
{
fprintf(stderr, "L%d: can't div, stack too short\n", line_number);
return (-1);
}
return (0);
}
/**
* mod_func - function to get the mod of topmost two values of the stack
* Afterwhich it removes the top node of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int mod_func(stack_t **top, unsigned int line_number)
{
int num_nodes;
stack_t *ptr;
int a, b;
num_nodes = get_num_nodes(top);
if (num_nodes >= 2)
{
ptr = *top;
a = ptr->n;
b = ptr->next->n;
if (a == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
return (-1);
}
ptr->next->n = b % a;
pop_func(top, line_number);
}
else
{
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
return (-1);
}
return (0);
}