Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct MiniMatrix {
- int cols, rows;
- int vals[8][8];
- };
- int parsematrix(struct MiniMatrix* dest, char* filename) {
- int i, j;
- char end;
- FILE* in = fopen(filename, "r");
- if(in == NULL) {
- return 1;
- }
- fscanf(in, "%d,%d\n", &dest->cols, &dest->rows);
- for(i = 0; i < dest->rows; i++) {
- for(j = 0; j < dest->cols; j++) {
- fscanf(in, "%d%c", &dest->vals[i][j], &end);
- }
- }
- fclose(in);
- return 0;
- }
- int outputmatrix(struct MiniMatrix* src, char* filename) {
- int i, j;
- FILE* out = fopen(filename, "w");
- if(out == NULL) {
- return 1;
- }
- fprintf(out, "%d,%d\n", src->cols, src->rows);
- for(i = 0; i < src->rows; i++) {
- for(j = 0; j < src->cols; j++) {
- fprintf(out, "%d", src->vals[i][j]);
- if(j < src->cols-1) {
- fprintf(out, ",");
- }
- }
- fprintf(out, "\n");
- }
- fclose(out);
- return 0;
- }
- int multmatrix(struct MiniMatrix* c, struct MiniMatrix* a, struct MiniMatrix* b) {
- int i, j, k;
- if(a->cols != b->rows) {
- return 1;
- }
- c->cols = b->cols;
- c->rows = a->rows;
- for(i = 0; i < a->rows; i++) {
- for(j = 0; j < b->cols; j++) {
- c->vals[i][j] = 0;
- for(k = 0; k < a->cols; k++) {
- c->vals[i][j] += (int)((((long long)a->vals[i][k])*((long long)b->vals[k][j]))%10000000LL);
- }
- }
- }
- return 0;
- }
- int main() {
- struct MiniMatrix res1, res2, res3;
- parsematrix(&res1, "sunday.txt");
- parsematrix(&res3, "saturday.txt");
- multmatrix(&res2, &res1, &res3);
- parsematrix(&res3, "friday.txt");
- multmatrix(&res1, &res2, &res3);
- parsematrix(&res3, "thursday.txt");
- multmatrix(&res2, &res1, &res3);
- parsematrix(&res3, "wednesday.txt");
- multmatrix(&res1, &res2, &res3);
- parsematrix(&res3, "tuesday.txt");
- multmatrix(&res2, &res1, &res3);
- parsematrix(&res3, "monday.txt");
- multmatrix(&res1, &res2, &res3);
- multmatrix(&res2, &res1, &res1);
- multmatrix(&res3, &res2, &res2);
- multmatrix(&res2, &res3, &res3);
- multmatrix(&res3, &res2, &res2);
- multmatrix(&res2, &res3, &res1);
- parsematrix(&res3, "finalize.txt");
- multmatrix(&res1, &res2, &res3);
- outputmatrix(&res1, "result.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement