#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
 
const int M = 3; // pocet radku
const int N = 4; // pocet sloupcu
typedef double T; // typ T = double
 
void clear(T p[M][N])
{
    for (int i = 0; i < M; i++) 
        for (int j = 0; j < N; j++)
            p[i][j]  = 0; 
}
 
void print(ostream& f, T p[M][N])
{
    for (int i = 0; i < M; i++) // cyklus pro radky
    {
        for (int j = 0; j < N; j++)  // cyklus pro sloupce
        {
            f << p[i][j] << " "; // vytiskni jedno cislo
        }
        f << endl; // konec radky
    }
    f << endl; // a jedna prazdna radka
}
 
void read (T p[M][N], string fileName)
{
    ifstream h(fileName);
 
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            h >> p[i][j];
 
    h.close();
}
 
void pricti(T p[M][N], int target, int src, T koef = 1)
// pricti koef * radku s indexem src k radce s indexem target
{
    for (int j = 0; j < N; j++)
        p[target][j] = p[target][j] + koef * p[src][j];
        // p[target][j] += koef * p[src][j];
}
 
void vydel(T p[M][N], int target, T koef)
// vydel radku s indexem target cislem koef
{
    for (int j = 0; j < N; j++)
        p[target][j] = p[target][j] / koef ;
    // p[target][j] /= koef ;
}
 
void uprav(T p[M][N])
{   
    for (int i = 0; i < M; i++)
    {
        vydel(p, i, p[i][i]); // doufam, ze a[i][i] != 0
        for (int k = 0; k < M; k++)
        {
            if (k != i) // pro radky jine nez radka i
            {
                pricti(p, k, i, -p[k][i]);
            }
        }
        print(cout, p);
    }
}
 
void priprav (T p[M][N], T r[N-1])
// priprav pravou stranu matice p, aby resenim byla cisla z pole r
{
    for (int i = 0; i < M; i++)
    {
        T v = 0;
        for (int j = 0; j < N - 1; j++) // vynecham pravou stranu
        {
            v = v + p[i][j] * r[j];
        }
        p[i][N - 1] = v; // ulozim pravou stranu
    }
}
 
 
T a [M][N];
 
int main()
{
    clear (a);
    read (a, "input.txt");
    print(cout, a);
    T reseni[N - 1] = { 3, 4, 5 };
    priprav(a, reseni);
    print(cout, a);
    uprav (a);
 
    ofstream f ("abc.txt");
    print (f, a);
    f.close();
 
    cout << "Data ze souboru abc.txt" << endl;
    ifstream g ("abc.txt");
    while (g.good())
    {
        string t;
        getline (g, t);
        if (g.good())
        {
            cout << t << endl;
        }
    }
    g.close ();
 
    cout << "O.K.";
}
 
pole2019d.txt · Last modified: 2019/10/17 15:21 by 147.32.8.115
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki