Linear equations

/* 
 * File:   linrce.cpp
 * Author: borovec
 *
 * Created on 18. listopad 2008, 10:26
 */
 
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
 
using namespace std;
 
bool readLine(string line, double * array, const int cols ){
 
    int i = 0;
    double a;
 
    istringstream iss (line);
 
    while(1) {
 
        if(i >= cols) return(false);
 
        iss >> a;
        if(iss.fail())  return (false);
 
        array[i]=a;
 
        if(iss.eof()){
            if(i==(cols-1)) return (true);
            return (false); // nacteno mene
        }
        i++;
    }
 
}
 
double ** allocMatrix(int rows ,int cols){
 
    double ** matrix ;
 
    matrix = new double * [rows];
 
    for (int i = 0; i < rows; i++) {
        matrix[i]= new double[cols];
 
    }
 
    return matrix;
 
}
 
void deleteMatrix(double ** matrix , int rows){
 
    for (int i = 0; i < rows; i++) {
        delete [] matrix[i];
    }
 
    delete [] matrix;
}
 
void printMatrix(double ** matrix, int rows, int cols){
 
    for (int i = 0; i < rows; i++) {
 
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
 
    }
 
}
 
void gausElMatrix(double ** matrix, int rows, int cols, int &sign){
 
       for (int j = 0; j < cols-1; j++) {
        for (int i = (rows-1); i > j ; i--) {
 
            if(matrix[(i-1)][j] == 0){
                if(matrix[i][j] == 0) continue;
                double * tmp = matrix[i];
                matrix[i]=matrix[i-1];
                matrix[i-1]=tmp;
                sign *= -1;
            }
 
            double koeficient = matrix[(i)][j] / matrix[(i-1)][j];
 
            for (int k = 0; k < cols; k++) {
                matrix[i][k] = matrix[i][k] - koeficient*matrix[(i-1)][k];
                if(abs(matrix[i][k])<1e-12) matrix[i][k] = 0;
 
            }
        }
    }
}
 
double determinant(double ** matrix, int rows, int cols, int &sign){
 
    double determinant = 1;
    for (int i = 0; i < rows; i++) determinant = determinant * matrix[i][i];
 
    determinant *= (double) sign;
 
    return determinant;
}
 
void evalMatrix(double ** matrix, int rows, int cols){
 
    double *results = new double [rows];
    double rce = 0;
    bool multiresult = false;
 
    for (int i = rows-1; i >= 0; i--) {
 
        rce = 0;
 
        for (int j = cols-2; j > i; j--) {
 
            rce = rce + results[j]*matrix[i][j];
 
        }
 
        if(matrix[i][i] == 0) {
 
            if((matrix[i][cols-1] - rce)==0){
               multiresult = true;
            }else if ((matrix[i][cols-1] - rce)!=0){
 
                bool a = true;
 
                for (int k = i; k < cols-1; k++) {
                    if(matrix[i][k]!=0){
                        a=false;
                        break;
                    }
                }
                if(a){
                cout << "Reseni neexistuje." << endl;
                delete [] results;
                return;
                }
            }
 
        }
 
        results[i]= (matrix[i][cols-1] - rce) / matrix[i][i];
    }
 
    if(multiresult){
        cout << "Nekonecne mnoho reseni." << endl;
        delete [] results;
        return;
    }
 
    // print results
    cout << "Jedno reseni:" << endl;
    for (int j = 0; j < rows; j++) {
        cout << "x" << j+1 << " = " << results[j] << endl;
    }
 
    delete [] results;
 
}
 
/*
 *
 */
int main(int argc, char** argv) {
 
    int rows=0,cols=0,i=1,sign=1;
    double **matrix;
    double a;
 
    string line;
    getline(cin,line);
    if(cin.eof()){
        cout << "Nespravny vstup." << endl ;
        return (EXIT_FAILURE);
    }
    istringstream iss (line);
 
    while(1){
        iss >> a;
        cols++;
        if(iss.eof()) break;
    }
 
     if(cols==1){
        cout << "Nespravny vstup." << endl ;
        return (EXIT_FAILURE);
    }
 
    rows = cols-1;
 
    matrix = allocMatrix(rows,cols);
 
    if(!readLine(line,matrix[0],cols)){
                cout << "Nespravny vstup." << endl;
                deleteMatrix(matrix,rows);
                return (EXIT_FAILURE);
            }
 
    // nacitani vsech radku matice
    while(1) {
 
        getline(cin,line);
 
        if(i < rows) {
 
            if(!readLine(line,matrix[i],cols)){
                cout << "Nespravny vstup." << endl;
                deleteMatrix(matrix,rows);
                return (EXIT_FAILURE);
            }
 
        } else {
 
            if(cin.eof()) break;
 
            cout << "Nespravny vstup." << endl ;
            deleteMatrix(matrix,rows);
            return (EXIT_FAILURE);
        }
 
        i++;
    }
 
    gausElMatrix(matrix,rows,cols,sign);
 
    evalMatrix(matrix,rows,cols);
 
    deleteMatrix(matrix,rows);
 
    return (EXIT_SUCCESS);
}
programming/c-cpp/linearequations.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0