-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode_fns_3.c
125 lines (107 loc) · 2.86 KB
/
opcode_fns_3.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
#include "monty.h"
/**
* _sub - Function that subtracts the top element of the
* stack from the second to element of the stack
*
* @stack: Pointer to stack
* @line_number: line number
*/
void _sub(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
/*Exit program if stack element is NULL of less that two*/
if (head == NULL || head->next == NULL)
{
fprintf(stderr, "L%d: can't sub, stack too short\n",
gv->line_number);
kill(NULL);
}
/*Subtract to element from second top element*/
gv->head->next->n = head->next->n - head->n;
/*Remove top element*/
_pop(&(gv->head), gv->line_number);
}
/**
* _div - Function that devides the second top element of
* the stack by the top element of the stack
*
* @stack: Pointer to stack
* @line_number: line number
*/
void _div(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
/*Exit program if stack element is NULL of less that two*/
if (head == NULL || head->next == NULL)
{
fprintf(stderr, "L%d: can't div, stack too short\n",
gv->line_number);
kill(NULL);
}
/*Exit if top element of stack is zero*/
if (head->n == 0)
{
fprintf(stderr, "L%d: division by zero\n",
gv->line_number);
kill(NULL);
}
/*Divide second top element by top element*/
gv->head->next->n = head->next->n / head->n;
/*Remove top element*/
_pop(&(gv->head), gv->line_number);
}
/**
* _mul - Function that multiplies the second top element of
* the stack with the top element of the stack
*
* @stack: Pointer to stack
* @line_number: line number
*/
void _mul(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
/*Exit program if stack element is NULL of less that two*/
if (head == NULL || head->next == NULL)
{
fprintf(stderr, "L%d: can't mul, stack too short\n",
gv->line_number);
kill(NULL);
}
/*Multiply second top element with top element*/
gv->head->next->n = head->next->n * head->n;
/*Remove top element*/
_pop(&(gv->head), gv->line_number);
}
/**
* _mod - Function that computes the rest of the second
* top element of the stack by the top element of the stack
*
* @stack: Pointer to stack
* @line_number: line number
*/
void _mod(__attribute((unused))stack_t **stack,
__attribute((unused))unsigned int line_number)
{
stack_t *head = gv->head;
/*Exit program if stack element is NULL of less that two*/
if (head == NULL || head->next == NULL)
{
fprintf(stderr, "L%d: can't mod, stack too short\n",
gv->line_number);
kill(NULL);
}
/*Exit if top element of stack is zero*/
if (head->n == 0)
{
fprintf(stderr, "L%d: division by zero\n",
gv->line_number);
kill(NULL);
}
/*Modulus second top element by top element*/
gv->head->next->n = head->next->n % head->n;
/*Remove top element*/
_pop(&(gv->head), gv->line_number);
}