main.cpp
#include <iostream>
#include "result.h"
using namespace std;
int main()
{
Result r(0,0);
r.gatherWxPx();
r.addition();
return 0;
}
result.h
#include <iostream>
using namespace std;
class Result
{
private:
int dWx; //degree of polynomial W(x)
int dPx; //degree of polynomial P(x)
int RWxPx;
public:
void gatherWxPx(); //gathering polynomial W(x) from user
Result(int, int);
~Result();
void addition(); //adding polynomials
void subtraction(); //subtracting polynomials
void multiplication(); //multiplying polynomials
void division(); //dividing polynomials
};
gather.cpp
#include <iostream>
#include "result.h"
using namespace std;
void Result::gatherWxPx()
{
cout<<"Input degree of polynomial W(x): ";
cin>>dWx;
while (dWx<=0)
{
cout<<"Degree of polynomial has to be greater than 0, try again: ";
cin>>dWx;
}
int HelperWx=dWx;
cout<<"Input degree of polynomial P(x): ";
cin>>dPx;
while (dPx<=0)
{
cout<<"Degree of polynomial has to be greater than 0, try again: ";
cin>>dPx;
}
int HelperPx=dPx;
float *cWx; //
cWx = new float [dWx+1]; //dynamically allocating arrays for W(x)
cout<<"Input coefficients and constant of polynomial W(x): ";
cout<<endl;
for (int i=dWx; i>=0; i--)
{
cin>>cWx[i]; //gathering coefficients of W(x)
}
cout<<"W(x)= "; //show W(x)
for (int i=dWx; i>=0; i--)
{
if (cWx[i]>0 && i!=HelperWx)
{
cout<<"+";
}
if ((cWx[i]<0 && i==0 && i==dWx) || cWx[i]==-1)
{
cout<<"-";
}
if (cWx[i]!=0)
{
if ((cWx[i]!=1 && cWx[i]!=-1) || i==0)
{
cout<<cWx[i];
}
if (i!=0)
{
cout<<"x";
}
if (i!=0 && i!=1)
{
cout<<i;
}
}
else if (cWx[i]==0)
{
HelperWx--;
}
if (i<=0 && HelperWx==-1)
{
cout<<"0";
}
}
cout<<endl;
// delete [] cWx;
float *cPx; //
cPx = new float [dPx+1]; //dynamically allocating arrays for P(x)
//
cout<<"Input coefficients and constant of polynomial P(x): ";
cout<<endl;
for (int i=dPx; i>=0; i--)
{
cin>>cPx[i]; //gathering coefficients of P(x)
}
cout<<"P(x)= "; //show P(x)
for (int i=dPx; i>=0; i--)
{
if (cPx[i]>0 && i!=HelperPx)
{
cout<<"+";
}
if ((cPx[i]<0 && i==0 && i==dPx) || cPx[i]==-1)
{
cout<<"-";
}
if (cPx[i]!=0)
{
if ((cPx[i]!=1 && cPx[i]!=-1) || i==0)
{
cout<<cPx[i];
}
if (i!=0)
{
cout<<"x";
}
if (i!=0 && i!=1)
{
cout<<i;
}
}
else if (cPx[i]==0)
{
HelperPx--;
}
if (i<=0 && HelperPx==-1)
{
cout<<"0";
}
}
cout<<endl;
// delete [] cPx;
}
Result::Result(int Wx, int Px)
{
dWx=Wx;
dPx=Px;
}
Result::~Result()
{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
addition.cpp
#include <iostream>
#include "result.h"
using namespace std;
void Result::addition()
{
if (dWx>dPx)
{
RWxPx=dWx;
float *AWxPx;
AWxPx = new float [RWxPx+1];
for (int i=dWx; i>dPx; i--)
{
AWxPx[i]=cWx[i];
}
for (int i=dWx; i>dPx; i--)
{
cout<<AWxPx[i];
cout<<" ";
}
}
else if (dWx<dPx)
{
RWxPx=dPx;
float *AWxPx;
AWxPx = new float [RWxPx+1];
}
}
Chodzi glownie o plik addition.cpp, mam tam cWx was not declared in this scope, wiem, że go w tej petli nie ma ale jak zrobic to tak ze komputer odczyta cWx[] ktore wczesniej wprowadzil uzytkownik ale jest w innym pliku no i nie w tej petli albo warunku sam nie wiem?
Poza tym czy moj zamysl jest dobry:
main:
1.Wywoluje konstruktor klasy Result
2.Wywoluje gatherWxPx w celu pobrania od uzytkownika potrzebnych danych
3.robie Switcha w ktorym sa wszystkie operacje do wyboru (dodawanie, odejmowanie, mnozenie, dzielenie)
4.Wywoluje destruktor ktory niszczy wszystkie dynamicznie zaalokowane tablice
Nie moge znalezc odpowiedzi na to pytanie bo na filmie Miroslawa Zelenta tam ten destruktor nic nie robil i byl wywolany przed innymi funkcjami a ja bede mial w innych funkcjach zaalokowane dynamicznie jeszcze kilka tablic i sie zastanawiam gdzie go umiescic.