-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.c
44 lines (42 loc) · 1.08 KB
/
mod.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "main.h"
#define MAX_LINE_LENGTH 256
/**
* mod - Modulus operation.
*
* @stack: A pointer to the stack.
* @line_number: Line number in the file where the operation occurs.
*
* Description:
* Computes the remainder of the division of the second top element
* by the top element.
* If the stack contains fewer than two elements or if the top
* element is zero,
* prints an error message and exits.
*
* Parameters:
* - stack: A pointer to the stack.
* - line_number: Line number in the file where the operation occurs.
*
* Example:
* mod(&stack, 28);
* // Computes the remainder of the division in the stack.
*/
void mod(stack_t **stack, unsigned int line_number)
{
if (*stack == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't mod, 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);
}
(*stack)->next->n %= (*stack)->n;
pop(stack, line_number);
}