Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #define BUFSZ 80
- #define SIZE 5
- #define OP_QUIT 0
- #define OP_ADD 1
- #define OP_MUL 2
- #define OP_DEL 3
- #define OP_INS 4
- #define RANGE_INVALID -1
- #define RANGE_ALL 0
- #define RANGE_ONE 1
- #define RANGE_TWO 2
- typedef struct Cell_t {
- int valid;
- int row,col;
- } cell;
- typedef struct Range_t {
- int Type;
- cell From;
- cell To;
- } range;
- double sheet[SIZE][SIZE];
- int clearSheet() {
- int row,col;
- for (row=0;row<SIZE;row++)
- for (col=0;col<SIZE;col++)
- sheet[row][col]=0;
- return 0;
- }
- int printSheet() {
- int row,col;
- printf("\t A\t B\t C\t D\t E\n");
- for (row=0;row<SIZE;row++) {
- printf("%d", row+1);
- for (col=0;col<SIZE;col++) {
- printf("\t%1.2f", sheet[row][col]);
- }
- printf("\n");
- }
- return 0;
- }
- int setSheet(int row, int col, double val) {
- if ((row >= 0 && row < SIZE) && (col >= 0 && col < SIZE)) {
- sheet[row][col] = val;
- } else {
- return -1;
- }
- return 0;
- }
- double getSheet(int row, int col) {
- return sheet[row][col];
- }
- void doOperation(int op, range r, double insval) {
- double result = 0;
- switch(op) {
- case OP_QUIT:
- // invalid operation for this function
- break;
- case OP_ADD:
- if (r.Type == RANGE_ALL) {
- int row, col;
- result = 0.0;
- for (row = 0; row < SIZE; row++)
- for (col = 0; col < SIZE; col++)
- result += getSheet(row, col);
- } else if (r.Type == RANGE_ONE) {
- // invalid range for this operation
- } else if (r.Type == RANGE_TWO) {
- }
- fprintf(stderr, "%1.2lf\n", result);
- break;
- case OP_MUL:
- if (r.Type == RANGE_ALL) {
- int row, col;
- result = 1;
- for (row = 0; row < SIZE; row++)
- for (col = 0; col < SIZE; col++)
- result *= getSheet(row, col);
- } else if (r.Type == RANGE_ONE) {
- // invalid range for this operation
- } else if (r.Type == RANGE_TWO) {
- }
- fprintf(stderr, "%1.2lf\n", result);
- break;
- case OP_DEL:
- if (r.Type == RANGE_ALL) {
- clearSheet();
- } else if (r.Type == RANGE_ONE) {
- int row = r.From.row;
- int col = r.From.col;
- sheet[row][col] = 0.0;
- } else if (r.Type == RANGE_TWO) {
- int row, col;
- for (row = r.From.row; row <= r.To.row; row++)
- for (col = r.From.col; col <= r.To.col; col++)
- sheet[row][col] = 0.0;
- }
- break;
- case OP_INS:
- if (r.Type == RANGE_ALL) {
- int row, col;
- for (row = 0; row < SIZE; row++)
- for (col = 0; col < SIZE; col++)
- sheet[row][col] = insval;
- } else if (r.Type == RANGE_ONE) {
- int row = r.From.row;
- int col = r.From.col;
- sheet[row][col] = insval;
- } else if (r.Type == RANGE_TWO) {
- int row, col;
- for (row = r.From.row; row <= r.To.row; row++)
- for (col = r.From.col; col <= r.To.col; col++)
- sheet[row][col] = insval;
- }
- break;
- default:
- break;
- }
- }
- int main(int argc, char **argv) {
- range r;
- double val;
- r.Type = RANGE_ALL;
- clearSheet();
- fprintf(stderr, ">> INS() 1.2;\n");
- doOperation(OP_INS, r, 1.2); // Set all cell to 1.2
- printSheet();
- r.Type = RANGE_ONE;
- r.From.row = 0; // row '1'
- r.From.col = 1; // col 'B'
- fprintf(stderr, ">> INS(B1) 9.99;\n");
- doOperation(OP_INS, r, 9.99); // Set B1 to 9.99
- printSheet();
- // This actually invalid rage from the question, but the subroutine support this
- r.Type = RANGE_TWO;
- r.From.row = 1; // From: row '2'
- r.From.col = 2; // col 'C'
- r.To.row = 4; // To: row '5'
- r.To.col = 4; // col 'E'
- fprintf(stderr, ">> INS(C2:E5) 7.89;\n");
- doOperation(OP_INS, r, 7.89); // Set C2:E5 to 9.99
- printSheet();
- fprintf(stderr, ">> DEL(C1:C5);\n");
- r.Type = RANGE_TWO;
- r.From.row = 0; // From: row '1'
- r.From.col = 2; // col 'C'
- r.To.row = 4; // To: row '5'
- r.To.col = 2; // col 'C'
- doOperation(OP_DEL, r, 0.0); // DEL
- printSheet();
- fprintf(stderr, ">> ADD();\n");
- r.Type = RANGE_ALL;
- doOperation(OP_ADD, r, 0.0); // ADD ALL
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement