fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. #define MAXNOP 50 /*Max number of operations allowed */
  8. #define MAXNMATR 20 /*Max number of matrices */
  9.  
  10. struct m{
  11. size_t row;
  12. size_t col;
  13. double *data;
  14. };
  15.  
  16. struct m multiply(struct m *A, struct m *B);
  17. void f(double x);
  18. void print_matrix(struct m *A);
  19. void read_file(int maxc, FILE *fp);
  20. void scalar_product(double scalar, struct m *B);
  21. void calculate(struct m *matrix, int nop, int id, char *op);
  22.  
  23. int main(int argc, char *argv[]){
  24.  
  25. FILE *file = argc > 1 ? fopen(argv[1], "rb") : stdin;
  26.  
  27. /*define max dimension of a matrix */
  28. int maxc = argc>2?atoi(argv[2])*atoi(argv[2]):100;
  29. read_file(maxc, file);
  30.  
  31. return 0;
  32. }
  33.  
  34. void read_file(int maxc, FILE *fp)
  35. {
  36. struct m *matrix;
  37. int id = 0; /* id of a matrix */
  38. size_t ncol,nrow; /* No of columns of a matrix*/
  39. ncol = nrow = 0;
  40. int nop = 0; /*No of operators*/
  41. int off = 0;
  42. int i;
  43. int n;
  44. double *d;
  45. char buf[2*maxc]; /*to store each lines of file */
  46. char *p = buf;
  47. char op[MAXNOP];
  48.  
  49. for (i=0; i < MAXNOP; i++)
  50. op[i]='?';
  51.  
  52. if (!(matrix = malloc(maxc*sizeof *matrix))) {
  53. perror ("malloc-matrix");
  54. exit(1);
  55. }
  56.  
  57. /*Read file line by line */
  58. while (fgets (buf, maxc, fp)){
  59.  
  60. if (nrow == 0){
  61. /* allocate/validate max no. of matrix */
  62. d = matrix[id].data = malloc(sizeof(double) * MAXNMATR);
  63. }
  64.  
  65.  
  66. /*check if line contains operator */
  67. if ( (!isdigit(*buf) && buf[1] =='\n'))
  68. {
  69. op[nop++] = *buf;
  70. matrix[id].col = ncol;
  71. matrix[id].row = nrow;
  72. nrow = ncol = 0;
  73. id++;
  74. continue;
  75. }
  76.  
  77. else /* read integers in a line into d */
  78. {
  79. while (sscanf (p + off, "%lf%n", d, &n) == 1) {
  80. d++;
  81. if(nrow == 0)
  82. ncol++;
  83. off += n;
  84. }
  85. nrow++;
  86. off = 0;
  87. }
  88. } /*end of while fgets cycle */
  89.  
  90. /*Assign last matrix No of columns and rows */
  91. matrix[id].col = ncol;
  92. matrix[id].row = nrow;
  93.  
  94. /*Printing the matrices and operations */
  95. for(i=0; i <= id; i++){
  96.  
  97. if (op[i] == '*' || op[i] == '-' || op[i] =='+')
  98. {
  99. print_matrix(&matrix[i]);
  100. if(op[i-1] != 'i')
  101. printf("%c\n", op[i]);
  102. else
  103. continue;
  104. }
  105.  
  106. else if(op[i] == '?'){
  107. print_matrix(&matrix[i]);
  108. }
  109. }
  110.  
  111. calculate(matrix, nop, id, op);
  112. }
  113.  
  114. void calculate(struct m *matrix, int nop, int id, char *op)
  115. {
  116. int i;
  117.  
  118. for(i=0; i <= nop; i+=2)
  119. {
  120.  
  121. if(op[i] == '*' && op[i+1] == '?'){
  122. if (matrix[i].row == 1 && matrix[i].col == 1)
  123. scalar_product(matrix[i].data[0], &matrix[i+1]); //Multiplication of Scalar per matrix
  124. else{
  125. matrix[i+1] = multiply(&matrix[i],&matrix[i+1]);
  126. matrix[i+2] = multiply(&matrix[i+1],&matrix[i+2]);
  127. }
  128. break;
  129. }
  130. }
  131.  
  132. printf("=\n");
  133. print_matrix(&matrix[id]); /*Print the result */
  134. free(matrix);
  135. }
  136.  
  137. struct m multiply(struct m *A, struct m *B)
  138. {
  139. size_t i, j, k;
  140. struct m C;
  141. C.data = malloc(sizeof(double) * A->row * B->col);
  142.  
  143. C.row = A->row;
  144. C.col = B->col;
  145.  
  146. for (i=0; i< C.row; i++)
  147. for (j=0; j < C.col; j++)
  148. C.data[i * C.col + j] = 0;
  149.  
  150. // Multiplying matrix A and B and storing in C.
  151. for(i = 0; i < A->row; ++i)
  152. for(j = 0; j < B->col; ++j)
  153. for(k=0; k < A->col; ++k)
  154. C.data[i * C.col + j] += A->data[i * A->col + k] * B->data[k * B->col + j];
  155.  
  156. return C;
  157. }
  158.  
  159. void f(double x)
  160. {
  161. double i,f= modf(x,&i);
  162.  
  163. if(f<.00001)
  164. printf("%.f ",i);
  165.  
  166. else printf("%f ",x);
  167. }
  168.  
  169. /*printing a Matrix*/
  170.  
  171. void print_matrix(struct m *A){
  172.  
  173. size_t i,j;
  174.  
  175. double *tmp = A->data;
  176.  
  177. for(i=0; i < A->row; i++){
  178. for(j=0; j < A->col; j++){
  179. f(*(tmp++));
  180. }
  181. putchar('\n');
  182. }
  183. }
  184.  
  185. void scalar_product(double scalar, struct m *B)
  186. {
  187. size_t i,j;
  188.  
  189. for(i=0; i < B->row; i++)
  190. for(j=0; j < B->col; j++)
  191. B->data[i * B->col + j] = scalar * B->data[i * B->col + j];
  192. }
  193.  
Success #stdin #stdout 0s 9424KB
stdin
1 2
2 3
*
-4 1
1 0
stdout
1 2 
2 3 
*
-4 1 
1 0 
=
-2 1 
-5 2