fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <string>
  7. #include <sstream>
  8. #include <algorithm>
  9. #include <iterator>
  10. #include <cstdlib>
  11.  
  12. using namespace std;
  13.  
  14. string str1,str2;
  15. string x;
  16. string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
  17. string s2 = "[3 4.2 10.2;12 -1 0;67 2 13]";
  18. string ns ;
  19. const int n=3; const int m=3;
  20. float matrix1 [n][m];
  21. float matrix2 [n][m];
  22. float matrixsum [n][m];
  23.  
  24.  
  25.  
  26.  
  27. string cutter(string &s) {
  28. s.erase(remove(s.begin(), s.end(), '['), s.end());
  29. s.erase(remove(s.begin(), s.end(), ']'), s.end());
  30. replace(s.begin(), s.end(), ';', ' ');
  31. ns = s;
  32. return s;
  33. }
  34.  
  35. void splitter(string x,float matrix[n][m]){
  36.  
  37.  
  38. stringstream os(x);
  39. string temp;
  40.  
  41.  
  42. for (int i=0;i<n;i++){
  43.  
  44. for (int j=0;j<m;j++){
  45.  
  46. if(os >> temp){
  47.  
  48. matrix [i][j] = atof(temp.c_str()) ;
  49.  
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. }
  57.  
  58. void showmatrix(float M[n][m]){
  59. cout <<"[" ;
  60. for (int i=0;i<n;i++){
  61.  
  62. for (int j=0;j<m;j++){
  63.  
  64. cout << M[i][j];
  65. if(j != m-1){
  66. cout<< " " ;
  67. }
  68. }
  69. if(i != n-1){
  70. cout << ";";
  71. }
  72.  
  73. }
  74.  
  75. cout << "]";
  76. }
  77.  
  78. float summing (float m1[n][m], float m2[n][m],float ms[n][m]){
  79.  
  80.  
  81. for (int i=0;i<n;i++){
  82.  
  83. for (int j=0;j<m;j++){
  84.  
  85. ms[i][j] = m1[i][j] + m2[i][j] ;
  86. }
  87.  
  88. }
  89.  
  90. return 0 ;
  91. }
  92.  
  93. float mult (float m1[n][m], float m2[n][m],float ms[n][m]){
  94.  
  95.  
  96. for (int i=0;i<n;i++){
  97.  
  98. for (int j=0;j<m;j++){
  99.  
  100. ms[i][j] = m1[i][j] * m2[i][j] ;
  101. }
  102.  
  103. }
  104.  
  105. return 0 ;
  106. }
  107.  
  108. float subbing (float m1[n][m], float m2[n][m],float ms[n][m]){
  109.  
  110.  
  111. for (int i=0;i<n;i++){
  112.  
  113. for (int j=0;j<m;j++){
  114.  
  115. ms[i][j] = m1[i][j] - m2[i][j] ;
  116. }
  117.  
  118. }
  119.  
  120. return 0 ;
  121. }
  122.  
  123. void functionchoosing (){
  124.  
  125. getline(cin,str1);
  126. getline(cin,x);
  127. if (x == "+" ){
  128. getline(cin,str2);
  129. cutter(str1);
  130. splitter(str1,matrix1);
  131. cutter(str2);
  132. splitter(str2,matrix2);
  133. summing(matrix1,matrix2,matrixsum);
  134. showmatrix(matrixsum);
  135.  
  136. }
  137. else if(x == "-"){
  138.  
  139. getline(cin,str2);
  140. cutter(str1);
  141. splitter(str1,matrix1);
  142. cutter(str2);
  143. splitter(str2,matrix2);
  144. subbing(matrix1,matrix2,matrixsum);
  145. showmatrix(matrixsum);
  146.  
  147. }
  148. else if(x== "*"){
  149.  
  150. getline(cin,str2);
  151. cutter(str1);
  152. splitter(str1,matrix1);
  153. cutter(str2);
  154. splitter(str2,matrix2);
  155. mult(matrix1,matrix2,matrixsum);
  156. showmatrix(matrixsum);
  157.  
  158. }
  159.  
  160.  
  161. }
  162.  
  163.  
  164.  
  165. int main()
  166. {
  167. functionchoosing();
  168.  
  169.  
  170.  
  171. return 0;
  172. }
  173.  
Success #stdin #stdout 0s 15240KB
stdin
[1 -2.5 3;4 5.25 6;7 8 9.12]
+
[3 4.2 10.2;12 -1 0;67 2 13]
stdout
[4 1.7 13.2;16 4.25 6;74 10 22.12]