forked from ALX-SE-Algorithmia/Demo-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (77 loc) · 1.57 KB
/
main.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
#include "calc.h"
/**
* print_selection - print choices
* Return: void
*/
void print_selection(void)
{
printf("\nPlease choose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulo\n");
printf("6. Nth Root\n");
printf("7. Exponentiation\n");
printf("8. log\u2081\u2080\n");
printf("9. Factorial\n");
printf("10. Fibonacci\n");
printf("Enter your choice (Ctrl+C to quit): ");
}
/**
* valid_input - validates user input.
* @str: str to validate.
* Return: true if str is double otherwise false.
*/
double valid_input(const char *str)
{
char *endptr;
strtod(str, &endptr);
if (*endptr != '\0')
{
printf("Error: Input must be a double or an integer.\n");
exit(EXIT_FAILURE);
}
return (strtod(str, NULL));
}
/**
* main - program entry point
* Return: 0 on success
*/
int main(void)
{
int choice;
char a[100];
char b[100];
double result;
printf("Welcome to C Calculator Program by Evance\n");
printf("============================================");
while (1)
{
print_selection();
scanf("%d", &choice);
if (choice >= 1 && choice <= 5)
{
printf("Enter first number: ");
scanf("%s", a);
printf("Enter second number: ");
scanf("%s", b);
two_operand_calc(a, b, choice);
}
else if (choice == 6 || choice == 7)
{
special_calc(choice);
}
else if (choice > 7 && choice <= 10)
{
printf("Enter number: ");
scanf("%s", a);
single_operand_calc(a, choice);
}
else
{
printf("Error: Invalid input.\n");
exit(EXIT_FAILURE);
}
}
}