diff options
| author | magh <magh@maghmogh.com> | 2024-09-09 10:10:45 +0900 |
|---|---|---|
| committer | magh <magh@maghmogh.com> | 2024-09-09 10:10:45 +0900 |
| commit | 41d01b3ac7a374792cc926dfc1877e7495ea23f3 (patch) | |
| tree | e6655600251df57daddda801cf57e8ef11b9e925 | |
| parent | 2ea1989bf7772b8322fa2691c698970bc65ae357 (diff) | |
| -rwxr-xr-x | infection_simulator | bin | 0 -> 70328 bytes | |||
| -rw-r--r-- | infection_simulator.c | 148 |
2 files changed, 66 insertions, 82 deletions
diff --git a/infection_simulator b/infection_simulator Binary files differnew file mode 100755 index 0000000..2aa29be --- /dev/null +++ b/infection_simulator diff --git a/infection_simulator.c b/infection_simulator.c index 4b9e7e7..ed7232b 100644 --- a/infection_simulator.c +++ b/infection_simulator.c @@ -1,93 +1,77 @@ #include <stdio.h> +#include <math.h> +void current(); +void print_menu(); -void current (); - -int main () +int main() { - int choice; - printf("Choose:\n"); - printf("1. Get the output and percentage\n"); - printf("2. Exit the program\n"); - - printf("Enter your choice: "); - scanf("%d", &choice); - - while (choice != 2) - { - switch (choice) - { - case 1: - current(); - break; - - case 2: - printf("Exiting...\n"); - break; - } - - printf("Choose:\n"); -printf("1. Get the output and percentage\n"); -printf("2. Exit the program\n"); - -printf("Enter your choice: "); -scanf("%d", &choice); + int choice; + do { + print_menu(); + scanf("%d", &choice); + switch (choice) + { + case 1: + current(); + break; + case 2: + printf("Exiting...\n"); + break; + default: + printf("Invalid choice. Please try again.\n"); + } + } + while (choice != 2); + return 0; +} - } +void print_menu() +{ + printf("Choose:\n"); + printf("1. Get the output and percentage\n"); + printf("2. Exit the program\n"); + printf("Enter your choice: "); } void current() { - float percentage = 0; - float R = 1.2; - float i = 7; - int d; - float loses = 0; - float total = 7; - float total_before; - int total_rounded; - float not_infected; - float discount = 0; - printf("Enter days: "); - scanf("%d", &d); - - int original_d = d; - - while (d != 0 && total <= 2440) + int days; + float infected = 7.0; + float infected_today = 7.0; + int withdraw; + const int total = 2440; + const float fee = 9972.0; + + printf("Enter days: "); + scanf("%d", &days); + + if (days <= 0) + { + printf("Invalid number of days. Please enter a positive number.\n"); + return; + } + + for (int i = 0; i < days && infected <= total; i++) + { + infected += infected_today * 0.2; + infected_today *= 1.2; + if (i < 14) { - i = i * R; - total = total + i; - - if (original_d - d == 14) - { - total_before = (int) total; - loses = total_before * 9972; - } - - d--; - } - - if (original_d < 14) - { - total_before = (int) total; - loses = total_before * 9972; - } - - if (total > 2440) - { - total = 2440; - } - - total_rounded = (int) total + discount; - - percentage = (total / 2440) * 100; - - not_infected = 2440 - total_before; - - discount = not_infected * (9972 * 0.05); - - loses = loses + discount; - - printf("The number of infected students is: %d, a percentage of %f, the total losses are $%.0f corresponding to %.0f students, the number of students receiving a 5 percent discount is %0.f, and the total amount given in discounts is $%0.f ", total_rounded, percentage, loses, total_before, not_infected, discount); - puts(""); + withdraw = (int)infected; + } + } + + if (infected > total) + { + infected = total; + } + + int total_rounded = (int)infected; + float percentage = (infected / total) * 100; + float not_infected = total - withdraw; + float discount = not_infected * (fee * 0.05); + float loses = withdraw * fee + discount; + + printf("The number of infected students is: %d, a percentage of %.2f, the total losses are $%.0f corresponding to %d students, the number of students receiving a 5 percent discount is %.0f, and the total amount given in discounts is $%.0f\n", total_rounded, percentage, loses, withdraw, not_infected, discount); } |
