Advertisement
chatchai_j

part2.c

Apr 18th, 2022
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6. #define BUFSZ   80
  7.  
  8. #define OP_QUIT 0
  9. #define OP_ADD  1
  10. #define OP_MUL  2
  11. #define OP_DEL  3
  12. #define OP_INS  4
  13.  
  14. #define RANGE_INVALID   -1
  15. #define RANGE_ALL   0
  16. #define RANGE_ONE   1
  17. #define RANGE_TWO   2
  18.  
  19. typedef struct  Cell_t {
  20.     int valid;
  21.     int row,col;
  22. } cell;
  23.  
  24. typedef struct  Range_t {
  25.     int Type;
  26.     cell    From;
  27.     cell    To;
  28. } range;
  29.  
  30. int getOp(char *cmd) {
  31.     if (strcmp(cmd,"QUIT")==0 || strcmp(cmd,"quit")==0) {
  32.         return OP_QUIT;
  33.     }
  34.     if (strncmp(cmd,"ADD",3)==0 || strncmp(cmd,"add",3)==0) {
  35.         return OP_ADD;
  36.     }
  37.     if (strncmp(cmd,"MUL",3)==0 || strncmp(cmd,"mul",3)==0) {
  38.         return OP_MUL;
  39.     }
  40.     if (strncmp(cmd,"DEL",3)==0 || strncmp(cmd,"del",3)==0) {
  41.         return OP_DEL;
  42.     }
  43.     if (strncmp(cmd,"INS",3)==0 || strncmp(cmd,"ins",3)==0) {
  44.         return OP_INS;
  45.     }
  46.     return -1;
  47. }
  48.  
  49. cell    getCell(char *ptr) {
  50.     cell    c;
  51.     char    col = (char)*ptr;
  52.     char    row = (char)*(ptr+1);
  53.  
  54.     // fprintf(stderr, "DEBUG: cell[%s] = (%c,%c)\n", ptr, row, col);
  55.     c.valid = 0;
  56.     if (    (col >= 'A' && col <= 'E') &&
  57.         (row >= '1' && row <= '5') ) {
  58.         c.valid = 1;
  59.         c.row = row - '1';
  60.         c.col = col - 'A';
  61.     }
  62.     return c;
  63. }
  64.  
  65. range   getRange(char *ptr) {
  66.     range   r;
  67.     cell    from, to;
  68.     int l=strlen(ptr);
  69.  
  70.     // fprintf(stderr, "DEBUG: range '%s' l=%d\n", ptr, l);
  71.     r.Type = RANGE_INVALID;
  72.  
  73.     if (l==3 && strcmp(ptr,"();")==0) {
  74.         r.Type = RANGE_ALL;
  75.         return r;
  76.     }
  77.     if (l==5 && *ptr == '(' && strcmp(ptr+3,");")==0) {
  78.         from = getCell(ptr+1);
  79.         if (from.valid == 1) {
  80.             r.From = from;
  81.             r.Type = RANGE_ONE;
  82.         }
  83.         return r;
  84.     }
  85.     if (l==8 && *ptr == '(' && *(ptr+3) == ':' && strcmp(ptr+6,");")==0) {
  86.         r.Type = RANGE_TWO;
  87.         from = getCell(ptr+1);
  88.         to = getCell(ptr+4);
  89.         if (from.valid == 1 && to.valid == 1) {
  90.             int f_row = r.From.row;
  91.             int t_row = r.To.row;
  92.             int f_col = r.From.col;
  93.             int t_col = r.To.col;
  94.  
  95.             if (    ((f_row == t_row) && (f_col < t_col)) ||
  96.                 ((f_col == t_col) && (f_row < t_row))   ) {
  97.                 r.From = from;
  98.                 r.To = to;
  99.                 r.Type = RANGE_TWO;
  100.             }
  101.         }
  102.         return r;
  103.     }
  104.     return r;
  105. }
  106.  
  107. range   getInsRange(char *ptr) {
  108.     range   r;
  109.     cell    from, to;
  110.     int l=strlen(ptr);
  111.  
  112.     // fprintf(stderr, "DEBUG: range '%s' l=%d\n", ptr, l);
  113.     r.Type = RANGE_INVALID;
  114.  
  115.     if (l==2 && strcmp(ptr,"()")==0) {
  116.         r.Type = RANGE_ALL;
  117.         return r;
  118.     }
  119.     if (l==4 && *ptr == '(' && strcmp(ptr+3,")")==0) {
  120.         from = getCell(ptr+1);
  121.         if (from.valid == 1) {
  122.             r.From = from;
  123.             r.Type = RANGE_ONE;
  124.         }
  125.         return r;
  126.     }
  127.     if (l==7 && *ptr == '(' && *(ptr+3) == ':' && strcmp(ptr+6,")")==0) {
  128.         r.Type = RANGE_TWO;
  129.         from = getCell(ptr+1);
  130.         to = getCell(ptr+4);
  131.         if (from.valid == 1 && to.valid == 1) {
  132.             int f_row = r.From.row;
  133.             int t_row = r.To.row;
  134.             int f_col = r.From.col;
  135.             int t_col = r.To.col;
  136.  
  137.             if (    ((f_row == t_row) && (f_col < t_col)) ||
  138.                 ((f_col == t_col) && (f_row < t_row))   ) {
  139.                 r.From = from;
  140.                 r.To = to;
  141.                 r.Type = RANGE_TWO;
  142.             }
  143.         }
  144.         return r;
  145.     }
  146.     return r;
  147. }
  148.  
  149. void    doOperation(int op, range r, double insval) {
  150.     fprintf(stderr, "OP = %d\t", op);
  151.     if (r.Type == RANGE_ALL)
  152.         fprintf(stderr, "ALL");
  153.     else if (r.Type == RANGE_ONE)
  154.         fprintf(stderr, "(%d,%d)", r.From.row, r.From.col);
  155.     else if (r.Type == RANGE_TWO)
  156.         fprintf(stderr, "(%d,%d)->(%d,%d)",
  157.             r.From.row, r.From.col,
  158.             r.To.row, r.To.col
  159.         );
  160.     else    fprintf(stderr, "[ERROR]");
  161.     if (op == OP_INS) {
  162.         fprintf(stderr, " %1.2lf", insval);
  163.     }
  164.     fprintf(stderr, "\n");
  165. }
  166.  
  167. int readCmdLoop() {
  168.     char    buf[BUFSZ];
  169.     char    cmd[BUFSZ], arg[BUFSZ], *ptr;
  170.     int op, res;
  171.     double  insval;
  172.     range   r;
  173.  
  174.     while (1) {
  175.         printf(">> ");
  176.        
  177.         if (fgets(buf, sizeof(buf), stdin)==NULL) {
  178.         // if user press CTRL+D
  179.             fprintf(stderr, "Good bye\n");
  180.             break;
  181.         }
  182.         // printf("[%s]\n", buf);
  183.  
  184.         res=sscanf(buf, "%s %s", cmd, arg);
  185.         // printf("res=%d cmd='%s' arg='%s'\n", res, cmd, arg);S
  186.  
  187.         op = getOp(cmd);
  188.         if (op==-1) {
  189.             fprintf(stderr, "Invalid operation (%s)\n", cmd);
  190.             continue;
  191.         }
  192.         if (op==OP_QUIT) {
  193.             fprintf(stderr, "Good Bye\n");
  194.             break;
  195.         }
  196.         ptr = cmd+3;
  197.         if (op == OP_INS) {
  198.             r = getInsRange(ptr);
  199.             sscanf(arg, "%lf", &insval);
  200.         } else {
  201.             r = getRange(ptr);
  202.         }
  203.         if (r.Type == RANGE_INVALID) {
  204.             fprintf(stderr, "Wrong Range [%s]\n", ptr);
  205.             continue;
  206.         }
  207.         doOperation(op, r, insval);
  208.     }
  209.    
  210.     return 0;
  211. }
  212.  
  213. int main(int argc, char **argv) {
  214.     readCmdLoop();
  215.     return 0;
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement