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
|
#include <stdio.h>
#include <ctype.h>
int getInt(const char* prm)
{
int n = 0;
while (printf(prm) && scanf("%i", &n) != 1) {
puts("Invalid number");
while (getchar() != '\n');
}
return n;
}
float getFloat(const char* prm)
{
float n = 0.0;
while (printf(prm) && scanf("%f", &n) != 1) {
puts("Invalid number");
while (getchar() != '\n');
}
return n;
}
int main()
{
const char* const numbs[] = {"zero", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th"};
bool cont = true;
while (cont) {
printf("\nPlease enter which arithmetic operation you would like to use:\n\n");
printf("Input a for Addition\n");
printf("Input b for Subtraction\n");
printf("Input c for Multiplication\n");
printf("Input d for Division\n");
printf("Input e for Modulo\n");
printf("Input f for exit\n");
printf("\nEnter option: ");
int opt = 0;
while (isspace(opt = getchar()));
switch (opt) {
case 'a':
{
printf("You have chosen addition!:\n");
int n = 0;
do {
n = getInt("How many numbers would you like to add (max 10): ");
} while ((n < 1 || n > 10) && printf("Invalid number\n"));
float result = 0.0;
for (int i = 1; i <= n; ++i) {
char prm[10] {};
snprintf(prm, sizeof(prm), "%s: ", numbs[i]);
result += getFloat(prm);
printf("Current sum= %g\n", result);
}
}
break;
case 'f':
cont = false;
break;
default:
puts("Unknown operation");
break;
}
}
}
|