-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.c
92 lines (87 loc) · 2.46 KB
/
math.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
#include "monty.h"
/**
* add - Calculates the sum of the top 2 elements on stack or queue.
* @stack: Stack.
* @line_number: Line number.
*/
void add(stack_t **stack, unsigned int line_number)
{
char message[100];
sprintf(message, "L%d: can't add, stack too short", line_number);
if (!stack)
error_mes("No stack present.", "", stack);
if (!*stack || !(*stack)->next)
error_mes(message, "", stack);
(*stack)->next->n += (*stack)->n;
pop(stack, line_number);
}
/**
* sub - Calculates the substraction of the top 2 elements on stack or queue.
* @stack: Stack.
* @line_number: Line number.
*/
void sub(stack_t **stack, unsigned int line_number)
{
char message[100];
sprintf(message, "L%d: can't sub, stack too short", line_number);
if (!stack)
error_mes("No stack present.", "", stack);
if (!*stack || !(*stack)->next)
error_mes(message, "", stack);
(*stack)->next->n -= (*stack)->n;
pop(stack, line_number);
}
/**
* divi - Calculates the division of the top 2 elements on stack or queue.
* @stack: Stack.
* @line_number: Line number.
*/
void divi(stack_t **stack, unsigned int line_number)
{
char message[100];
sprintf(message, "L%d: can't div, stack too short", line_number);
if (!stack)
error_mes("No stack present.", "", stack);
if (!*stack || !(*stack)->next)
error_mes(message, "", stack);
sprintf(message, "L%d: division by zero", line_number);
if (!(*stack)->n)
error_mes(message, "", stack);
(*stack)->next->n /= (*stack)->n;
pop(stack, line_number);
}
/**
* mul - Calculates the multiplication of the top 2 elements on stack or queue.
* @stack: Stack.
* @line_number: Line number.
*/
void mul(stack_t **stack, unsigned int line_number)
{
char message[100];
sprintf(message, "L%d: can't mul, stack too short", line_number);
if (!stack)
error_mes("No stack present.", "", stack);
if (!*stack || !(*stack)->next)
error_mes(message, "", stack);
(*stack)->next->n *= (*stack)->n;
pop(stack, line_number);
}
/**
* mod - Calculates the modulus of the top 2 elements on stack or queue.
* @stack: Stack.
* @line_number: Line number.
*/
void mod(stack_t **stack, unsigned int line_number)
{
char message[100];
sprintf(message, "L%d: can't mod, stack too short", line_number);
if (!stack)
error_mes("No stack present.", "", stack);
if (!*stack || !(*stack)->next)
error_mes(message, "", stack);
sprintf(message, "L%d: division by zero", line_number);
if (!(*stack)->n)
error_mes(message, "", stack);
(*stack)->next->n %= (*stack)->n;
pop(stack, line_number);
}