-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionary2.c
88 lines (85 loc) · 1.83 KB
/
dictionary2.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
#include "monty.h"
/**
* addStack - check the code
* @stack: first variable
* @line_number: Second variable
* Return: void.
*/
void addStack(stack_t **stack, unsigned int line_number)
{
stack_t *top, *tmp;
int sum = 0;
if (*stack == NULL || (*stack)->prev == NULL)
{
fprintf(stderr, "L%u: can't add, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
top = *stack;
sum = top->n + top->prev->n;
tmp = top->prev;
top->prev = tmp->prev;
top->n = sum;
free(tmp);
}
/**
* nopStack - check the code
* @stack: first variable
* @line_number: Second variable
* Return: void.
*/
void nopStack(stack_t **stack, unsigned int line_number)
{
(void)stack;
(void)line_number;
}
/**
* subStack - check the code
* @stack: first variable
* @line_number: Second variable
* Return: void.
*/
void subStack(stack_t **stack, unsigned int line_number)
{
int num1, num2;
stack_t *temp;
if (*stack == NULL || (*stack)->prev == NULL)
{
fprintf(stderr, "L%u: can't sub, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
num1 = (*stack)->n;
num2 = (*stack)->prev->n;
(*stack)->prev->n = num2 - num1;
temp = *stack;
*stack = (*stack)->prev;
(*stack)->next = NULL;
free(temp);
}
/**
* divStack - check the code
* @stack: first variable
* @line_number: Second variable
* Return: void.
*/
void divStack(stack_t **stack, unsigned int line_number)
{
int num1, num2;
stack_t *tmp;
if (*stack == NULL || (*stack)->prev == NULL)
{
fprintf(stderr, "L%u: can't div, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*stack)->n == 0)
{
fprintf(stderr, "L%u: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
num1 = (*stack)->n;
num2 = (*stack)->prev->n;
(*stack)->prev->n = num1 / num2;
tmp = *stack;
*stack = (*stack)->prev;
(*stack)->next = NULL;
free(tmp);
}