Created
November 30, 2012 22:11
-
-
Save ernado/4179070 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iomanip> | |
#include <stdlib.h> | |
#include <iostream> | |
#include <stdio.h> | |
#include <conio.h> | |
using namespace std; | |
// ширина консоли | |
const int MAXLEN = 80; | |
// создает матрицу | |
double** sozdatMtr(int &m, int&n) | |
{ | |
// массив из строк матрицы | |
double** matrix = new double*[m]; | |
// в цикле инициализируем строки матрицы как столбцы | |
for (int i = 0; i < m; i ++) | |
{ | |
matrix[i] = new double[n]; | |
for (int k = 0; k < n; k++) | |
matrix[i][k] = 0; | |
} | |
return matrix; | |
} | |
// создает квадратную матрицу, заполненную нулями | |
double** sozdatMtr(int &n) | |
{ | |
return sozdatMtr(n,n); | |
} | |
// заполняет матрицу по формуле один | |
void zapolnF1(double** &matrix, const int n) | |
{ | |
double x, d, h = 1; | |
for (int j = 0; j < n; j++) | |
{ | |
h *= (j+1); d = h; x = -1; | |
for (int i = 0; i < n; i ++) | |
{ | |
if (i == j) // обработка элементов главной диагонали | |
matrix[i][j] = 1; | |
else if (i < j) // обработка элементов выше главной диагонали | |
matrix[i][j] = 1/d; | |
else // обработка элементов ниже главной диагонали | |
matrix[i][j] = x/d; | |
d *= h; x *= -1; | |
} | |
} | |
} | |
// печатает матрицу MxN | |
void pechMtr(double** matrix, const int m, const int n, const int toch) | |
{ | |
// 0.(toch)e+000_ | |
int width = toch + 9; | |
// сколько влезет в ширину максимум столбцов ширины width | |
int max_vlezet = MAXLEN / width; | |
cout << "Влезет: " << max_vlezet << endl; | |
// число групп | |
int grupp = n / max_vlezet; | |
if (n % max_vlezet != 0) | |
grupp++; | |
// устанавливаем точность | |
// можно как обычно - setprecision(toch); | |
cout.precision(toch); | |
cout << "Матрица " << m <<"x"<< n << endl; | |
cout << "Групп: " << grupp << endl; | |
int start,end; | |
for (int g = 0; g < grupp; g++) | |
{ | |
start = g * max_vlezet; // первый столбик в группе | |
end = start + max_vlezet; // а тут последний | |
// start start+2 start+3 ... end-1 | |
// N N N ... N | |
// N N N ... N | |
// конечный столбец не может быть | |
if (end > n) | |
end = n; | |
// печать номеров столцов | |
for (int j = start; j < end; j++) | |
cout << setw(width) << j; | |
cout << endl; | |
// i - строка | |
for (int i = 0; i < m; i++) | |
{ | |
// j - столбец | |
for (int j = start; j < end; j++) | |
{ | |
// [M]ij | |
cout << setw(width) << scientific << matrix[i][j]; | |
} | |
cout << endl; | |
} | |
cout << endl; | |
} | |
} | |
// печатает квадратную матрицу | |
void pechMtr(double** matrix, const int n, const int toch) | |
{ | |
pechMtr(matrix, n, n, toch); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
setlocale(0,"ru"); | |
double** matrix; | |
int n = 5, toch = 3; char c; | |
while(1) | |
{ | |
// обработка первой матрицы | |
cout << "Размер первой матрицы: "; cin >> n; | |
cout << "Точность вывода: "; cin >> toch; | |
double** matrix = sozdatMtr(n); | |
zapolnF1(matrix,n); | |
pechMtr(matrix, n, toch); | |
delete[] matrix; | |
//cout << "Нажмите любую клавишу для отображения второй матрицы" << endl; | |
//getch(); | |
// Обработка второй матрицы | |
cout << "Вторая матрица" << endl; | |
const int N = 10; | |
double cmatrix[N][N]; | |
for (int i = 0; i < N; i++) | |
for (int j = 0; j < N; j++) | |
cmatrix[i][j] = (i+1)*10+(j+1); | |
// создание вспомогательного массива строок | |
matrix = new double*[N]; | |
for (int i = 0; i < N; i++) | |
matrix[i] = cmatrix[i]; | |
pechMtr(matrix, N, toch); | |
cout << "Повторить? (y/n) "; c = getche(); cout << endl; | |
if (c != 'y') | |
{ | |
cout << "Вторая часть задания: " << endl; | |
cout<<cmatrix<<" "<<cmatrix[0]<<" "<<cmatrix[2]<<endl; | |
cout<<cmatrix[0][0]<<" "<<**cmatrix<<" "<<*cmatrix[0]<<endl; | |
cout<<*(*(cmatrix+1))<<" "<<*cmatrix[1]<<endl; | |
cout<<*(cmatrix[0]+1)<<" " <<*(*cmatrix+1)<<endl; | |
cout<<cmatrix[0][20]<<" "<<*(cmatrix[0]+20)<<" "<<*cmatrix[2]<<endl; | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment