HTMLify
temp.c
Views: 1 | Author: abh
/* I just created this simulation type, this wasn't a requirement */
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
enum state {
ALIVE,
SICK,
VERY_SICK,
DEAD,
};
typedef struct {
enum state status;
char name[10];
} Human;
Human new_human(char *name) {
Human human;
human.status = rand() % 3 < 1 ? ALIVE : SICK;
strcpy(human.name, name);
return human;
}
void update_state(Human *human, int temperature) {
if (human->status == DEAD) return;
// give death
if (temperature <= 0 || temperature >= 60) {
human->status = DEAD;
return;
}
// make sick or sicker
if ((0 < temperature && temperature < 20) || (30 < temperature && temperature < 60)) {
switch (human->status) {
case DEAD:
break;
case VERY_SICK:
human->status = DEAD;
break;
case SICK:
human->status = VERY_SICK;
break;
case ALIVE:
human->status = SICK;
break;
}
return;
}
// heal
if (20 <= temperature && temperature <= 30) {
switch (human->status) {
case ALIVE:
break;
case SICK:
human->status = ALIVE;
break;
case VERY_SICK:
human->status = SICK;
break;
case DEAD:
break;
}
return;
}
}
char* status_in_string(enum state status) {
switch (status) {
case DEAD:
return "x DEAD x";
case VERY_SICK:
return "-VERY-SICK-";
case SICK:
return "-SICK-";
case ALIVE:
return "ALIVE";
}
}
void print_human(Human human) {
printf("%s : %s\n", human.name, status_in_string(human.status));
}
int main() {
int temperature = 30;
Human *humans;
int human_count;
srand(time(NULL));
/*
* 0 - human die
* 10 - cooler
* 20 - cool
* 25 - ok
* 30 - hot
* 40 - hotter
* 60 - humans die
*
*/
printf("Enter number of humans: ");
scanf("%d", &human_count);
humans = malloc(sizeof(Human) * human_count);
for (int i=i; i<human_count; i++) {
humans[i] = new_human("Human");
}
printf("Created %d humans\n", human_count);
while (0x1) {
printf("Temperature: %d\n", temperature);
printf("Humans' status: \n");
printf("----------------------------\n");
for (int i=0; i<human_count; i++) {
print_human(humans[i]);
}
printf("----------------------------\n");
printf("Temeperature Update: ");
scanf("%d", &temperature);
for (int i=0; i<human_count; i++) {
update_state(&humans[i], temperature);
}
}
return 0;
}