programowanie obiektowe a programy matematyczne

0

Cześć,

napisałem program do obliczania regresji liniowej, ładuję dane z pliku potem obliczam, działa cacy:)

jest tylko jedna sprawa. Program ma być napisany w C++, a ja nie stworzyłem, żadnej klasy i żadnego obiektu. Owszem korzystam z vectora czy stringa ale nie wiem czy to nie za mało??

jaki obiekty mogę w czymś takim zrobić??

pozdrawiam
a

0

Ale program ma być napisany koniecznie obiektowo? W C++ nie musisz koniecznie pisać stricte obiektowo. To jest język wieloparadygmatowy.
No ale jeżeli tak ma być, to:
Robisz to na macierzach? Aż się prosi, żeby stworzyć klasę Matrix.

0

czytam sobie z pliku i prościutkie funkcje sumują, mnożą i obliczają co trzeba,
wiec własnie nie bardzo widzę co tu mogło by być obiektem?? nazwa pliku?? to chyba troszkę bez sensu?
no nie wiem?

0
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int linenumber(string filename)
{
    int licznik=0;
    vector<string> v;

    ifstream file(filename.c_str()); //Funkcja c_str() zwraca sta³y wskaŸnik do ³añcucha znaków C, identycznego z aktualnym stringiem. Zwrócony ³añcuch znaków jest zakoñczony znakiem NULL

     char line [256];

     while(file.getline(line,256))
     v.push_back(line);

     for(int i=0;i<v.size();i++)
        licznik++;

    return licznik;
}

double sumax(string filename)
{


    double suma1=0;
    double suma2=0;

    ifstream liczby(filename.c_str());

    double liczba1;
    double liczba2;

    while (!liczby.eof())
    {
        liczby >> liczba1 >> liczba2;



            suma1=suma1 + liczba1;


            suma2=suma2 + liczba2;


            liczba1=0;
            liczba2=0;
    }

    liczby.close();

    return suma1;
}

double sumay(string filename)
{


    double suma1=0;
    double suma2=0;

    ifstream liczby(filename.c_str());

    double liczba1;
    double liczba2;

    while (!liczby.eof())
    {
        liczby >> liczba1 >> liczba2;



            suma1=suma1 + liczba1;


            suma2=suma2 + liczba2;


            liczba1=0;
            liczba2=0;
    }

    liczby.close();

    return suma2;
}
double sumofproduct(string filename)
{

    double suma1=0;
    double suma2=0;
    double sumil=0;

    ifstream liczby(filename.c_str());

    double liczba1;
    double liczba2;



    while (!liczby.eof())
    {
            liczby >> liczba1 >> liczba2;

            sumil= sumil + liczba1*liczba2;

    }



    liczby.close();

    return sumil;
}


int main()
{
    double a;
    double b;
     string filename;
     cout << "put name of file" << endl;
     cin >> filename;
     cout << linenumber(filename) << endl;
     cout << sumax(filename) << endl;
     cout << sumay(filename) << endl;
     cout << sumofproduct(filename) << endl;

     a=(linenumber(filename)*sumofproduct(filename)-sumax(filename)*sumay(filename))/(linenumber(filename)*sumax(filename)*sumax(filename)-sumax(filename)*sumax(filename));
     cout << a <<endl;

     b=(sumay(filename)-a*sumax(filename))/(linenumber((filename)));

    cout << b << endl;
    cout << "y="<<a<<"x+"<<b << endl;

     return 0;
 }
0

Stworz klase i to co masz w main dodaj do tej klasy stworz strumienie wejscia/wyjscia w main mozesz miec cos takiego

MojaKlasa a;
plik >> a;
cout << a;
return 0;

1 użytkowników online, w tym zalogowanych: 0, gości: 1