Advertisement
chatchai_j

part3.c

Apr 18th, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 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. #define SIZE    5
  8.  
  9. #define OP_QUIT 0
  10. #define OP_ADD  1
  11. #define OP_MUL  2
  12. #define OP_DEL  3
  13. #define OP_INS  4
  14.  
  15. #define RANGE_INVALID   -1
  16. #define RANGE_ALL   0
  17. #define RANGE_ONE   1
  18. #define RANGE_TWO   2
  19.  
  20. typedef struct  Cell_t {
  21.     int valid;
  22.     int row,col;
  23. } cell;
  24.  
  25. typedef struct  Range_t {
  26.     int Type;
  27.     cell    From;
  28.     cell    To;
  29. } range;
  30.  
  31. double  sheet[SIZE][SIZE];
  32.  
  33. int clearSheet() {
  34.     int row,col;
  35.  
  36.     for (row=0;row<SIZE;row++)
  37.         for (col=0;col<SIZE;col++)
  38.             sheet[row][col]=0;
  39.  
  40.     return 0;
  41. }
  42.  
  43. int printSheet() {
  44.     int row,col;
  45.  
  46.     printf("\t A\t B\t C\t D\t E\n");
  47.     for (row=0;row<SIZE;row++) {
  48.         printf("%d", row+1);
  49.         for (col=0;col<SIZE;col++) {
  50.             printf("\t%1.2f", sheet[row][col]);
  51.         }
  52.         printf("\n");
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58. int setSheet(int row, int col, double val) {
  59.     if ((row >= 0 && row < SIZE) && (col >= 0 && col < SIZE)) {
  60.         sheet[row][col] = val;
  61.     } else {
  62.         return -1;
  63.     }
  64.  
  65.     return 0;
  66. }
  67.  
  68. double  getSheet(int row, int col) {
  69.     return sheet[row][col];
  70. }
  71.  
  72. void    doOperation(int op, range r, double insval) {
  73.     double  result = 0;
  74.  
  75.     switch(op) {
  76.     case    OP_QUIT:
  77.         // invalid operation for this function
  78.         break;
  79.     case    OP_ADD:
  80.         if (r.Type == RANGE_ALL) {
  81.             int row, col;
  82.  
  83.             result = 0.0;
  84.             for (row = 0; row < SIZE; row++)
  85.                 for (col = 0; col < SIZE; col++)
  86.                     result += getSheet(row, col);
  87.         } else if (r.Type == RANGE_ONE) {
  88.             // invalid range for this operation
  89.         } else if (r.Type == RANGE_TWO) {
  90.         }
  91.         fprintf(stderr, "%1.2lf\n", result);
  92.         break;
  93.     case    OP_MUL:
  94.         if (r.Type == RANGE_ALL) {
  95.             int row, col;
  96.  
  97.             result = 1;
  98.             for (row = 0; row < SIZE; row++)
  99.                 for (col = 0; col < SIZE; col++)
  100.                     result *= getSheet(row, col);
  101.         } else if (r.Type == RANGE_ONE) {
  102.             // invalid range for this operation
  103.         } else if (r.Type == RANGE_TWO) {
  104.         }
  105.         fprintf(stderr, "%1.2lf\n", result);
  106.         break;
  107.     case    OP_DEL:
  108.         if (r.Type == RANGE_ALL) {
  109.             clearSheet();
  110.         } else if (r.Type == RANGE_ONE) {
  111.             int row = r.From.row;
  112.             int col = r.From.col;
  113.             sheet[row][col] = 0.0;
  114.         } else if (r.Type == RANGE_TWO) {
  115.             int row, col;
  116.             for (row = r.From.row; row <= r.To.row; row++)
  117.                 for (col = r.From.col; col <= r.To.col; col++)
  118.                     sheet[row][col] = 0.0;
  119.         }
  120.         break;
  121.     case    OP_INS:
  122.         if (r.Type == RANGE_ALL) {
  123.             int row, col;
  124.             for (row = 0; row < SIZE; row++)
  125.                 for (col = 0; col < SIZE; col++)
  126.                     sheet[row][col] = insval;
  127.         } else if (r.Type == RANGE_ONE) {
  128.             int row = r.From.row;
  129.             int col = r.From.col;
  130.             sheet[row][col] = insval;
  131.         } else if (r.Type == RANGE_TWO) {
  132.             int row, col;
  133.             for (row = r.From.row; row <= r.To.row; row++)
  134.                 for (col = r.From.col; col <= r.To.col; col++)
  135.                     sheet[row][col] = insval;
  136.         }
  137.         break;
  138.     default:
  139.         break;
  140.     }
  141. }
  142.  
  143. int main(int argc, char **argv) {
  144.     range   r;
  145.     double  val;
  146.  
  147.     r.Type = RANGE_ALL;
  148.    
  149.     clearSheet();
  150.  
  151.     fprintf(stderr, ">> INS() 1.2;\n");
  152.     doOperation(OP_INS, r, 1.2);    // Set all cell to 1.2
  153.     printSheet();
  154.  
  155.     r.Type = RANGE_ONE;
  156.     r.From.row = 0;         // row '1'
  157.     r.From.col = 1;         // col 'B'
  158.  
  159.     fprintf(stderr, ">> INS(B1) 9.99;\n");
  160.     doOperation(OP_INS, r, 9.99);   // Set B1 to 9.99
  161.     printSheet();
  162.  
  163.     // This actually invalid rage from the question, but the subroutine support this
  164.     r.Type = RANGE_TWO;
  165.     r.From.row = 1;         // From: row '2'
  166.     r.From.col = 2;         //       col 'C'
  167.     r.To.row = 4;           // To:   row '5'
  168.     r.To.col = 4;           //       col 'E'
  169.  
  170.     fprintf(stderr, ">> INS(C2:E5) 7.89;\n");
  171.     doOperation(OP_INS, r, 7.89);   // Set C2:E5 to 9.99
  172.     printSheet();
  173.  
  174.     fprintf(stderr, ">> DEL(C1:C5);\n");
  175.     r.Type = RANGE_TWO;
  176.     r.From.row = 0;         // From: row '1'
  177.     r.From.col = 2;         //       col 'C'
  178.     r.To.row = 4;           // To:   row '5'
  179.     r.To.col = 2;           //       col 'C'
  180.     doOperation(OP_DEL, r, 0.0);    // DEL
  181.     printSheet();
  182.  
  183.     fprintf(stderr, ">> ADD();\n");
  184.     r.Type = RANGE_ALL;
  185.     doOperation(OP_ADD, r, 0.0);    // ADD ALL
  186.    
  187.     return 0;
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement