cptpingu
Messages postés
3837
Date d'inscription
dimanche 12 décembre 2004
Statut
Modérateur
Dernière intervention
28 mars 2023
123
10 juin 2009 à 23:03
Je t'ai partiellement corrigé ce qu'il fallait. Notamment la partie de délimitation qui te posait problème. Je te laisse le soin de finir les procédures enregistrer() et recuperer(), qui sont bien avancé.
#include
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string>
&split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
elems.push_back(item);
return elems;
}
std::vector<std::string>
split(const std::string &s, char delim)
{
std::vector<std::string> elems;
return split(s, delim, elems);
}
class Personne
{
friend std::ostream& operator<<(std::ostream& out, Personne* v); // objet de type ostream pour ecrire dans un fichier
friend std::istream& operator>>(std::istream& in, Personne* v); // objet de type istream pour lire un fichier
public:
Personne(const std::string& nom, const std::string& prenom, const std::string& telephone);
virtual void affiche() const;
virtual int getSalaire() const = 0;
virtual Personne* getSuivant() const;
virtual const std::string& getNom() const;
virtual void setSuivant(Personne* suivant);
virtual void print(std::ostream& o) const;
virtual const std::string getInformations() const = 0;
protected:
std::string _nom;
std::string _prenom;
std::string _telephone;
Personne* _pSuivant;
};
class Entreprise
{
public :
Entreprise();
virtual ~Entreprise();
void ajouter(Personne *pEle);
void afficheTout();
void enregistrer();
void recuperer();
void supprimer(Personne *pEle);
private :
Personne* _pPremier;
Personne* _pCourant;
};
class Externe : public Personne
{
public:
Externe(const std::string& nom, const std::string& prenom, const std::string& telephone, int fax);
virtual void affiche() const;
virtual int getSalaire() const;
virtual const std::string getInformations() const;
virtual void print(std::ostream& o) const;
protected:
int _fax;
};
class Interne : public Personne
{
public:
Interne(const std::string& nom, const std::string& prenom, const std::string& telephone, int matricule);
virtual void affiche() const;
virtual int getSalaire() const;
virtual const std::string getInformations() const;
virtual void print(std::ostream& o) const;
protected:
int _matricule;
};
Personne::Personne(const std::string& nom, const std::string& prenom, const std::string& telephone)
: _nom(nom), _prenom(prenom), _telephone(telephone), _pSuivant(0)
{
}
Externe::Externe(const std::string& nom, const std::string& prenom, const std::string& telephone, int fax)
: Personne(nom, prenom, telephone), _fax(fax)
{
}
Interne::Interne(const std::string& nom, const std::string& prenom, const std::string& telephone, int matricule)
: Personne(nom, prenom, telephone), _matricule(matricule)
{
}
void Personne::affiche() const
{
// affiche le contenu avec alignement a gauche
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << " Nom : ";
// width permet de definir le nombre de caractere maxi
std::cout.width(10);
std::cout << _nom;
std::cout << " Prenom : ";
std::cout.width(10);
std::cout << _prenom;
std::cout << " Telephone : ";
std::cout.width(10);
std::cout << _telephone;
}
void Externe::affiche() const
{
Personne::affiche();
std::cout << " Fax : ";
std::cout << _fax << std::endl;
}
void Interne::affiche() const
{
Personne::affiche();
std::cout << " Matricule : ";
std::cout << _matricule << std::endl;
}
const std::string Personne::getInformations() const
{
std::stringstream ss;
ss << _nom << " : " << _prenom << " : " << _telephone;
return ss.str();
}
const std::string Externe::getInformations() const
{
std::stringstream ss;
ss << "Externe : " << Personne::getInformations()
<< " : " << _fax << " ; " << std::endl;
return ss.str();
}
const std::string Interne::getInformations() const
{
std::stringstream ss;
ss << "Interne : " << Personne::getInformations()
<< " : " << _matricule << " ; " << std::endl;
return ss.str();
}
int Externe::getSalaire() const
{
return 2500;
}
int Interne::getSalaire() const
{
return 1200;
}
Personne* Personne::getSuivant() const
{
return _pSuivant;
}
const std::string& Personne::getNom() const
{
return _nom;
}
void Personne::setSuivant(Personne* suivant)
{
_pSuivant = suivant;
}
void Personne::print(std::ostream& out) const
{
out << _nom << ':';
out << _prenom << ':';
out << _telephone << ':';
}
void Externe::print(std::ostream& out) const
{
out << _fax << std::endl;
}
void Interne::print(std::ostream& out) const
{
out << _matricule << std::endl;
}
Entreprise::Entreprise() : _pPremier(0), _pCourant(0)
{
}
void Entreprise::supprimer(Personne* pEle)
{
*_pPremier = *pEle;
_pCourant = 0;
std::string name;
std::ofstream Fichier("Entreprise.txt");
if (Fichier.eof())
std::cout << "La liste est vide!\n";
else
{
while (_pPremier->getNom() == name && _pPremier->getSuivant() != NULL)
{
_pCourant = _pPremier;
_pPremier = _pPremier->getSuivant();
}
if (_pPremier->getNom() != name)
{
if (_pCourant == 0)
{
_pPremier = (*pEle).getSuivant();
delete(pEle);
pEle = _pPremier;
}
else
{
_pCourant->setSuivant(_pPremier->getSuivant());
delete(_pPremier);
}
std::cout << "Fiche " << name << " effac,e avec succ1B;%GŠ1B;%@s!\n";
}
else
std::cout << "\nErreur: Personne" << name << " non trouv,e!\n";
}
}
void Entreprise::enregistrer()
{
remove("Entreprise.txt");
std::ofstream fichier("Entreprise.txt");
if (!fichier)
return;
Personne* pTemp = 0;
pTemp = _pPremier;
while (pTemp)
{
fichier << (pTemp->getInformations());
pTemp = pTemp->getSuivant();
}
fichier.close();
}
void Entreprise::recuperer()
{
std::ifstream fichier("Entreprise.txt");
if (!fichier)
return;
std::string line;
while (std::getline(fichier, line))
{
const std::vector<std::string>& tmp = split(line, ':');
typedef std::vector<std::string>::const_iterator iter;
for (iter it = tmp.begin(); it != tmp.end(); ++it)
{
std::cout << *it << " | ";
}
std::cout << std::endl;
}
}
void Entreprise::ajouter(Personne *pEle)
{
if (_pPremier == 0)
{
//initialisation des pointeurs grace a l'objet
_pPremier = pEle;
_pCourant = pEle;
}
else
{
_pCourant->setSuivant(pEle);
_pCourant = _pCourant->getSuivant();
}
}
void Entreprise::afficheTout()
{
Personne *pTemp;
pTemp = _pPremier;
while (pTemp)
{
pTemp->affiche();
pTemp = pTemp->getSuivant();
}
}
std::ostream& operator<< (std::ostream& out, Personne* v)
{
v->print(out);
return out;
}
std::istream& operator>> (std::istream& in, Personne* v)
{
// Recuperation des donnees du Fichier
// (voir fonction recuperer()) jusqu'au caractere ':'
std::getline(in, v->_nom, ':');
std::getline(in, v->_prenom, ':');
std::getline(in, v->_telephone, ':');
return in;
}
Entreprise::~Entreprise()
{
// appel de la fonction Enregistrer avant appel du destructeur
enregistrer();
delete _pPremier;
}
Personne* saisirPersonne()
{
bool continuer = true;
int choix;
Personne *pEle = 0;
std::string nom;
std::string prenom;
std::string telephone;
int matricule = 0;
int fax = 0;
while (continuer)
{
std::cout << " Nom : ";
std::getline(std::cin, nom);
std::cout << std::endl << " Prenom : ";
std::getline(std::cin, prenom);
std::cout << std::endl << " Telephone : ";
std::getline(std::cin, telephone);
std::cout << "Vous etes interne a l'entreprise, taper 1" << std::endl
<< "Vous etes externe a l'entreprise, taper 2" << std::endl
<< "Votre choix : ";
std::cin >> choix;
switch (choix)
{
case 1:
std::cout << std::endl << "Matricule : ";
std::cin >> matricule;
pEle = new Interne(nom, prenom, telephone, matricule);
continuer = false;
break;
case 2:
std::cout << std::endl << "Fax : ";
std::cin >> fax;
pEle = new Externe(nom, prenom, telephone, fax);
continuer = false;
break;
default:
std::cerr << "Erreur choix invalide, recommencez" << std::endl;
}
}
return pEle;
}
Personne* supprimerPersonne()
{
std::string name;
std::cout << " Nom de l'Personne a supprimer : ";
std::getline(std::cin, name);
}
void menu()
{
Entreprise cours;
int option = 0;
bool continuer = true;
while (continuer)
{
std::cout << "\nChoisissez une option de menu \n";
std::cout << "1-Creation d'une Fiche \n"
"2-Afficher Liste \n"
"3-Supprimer une Fiche \n"
"4-Enregistrer dans un fichier \n"
"5-Charger depuis un fichier \n"
"0-Quitter \n\n"
"Votre choix : ";
std::cin >> option;
std::cin.ignore();
switch (option)
{
case 1:
cours.ajouter(saisirPersonne());
break;
case 2:
cours.afficheTout();
break;
case 3:
cours.supprimer(supprimerPersonne());
break;
case 4:
cours.enregistrer();
break;
case 5:
cours.recuperer();
break;
case 0:
continuer = false;
break;
default :
std::cerr << std::endl << "Choix invalide\a" << std::endl;
}
}
}
int main()
{
menu();
return 0;
}