Advertisement
sartions

generating-diagram

May 6th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. /*                                                      |
  2. |   @author Adrián Gil Andrés                         |
  3. |   @date 06-05-2016                                    |
  4. |   compilado con g++ en linux y corrido con ./a.out    |
  5. |   (debian "sudo apt-get install g++" for compiler)    |
  6. |   tener en la misma carpeta el archivo data.txt       |
  7. |   probar salida con dot -Tpng -O graph.gv             |
  8. |                                                       */
  9.  
  10. #include <string>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <fstream>
  14.  
  15.  
  16. using namespace std;
  17.  
  18. int main(){
  19.     ifstream f;
  20.     string frase;                                   //cada linea de texto que cogemos
  21.     string val = "  '";                             //el valor (nombre) de cada nodo
  22.     string type;                                    //guardamos el tipo de nodo para la decoración
  23.     f.open ("data.txt");
  24.    
  25.     if(f.is_open()){                                //si abrimos correctamente el fichero
  26.         cout << "digraph {" << endl;           
  27.         while(!f.eof()){                            //mientras no se acabe el fichero
  28.             getline (f,frase);                      //leemos siguiente linea
  29.             for(int i = 1; i < frase.size(); i++){  //recorremos la cadena letra a letra
  30.                 if (frase[i] == '#'){               //si es # empezamos a coger el tipo
  31.                     val = "  '";
  32.                     i++;
  33.                     while(frase[i] != ' '){         //cuando hay espacio deja de ser nuestro tipo de nodo
  34.                         type = type + frase[i];
  35.                         i++;
  36.                     }
  37.                     i++;
  38.                 }
  39.                 val = val + frase[i];               //guardamos el resto de la linea como valor del nodo
  40.             }
  41.             cout << val + " [shape=";               //imprimimos
  42.             if(type=="attribute"){                  //como C++ no tiene switch para strings, lo hago con if-else (que tostón para hacerlo...)
  43.                 cout << "diamond]";
  44.             }else if(type=="cdata-section"){
  45.                 cout << "trapezium]";
  46.             }else if(type=="comment"){
  47.                 cout << "note]";
  48.             }else if(type=="element"){
  49.                 cout << "circle]";
  50.             }else if(type=="entity-reference"){
  51.                 cout << "oval]";
  52.             }else if(type=="processing-instruction"){
  53.                 cout << "triangle]";
  54.             }else if(type=="text"){
  55.                 cout << "plaintext]";
  56.             }
  57.             cout << "'" << endl;
  58.             val = "  '";                            //reiniciamos los datos
  59.             type = "";                      //para el siguiente nodo
  60.             //cout << "Leido: " << frase << frase.size() << endl;
  61.         }  
  62.     }else{
  63.         cout << "Could not open the file or file doesn't exist" << endl;  
  64.     }
  65.     cout << "}" << endl;
  66.     return 0;  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement