Advertisement
hinagawa

Умножение-сумма матриц

Dec 2nd, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1.  
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <fstream>
  5. #include<iomanip>
  6. using namespace std;
  7.  
  8. int**create(ifstream &f, int n, int m)
  9. {
  10. int i, j;
  11.  
  12. int**a = new int *[n];
  13. for (i = 0; i < m; i++)
  14. a[i] = new int[n];
  15. for (i = 0; i < n; i++)
  16. {
  17. for (j = 0; j < m; j++)
  18. f >> a[i][j];
  19. }
  20. return a;
  21. }
  22. void show(ofstream &f, int**x, int n, int m)
  23. {
  24. int i, j;
  25. for (i = 0; i < n; i++)
  26. {
  27. for (j = 0; j < m; j++)
  28. f << setw(5) << x[i][j];
  29. f << endl;
  30. }
  31. }
  32. void sum(int**x, int**y, int**z, int n, int m)
  33. {
  34.  
  35. int i, j;
  36. for (i = 0; i < n; i++)
  37. for (j = 0; j < m; j++)
  38. z[i][j] = x[i][j] + y[i][j];
  39.  
  40. }
  41. void difference(int**x, int**y, int**z, int n, int m)
  42. {
  43. int i, j;
  44. for (i = 0; i < n; i++)
  45. for (j = 0; j < m; j++)
  46. z[i][j] = x[i][j] + (-1)*y[i][j];
  47.  
  48. }
  49. void mult(ofstream &f, int**x, int**y, int n, int m, int n1, int m1)
  50.  
  51. {
  52. if (m != n1)
  53. cout << "Can't find mult!";
  54. else
  55. {
  56. int i, j, **z;
  57. z = new int*[n];
  58. for (i = 0; i < m1; i++)
  59. z[i] = new int[m1];
  60. for (i = 0; i < n; i++)
  61. {
  62. for (j = 0; j < m1; j++)
  63. {
  64. z[i][j] = 0;
  65. for (int k = 0; k < m; k++)
  66. z[i][j] += x[i][k] * y[k][j];
  67. f << z[i][j] << " ";
  68.  
  69. }
  70. f << endl;
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. }
  83.  
  84. }
  85.  
  86. void kmult(ofstream &f, int**x, int n, int m)
  87. {
  88. int i, j, k;
  89. cout << "Vvedite k=";
  90. cin >> k;
  91. for (i = 0; i < n; i++)
  92. {
  93. for (j = 0; j < m; j++)
  94. {
  95. x[i][j] *= k;
  96. f << x[i][j] << " ";
  97. }
  98.  
  99. f << endl;
  100. }
  101.  
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107. ifstream matr("matrica.txt");
  108. ofstream r("rezultat.txt");
  109. if (!matr)
  110. {
  111. cout << "Can't open matrica.txt";
  112. exit(-1);
  113. }
  114.  
  115. int n, m, n1, m1;
  116. cout << "Vvedite n=";
  117. cin >> n;
  118. cout << "Vvedite m=";
  119. cin >> m;
  120. cout << "VVedite n1=";
  121. cin >> n1;
  122. cout << "Vvedite m1=";
  123. cin >> m1;
  124. int**a, **b, **c;
  125. a = create(matr, n, m);
  126. b = create(matr, n1, m1);
  127. c = new int*[n];
  128. int i;
  129. for (i = 0; i < m; i++)
  130. c[i] = new int[n];
  131. matr.close();
  132. r << "Mult a*b=" << endl;
  133. mult(r, a, b, n, m, n1, m1);
  134. r << "Kmult k*a=" << endl;
  135. kmult(r, a, n, m);
  136. r << "Kmult k*b=" << endl;
  137. kmult(r, b, n1, m1);
  138. r << endl;
  139. if (n == n1 && m == m1)
  140. {sum(a, b, c, n, m);
  141. r << "Summa a+b=" << endl;
  142. show(r, c, n, m);
  143. difference(a, b, c, n, m);
  144. r << "Difference a-b=" << endl;
  145. show(r, c, n, m);
  146. r.close();
  147. }
  148. else
  149. cout << "Can't find sum and difference";
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement