summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xinfection_simulatorbin0 -> 70328 bytes
-rw-r--r--infection_simulator.c148
2 files changed, 66 insertions, 82 deletions
diff --git a/infection_simulator b/infection_simulator
new file mode 100755
index 0000000..2aa29be
--- /dev/null
+++ b/infection_simulator
Binary files differ
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);
}