Advertisement
cd62131

Japanese Zodiacs

Jan 18th, 2019
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4.   struct list {
  5.     char name[20];
  6.     struct list *prev, *next;
  7.   } zodiac[] = {{.name = "boar"},   {.name = "rat"},     {.name = "ox"},
  8.                 {.name = "tiger"},  {.name = "rabbit"},  {.name = "dragon"},
  9.                 {.name = "snake"},  {.name = "horse"},   {.name = "sheep"},
  10.                 {.name = "monkey"}, {.name = "rooster"}, {.name = "dog"}};
  11.   size_t size = sizeof(zodiac) / sizeof(zodiac[0]);
  12.   for (size_t i = 0; i < size; ++i) {
  13.     zodiac[i].next = &zodiac[(i + 1) % size];
  14.     zodiac[(i + 1) % size].prev = &zodiac[i];
  15.   }
  16.   struct list *current = &zodiac[0];
  17.   printf("This year is [%s] in oriental zodiac.\n", current->name);
  18.   for (int i = 0; i < 5; ++i) {
  19.     printf("Please enter how many years to move =>");
  20.     int move;
  21.     if (scanf("%d", &move) != 1) {
  22.       exit(1);
  23.     }
  24.     move %= size;
  25.     move += size;
  26.     move %= size;
  27.     for (int j = 0; j < move; ++j) {
  28.       current = current->next;
  29.     }
  30.     printf("This year is [%s] in oriental zodiac.\n", current->name);
  31.   }
  32.   puts("Finish.");
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement