Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <unistd.h>
- #define TITLE "Test1"
- typedef struct Node {
- int id;
- char question[150];
- char answer[20];
- } Data_base;
- enum { QUESTIONS = 5 };
- int init(Data_base *test) {
- int id = 0;
- test[id].id = id + 1;
- strcpy(test[id].question, "5 + 10");
- strcpy(test[id].answer, "15");
- id++;
- test[id].id = id + 1;
- strcpy(test[id].question, "6 ^ 2");
- strcpy(test[id].answer, "36");
- id++;
- test[id].id = id + 1;
- strcpy(test[id].question, "6 / 3");
- strcpy(test[id].answer, "2");
- id++;
- test[id].id = id + 1;
- strcpy(test[id].question, "Which company is owner of iPhone?");
- strcpy(test[id].answer, "Apple");
- id++;
- test[id].id = id + 1;
- strcpy(test[id].question, "121 - 21");
- strcpy(test[id].answer, "100");
- return 0;
- }
- int
- get_id(char *param) {
- char c;
- int i = 0, id = 0;
- c = param[0];
- while (c != 0) {
- if (c < '0' || c > '9') {
- return -1;
- }
- id = id * 10 + c - '0';
- i++;
- c = param[i];
- }
- return --id;
- }
- void
- execute(char *command,char *param1, char *param2, Data_base *test) {
- int id = 0;
- if (!strcmp(command, "title")) {
- printf("%s\n", TITLE);
- } else {
- if (!strcmp(command, "get_q")) {
- id = get_id(param1);
- if (id == -1) {
- goto Error;
- }
- if (id < 0 || id >= QUESTIONS) {
- printf("Not found\n");
- } else {
- printf("%s\n", test[id].question);
- }
- } else {
- if (!strcmp(command, "check_a")) {
- id = get_id(param1);
- if (id == -1) {
- goto Error;
- }
- if (id < 0 || id >= QUESTIONS) {
- printf("Not found\n");
- } else {
- if (!strcmp(param2, test[id].answer)) {
- printf("+\n");
- } else {
- printf("-\n");
- }
- }
- } else {
- if (!strcmp(command, "num")) {
- printf("%d\n", QUESTIONS);
- } else {
- printf("Unknown command: '%s'", command);
- }
- }
- }
- }
- return;
- Error:
- printf("Bad command or param");
- return;
- }
- int
- pars(char *str, Data_base *test) {
- char command[11] = { 0 }, param1[6] = { 0 }, param2[86] = { 0 };
- int done = 0, pred = 0, j = 0, i = 0;
- for (i = 0; i < 100; i++) {
- if (str[i] == 0){
- break;
- }
- if (str[i] != ' ') {
- pred = 0;
- if (done == 0) {
- command[j] = str[i];
- j++;
- if (j == 11) {
- goto Error;
- }
- }
- if (done == 1) {
- param1[j] = str[i];
- j++;
- if (j == 6) {
- goto Error;
- }
- }
- if (done == 2) {
- param2[j] = str[i];
- j++;
- if (j == 86) {
- goto Error;
- }
- }
- } else {
- if (pred == 0) {
- pred = 1;
- done++;
- j=0;
- }
- }
- }
- execute(command, param1, param2, test);
- return 0;
- Error:
- printf("Bad command or params");
- return 1;
- }
- int main()
- {
- #define MAXSIZE 1024
- int n;
- Data_base test[5];
- init(test);
- int bad_command = 0, i = 0;
- char command[MAXSIZE+1] = { 0 };
- char input[MAXSIZE] = { 0 };
- char *c;
- setvbuf(stdout, NULL, _IOLBF, 0);
- while(fgets(input, 1024, stdin) != NULL)
- {
- strcpy(command, input);
- command[strlen(command)-1]=0;
- pars(command, test);
- //memset(command, 0, sizeof(command));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement