C++: Correction Gestionnaire de documents

 

Première version sans lien dynamique

#include <iostream.h>
#include <vector>
#include <string>

class Document {
public:
  enum Type { _texte_, _pdf_ };
  Document(Type _type, string _code, string _path) : type_(_type), code_(_code), path_(_path) {}
  string code() const { return code_; }
  Type type() const { return type_; }

  void ouvrir() const {
    string commande;
    switch (type_) {
      case _texte_ : commande = string("emacs ") + path_; break;
      case _pdf_ : commande = string("acroread ") + path_; break;
    }
    system(commande.c_str());
  }

protected:
  Type type_;
  string code_;
  string path_;
};

class GestDoc {
public:
  GestDoc() {}
  ~GestDoc() {
    for (size_t n = 0; n < documents.size(); n++) {
      delete documents[n];
    }
  }

  void ajouter(Document::Type _t, string _code, string _path) {
    ajouter(new Document(_t, _code, _path));
  }

  const Document * trouver(string _code) const {
    for (size_t n = 0; n < documents.size(); n++) {
      if (_code == documents[n]->code()) {
	return documents[n];
      }
    }
    return 0;
  }

  bool ouvrir(string _code) const {
    const Document * found = trouver(_code);
    if (found) {
      found->ouvrir();
      return true;
    }
    return false;
  }

private:
  const GestDoc & operator = (const GestDoc & _autre) { return *this; }
  GestDoc(const GestDoc & _autre) { }

protected:
  void ajouter(Document * _doc) {
    documents.push_back(_doc);
  }

  vector<Document*> documents;
};

int main() {
  GestDoc gd;

  char c = 0;
  while (c != 'q') {
    cout << "(a)jouter (o)uvrir (q)uitter\n";
    cin >> c;
    switch (c) {
      case 'a' : {
	string code, path, type;
	cout << "type (texte ou pdf), code et path : ";
	cin >> type >> code >> path;
	if (type == "texte") {
	  gd.ajouter(Document::_texte_, code, path);
	} else if (type == "pdf") {
	  gd.ajouter(Document::_pdf_, code, path);
	} else {
	  cout << "type de document non géré." << endl;
	}
	break;
      }
      case 'o' : {
	string code;
	cout << "code : ";
	cin >> code;
	if (!gd.ouvrir(code)) {
	  cout << "impossible d'ouvrir le document." << endl;
	}
	break;
      }
      default : if (c != 'q') cout << "choix invalide." << endl;
    }
  }
}

Seconde version avec liens dynamiques

#include <iostream.h>
#include <vector>
#include <string>

class Document {
public:
  Document(string _code, string _path) : code_(_code), path_(_path) {}
  string code() const { return code_; }
  void ouvrir() const {
    string commande(readerName() + " " + path_);
    system(commande.c_str());
  }
  virtual string readerName() const = 0;

protected:
  string code_;
  string path_;
};

class DocumentTexte : public Document {
public:
  DocumentTexte(string _code, string _path) : Document(_code,_path) {}
  string readerName() const { return "emacs"; }
};

class DocumentPdf : public Document {
public:
  DocumentPdf(string _code, string _path) : Document(_code,_path) {}
  string readerName() const { return "acroread"; }
};


class GestDoc {
public:
  GestDoc() {}
  ~GestDoc() {
    for (size_t n = 0; n < documents.size(); n++) {
      delete documents[n];
    }
  }

  void ajouterDocumentTexte(string _code, string _path) {
    ajouter(new DocumentTexte(_code, _path));
  }
  void ajouterDocumentPdf(string _code, string _path) {
    ajouter(new DocumentPdf(_code, _path));
  }

  const Document * trouver(string _code) const {
    for (size_t n = 0; n < documents.size(); n++) {
      if (_code == documents[n]->code()) {
	return documents[n];
      }
    }
    return 0;
  }

  bool ouvrir(string _code) const {
    const Document * found = trouver(_code);
    if (found) {
      found->ouvrir();
      return true;
    }
    return false;
  }

private:
  const GestDoc & operator = (const GestDoc & _autre) { return *this; }
  GestDoc(const GestDoc & _autre) { }

protected:
  void ajouter(Document * _doc) {
    documents.push_back(_doc);
  }

  vector<Document*> documents;
};

int main() {
  GestDoc gd;

  char c = 0;
  while (c != 'q') {
    cout << "(a)jouter (o)uvrir (q)uitter\n";
    cin >> c;
    switch (c) {
      case 'a' : {
	string code, path, type;
	cout << "type (texte ou pdf), code et path : ";
	cin >> type >> code >> path;
	if (type == "texte") {
	  gd.ajouterDocumentTexte(code, path);
	} else if (type == "pdf") {
	  gd.ajouterDocumentPdf(code, path);
	} else {
	  cout << "type de document non géré." << endl;
	}
	break;
      }
      case 'o' : {
	string code;
	cout << "code : ";
	cin >> code;
	if (!gd.ouvrir(code)) {
	  cout << "impossible d'ouvrir le document." << endl;
	}
	break;
      }
      default : if (c != 'q') cout << "choix invalide." << endl;
    }
  }
}