Advertisement
steverobinson

Steve Robinson

Jan 11th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct MiniMatrix {
  5.     int cols, rows;
  6.     int vals[8][8];
  7. };
  8.  
  9. int parsematrix(struct MiniMatrix* dest, char* filename) {
  10.     int i, j;
  11.     char end;
  12.     FILE* in = fopen(filename, "r");
  13.  
  14.     if(in == NULL) {
  15.         return 1;
  16.     }
  17.  
  18.     fscanf(in, "%d,%d\n", &dest->cols, &dest->rows);
  19.  
  20.     for(i = 0; i < dest->rows; i++) {
  21.         for(j = 0; j < dest->cols; j++) {
  22.             fscanf(in, "%d%c", &dest->vals[i][j], &end);
  23.         }
  24.     }
  25.     fclose(in);
  26.  
  27.     return 0;
  28. }
  29.  
  30. int outputmatrix(struct MiniMatrix* src, char* filename) {
  31.     int i, j;
  32.     FILE* out = fopen(filename, "w");
  33.  
  34.     if(out == NULL) {
  35.         return 1;
  36.     }
  37.  
  38.     fprintf(out, "%d,%d\n", src->cols, src->rows);
  39.  
  40.     for(i = 0; i < src->rows; i++) {
  41.         for(j = 0; j < src->cols; j++) {
  42.             fprintf(out, "%d", src->vals[i][j]);
  43.  
  44.             if(j < src->cols-1) {
  45.                 fprintf(out, ",");
  46.             }
  47.         }
  48.         fprintf(out, "\n");
  49.     }
  50.  
  51.     fclose(out);
  52.  
  53.     return 0;
  54. }
  55.  
  56. int multmatrix(struct MiniMatrix* c, struct MiniMatrix* a, struct MiniMatrix* b) {
  57.     int i, j, k;
  58.  
  59.     if(a->cols != b->rows) {
  60.         return 1;
  61.     }
  62.  
  63.     c->cols = b->cols;
  64.     c->rows = a->rows;
  65.  
  66.     for(i = 0; i < a->rows; i++) {
  67.         for(j = 0; j < b->cols; j++) {
  68.             c->vals[i][j] = 0;
  69.  
  70.             for(k = 0; k < a->cols; k++) {
  71.                 c->vals[i][j] += (int)((((long long)a->vals[i][k])*((long long)b->vals[k][j]))%10000000LL);
  72.             }
  73.         }
  74.     }
  75.  
  76.     return 0;
  77. }
  78.  
  79. int main() {
  80.     struct MiniMatrix res1, res2, res3;
  81.  
  82.     parsematrix(&res1, "sunday.txt");
  83.     parsematrix(&res3, "saturday.txt");
  84.     multmatrix(&res2, &res1, &res3);
  85.     parsematrix(&res3, "friday.txt");
  86.     multmatrix(&res1, &res2, &res3);
  87.     parsematrix(&res3, "thursday.txt");
  88.     multmatrix(&res2, &res1, &res3);
  89.     parsematrix(&res3, "wednesday.txt");
  90.     multmatrix(&res1, &res2, &res3);
  91.     parsematrix(&res3, "tuesday.txt");
  92.     multmatrix(&res2, &res1, &res3);
  93.     parsematrix(&res3, "monday.txt");
  94.     multmatrix(&res1, &res2, &res3);
  95.  
  96.     multmatrix(&res2, &res1, &res1);
  97.     multmatrix(&res3, &res2, &res2);
  98.     multmatrix(&res2, &res3, &res3);
  99.     multmatrix(&res3, &res2, &res2);
  100.     multmatrix(&res2, &res3, &res1);
  101.  
  102.     parsematrix(&res3, "finalize.txt");
  103.     multmatrix(&res1, &res2, &res3);
  104.  
  105.     outputmatrix(&res1, "result.txt");
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement