-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmontyfunc3.c
96 lines (90 loc) · 2.14 KB
/
montyfunc3.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
#include "monty.h"
/**
* divs - a function that divides the first two stack members
* @head: pointer to the head of the stack
* @line_number: line in which bytecode is being executed
*/
void divs(stack_t **head, unsigned int line_number)
{
if (*head == NULL || (*head)->next == NULL)
{
fprintf(stderr, "L%d: can't div, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*head)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
(void) line_number;
(*head)->next->n /= (*head)->n;
pop(head, 0);
}
/**
* mul - a function that multiplies the first two stack members
* @head: pointer to the head of the stack
* @line_number: line in which bytecode is being executed
*/
void mul(stack_t **head, unsigned int line_number)
{
if (*head == NULL || (*head)->next == NULL)
{
fprintf(stderr, "L%d: can't mul, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
(void) line_number;
(*head)->next->n *= (*head)->n;
pop(head, 0);
}
/**
* mod - a function that gets the modulo of the first two stack members
* @head: pointer to the head of the stack
* @line_number: line in which bytecode is being executed
*/
void mod(stack_t **head, unsigned int line_number)
{
if (*head == NULL || (*head)->next == NULL)
{
fprintf(stderr, "L%d: can't mod, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
if ((*head)->n == 0)
{
fprintf(stderr, "L%d: division by zero\n", line_number);
exit(EXIT_FAILURE);
}
(void) line_number;
(*head)->next->n %= (*head)->n;
pop(head, 0);
}
/**
* is_numeric - Checks if a string is a number
* @str: String parameter
* Return: int
*/
int is_numeric(const char *str)
{
int digit_count = 0;
if (str == NULL || *str == '\0')
return (0);
if (*str == '+' || *str == '-')
str++;
while (*str != '\0')
{
if (!isdigit(*str))
return (0);
digit_count++;
str++;
}
return (digit_count > 0);
}
/**
* nop - implementation of nop which is a function that does nothing
* @stack: pointer to the head of the stack
* @line_number: line that bytecode op is being searched
*/
void nop(stack_t **stack, unsigned int line_number)
{
(void) stack;
(void) line_number;
}