int main (int argc, char *argv[]) { int code; TParams tp = {.mat1.add = NULL,.mat2.add = NULL }; if ((code = processParams (&tp, argc, argv)) != RET_OK) { errmsg (code); return EXIT_FAILURE; }; switch (tp.function) { case 0: errmsg (RET_OK); break; case 1: code = sucet (&(tp.mat1), &(tp.mat2)); break; case 2: code = sucin (&(tp.mat1), &(tp.mat2)); break; case 3: code = submatrix (&(tp.mat1), &(tp.mat2)); break; case 4: code = crot (&(tp.mat1)); break; case 5: code = plough (&(tp.mat1)); break; case 6: code = sudoku (&(tp.mat1)); break; default: return EXIT_FAILURE; } if (tp.mat1.add != NULL) { freeMat (&(tp.mat1)); } if (tp.mat2.add != NULL) { freeMat (&(tp.mat2)); } if (code != RET_OK) { errmsg (code); return EXIT_FAILURE; } return EXIT_SUCCESS; }
//sucin int sucin (p_TMatrix ptm1, p_TMatrix ptm2) { int code, i = 0, j = 0, k = 0; if (ptm1->m != ptm2->n) return RET_INP; TMatrix tmp; tmp.n = ptm1->n; tmp.m = ptm2->m; if ((code = allocMat (&tmp)) != RET_OK) return code; zeroMat (&tmp); for (i = 0; i < tmp.n; i++) for (j = 0; j < tmp.m; j++) for (k = 0; k < ptm1->m; k++) tmp.add[i][j] += (ptm1->add[i][k] * ptm2->add[k][j]); vypisMat (&tmp); freeMat (&tmp); return RET_OK; }
//krizova rotacia int crot (p_TMatrix ptm1) { int code, i = 0, j = 0; int l; TMatrix tmp1; tmp1.n = ptm1->n; tmp1.m = ptm1->m; //pomocna matica 1 if ((code = allocMat (&tmp1)) != RET_OK) return code; zeroMat (&tmp1); //spravnim indexovanim naplnime pomocnu maticu posunutymi riadkami for (i = 0; i < tmp1.n; i++) { l = ptm1->add[i][0]; l = l % (ptm1->m); for (j = 0; j < ptm1->m; j++) tmp1.add[i][j] = ptm1->add[i][(j - l + ptm1->m) % (ptm1->m)]; } //podobne posunieme stlpce TMatrix tmp2; tmp2.n = ptm1->n; tmp2.m = ptm1->m; if ((code = allocMat (&tmp2)) != RET_OK) return code; zeroMat (&tmp2); for (i = 0; i < tmp1.m; i++) { l = tmp1.add[0][i]; l = l % (ptm1->n); for (j = 0; j < ptm1->n; j++) tmp2.add[j][i] = tmp1.add[(j - l + ptm1->n) % (ptm1->n)][i]; } vypisMat (&tmp2); freeMat (&tmp1); freeMat (&tmp2); return RET_OK; }
int setOrder(matrix *mat) { char param[BUFSIZE + 1]; int order; if (!getNextParam(param)) return 0; /* Parameter auslesen */ if ((order = atol(param)) < 1) return 0; freeMat(mat); mat = allocMat(order, order); /* Neue Matrix mit eingegebenen Rang erstellen */ return 1; }
int allocMat (p_TMatrix ptm) { int i; /*najskor sa pole alokuje NULL mi, ak zlyha alokovanie niektoreho z mozeme to vyuzit na zistenie ci uz bol dany riadok alokovany (nakolko by obsahoval neinicializovanu hodnotu) */ if ((ptm->add = calloc (ptm->n, sizeof (int *))) == NULL) //alokujeme pole ukazatelov return RET_ALK; for (i = 0; i < ptm->n; i++) //alokujeme samotne riadky if ((*(ptm->add + i) = (int *) malloc (ptm->m * sizeof (int))) == NULL) { freeMat (ptm); //ak niekde nastala chyba tak odalokujem co sme naalokovali return RET_ALK; } return RET_OK; }
//sucet matic int sucet (p_TMatrix ptm1, p_TMatrix ptm2) { int code, i = 0, j = 0; if (ptm1->n != ptm2->n || ptm1->m != ptm2->m) return RET_INP; TMatrix tmp; tmp.n = ptm1->n; tmp.m = ptm1->m; if ((code = allocMat (&tmp)) != RET_OK) return code; for (i = 0; i < ptm1->n; i++) for (j = 0; j < ptm1->m; j++) tmp.add[i][j] = ptm1->add[i][j] + ptm2->add[i][j]; vypisMat (&tmp); freeMat (&tmp); return RET_OK; }
int processParams (p_TParams ptp, int count, char **vector) { int code; FILE *fp = NULL; switch (count) { case 1:return RET_ARGS; case 2: if (strcmp (vector[1], "-h") == 0) {ptp->function = 0; return RET_OK;} return RET_ARGS; case 3: if (strcmp (vector[1], "-crot") == 0) ptp->function = 4; else if (strcmp (vector[1], "-plough") == 0) ptp->function = 5; else if (strcmp (vector[1], "-sudoku") == 0) ptp->function = 6; else return RET_ARGS; fp = fopen (vector[2], "rb"); if (fp == NULL) return RET_FILE; if ((code = readandallocMat (fp, &(ptp->mat1))) != RET_OK) { if (ptp->mat1.add != NULL) { freeMat (&(ptp->mat1)); } fclose (fp); return code; } fclose (fp); (ptp->mat2).add = NULL; return RET_OK; case 4: if (strcmp (vector[1], "-add") == 0) ptp->function = 1; else if (strcmp (vector[1], "-mult") == 0) ptp->function = 2; else if (strcmp (vector[1], "-submatrix") == 0) ptp->function = 3; else return RET_ARGS; fp = fopen (vector[2], "rb"); if (fp == NULL) return RET_FILE; if ((code = readandallocMat (fp, &(ptp->mat1))) != RET_OK) { if (ptp->mat1.add != NULL) { freeMat (&(ptp->mat1)); } fclose (fp); return code; } fclose (fp); fp = fopen (vector[3], "rb"); if (fp == NULL) return RET_FILE; if ((code = readandallocMat (fp, &(ptp->mat2))) != RET_OK) { if (ptp->mat2.add != NULL) { freeMat (&(ptp->mat2)); } fclose (fp); return code; } fclose (fp); return RET_OK; default: return RET_ARGS; } return RET_ERR; }
int plough (p_TMatrix ptm1) { int code = 0, i = 0, j = 0, k = 0, l = 0; TMatrix tmp; tmp.n = ptm1->n; tmp.m = ptm1->m; //Alokacia pomocnej matice if ((code = allocMat (&tmp)) != RET_OK) return code; zeroMat (&tmp); code = start; /* v zdrojovej matici sa pohybujeme premennymi k,l v kazdej iteracii zistujeme ci nedoslo k zmene podmienky daneho stavu ak ano prepneme na novy smer */ for (i = 0; i < ptm1->n; i++) for (j = 0; j < ptm1->m; j++) if (!((k == ptm1->n - 1) && (l == ptm1->m - 1))) { switch (code) { case start: tmp.add[i][j] = ptm1->add[k][l]; code = right; break; case right: l++; tmp.add[i][j] = ptm1->add[k][l]; if (k == 0) code = down_left; if (k == ptm1->n - 1) code = up_right; break; case down_left: k++; l--; tmp.add[i][j] = ptm1->add[k][l]; if ((k == ptm1->n - 1)) code = right; else if ((l == 0) && (k != ptm1->n - 1)) code = down; else code = down_left; break; case down: k++; tmp.add[i][j] = ptm1->add[k][l]; if (l == 0) code = up_right; if (l == ptm1->m - 1) code = down_left; break; case up_right: k--; l++; tmp.add[i][j] = ptm1->add[k][l]; if ((l == ptm1->m - 1)) code = down; else if ((k == 0) && (k != ptm1->m - 1)) code = right; else code = up_right; break; } } vypisMat (&tmp); freeMat (&tmp); return RET_OK; }
int main(int argc, char **argv) { GDISPLAY *video, *feedback; GPOINT *gPoints; XEvent event; WSETUP *wayv; WGESTURE *gaction; MATRIX *gesture; VECTOR *vector; int gPosition = 0; char *file; printf("wayV version %s\n", VERSION); /* Decide what configuration file to open */ if(argc != 2) file = strdup(SETUPFILE); else file = strdup(argv[1]); if((wayv = readSetup(file)) == NULL) exit(BAD); // writeSetup(wayv, SETUPOUT); /* Setup the display */ if(!(video = setupDrawable(wayv->pretty, wayv->pretty->window, argc, argv))) { printf("Cannot connected to X server\n"); return BAD; } /* Setup the mouse/pointer as an input device */ setupPointer(wayv, video); /* Allocate memory to store the gesture points */ gPoints = setupGPoints(wayv->universe->maxgpoints); if(wayv->pretty->display[0] == 'i') handleDrawableWindow(video, MAP); setupSignals(); /* Process the events */ while(1) { XNextEvent(video->display, &event); switch(event.type) { case MotionNotify: if(gPosition < wayv->universe->maxgpoints) { gPoints[gPosition].x = event.xmotion.x; gPoints[gPosition].y = event.xmotion.y; if(wayv->pretty->display[0] == 'y') writePixels(video, (int)gPoints[gPosition-1].x - GDEF_BORDER, (int)gPoints[gPosition-1].y - GDEF_BORDER, (int)gPoints[gPosition].x - GDEF_BORDER, (int)gPoints[gPosition].y - GDEF_BORDER); else if(wayv->pretty->display[0] == 'i') writePixels(video, (int)gPoints[gPosition-1].x, (int)gPoints[gPosition-1].y, (int)gPoints[gPosition].x, (int)gPoints[gPosition].y); gPosition++; } break; case ButtonPress: if(wayv->pretty->display[0] == 'y') handleDrawableWindow(video, MAP); gPosition = 0; gPoints[gPosition].x = event.xbutton.x; gPoints[gPosition].y = event.xbutton.y; gPosition++; break; case ButtonRelease: if(wayv->pretty->display[0] == 'y') handleDrawableWindow(video, UNMAP); gPoints[gPosition].x = GEND; gPoints[gPosition].y = GEND; gesture = gridGPoints(gPoints, wayv->universe->xgrid, wayv->universe->ygrid); vector = vectGPoints(gPoints, wayv->universe->xgrid, wayv->universe->ygrid, wayv->universe->maxvectors); printf("\n\n=======Gesture======="); printMat(gesture); printVec(vector); if(wayv->pretty->display[0] != 'n') handleDrawableWindow(video, CLEAR); if((gaction = findGesture(wayv, gesture, vector, wayv->think))) { printf("Is Action : %s\n", gaction->name); performAction(wayv, gaction, video, gPoints); /* Fork off program to handle feedback window */ if(wayv->pretty->feedback[0] == 'y' && fork() == 0) { feedback = writeText(gaction->action); waitMilli(wayv->pretty->wait); handleDrawableWindow(feedback, DESTROY); close(ConnectionNumber(video->display)); setsid(); exit(BAD); } } freeMat(gesture); free(vector); break; case ClientMessage: if(event.xclient.data.l[0] == video->wmdelete) { closeDisplay(video); exit(GOOD); } break; } } }