// soustava.cpp 
 
#include "stdafx.h"
 
#include <iostream>
#include <string>
using namespace std;
 
const int R = 3;
const int S = 3;
 
double a[R][S] = { { 0, 1, 0 },
                   { 1, 0, 2 },
                   { 1, 1, 1 } };
 
double b[R]   = { 1,
                  0,
                  0 };
 
string jmena[S] = { "x", "y", "z" /*, "a", "b", "c" */ };
 
void print ()
{
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < S; j++)
        {
            if (a[i][j] >= 0 && j > 0) cout << "+";
            cout << a[i][j] << "*" << jmena[j];
        }
        cout << " = " << b[i] << endl;
    }
    cout << endl;
}
 
const double eps = 1e-9;
 
void inline swap (double & a, double & b)
{
    double c = a;
    a = b;
    b = a;
}
 
void uprav (int r, int s)
{
    if (fabs (a[r][s]) < eps)
    {
        int t = 0; // cislo radky s nejvetsi |a[*][s]|
        for (int i = 0; i < R; i++)
            if (fabs (a[t][s]) < fabs (a[i][s]))
                t = i;
        // prohodime radky cislo r a t
        for (int j = 0; j < S; j++)
            swap (a[r][j], a[t][j]);
 
    }
 
 
    double q = a[r][s];
    // r-tou radku vydelim q
    // a[r][s] bude 1
    for (int j = 0; j < S; j++)
    {
        a[r][j] = a[r][j] / q;
    }
    b[r] = b[r] / q;
    // print ();
    for (int i = 0; i < R; i++)
    {
        if (i != r)
        {
            double p = a[i][s]; 
            // od i-te radky odectu p * r-tou radku 
            // a[i][s] bude 0
            for (int j = 0; j < S; j++)
            {
                a[i][j] = a[i][j] - p*a[r][j];
            }
            b[i] = b[i] - p*b[r];
        }
    }
}
 
 
double v[S] = { 10, 5, 15 };
 
void priprav ()
{
    for (int i = 0; i < R; i++)
    {
        double s = 0;
        for (int j = 0; j < S; j++)
            s = s + a[i][j] * v[j];
        b[i] = s;
    }
}
 
int main()
{
    priprav ();
    print ();
    for (int k = 0; k < R; k++)
    {
        uprav (k, k);
        print ();
    }
    system ("pause");
    return 0;
}
 
soustava2.txt · Last modified: 2017/12/19 13:19 by 147.32.8.110
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki