Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<math.h>
- using namespace std;
- class Polinom{
- private:
- int grad;
- float *coef;
- public:
- Polinom(int grad=0);
- Polinom(const Polinom &p);
- ~Polinom();
- void citire();
- void afisare();
- float valPunct(float x);
- Polinom operator +(const Polinom &p);
- Polinom& operator =(const Polinom &p);
- };
- Polinom::Polinom(int grad){
- this->grad=grad;
- this->coef=new float[grad+1];
- for(int i=0; i<=grad+1; i++){
- this->coef[i]=0;
- }
- cout<<"C cu param"<<endl;
- }
- Polinom::Polinom(const Polinom &p){
- this->grad=p.grad;
- this->coef=new float[p.grad+1];
- for(int i=0; i<=p.grad+1; i++){
- this->coef[i]=p.coef[i];
- }
- // de copiat coeficientii
- cout<<"C de copiere"<<endl;
- }
- Polinom::~Polinom(){
- if(coef!=0){
- delete coef;
- }
- cout<<"Destructor"<<endl;
- }
- void Polinom::citire(){
- cout<<"Dati gradul polinomului"<<endl;
- int temp;
- cin>>temp;
- if(temp>grad){
- delete coef;
- this->coef=new float[temp+1];
- }
- grad=temp;
- //Daca citesc gradul trebuie realocat tabloul.
- cout<<"Dati coeficientii:"<<endl;
- for(int i=0; i<=grad;i++){
- cout<<"Dati coef lui X^"<<i<<"= "<<endl;
- cin>>coef[i];
- }
- }
- void Polinom:: afisare(){
- for(int i=0; i<=grad; i++){
- cout<<coef[i]<<"*X^"<<i<<" + ";
- }
- cout<<endl;
- }
- Polinom Polinom ::operator +(const Polinom &p){
- int max=(this->grad>p.grad) ? this->grad : p.grad;
- Polinom suma(max);
- for (int i=0; i<=this->grad; i++){
- suma.coef[i]+=this->coef[i];
- }
- for (int i=0; i<=p.grad; i++){
- suma.coef[i]+=p.coef[i];
- }
- return suma;
- }
- Polinom & Polinom::operator=(const Polinom &p){//p este obiectul din dreapta lui =
- if(this != &p){
- if(this->grad<p.grad){
- delete this->coef;
- this->coef=new float[p.grad+1];
- }
- this->grad=p.grad;
- for(int i=0; i<=p.grad; i++){
- this->coef[i]=p.coef[i];
- }
- }
- return *this;
- }
- float Polinom:: valPunct(float x){
- int i;
- float suma=0;
- for(i=0; i<=grad; i++){
- suma+=this->coef[i]*pow(x,i);
- }
- return suma;
- }
- int main(){
- Polinom p,q;
- p.citire();
- p.afisare();
- q.citire();
- q.afisare();
- Polinom rez;
- rez=p+q;
- cout<<"Suma celor 2 polinoame este: ";
- rez.afisare();
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement