fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define NUM_CHOICES 5
  7. #define MAX_WORD_LEN 50
  8. #define MAX_MEANING_LEN 100
  9.  
  10. struct Word {
  11. char word[MAX_WORD_LEN];
  12. char meaning[MAX_MEANING_LEN];
  13. };
  14.  
  15. void shuffleArray(int *array, int n) {
  16. if (n > 1) {
  17. int i;
  18. for (i = 0; i < n - 1; i++) {
  19. int j = i + rand() / (RAND_MAX / (n - i) + 1);
  20. int temp = array[j];
  21. array[j] = array[i];
  22. array[i] = temp;
  23. }
  24. }
  25. }
  26.  
  27. void dailyWordTest(struct Word *word, char **wrong_choices) {
  28. srand(time(NULL));
  29. int random_indices[NUM_CHOICES];
  30. char choices[NUM_CHOICES][MAX_MEANING_LEN];
  31. char user_answer[10]; // 사용자 답변을 문자열로 받음
  32.  
  33. // 문제 설정
  34. printf("\n\n--- choose answer!!---\n'%s'\n", word->word);
  35.  
  36. // 정답과 오답 선택지 생성
  37. random_indices[0] = 0; // 0번 인덱스는 항상 정답으로 설정
  38. for (int i = 1; i < NUM_CHOICES; i++) {
  39. random_indices[i] = i; // 오답 선택지는 1부터 NUM_CHOICES-1까지
  40. }
  41.  
  42. // 오답 선택지 섞기
  43. shuffleArray(random_indices, NUM_CHOICES);
  44.  
  45. // 선택지 설정
  46. for (int i = 0; i < NUM_CHOICES; i++) {
  47. if (random_indices[i] == 0) {
  48. strcpy(choices[i], word->meaning); // 정답 설정
  49. } else {
  50. // 오답 선택지 설정
  51. strcpy(choices[i], wrong_choices[random_indices[i] - 1]);
  52. }
  53. }
  54.  
  55. // 선택지 섞기
  56. shuffleArray(random_indices, NUM_CHOICES);
  57.  
  58. // 선택지 출력
  59. for (int i = 0; i < NUM_CHOICES; i++) {
  60. printf("%d. %s\n", i + 1, choices[i]);
  61. }
  62.  
  63. // 사용자 답변 받기
  64. printf("답을 선택하세요 (1-%d): ", NUM_CHOICES);
  65. scanf("%s", user_answer);
  66. int user_choice = atoi(user_answer); // 정수로 변환
  67.  
  68. // 정답 확인
  69. if (user_choice >= 1 && user_choice <= NUM_CHOICES) {
  70. if (random_indices[user_choice - 1] == 0) {
  71. printf("정답은 '%s'입니다.\n");
  72. } else {
  73. printf("정답은 '%s'입니다.\n", word->meaning);
  74. }
  75. } else {
  76. printf("잘못된 입력입니다. 1부터 %d까지의 숫자 중에서 선택해주세요.\n", NUM_CHOICES);
  77. }
  78. }
  79.  
  80. int main() {
  81. struct Word daily_word;
  82.  
  83. // 오늘의 단어와 뜻 설정
  84. strcpy(daily_word.word, "His speech might ______ any fears about the war ");
  85. strcpy(daily_word.meaning, "dispel");
  86.  
  87. // 오답 선택지 배열
  88. char *wrong_choices[NUM_CHOICES - 1] = {
  89. "choose", "compute", "deduce", "charge"
  90. };
  91.  
  92. // 테스트 시작
  93. dailyWordTest(&daily_word, wrong_choices);
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout

--- choose answer!!---
'His speech might ______ any fears about the war '
1. deduce
2. charge
3. dispel
4. compute
5. choose
답을 선택하세요 (1-5): 잘못된 입력입니다. 1부터 5까지의 숫자 중에서 선택해주세요.