Advertisement
Guest User

lista_dipendenti

a guest
Jan 14th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <string>
  6. #include <vector>
  7. #include <list>
  8. #include <string.h>
  9. #include "Dipendente.h"
  10. #include <fstream>
  11.  
  12. Dipendente::Dipendente(){
  13.    
  14.     nome=0;
  15.     cognome=0;
  16.     tipoContratto=0;
  17.    
  18.    
  19. }
  20.  
  21.  
  22. Dipendente::Dipendente (const Dipendente &r ){
  23.  
  24. nome=r.nome;
  25. cognome=r.cognome;
  26. tipoContratto=r.tipoContratto;
  27. redditoAnnuo.resize(r.redditoAnnuo.size());
  28. copy(r.redditoAnnuo.begin(),r.redditoAnnuo.end(),redditoAnnuo.begin());
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
  35. void Dipendente::setNome(char* n){
  36.     if(nome==NULL){
  37.         nome=new char[strlen(n)+1];
  38.     }
  39.     else if(strlen(nome)<strlen(n)){
  40.         delete []nome;
  41.         nome=new char[strlen(n)+1];
  42.     }
  43.    
  44.     strcpy(nome,n);
  45. }
  46.  
  47.  void Dipendente::setCognome(char *c)
  48. {
  49.  if(cognome==NULL)
  50.  {
  51.    cognome=new char[strlen(c)+1];
  52.  }
  53.   else if(strlen(cognome)<strlen(c))
  54.   {
  55.    delete []cognome;
  56.    cognome=new char[strlen(c)+1];
  57.   }
  58.   strcpy(cognome,c);
  59. }
  60.  
  61.  
  62. void Dipendente::setTipoContratto(char *t)
  63. {
  64.  if(tipoContratto==NULL)
  65.  {
  66.    tipoContratto=new char[strlen(t)+1];
  67.  }
  68.   else if(strlen(tipoContratto)<strlen(t))
  69.   {
  70.    delete []tipoContratto;
  71.    tipoContratto=new char[strlen(t)+1];
  72.   }
  73.   strcpy(tipoContratto,t);
  74. }
  75.  
  76.  
  77.  
  78.  /*
  79. void Dipendente::setAnni(vector<int> anni){
  80.    
  81.     redditoAnnuo.size()=anni.size();
  82.    
  83. }
  84. */
  85. float Dipendente::mediaReddito(){
  86.    
  87.     float somma=0;
  88.     for(int i=0;i<redditoAnnuo.size();i++){
  89.        
  90.         somma=redditoAnnuo[i]+somma;
  91.        
  92.     }
  93.    
  94.     return somma/redditoAnnuo.size();
  95.    
  96. }
  97.  
  98.  
  99. bool Dipendente::operator ==(Dipendente& o){
  100.    
  101.     return nome==o.nome;
  102.    
  103. }
  104.  
  105.  
  106. ostream& operator<<(ostream &os, Dipendente &d){
  107.    
  108.     os<<d.nome<<" "<<d.cognome<<" "<<d.tipoContratto<<" "<<d.redditoAnnuo.size()<<" ";
  109.    
  110.     for(int i=0;i<d.redditoAnnuo.size();i++){
  111.         os<<d.redditoAnnuo[i];
  112.        
  113.     }
  114.    
  115.     return os;
  116. }
  117.  
  118.  
  119. istream &operator>>(istream &in,Dipendente &d){
  120.    
  121.     char appoggio[50];
  122.     in>>appoggio;
  123.     d.setNome(appoggio);
  124.     in>>appoggio;
  125.     d.setCognome(appoggio);
  126.     in>>appoggio;
  127.     d.setTipoContratto(appoggio);
  128.     int n_esami;
  129.     in>>n_esami;
  130.     int tmp;
  131.    // d.redditoAnnuo.resize(n_esami);
  132.     for(int i=0;i<n_esami;i++){
  133.         in>>tmp;
  134.         d.redditoAnnuo.push_back(tmp);
  135.        
  136.        
  137.     }
  138.     in.ignore();
  139.     return in;
  140. }
  141.  
  142. bool operator<(Dipendente &d1,Dipendente &d2){
  143.    
  144.     return  d1.cognome<d2.cognome || d1.cognome==d2.cognome && d1.nome<d2.nome ;    
  145.    
  146. }
  147.  
  148. ostream &operator<<(ostream &out,vector<Dipendente> &d){
  149.    
  150.     for(int i=0;i<d.size();i++){
  151.        
  152.         out<<d[i];
  153.        
  154.        
  155.     }
  156.    
  157.     return out;
  158.    
  159. }
  160.  
  161.  
  162. istream &operator>>(istream &in,vector<Dipendente> &d){
  163.    
  164.     Dipendente app;
  165.     while(!in.eof()){
  166.         in>>app;
  167.         d.push_back(app);
  168.        
  169.     }
  170.     return in;
  171.    
  172. }
  173.  Dipendente::~Dipendente(){
  174.      
  175.      delete [] nome;
  176.      delete []cognome;
  177.      delete [] tipoContratto;
  178.      
  179.      
  180.  }
  181.  
  182. void split_soglia(vector<Dipendente>& v, int s, vector<Dipendente>& v1, vector<Dipendente>& v2){
  183.    
  184.    
  185.     vector<Dipendente>::iterator my_it;
  186.    
  187.    
  188.     for(  my_it=v.begin(); my_it!=v.end();my_it++){
  189.        
  190.         if( my_it->mediaReddito()<=s)
  191.            
  192.             v1.push_back(*my_it);
  193.         else
  194.            
  195.             v2.push_back(*my_it);
  196.        
  197.        
  198.     }
  199.    
  200.    
  201.        
  202.    
  203.    
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement