I learned all about C grammar at Bitter C, so I made some Omikuji as an exercise. I made 3 patterns, one using if, one using switch, and one using an array of character strings. (Addition 5/14) Changed the type of main function from void to int.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
int num;
printf("Press the Enter key... ");
getchar();
srand((unsigned int)time(NULL));
num = rand() % 6;
if (num == 0) {
printf("Daikichi\n");
} else if (num == 1) {
printf("Nakayoshi\n");
} else if (num == 2) {
printf("Kokichi\n");
} else if (num == 3) {
printf("Sueyoshi\n");
} else if (num == 4) {
printf("Bad\n");
} else {
printf("Great villain\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int num;
printf("Press the Enter key... ");
getchar();
srand((unsigned int)time(NULL));
num = rand() % 6;
switch (num) {
case 0:
printf("Daikichi\n");
break;
case 1:
printf("Nakayoshi\n");
break;
case 2:
printf("Kokichi\n");
break;
case 3:
printf("Sueyoshi\n");
break;
case 4:
printf("Bad\n");
break;
case 5:
printf("Great villain\n");
break;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
char f[][8] = {"Daikichi", "Nakayoshi", "Kokichi", "Sueyoshi", "Bad", "大Bad"};
int num;
printf("Press the Enter key... ");
getchar();
srand((unsigned int)time(NULL));
num = rand() % 6;
printf("%s\n", f[num]);
return 0;
}
that's all
Recommended Posts