Dam tylko tą część kodu, która ma znaczenie:
main.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
int main()
{
Wx w;
Px p;
Result r;
//for (;;)
//{
Polynomial *pointerWx;
Polynomial *pointerPx;
pointerWx=&w;
pointerPx=&p;
pointerWx->gather();
pointerWx->show();
pointerPx->gather();
pointerPx->show();
r.menu(w, p);
r.showResult(w, p);
//}
}
polynomials.h:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
class Polynomial
{
public:
virtual void gather()=0;
virtual void show()=0;
};
class Px;
class Wx;
class Result
{
protected:
int resultDegree;
int resultHelper;
int choice;
float *resultCoefficients;
public:
void menu(Wx w, Px p);
void addition(Wx w, Px p);
void showResult(Wx w, Px p);
Result(int=0, int=0, int=0);
~Result();
};
class Wx :public Polynomial
{
protected:
string degreeStringNum;
int degree;
int helper;
float *coefficients;
public:
virtual void gather();
virtual void show();
void isInteger(string &s, int &i);
friend class Result;
Wx(string="0", int=0, int=0);
~Wx();
};
class Px :public Wx
{
public:
virtual void gather();
virtual void show();
friend class Result;
Px(string="0", int=0, int=0);
~Px();
};
structors.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
Wx::Wx(string dNS, int d, int h)
{
degreeStringNum=dNS;
degree=d;
helper=h;
coefficients=new float[d+1];
cout<<"Wx constructor called"<<endl;
}
Wx::~Wx()
{
delete [] coefficients;
cout<<"Wx destructor called"<<endl;
}
Px::Px(string dNS, int d, int h)
:Wx(dNS, d, h)
{
coefficients=new float[d+1];
cout<<"Px constructor called"<<endl;
}
Px::~Px()
{
delete [] coefficients;
cout<<"Px destructor called"<<endl;
}
Result::Result(int rD, int rH, int ch)
{
resultDegree=rD;
resultHelper=rH;
choice=ch;
resultCoefficients=new float[rD+1];
cout<<"Result constructor called"<<endl;
}
Result::~Result()
{
delete [] resultCoefficients;
cout<<"Result destructor called"<<endl;
}
menu.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
void Result::menu(Wx w, Px p)
{
cout<<"Main menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition"<<endl;
cout<<"2.Subtraction"<<endl;
cout<<"3.Multiplication"<<endl;
cout<<"4.Division"<<endl;
cout<<"5.Exit"<<endl;
cout<<endl;
cout<<"Choose an operation: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"W(x)+P(x) = ";
addition(w, p);
}
break;
case 2:
{
cout<<"W(x)-P(x) = ";
//subtraction();
}
break;
case 3:
{
}
break;
case 4:
{
}
break;
case 5:
{
cout<<"Goodbye";
exit(0);
}
break;
default:
{
cout<<"There is no such option";
}
}
}
showResult.cpp:
#include <iostream>
#include "polynomials.h"
using namespace std;
void Result::showResult(Wx w, Px p)
{
if (choice==1 || choice==2 || choice==3 || choice==4 || choice==5)
{
for (int i=resultDegree; i>=0; i--)
{
if (resultCoefficients[i]>0 && i!=resultHelper)
{
cout<<"+";
}
if (resultCoefficients[i]==-1 && i!=0)
{
cout<<"-";
}
if (resultCoefficients[i]!=0)
{
if ((resultCoefficients[i]!=1 && resultCoefficients[i]!=-1) || i==0)
{
cout<<resultCoefficients[i];
}
if (i!=0)
{
cout<<"x";
}
if (i!=0 && i!=1)
{
cout<<i;
}
}
else if (resultCoefficients[i]==0)
{
resultHelper--;
}
if (i<=0 && resultHelper==-1)
{
cout<<"0";
}
}
}
cout<<endl;
cout<<endl;
cout<<"Press enter";
getchar();getchar();
system("cls");
}
jest taki output:
Wx constructor called
Wx constructor called
Px constructor called
Result constructor called
Input degree of polynomial W(x): 1
Input coefficients and constant of polynomial W(x):
1
1
W(x)= x+1
Input degree of polynomial P(x): 1
Input coefficients and constant of polynomial P(x):
1
1
P(x)= x+1
Main menu
---------------------
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Choose an operation: 1
W(x)+P(x) = Wx destructor called
Px destructor called
Wx destructor called
Wx destructor called
Px destructor called
Wx destructor called
2x+2
Press enter
i po nacisnieciu entera:
Wx destructor called
Px destructor called
Wx destructor called
Result destructor called
Px destructor called
Wx destructor called
Wx destructor called
Process returned 0 (0x0) execution time : 26.947 s
Press any key to continue.
a gdy zamkne w nieskonczona petle w mainie program (zmaze komentarze) to mam tak:
Wx constructor called
Wx constructor called
Px constructor called
Result constructor called
Input degree of polynomial W(x): 1
Input coefficients and constant of polynomial W(x):
1
1
W(x)= x+1
Input degree of polynomial P(x): 1
Input coefficients and constant of polynomial P(x):
1
1
P(x)= x+1
Main menu
---------------------
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Choose an operation: 1
W(x)+P(x) = Wx destructor called
Px destructor called
Wx destructor called
Wx destructor called
Px destructor called
Wx destructor called
2x+2
Press enter
i po nacisnieciu entera:
Wx destructor called
Px destructor called
Wx destructor called
Input degree of polynomial W(x):
nawet resultDestruktor nie zadzwonil, jak to rozwiazac? bo chcialbym zeby to bylo zapetlone a nie chce zeby doszlo do memory leaku, no i nawet jak to nie jest zapetlone to nie dziala tak jakbym chcial, bo program najpierw maze ekran konsoli a potem dopiero wywoluja sie te ostatnie destruktory, jak dam system("cls") do destruktora Result to nawet nie maze ekranu bo sie nie wywoluje ;p