Variants
Actions

Cppreference:Administrators

From cppreference.com
  1. ifndef NUAGE_H
  2. define NUAGE_H
  1. include <vector>
  2. include "Point.h"
  3. include "Cartesien.h"

using namespace std;

class Nuage { public:

 Nuage(); vector<Point*> vectorPolymorfisme; vector<Point*>::iterator it; Cartesien baricentre(); 

};


  1. endif // NUAGE_H_INCLUDED

using namespace std;

Nuage::Nuage() {

 it = vectorPolymorfisme.begin(); 

}


Cartesien Nuage:: baricentre() {

 float cx=0; float cy=cx; int count =0; for (it=vectorPolymorfisme.begin(); it<vectorPolymorfisme.end(); it++) { Polaire* teste = dynamic_cast<Polaire*>(*it); if( teste != 0 ) { //cout << typeid(*teste).name(); int k; cin>>k; Polaire* pp = (Polaire *) *it; //donwcasting Cartesien p(*pp); cx+= p.getX(); cy+= p.getY(); } Cartesien* teste2 = dynamic_cast<Cartesien*>(*it); if( teste2 != 0 ) { Cartesien* p = (Cartesien *) *it; //donwcasting cx+= p->getX(); cy+= p->getY(); } count++; } cx= (float) cx/count; cy= (float) cy/count; cout<<"Baricentre = ("<<cx<<","<<cy<<")"<<endl; Cartesien car(cx,cy); return car; 

}





using namespace std;

class Pessoa { public: string nome; Pessoa(string a) { nome = a;} //void setNome(const char *nm) { /* liberta, aloca e copia */ } operator string() const { return nome; } // ... };

void teste() { Pessoa p1 ("Joao"); Pessoa p2 ("Maria"); string s = p2; // Agora é possível p2 = s; cout<<s; cout<<p2.nome; }

int main() {

 teste(); 
 cout<<"estou em main.cpp"<<endl; Point *p; vector<Point *> vectorPolymorfisme; vector<Point> vectorNNNN; 
 const float r = 1.0; const float theta = 45.0; 
 Polaire polaire(r,theta), p2(1,90),p3; Cartesien cartesien(456,431), c2(0,0); 


 polaire=cartesien; cout<<" apos atribuicao 2 "<<endl; cout<<cartesien; cout<<polaire; 


 cartesien = polaire; 


 cout<<" apos atribuicao"<<endl; cout<<cartesien; cout<<polaire; 


 vector<Point*>::iterator it = vectorPolymorfisme.begin(); vectorPolymorfisme.push_back(&polaire); vectorPolymorfisme.push_back(&cartesien); vectorPolymorfisme.push_back(&p2); vectorPolymorfisme.push_back(&c2); vectorPolymorfisme.push_back(&p3); 
 for (it=vectorPolymorfisme.begin(); it<vectorPolymorfisme.end(); it++) { cout<<**it; } 
 /* cannot allocate an object of abstract type 'Point'| 
 vector<Point>::iterator it2 = vectorNNNN.begin(); vectorNNNN.push_back(polaire); vectorNNNN.push_back(cartesien); vectorNNNN.push_back(p2); vectorNNNN.push_back(c2); vectorNNNN.push_back(p3); for (it2=vectorNNNN.begin(); it2<vectorNNNN.end(); it2++) { cout<<**it2; } */ 


 cout<<" USANDO NUAGE ................................"<<endl; Nuage nuage; nuage.vectorPolymorfisme.push_back(&polaire); nuage.vectorPolymorfisme.push_back(&cartesien); nuage.vectorPolymorfisme.push_back(&p2); nuage.vectorPolymorfisme.push_back(&c2); nuage.vectorPolymorfisme.push_back(&p3); 


 for (nuage.it=nuage.vectorPolymorfisme.begin(); nuage.it<nuage.vectorPolymorfisme.end(); nuage.it++) { 

cout<<**nuage.it;

 } 
 Cartesien baricentre = nuage.baricentre(); cout<<baricentre; return EXIT_SUCCESS; 

}




  1. ifndef POINT_H
  2. define POINT_H
  1. include <iostream>
  2. include <cmath>
  3. include <cstdlib>
  4. include <iomanip>

class Point;

using namespace std;

class Point {

friend ostream& operator << (ostream&, const Point&); friend istream& operator >> (istream&, Point&);

public:

Point(float r=10, float theta = 0.5); operator Point&();

virtual void afficher() const = 0; virtual void print() const = 0; virtual void insert(istream&) = 0; virtual void converter(Point*) = 0;


float r; //sera o mesmo de x float theta; //sera o mesmo de y

};

  1. endif
  2. include "Point.h"
  3. include <iostream>

using namespace std;

Point::Point(float r, float theta) { this->r = r; this->theta = theta; }


/*bool Point::EXPECT_EQ(float x , float y) { if (x==y) { return 1; }

else {return 0;} }*/

ostream& operator << (ostream& out, const Point& p) { p.print(); return out; }

istream& operator >> (istream& in, Point& p) { cout<<"entrer avec donnees selon: (x,y) ou (r,theta) "<<endl; p.insert(in); return in; }

Point::operator Point&() {

 Point* p = this; p->converter(p); return *p; 

}



  1. ifndef CARTESIEN_H
  2. define CARTESIEN_H
  1. include <iostream>
  2. include "Point.h"

using namespace std; class Polaire;

class Cartesien : public Point {

public:

Cartesien(float r=1, float theta=1); Cartesien(Polaire&);

float getX();

 float getY(); 

virtual void afficher() const; //virtual bool EXPECT_EQ(float x , float y); virtual void print() const;

virtual void insert(istream&); virtual void converter(Point*);

const Cartesien& operator = (const Cartesien& ); //operator Polaire() ; };

  1. endif
  2. include "Cartesien.h"
  3. include "Polaire.h"
  4. include <iostream>


using namespace std;

Cartesien::Cartesien(float r, float theta): Point( r, theta) {

}

void Cartesien::afficher() const { cout<<" point Cartesien: "; }


float Cartesien::getX() { return this->r; } float Cartesien::getY() { return this->theta; }


void Cartesien::print() const {

 afficher(); cout<<"("<<this->r<<","<<this->theta<<")"<<endl; 

}

void Cartesien::insert(istream& in) {// lendo (x,y)

char a;

in>>a; // igora leitura do caractere ( in>>this->r; in>>a; // igora leitura do caractere , in>>this->theta; in>>a; // igora leitura do caractere (


/*in.ignore(); // igora leitura do caractere ( in>>this->r; in.ignore(); // igora leitura do caractere , in>>this->theta; in.ignore(); // igora leitura do caractere (*/ }

void Cartesien::converter(Point* point) // estou convertendo cartesein para polaire {

 float PI = 3.14159265; 

Polaire* p = (Polaire *) point; //donwcasting

//cout<<"p->getR() = "<

getR()<<endl; //cout<<"p->getT() = "<<p->getT()<<endl; this->r = sqrt(p->getR()*p->getR() + p->getT()*p->getT()); this->theta = atan2(p->getT(),p->getR()) *180/PI; } const Cartesien& Cartesien::operator = (const Cartesien& C) { if(&C != this) { this->r = C.r; this->theta=C.theta; } return *this; } /* Cartesien:: operator Polaire() // converte cartesien p/ polaire {// usado na chamada polaire=cartesien; cout<<"estou no operator de funcao cartesein"<<endl; cout<<"r = "<<r<<"theta = "<<theta<<endl; Polaire polaire; Point* point = this; //aux.converter(point); //so rola conversao aqui, n rola atribuicao. Cartesien aux; float PI = 3.14159265; aux.r = sqrt(r*r + theta*theta); aux.theta = atan2(theta,r) *180/PI; polaire.r = aux.r; polaire.theta=aux.theta; return polaire; //consigo usar so em uma conversao, n consigo voltar }*/ Cartesien::Cartesien(Polaire& p) //converte polaire pra cartesiane ja fazendo a atribuicao pelo operador sobrecarregado = {//esse objeto aqui eh temporario, sera usado na operacao ( Cartesien c = Cartesien temporario) cout<<"estou no construtor de convert de funcao Cartesien"<<endl; float PI = 3.14159265; float aux = PI / 180; cout<<"r = "<<this->r<<endl; cout<<"theta = "<<theta<<endl; r = p.r*cos(p.theta*aux); theta = p.r*sin(p.theta*aux); cout<<"r = "<<this->r<<endl; cout<<"theta = "<<theta<<endl; }

  1. ifndef VECTEUR_H
  2. define VECTEUR_H
  1. include <iostream>
using namespace std; class vecteur { friend ostream& operator << (ostream& out, vecteur& ); //friend vecteur operator * (vecteur& v1, vecteur& v); int** tableux; int taille; public: // COMECO DA FORMA DE COPLIEN vecteur(int = 10); vecteur(vecteur&); const vecteur& operator = (const vecteur&); ~vecteur(); // FIM DA FORMA DE COPLIEN int getTaille(); int** getTableaux(); void print() ; void print() const; vecteur operator + (const vecteur&); //const vecteur operator + (const vecteur& v) ver comentarios no .cpp int& operator [] (int); int operator [] (int) const; int operator * (const vecteur&); int* begin(); int* end(); };
  1. endif // VECTEUR_H_INCLUDED
  1. include <iostream>
  2. include "vecteur.h"
  3. include <iomanip>
  4. include <cstdlib>
using namespace std; vecteur::vecteur(int t):taille(t) { this->tableux = new int* [taille]; for(int i=0; i<taille;i++) { tableux[i] = new int[taille]; for(int j=0; j<taille;j++) tableux[i][j]=0; } } vecteur::vecteur(vecteur& v) { this->taille = v.taille; this->tableux = new int* [taille]; for(int i=0; i<taille;i++) { this->tableux[i] = new int[taille]; for(int j=0; j<taille;j++) this->tableux[i][j]=v.tableux[i][j]  ; } } const vecteur& vecteur::operator = (const vecteur& v) { if(&v != this) { if(v.taille!= this->taille) { for(int i=0; i<taille;i++) delete [] this->tableux[i]; delete [] this->tableux; this->taille=v.taille; this->tableux = new int* [taille]; for(int i=0; i<taille;i++) { this->tableux[i] = new int[taille]; for(int j=0; j<taille;j++) this->tableux[i][j]=v.tableux[i][j]  ; } } else { for(int i=0; i<taille;i++) for(int j=0; j<taille;j++) this->tableux[i][j]=v.tableux[i][j]  ; } } return *this; } vecteur::~vecteur() { for(int i=0; i<taille;i++) delete [] this->tableux[i]; delete [] this->tableux; cout<<"destrutor foi chamado"<<endl; } int vecteur::getTaille() { return taille; } int** vecteur::getTableaux() { return this->tableux; } void vecteur::print() { for(int i=0; i<getTaille();i++){ for(int j=0; j<getTaille();j++) { cout<<getTableaux()[i][j]<<" "; } cout<<endl; } } void vecteur::print() const { for(int i=0; i<taille;i++) { for(int j=0; j<taille;j++) { cout<<tableux[i][j]<<" "; } cout<<endl; } } ostream& operator << (ostream& out, vecteur& c) { c.print(); return out; } //const vecteur vecteur::operator + (const vecteur& v) // vsome = vsome + v. nao retorna vecteur& pois vai chamar o construtor de copia no retorno // mas essa sobrecarga nao serve para somar tres membros. vsome = v1 + v2 + v3 vecteur vecteur::operator + (const vecteur& v) { if(this->taille == v.taille) { cout<<*this; cout<<endl<<"agora eh v:"<<endl; v.print(); vecteur vsoma(taille); for(int i=0; i<getTaille();i++){ for(int j=0; j<getTaille();j++) { vsoma.getTableaux()[i][j] = v.tableux[i][j] + tableux[i][j]; } } return vsoma; } else { cout<<"impossible faire le some entre les tables"; return *this; } } int& vecteur::operator [] (int j) { if(j>=taille*taille) { cout<<"out of range"<<endl; exit(1); } else{ int i = j%taille; //cout<<"i="<<i<<endl; int k = j/taille; //cout<<"k = "<<k<<endl; return tableux[k][i]; } } int vecteur::operator [] (int j) const { if(j>=taille*taille) { cout<<"out of range"<<endl; exit(1); } else{ int i = j%taille; //cout<<"i="<<i<<endl; int k = j/taille; //cout<<"k = "<<k<<endl; return tableux[k][i]; } } int vecteur::operator * (const vecteur& v) { if(this->taille == v.taille) { cout<<*this; cout<<endl<<"agora eh v:"<<endl; v.print(); int total = 0; for(int i=0; i<getTaille();i++){ for(int j=0; j<getTaille();j++) { total = v.tableux[i][j] * tableux[i][j]; } } return total; } else { cout<<"impossible faire le some entre les tables"; int k = 0; return k; } } int* vecteur::begin() { return &(*this).tableux[0][0]; } int* vecteur::end() { return &(*this).tableux[taille-1][taille-1]; } /*vecteur operator * (vecteur& v1, vecteur& v) { vecteur aux(v); for(int i=0; i<aux.getTaille();i++){ for(int j=0; j<aux.getTaille();j++) { aux.tableux[i][j] = v.tableux[i][j] * v1.tableux[i][j]; } } return aux; }
  • /
  1. ifndef ITERATORE
  2. define ITERATORE
  1. include <iostream>
  2. include "vecteur.h"
using namespace std; class iteratore { public: int *it; iteratore(); iteratore(int*); iteratore & operator ++ (); iteratore operator ++ (int); int operator *() const; bool operator == (const iteratore &) const; };
  1. endif // ITERATOR_H_INCLUDED
  1. include <iostream>
  2. include "iterator.h"
  3. include <iomanip>
  4. include <cstdlib>
using namespace std; iteratore::iteratore() { this->it = 0; } iteratore::iteratore(int* i) { it = i; } iteratore& iteratore::operator ++ () { if(it + 1 != NULL ) this->it++; else cout<<"iterator acesando adress fora do vetor, nada foi feito"<<endl; return *this; } iteratore iteratore::operator ++ (int) { iteratore temp(*this); if(it + 1 != NULL ) this->it++; else cout<<"iterator acesando adress fora do vetor, nada foi feito"<<endl; cout<<*temp.it; return temp; } int iteratore::operator *() const { return *(this->it); } bool iteratore::operator == (const iteratore & i) const { if(i.it == this->it) return true; return false; }
  1. include <iostream>
  2. include "vecteur.h"
  3. include "iterator.h"
using namespace std; int main() { cout << "Hello world!" << endl; vecteur mic; mic.print(); vecteur m(5); for(int i=0; i<m.getTaille();i++) for(int j=0; j<m.getTaille();j++) m.getTableaux()[i][j]=i*j; m.print(); { vecteur teste(m); teste.print(); } mic = m; cout<<mic; vecteur teste(5); m = mic + teste+mic; // quero fazer m = mic + teste + mic; cout<<m; cout<<m[24]; m[24]=11; cout<<m; int escalar = m*m; // int escalar2 = m*m*m; cout<<"escalar m*m = "<<escalar<<endl; cout<<*m.end(); *m.end()=2; cout<<*m.end(); cout<<m; mic[1] = 1; mic[2] = 2; mic[3] = 3; mic[4] = 4; cout<<endl<<mic<<endl; iteratore ite; ite.it = mic.begin(); *ite.it = 3; cout<<mic<<endl<<endl; iteratore ite2(ite); *(ite2.it) = 5; cout<<mic<<endl<<endl; if(*(++ite2.it) == 1) cout<<"teste bonito de pre fixado"<<endl; cout<<*(ite2.it); if(*(ite2.it)==1) cout<<"teste bonito de pre fixado"<<endl; ite2.it = mic.begin(); if(*(ite2.it++) == 5) cout<<"teste bonito de pos fixado"<<endl; cout<<*(ite2.it); cout<<" teste de == :"<<endl; ite2.it = mic.begin(); if(ite == ite2) cout<<"teste ok de == "<<endl; cout<<"test de operador *"<<endl; int* oi = mic.begin(); ite = oi; *oi = 58; cout<<*(ite.it); cout<<endl<<mic<<endl<<endl; return 0; }

funcoes diferentes

  1. include "Polaire.h"
  2. include "Cartesien.h"
  3. include <iostream>

using namespace std;


void Polaire::insert(istream& in) {// lendo (x,y)

char a;

in>>a; // igora leitura do caractere ( in>>this->r; in>>a; // igora leitura do caractere , in>>this->theta; in>>a; // igora leitura do caractere (


/*in.ignore(); // igora leitura do caractere ( in>>this->r; in.ignore(); // igora leitura do caractere , in>>this->theta; in.ignore(); // igora leitura do caractere (*/ }

void Polaire::converter(Point* point) // converte polaire pra cartesien {

 cout<<"oi"<<endl; float PI = 3.14159265; float aux = PI / 180; Cartesien* p = (Cartesien *) point; //donwcasting 
 cout<<p->getX()<<p->getY()<<endl; 

this->r = p->getX()*cos(p->getY()*aux); this->theta = p->getX()*sin(p->getY()*aux); cout<<p->getX()<<p->getY()<<endl; cout<<r<<theta<<endl;

}

const Polaire& Polaire::operator = (const Polaire& C) { if(&C != this)

 { this->r = C.r; this->theta=C.theta; } return *this; 

}

Polaire:: operator Cartesien() // converte Polaire p/ cartesien {// usado na chamada cartesien=polaire; cout<<"estou no operator Cartesien() de funcao Polaire"<<endl; cout<<" (r,theta) em polaire = ("<<r<<","<<theta<<")"<<endl;


Cartesien cartesien;

 float aux; float PI = 3.14159265; 

aux = r*cos(theta*PI/180); cartesien.r = aux;

 aux = r*sin(theta*PI/180); 

cartesien.theta=aux;

return cartesien; //consigo usar so em uma conversao, n consigo voltar }


/* Polaire::Polaire(Cartesien& c) //converte cartesiane pra Polaire ja fazendo a atribuicao pelo operador sobrecarregado = {// usado na chamada polaire=cartesien;

cout<<"estou no construtor de convert de funcao Polaire"<<endl;

r = sqrt(c.r*c.r + c.theta*c.theta); float PI = 3.14159265; theta = atan2(c.theta,c.r) *180/PI; }

  • /

const Polaire& Polaire::operator = (const Cartesien & c) { cout<<"estou no operador= (sobrecarga )de polaire=cartesien de funcao Polaire"<<endl;

r = sqrt(c.r*c.r + c.theta*c.theta); float PI = 3.14159265; theta = atan2(c.theta,c.r) *180/PI; return *this;

}



  1. ifndef NUAGE_H
  2. define NUAGE_H
  1. include <vector>
  2. include "Point.h"

using namespace std;

template <typename T> class Nuage { public:

 Nuage(); typedef vector<T*> tvector; tvector vectorPolymorfisme; typename tvector::iterator it; //isso conserta a linha de antes vector<T*>::iterator it // tem q colocar typename antes, isso significa que // estou assegurando o compilador q exite meu vetor polimorfico definido. // O lance é assegurar que tvector::iterator ja eh tipo definido 
 Cartesien baricentre(); 

};

  1. include "Nuage.cpp" // modo de fazer
  2. endif // NUAGE_H_INCLUDED


  1. include "Cartesien.h"
  2. include "Polaire.h"
  3. include <iostream>


using namespace std;

template <class T> Nuage<T>::Nuage() {

 it = vectorPolymorfisme.begin(); cout<< "parabens! consegui mexer nessa coisa linda"<<endl<<endl; 

}

template <class T> Cartesien Nuage<T>:: baricentre() {

 float cx=0; float cy=cx; int count =0; //se nuage estiver vide vai retornar ponto (0,0) como baricentro for (it=vectorPolymorfisme.begin(); it<vectorPolymorfisme.end(); it++) { Polaire* teste = dynamic_cast<Polaire*>(*it); if( teste != 0 ) { Polaire* pp = (Polaire *) *it; //donwcasting 

Cartesien p(*pp);

 cx+= p.getX(); cy+= p.getY(); } Cartesien* teste2 = dynamic_cast<Cartesien*>(*it); if( teste2 != 0 ) { Cartesien* p = (Cartesien *) *it; //donwcasting cx+= p->getX(); cy+= p->getY(); } count++; } if(count!=0) { cx= (float) cx/count; cy= (float) cy/count; } Cartesien c(cx,cy); return c; 

}

template <> Cartesien Nuage<Polaire>:: baricentre() {

 float cx=0; float cy=cx; int count =0; for (it=vectorPolymorfisme.begin(); it<vectorPolymorfisme.end(); it++) { //cout<<**it; Cartesien c(**it); cx+= c.getX(); cy+= c.getY(); count++; } if(count!=0) { cx= (float) cx/count; cy= (float) cy/count; } Cartesien c(cx,cy); //cout<< c;int k; cin>>k; return c; 

}




  1. include <cstdlib>
  2. include "Cartesien.h"
  3. include "Polaire.h"
  4. include "Nuage.h"
  5. include <vector>


using namespace std;


template <class T,class T2> T Min(T x, T2 y) {

 // T2 a; return x<y ? x : y; 

}

template <> int Min<int,int>(int s1, int s2) {

 if (s1>s2) return s1; else return s2; 

}


int main() {


 cout<<" USANDO NUAGE ................................"<<endl; Nuage<Point> nuage; cout<<nuage.vectorPolymorfisme.size()<<endl; nuage.vectorPolymorfisme.push_back(&polaire); nuage.vectorPolymorfisme.push_back(&cartesien); nuage.vectorPolymorfisme.push_back(&p2); nuage.vectorPolymorfisme.push_back(&c2); nuage.vectorPolymorfisme.push_back(&cartesien); Polaire *teste = dynamic_cast <Polaire*> (nuage.vectorPolymorfisme.back()); if(teste!=0) cout<<endl<<*teste; // aqui eh polaire else { Cartesien *teste2 = dynamic_cast <Cartesien*> (nuage.vectorPolymorfisme.back()); cout<<endl<<*teste2;// aqui eh cartesian } 
 nuage.vectorPolymorfisme.pop_back(); 


 for (nuage.it=nuage.vectorPolymorfisme.begin(); nuage.it<nuage.vectorPolymorfisme.end(); nuage.it++) { 

cout<<**nuage.it;

 } 
 cout<<endl<<endl<<endl; const float x1 = 2; const float x2 = 5; const float x3 = 2; 
 const float y1 = 7; const float y2 = 3; const float y3 = 2; 
 Cartesien a(x1,y1), b(x2,y2), c(x3,y3); 
 Nuage<Point> nuage2; Cartesien barycentre = nuage2.baricentre(); cout<<"baricentre = "<<barycentre; 
 nuage2.vectorPolymorfisme.push_back(&a); barycentre = nuage2.baricentre(); cout<<"baricentre = "<<barycentre; 
 nuage2.vectorPolymorfisme.push_back(&b); nuage2.vectorPolymorfisme.push_back(&c); barycentre = nuage2.baricentre(); cout<<"baricentre = "<<barycentre; 


cout<<endl<<endl<<endl;

cout<<endl<<"teste da specificacao da fonction template"<<endl;

 Nuage<Polaire> nuagePo; Polaire aa(a), bb(b), cc(c); 
 Cartesien baricentre2 = nuagePo.baricentre(); cout<<"baricentre = "<<baricentre2; 
 nuagePo.vectorPolymorfisme.push_back(&aa); baricentre2 = nuagePo.baricentre(); cout<<"baricentre = "<<baricentre2; 
 nuagePo.vectorPolymorfisme.push_back(&bb); nuagePo.vectorPolymorfisme.push_back(&cc); baricentre2 = nuagePo.baricentre(); cout<<"baricentre = "<<baricentre2; return EXIT_SUCCESS; 

}


META----------------------------------------------------------------


  1. ifndef FACTORIELLE_HPP
  2. define FACTORIELLE_HPP

using namespace std;

template <int N> class Factorielle {

 public: static const long valeur= N * Factorielle<N-1>::valeur; 

}; template <> class Factorielle<0> {

 public: static const long valeur = 1; 

};


/*template <class N> inline long factorielleFonction() {

 return (0); 

}*/ template <int N> inline long factorielleFonction() {

 return (N * factorielleFonction<N-1>()); 

} template <> inline long factorielleFonction<0>() {

 return 1; 

}

  1. endif //FACTORIELLE_HPP
  1. ifndef PUISSANCE_HPP
  2. define PUISSANCE_HPP

using namespace std;

template <int N> struct Puissance {

 static double calculer(double x) { return (Puissance<N-1>::calculer(x) * x); } 

}; template <> struct Puissance<0> {

 static double calculer(double ) { return 1.0; } 

};

  1. endif //PUISSANCE_HPP


  1. ifndef EXPONENTIELLE_HPP
  2. define EXPONENTIELLE_HPP
  1. include "Factorielle.hpp"
  2. include "puissance.hpp"

using namespace std;

template <int k> inline double exponentielle(double x) {

 return (exponentielle<k-1>(x) + ((double) Puissance<k>::calculer(x)/factorielleFonction<k>())); 

} template <> inline double exponentielle<0>(double) {

 return 1; 

}


  1. endif //EXPONENTIELLE_HPP


  1. ifndef COS_HPP
  2. define COS_HPP
  1. include "exponentielle.hpp"

using namespace std;

template <int k> inline double cos(double x) {

 const int k2 = 2*k; return (cos<k-1>(x) + ((double) (Puissance<k>::calculer(-1))*(Puissance<k2>::calculer(x)/factorielleFonction<k2>()))); 

} template <> inline double cos<0>(double) {

 return 1; 

}


  1. endif //COS_HPP
  1. ifndef SIN_HPP
  2. define SIN_HPP
  1. include "exponentielle.hpp"

using namespace std;

template <int k> inline double sin(double x) {

 const int k2 = 2*k + 1; return (sin<k-1>(x) + ((double) (Puissance<k>::calculer(-1))*(Puissance<k2>::calculer(x)/factorielleFonction<k2>()))); 

} template <> inline double sin<0>(double) {

 return 0; 

}


  1. endif //SIN_HPP


  1. include <iostream>
  2. include "sin.hpp"
  3. include "cos.hpp"

using namespace std;

int main() {

 unsigned long fac5 = Factorielle<5>::valeur; cout<< fac5<<endl<<endl; unsigned long fact20 = factorielleFonction<20>(); double puis2_5 = Puissance<5>::calculer(2); // 2^5 
 double exp2 = exponentielle<3>(2); double sin2 = sin<3>(2); double cos2 = cos<3>(2); cout<<fact20<<endl<<puis2_5<<endl<<exp2<<endl; cout<<sin2<<endl; cout<<cos2<<endl; return 0; 

}


  1. ifndef VALEUR_HPP
  2. define VALEUR_HPP
  1. include "Etudiant.hpp"
  2. include <iostream>

using namespace std;

class Valeur {

 double reel; //sont les donnes, comme notes des élèves sur en éxamen Etudiant aluno; 

public:

 Valeur(Etudiant,const double =0); Valeur(); void setReel(const double&); double getReel() const; void setEtudiant(const Etudiant& x); Etudiant getEtudiant(); 

};

  1. endif // VALEUR_HPP
  1. ifndef FONCTEUR_VALEUR_HPP
  2. define FONCTEUR_VALEUR_HPP

class Foncteur_Valeur // c'est une definissition de foncteur comme less de la stl. { public: bool operator() (const Valeur & c1, const Valeur & c2) const // la creation de foncteur = surcharge d'operateur () { return c1.getReel() > c2.getReel(); } };

  1. endif // FONCTEUR_VALEUR_HPP


 n pus valor cpp 


  1. ifndef CLASSE_HPP
  2. define CLASSE_HPP


  1. include <iostream>

using namespace std;

class Classe {

 double minimum; //limite inferior da classe double maximum; //limite superior da classe int quantite; 

public:

 Classe(const double =0, double = 0, int = 0); void setMin(const double&); void setMax(const double&); void setQuantite(const int&); void setQuantite( int&); double getMin() const; double getMax()const; int getQuantite()const; bool operator<(Classe&)const; bool operator==(Classe&)const; bool operator<(Classe)const; bool operator==(Classe)const; 

};

  1. endif // CLASSE_HPP
  1. ifndef VALUECMP_HPP
  2. define VALUECMP_HPP

//template <typename T> class ValueCmp // c'est une definissition de foncteur comme less de la stl.

 //le foncteur s'appelle ValueCmp, olha a utilizacao no ficheir Histogramme.hpp linha 18 

{ public: bool operator() (const Classe & c1, const Classe & c2) const // la creation de foncteur = surcharge d'operateur () { return c1.getQuantite() > c2.getQuantite(); } }; /* template <> bool ValueCmp<Classe>::operator() (const T & c1, const T & c2) const // la creation de foncteur = surcharge d'operateur () { return c1.getQuantite() > c2.getQuantite(); }

  • /
  1. endif // VALUECMP_HPP


 n pus classe cpp 


  1. ifndef HISTOGRAMME_HPP
  2. define HISTOGRAMME_HPP


  1. include <iostream>
  2. include "Classe.hpp"
  3. include "Echantillon.hpp"
  4. include <set>
  5. include <algorithm>
  6. include <iterator>

using namespace std;

class Histogramme {

 static int nombreClasse; Classe *classes; typedef set<Classe,less<Classe> > ClasseSetMin; typedef multiset<Classe,ValueCmp > ClasseSetQuantite; //typedef set<Classe,less<Classe.getMin()>> ClasseSetQuantite; 

public:

 Histogramme(Echantillon &e,int ); static void setNombreClasse(const int&); static int getNombreClasse(); void construirHis (Echantillon&); Classe* getClasses() const; 


};

  1. endif // HISTOGRAMME_HPP


  1. include <iostream>
  2. include "Histogramme.hpp"

using namespace std;

  1. define alpha 0.00001

int Histogramme::nombreClasse=0; Histogramme::Histogramme(Echantillon & e, int nombreDEclasses) {

 setNombreClasse(nombreDEclasses); classes = new Classe [getNombreClasse()]; construirHis(e); 

}

void Histogramme::setNombreClasse(const int& x) {

 nombreClasse = x; 

}

int Histogramme::getNombreClasse() {

 return nombreClasse ; 

}

void Histogramme::construirHis (Echantillon& e) {

 double valeurMaxEchantillon = e.getInVecteur(0); double valeurMinEchantillon = valeurMaxEchantillon; for(int i=1; i<e.getTaille();i++) { if(e.getInVecteur(i)>valeurMaxEchantillon) valeurMaxEchantillon = e.getInVecteur(i); if(e.getInVecteur(i)<valeurMinEchantillon) valeurMinEchantillon = e.getInVecteur(i); } 
 double amplitude = (valeurMaxEchantillon - valeurMinEchantillon)/getNombreClasse(); //cout<<"valeurMaxEchantillon = "<<valeurMaxEchantillon<<endl; //cout<<"valeurMinEchantillon = "<<valeurMinEchantillon<<endl; //cout<<"amplitude = "<<amplitude<<endl; //int k; cin>>k; 
 cout<<"Utilizant le vecteur"<<endl<<endl; 
 //definindo limites das classe classes[0].setMin(valeurMinEchantillon); classes[0].setMax(classes[0].getMin() + amplitude-alpha); for(int i=1; i<getNombreClasse();i++) { classes[i].setMin(classes[i-1].getMax()+alpha); classes[i].setMax(classes[i].getMin() + amplitude-alpha); } 


 //fazendo Freqüência Absoluta for(int i=0; i<e.getTaille();i++) { bool ValorAlocado = false; for(int j=0; j<getNombreClasse();j++) { if ((e.getInVecteur(i)>=classes[j].getMin()-alpha) && (e.getInVecteur(i)<=classes[j].getMax())) { classes[j].setQuantite(classes[j].getQuantite()+1); ValorAlocado = true; } } if(ValorAlocado==false){ if(e.getInVecteur(i)<=classes[getNombreClasse()-1].getMax()+alpha*2) classes[getNombreClasse()-1].setQuantite(classes[getNombreClasse()-1].getQuantite()+1); else { cout<<"erro na funcao ConstruirHis, pois teve valor de echatillon n alocado"<<endl; cout<<"i = "<<i<<"valor = "<<e.getInVecteur(i)<<endl; } } } 


 cout<<"classe Frequency"<<endl; for(int j=0; j<getNombreClasse();j++) { cout<<"["<<classes[j].getMin()<<" - "<<classes[j].getMax()<<"]"<<": "<<classes[j].getQuantite()<<endl; } cout<<endl<<"HISTOGRAMME"<<endl<<endl; for(int j=0; j<getNombreClasse();j++) { cout<<"["<<classes[j].getMin()<<" - "<<classes[j].getMax()<<"]"<<": "; for(int ka=0;ka<classes[j].getQuantite();ka++) cout<<"*"; cout<<endl; } 


 cout<<endl<<endl<<"Utilizant le set de la stl avec classification sur valeur Min"<<endl<<endl; 
 ClasseSetMin setHis; 
 Classe start(valeurMinEchantillon,valeurMinEchantillon+ amplitude-alpha,0); //cout<<"min in start = "<<start.getMin()<<endl; setHis.insert(start); //cout<<"step 1"<<endl; //cin>>k; 


 Classe aux = start; for(int i=1; i<getNombreClasse();i++) { aux.setMin(start.getMax()+alpha); aux.setMax(aux.getMin() + amplitude-alpha); setHis.insert(aux); start=aux; } //aux.setMin(0); //teste //setHis.insert(aux);//teste para ver se esta ordenando conforme linha 17 de #include "Histogramme.hpp" //teste ok 
 ClasseSetMin::iterator it; //fazendo Freqüência Absoluta for(int i=0; i<e.getTaille();i++) { // cout<<i<<endl; bool ValorAlocado = false; for(it=setHis.begin(); it!=setHis.end();++it) { // cout<<"step 0"<<endl; if ((e.getInVecteur(i)>=it->getMin()-alpha) && (e.getInVecteur(i)<=it->getMax())) { Classe aux(it->getMin(),it->getMax(),it->getQuantite()+1); setHis.erase(it); setHis.insert(aux); //it->setQuantite(quant); ValorAlocado = true; break; } // cout<<"step 1/2"<<endl; } // cout<<"step 1"<<endl; if(ValorAlocado==false){ it = setHis.end(); --it; if(e.getInVecteur(i)<=it->getMax()+alpha*2) { 
 Classe aux(it->getMin(),it->getMax(),it->getQuantite()+1); setHis.erase(it); setHis.insert(aux); //it->setQuantite(quant); } else { cout<<"erro na funcao ConstruirHis, pois teve valor de echatillon n alocado"<<endl; cout<<"i = "<<i<<"valor = "<<e.getInVecteur(i)<<endl; } } //int k; cin>>k; } 
 cout<<"classe Frequency"<<endl; for(it=setHis.begin(); it!=setHis.end();++it) { cout<<"["<<it->getMin()<<" - "<<it->getMax()<<"]"<<": "<<it->getQuantite()<<endl; } 


 cout<<endl<<"HISTOGRAMME"<<endl<<endl; for(it=setHis.begin(); it!=setHis.end();++it) { cout<<"["<<it->getMin()<<" - "<<it->getMax()<<"]"<<": "; for(int ka=0;ka<it->getQuantite();ka++) cout<<"*"; cout<<endl; } 


 cout<<endl<<endl<<"Utilizant le set de la stl avec classification sur Quantite decroissante"<<endl<<endl; 
 ClasseSetQuantite setHis2; ClasseSetQuantite::iterator it2; Classe start2(valeurMinEchantillon,valeurMinEchantillon+ amplitude-alpha,0); //cout<<"min in start = "<<start.getMin()<<endl; setHis2.insert(start2); //cout<<"step 1"<<endl; //cin>>k; 


 Classe aux2 = start2; for(int i=1; i<getNombreClasse();i++) { aux2.setMin(start2.getMax()+alpha); aux2.setMax(aux2.getMin() + amplitude-alpha); setHis2.insert(aux2); start2=aux2; } 
 //aux.setMin(0); //teste //setHis.insert(aux);//teste para ver se esta ordenando conforme linha 17 de #include "Histogramme.hpp" //teste ok 


 //fazendo Freqüência Absoluta for(int i=0; i<e.getTaille();i++) { bool ValorAlocado = false; for(it2=setHis2.begin(); it2!=setHis2.end();++it2) { if ((e.getInVecteur(i)>=it2->getMin()-alpha) && (e.getInVecteur(i)<=it2->getMax())) { 


 Classe aux(it2->getMin(),it2->getMax(),it2->getQuantite()+1); setHis2.erase(it2); setHis2.insert(aux); //it->setQuantite(quant); ValorAlocado = true; break; } } if(ValorAlocado==false){ if(e.getInVecteur(i)<=valeurMaxEchantillon) { 

for(it2=setHis2.begin(); it2!=setHis2.end();++it2) { if(valeurMaxEchantillon<=(it2->getMax()+alpha*2)) { Classe aux(it2->getMin(),it2->getMax(),it2->getQuantite()+1); setHis2.erase(it2); setHis2.insert(aux); //it->setQuantite(quant); break; } } }

 else { cout<<"erro na funcao ConstruirHis, pois teve valor de echatillon n alocado"<<endl; cout<<"i = "<<i<<"valor = "<<e.getInVecteur(i)<<endl; } } } 
 cout<<"classe Frequency"<<endl; for(it2=setHis2.begin(); it2!=setHis2.end();++it2) { cout<<"["<<it2->getMin()<<" - "<<it2->getMax()<<"]"<<": "<<it2->getQuantite()<<endl; } 


 cout<<endl<<"HISTOGRAMME"<<endl<<endl; for(it2=setHis2.begin(); it2!=setHis2.end();++it2) { cout<<"["<<it2->getMin()<<" - "<<it2->getMax()<<"]"<<": "; for(int ka=0;ka<it2->getQuantite();ka++) cout<<"*"; cout<<endl; } 

}


Classe* Histogramme::getClasses() const {

 return this->classes; 

}


  1. include <iostream>
  2. include "Histogramme.hpp"
  3. include <fstream>
  4. include <map>

using namespace std;

  1. define numeroClasses 8
  2. define tailleAmostra 200

int main() {

 Echantillon amostra(tailleAmostra); 
 ifstream input("input.txt",ios::in); for(int i=0; i<tailleAmostra;i++) { double valeur; input>>valeur; //cout<<"i = "<<i<<" valeur="<<valeur<<endl; amostra.setInVecteur(i,valeur); } 
 cout<<amostra.calculeMoyenne()<<endl<<amostra.calculeEcartType(); int k; cin>>k; 
 Histogramme histo(amostra,numeroClasses); 


 cout<<endl<<endl<<"Etudiantes par classes :"<<endl; 
 typedef multimap<Valeur,Etudiant,Foncteur_Valeur > MultimapNotes; MultimapNotes notes; 
 for(int i=0; i<amostra.getTaille();i++) { notes.insert( MultimapNotes::value_type(amostra.vecteur[i],amostra.getInVecteurET(i)) ); } //for( MultimapNotes::iterator iter = notes.begin(); iter!=notes.end();++iter) // cout<<iter->first.getReel()<<"\t"<<iter->second.getnom()<<endl; 
 cout<<endl<<endl; for(int i = 0; i<histo.getNombreClasse();i++) { cout<<"["<<histo.getClasses()[i].getMin()<<" - "<<histo.getClasses()[i].getMax()<<"]"<<": "; for( MultimapNotes::iterator iter = notes.begin(); iter!=notes.end();++iter) { if( (histo.getClasses()[i].getMax()>= iter->first.getReel()) && (histo.getClasses()[i].getMin()<= iter->first.getReel()) ) cout<<iter->second.getnom()<<" , "; } cout<<endl<<endl; } 


 return 0; 

}



converter string:

  1. include <iostream>
  2. include <sstream>
  3. include <string>

int i = 9; stringstream ss;

 ss << i; 
string s = ss.str();

close