#include <iostream>
#include <fstream>
using namespace std;
//C=A*42+B
void matrix( int n, int m, double * Pt)
{
for ( int i = 0 ; i< n; i++ )
for ( int j = 0 ; j< m; j++ )
{
cout << "Matrix[" << i << "][" << j << "]=" ;
cin >> Pt[ i* m + j] ;
}
}
void display( int n, int m, double * Pt)
{
for ( int i = 0 ; i< n; cout << endl, i++ )
for ( int j = 0 ; j< m; j++ )
cout << Pt[ i* m + j] << "\t " ;
}
void display_file( int n, int m, double * Pt, ofstream & wStream)
{
for ( int i = 0 ; i< n; wStream << endl, i++ )
for ( int j = 0 ; j< m; j++ )
wStream << Pt[ i* m + j] << "\t " ;
wStream << endl;
}
void matrix_multi( int n, int m, double * Pt, double numb)
{
for ( int i = 0 ; i < n; i++ )
for ( int j = 0 ; j < m; j++ )
Pt[ i* m + j] = Pt[ i* m + j] * numb;
}
void matrix_summ( int n, int m, double * Pt1, double * Pt2, double * Pt3)
{
for ( int i = 0 ; i< n; i++ )
for ( int j = 0 ; j< m; j++ )
Pt3[ i* m + j] = Pt1[ i* m + j] + Pt2[ i* m + j] ;
}
int main( )
{
setlocale( LC_ALL, "rus" ) ;
int n = 0 ;
int m = 0 ;
ifstream rStream;
rStream.open ( "file.txt" , ios_base:: in ) ;
double * dblPtA = new double [ n* m] ;
double * dblPtB = new double [ n* m] ;
double * dblPtC = new double [ n* m] ;
if ( ! rStream)
{
rStream.close ( ) ;
ofstream wStream; //поток для записи
//открываем файл для записи(файл автоматически обнуляется)
wStream.open ( "file.txt" , ios_base:: out ) ;
cout << "Введите размерность матриц\n " ;
cout << "Введите N\n " ;
cin >> n;
wStream << n << " " ;
cout << "Введите M\n " ;
cin >> m;
wStream << m << " " << endl;
cout << "Введите матрицу A:\n " ;
matrix( n, m, dblPtA) ;
display_file( n, m, dblPtA, wStream) ;
cout << "Введите матрицу B:\n " ;
matrix( n, m, dblPtB) ;
display_file( n, m, dblPtB, wStream) ;
int numb = 0 ;
cout << "Введите число на которое будем множить матрицу A\n " ;
cin >> numb;
matrix_multi( n, m, dblPtA, numb) ;
cout << "C = A*numb+B\n Matrix C:\n " ;
matrix_summ( n, m, dblPtA, dblPtB, dblPtC) ;
display( n, m, dblPtC) ;
display_file( n, m, dblPtC, wStream) ;
wStream.close ( ) ;
delete [ ] dblPtA;
delete [ ] dblPtB;
delete [ ] dblPtC;
}
system ( "pause" ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAovL0M9QSo0MitCCnZvaWQgbWF0cml4KGludCBuLCBpbnQgbSwgZG91YmxlICpQdCkKewoJZm9yIChpbnQgaSA9IDA7IGk8bjsgaSsrKQoJCWZvciAoaW50IGogPSAwOyBqPG07IGorKykKCQl7CgkJCWNvdXQgPDwgIk1hdHJpeFsiIDw8IGkgPDwgIl1bIiA8PCBqIDw8ICJdPSI7CgkJCWNpbiA+PiBQdFtpKm0gKyBqXTsKCQl9Cn0KIAogCnZvaWQgZGlzcGxheShpbnQgbiwgaW50IG0sIGRvdWJsZSAqUHQpCnsKCWZvciAoaW50IGkgPSAwOyBpPG47IGNvdXQgPDwgZW5kbCwgaSsrKQoJCWZvciAoaW50IGogPSAwOyBqPG07IGorKykKCQkJY291dCA8PCBQdFtpKm0gKyBqXSA8PCAiXHQiOwp9CiAKdm9pZCBkaXNwbGF5X2ZpbGUoaW50IG4sIGludCBtLCBkb3VibGUgKlB0LCBvZnN0cmVhbSAmd1N0cmVhbSkKewoJZm9yIChpbnQgaSA9IDA7IGk8bjsgd1N0cmVhbSA8PCBlbmRsLCBpKyspCgkJZm9yIChpbnQgaiA9IDA7IGo8bTsgaisrKQoJCQl3U3RyZWFtIDw8IFB0W2kqbSArIGpdIDw8ICJcdCI7Cgl3U3RyZWFtIDw8IGVuZGw7Cn0KIAp2b2lkIG1hdHJpeF9tdWx0aShpbnQgbiwgaW50IG0sIGRvdWJsZSAqUHQsIGRvdWJsZSBudW1iKQp7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