Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <unistd.h>
  7.  
  8. #define TITLE "Test1"
  9.  
  10. typedef struct Node {
  11. int id;
  12. char question[150];
  13. char answer[20];
  14. } Data_base;
  15.  
  16.  
  17. enum { QUESTIONS = 5 };
  18.  
  19.  
  20. int init(Data_base *test) {
  21. int id = 0;
  22.  
  23. test[id].id = id + 1;
  24. strcpy(test[id].question, "5 + 10");
  25. strcpy(test[id].answer, "15");
  26. id++;
  27.  
  28. test[id].id = id + 1;
  29. strcpy(test[id].question, "6 ^ 2");
  30. strcpy(test[id].answer, "36");
  31. id++;
  32.  
  33. test[id].id = id + 1;
  34. strcpy(test[id].question, "6 / 3");
  35. strcpy(test[id].answer, "2");
  36. id++;
  37.  
  38. test[id].id = id + 1;
  39. strcpy(test[id].question, "Which company is owner of iPhone?");
  40. strcpy(test[id].answer, "Apple");
  41. id++;
  42.  
  43. test[id].id = id + 1;
  44. strcpy(test[id].question, "121 - 21");
  45. strcpy(test[id].answer, "100");
  46.  
  47. return 0;
  48. }
  49.  
  50. int
  51. get_id(char *param) {
  52. char c;
  53. int i = 0, id = 0;
  54. c = param[0];
  55. while (c != 0) {
  56. if (c < '0' || c > '9') {
  57. return -1;
  58. }
  59. id = id * 10 + c - '0';
  60. i++;
  61. c = param[i];
  62. }
  63. return --id;
  64. }
  65.  
  66. void
  67. execute(char *command,char *param1, char *param2, Data_base *test) {
  68. int id = 0;
  69. if (!strcmp(command, "title")) {
  70. printf("%s\n", TITLE);
  71. } else {
  72. if (!strcmp(command, "get_q")) {
  73. id = get_id(param1);
  74. if (id == -1) {
  75. goto Error;
  76. }
  77. if (id < 0 || id >= QUESTIONS) {
  78. printf("Not found\n");
  79. } else {
  80. printf("%s\n", test[id].question);
  81. }
  82. } else {
  83. if (!strcmp(command, "check_a")) {
  84. id = get_id(param1);
  85. if (id == -1) {
  86. goto Error;
  87. }
  88. if (id < 0 || id >= QUESTIONS) {
  89. printf("Not found\n");
  90. } else {
  91. if (!strcmp(param2, test[id].answer)) {
  92. printf("+\n");
  93. } else {
  94. printf("-\n");
  95. }
  96. }
  97. } else {
  98. if (!strcmp(command, "num")) {
  99. printf("%d\n", QUESTIONS);
  100. } else {
  101. printf("Unknown command: '%s'", command);
  102. }
  103. }
  104. }
  105. }
  106. return;
  107. Error:
  108. printf("Bad command or param");
  109. return;
  110. }
  111.  
  112. int
  113. pars(char *str, Data_base *test) {
  114. char command[11] = { 0 }, param1[6] = { 0 }, param2[86] = { 0 };
  115. int done = 0, pred = 0, j = 0, i = 0;
  116. for (i = 0; i < 100; i++) {
  117. if (str[i] == 0){
  118. break;
  119. }
  120. if (str[i] != ' ') {
  121. pred = 0;
  122. if (done == 0) {
  123. command[j] = str[i];
  124. j++;
  125. if (j == 11) {
  126. goto Error;
  127. }
  128. }
  129. if (done == 1) {
  130. param1[j] = str[i];
  131. j++;
  132. if (j == 6) {
  133. goto Error;
  134. }
  135. }
  136. if (done == 2) {
  137. param2[j] = str[i];
  138. j++;
  139. if (j == 86) {
  140. goto Error;
  141. }
  142. }
  143. } else {
  144. if (pred == 0) {
  145. pred = 1;
  146. done++;
  147. j=0;
  148. }
  149. }
  150. }
  151. execute(command, param1, param2, test);
  152. return 0;
  153. Error:
  154. printf("Bad command or params");
  155. return 1;
  156. }
  157.  
  158.  
  159.  
  160. int main()
  161. {
  162. #define MAXSIZE 1024
  163. int n;
  164. Data_base test[5];
  165. init(test);
  166. int bad_command = 0, i = 0;
  167. char command[MAXSIZE+1] = { 0 };
  168. char input[MAXSIZE] = { 0 };
  169. char *c;
  170. setvbuf(stdout, NULL, _IOLBF, 0);
  171. while(fgets(input, 1024, stdin) != NULL)
  172. {
  173. strcpy(command, input);
  174. command[strlen(command)-1]=0;
  175. pars(command, test);
  176. //memset(command, 0, sizeof(command));
  177. }
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement