Last active
October 28, 2020 15:17
-
-
Save Jackzmc/3464223f3ce3129377a761c7afd4d3c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <math.h> | |
| #include <ctype.h> | |
| #include <stdbool.h> | |
| #include <string.h> | |
| #include "matrix.h" | |
| unsigned char matrixCount = 0; | |
| matrix* matrices; | |
| int main(int argc, char *argv[]) { | |
| //this parses to a count of number of matrices user wants | |
| while(matrixCount == 0) { | |
| printf("Number of matrixes (1-26): "); | |
| char input[4]; | |
| fgets(input, sizeof(input), stdin); | |
| int parseInput = atoi(input); | |
| if(parseInput >= 0 && parseInput <= 24) { | |
| matrixCount = parseInput; | |
| } | |
| } | |
| //Run for every matrixCount, getting valid dimensions | |
| printf("Specify dimensions with an X (ex: 2x2)\n"); | |
| matrices = malloc(matrixCount * sizeof(matrix)); | |
| for (u_char mtxNumber = 0; mtxNumber < matrixCount; mtxNumber++) | |
| { | |
| u_int rows = 0, cols = 0; | |
| getDimensions(mtxNumber, &rows, &cols); | |
| //initalize a new matrix struct | |
| matrix mtx = { .rows = rows, .cols = cols}; | |
| //allocate mem for 2d array | |
| mtx.data = malloc(rows*sizeof(double*)); | |
| for(uint i=0;i<rows;i++) mtx.data[i] = malloc(cols*sizeof(double)); | |
| for(uint row = 0; row < rows; ++row) { | |
| for(uint col = 0; col < cols; ++col) { | |
| double num; | |
| char inp[16]; | |
| printf("Matrix #%d (%dx%d) | ROW %d COL %d Value: ", (mtxNumber+1), rows, cols, row, col); | |
| fgets(inp, sizeof(inp), stdin); | |
| num = strtod(inp, NULL); | |
| mtx.data[row][col] = num; | |
| } | |
| } | |
| *(matrices + mtxNumber) = mtx; | |
| printMtx(mtx, mtxNumber); | |
| } | |
| printOpts(); | |
| return 0; | |
| } | |
| void getDimensions(int matNo, int *dimX, int *dimY) { | |
| char input[8]; | |
| bool success = false; | |
| do { | |
| printf("Dimensions for Matrix %d: ", matNo); | |
| fgets(input, sizeof(input), stdin); | |
| for(int i = 0; input[i]; i++){ | |
| input[i] = tolower(input[i]); | |
| } | |
| if(strchr(input, 'x') != NULL) { | |
| const char* delim = "x"; | |
| char *pair0 = strtok(input, delim); | |
| char *pair1 = strtok(NULL, delim); | |
| if(pair0 != NULL && pair1 != NULL) { | |
| strtok(pair1, "\n"); | |
| *dimX = atoi(pair0); | |
| *dimY = atoi(pair1); | |
| if(dimX > 0 && dimY > 0) { | |
| success = true; | |
| } | |
| } | |
| } | |
| } while (!success); | |
| } | |
| bool ValidDimension(const char input[], int *dimX, int *dimY) { | |
| if(strchr(input, 'x') != NULL) { | |
| char * copy = malloc(strlen(input) - 1); | |
| const char* delim = "x"; | |
| strcpy(copy, input); | |
| char *pair0 = strtok(copy, delim); | |
| char *pair1 = strtok(NULL, delim); | |
| if(pair0 != NULL && pair1 != NULL) { | |
| strtok(pair1, "\n"); | |
| *dimX = atoi(pair0); | |
| *dimY = atoi(pair1); | |
| if(dimX > 0 && dimY > 0) { | |
| return true; | |
| } | |
| } | |
| free(copy); | |
| } | |
| *dimX = -1; | |
| *dimY = -1; | |
| return false; | |
| } | |
| void printMtx(matrix mtx, int num) { | |
| printf("Matrix %d (%dx%d)\n", num, mtx.rows, mtx.cols); | |
| for (int row = 0; row < mtx.rows; row++) { | |
| printf("[ "); | |
| for (int col = 0; col < mtx.cols; col++) { | |
| printf("%-2f ", mtx.data[row][col]); | |
| } | |
| printf("]\n"); | |
| } | |
| } | |
| void printOpts() { | |
| printf("\n"); | |
| int op; | |
| do { | |
| printf("1. Print Matrixes\n"); | |
| printf("2. Multiplication (single)\n"); | |
| printf("3. Addition (single)\n"); | |
| if (matrixCount > 1) { | |
| printf("4. Multiplication (M1*M2) [WIP]\n"); | |
| printf("5. Addition (M1+M2)\n"); | |
| } | |
| printf("Choose an operation: "); | |
| char inp[8]; | |
| fgets(inp, sizeof(inp), stdin); | |
| op = atoi(inp); | |
| if(op > 0) { | |
| printf("\n"); | |
| switch(op) { | |
| case 1: | |
| printMtx(*matrices, 1); | |
| break; | |
| case 2: | |
| case 3: | |
| case 4: | |
| case 5: | |
| printf("not implemented.\n"); | |
| op = 0; | |
| } | |
| } | |
| }while(op == 0); | |
| //parse opt | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct { | |
| int rows; | |
| int cols; | |
| double **data; | |
| } matrix; | |
| bool ValidDimension(const char input[], int *dimX, int *dimY); | |
| void strtolower(char* str); | |
| void printMtx(matrix mtx, int num); | |
| void printOpts(); | |
| void getDimensions(int matNo, int *dimX, int *dimY); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment