elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía rápida para descarga de herramientas gratuitas de seguridad y desinfección


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Problema al ordenar una Lista Doble(Lectura de XML)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema al ordenar una Lista Doble(Lectura de XML)  (Leído 1,925 veces)
falconez

Desconectado Desconectado

Mensajes: 18



Ver Perfil
Problema al ordenar una Lista Doble(Lectura de XML)
« en: 23 Febrero 2015, 03:21 am »

Alguien podría ayudarme a corregir mi error; lo que pasa es que estoy intentando ordenar mi lista Doble que la hice a partir de la lectura de un archivo xml.(La lectura funciona correctamente). Todo funciona hasta que ejecuta la funcion de ordenar y ahi deja de funcionar!

Les agradecería infinitamente si me ayudan a resolver este problema! Igual si encuentro la solución les aviso! Muchas Gracias de antemano!



Código:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>


using namespace std;

struct datos {
    string TITLE;
    string ARTIST;
    string COUNTRY;
    string COMPANY;
    float PRICE;
    int YEAR;
};

struct node {
    datos data;
    node *left;
    node *right;
};

string getTagName(string str);
string getTagContent(string str);
node * newList();
node * insert_right(node *list, datos nuevo_nodeo);
void printList(node* list);
void readXML(node *head);
void sortList(node **lista, int rule, bool dsc) ;


int main() {

    node *head = newList();
    node *current = head;

    //LEYENDO MI ARCHIVO XML
    readXML(head);


    cout << "IMPRIMIENDO LISTA" << endl << endl;
    printList(head);
     // Todo funciona hasta que le incorpora la parte para ordenar y la aplicacion deja de funcionar en ese momento!!
    
    
     sortList( &head, 1, true );  // Ordenando por precio, pero no me funciona y no encuentro el error!
    
     return 0;
}

node * newList() {
    node *nuevo;
    nuevo = new node;
    nuevo->data.ARTIST=-1;
     nuevo->data.COMPANY=-1;
      nuevo->data.COUNTRY=-1;
       nuevo->data.PRICE=-1;
        nuevo->data.TITLE=-1;
         nuevo->data.YEAR=-1;
    nuevo->right = nuevo;
    nuevo->left = nuevo;
    return nuevo;
}


string getTagName(string str) {
    int posInit, posFin;
    int delta;
    posInit = str.find("<");
    posFin = str.find(">");
    delta = posFin - posInit;
    return str.substr(posInit + 1, delta - 1);
}

string getTagContent(string str) {

    int posInit, posFin;
    int delta;
    posInit = str.find(">");
    posFin = str.find("</");
    delta = posFin - posInit;

    return str.substr(posInit + 1, delta - 1);
}

node * insert_right(node *list, datos nuevo_nodeo) {
    node *nuevo;
    nuevo = new node;

    nuevo->data.TITLE = nuevo_nodeo.TITLE;
    nuevo->data.ARTIST = nuevo_nodeo.ARTIST;
    nuevo->data.COUNTRY = nuevo_nodeo.COUNTRY;
    nuevo->data.COMPANY = nuevo_nodeo.COMPANY;
    nuevo->data.PRICE = nuevo_nodeo.PRICE;
    nuevo->data.YEAR = nuevo_nodeo.YEAR;

    nuevo->left = list;
    nuevo->right = list->right;
    list->right = nuevo;
    nuevo->right->left = nuevo;
    return nuevo;
}

void printList(node* list) {
    node *head = list;
    node *current = list;

    while (head != (current = current->right)) {
        cout << "------------------------" << endl;
        cout << current->data.TITLE << endl;
        cout << current->data.ARTIST << endl;
        cout << current->data.COUNTRY << endl;
        cout << current->data.COMPANY << endl;
        cout << current->data.PRICE << endl;
        cout << current->data.YEAR << endl;
    }

}

void readXML(node *current) {

    datos nuevo;
    string fileName = "cd_catalog.XML";
    string line;
    string tagName;
    int posInit, posFin;
    int delta;
    int ban = 0;
    cout << "-----archivo XML->" << fileName << "--------" << endl << endl << endl;
    ifstream myFile((char*) fileName.c_str());

    while (getline(myFile, line, '\n')) {
        tagName = getTagName(line);

        if (ban == 0) {
            datos nuevo; //Para acceder a mi estructura facilmente!
        }

        if (tagName.compare("TITLE") == 0) {
            nuevo.TITLE = getTagContent(line);
            ban++;
        } else if (tagName.compare("ARTIST") == 0) {
            nuevo.ARTIST = getTagContent(line);
            ban++;
        } else if (tagName.compare("COUNTRY") == 0) {
            nuevo.COUNTRY = getTagContent(line);
            ban++;
        } else if (tagName.compare("COMPANY") == 0) {
            nuevo.COMPANY = getTagContent(line);
            ban++;
        } else if (tagName.compare("PRICE") == 0) {
            nuevo.PRICE = atof(getTagContent(line).c_str());
            ban++;
        } else if (tagName.compare("YEAR") == 0) {
            nuevo.YEAR = atoi(getTagContent(line).c_str());
            ban++;
        }

        if (ban == 6) {
            current = insert_right(current, nuevo);
            ban = 0;
        }

    }

    myFile.close();
        
}



void sortList(node **lista, int rule, bool dsc) {

    // rule { 1 --> Price, 2 --> year, 3 --> Artis, 4 --> title }

    node *lst = *lista;

    node *sig;

    datos temp;

    //-- Boolean para validar situación de cambio ---//
    bool valid;

    do {

        sig = lst->right;

        while (sig->right != NULL) {

            switch (rule) {

                case 1:


                    valid = dsc ? (lst->data.PRICE > sig->data.PRICE) : (lst->data.PRICE < sig->data.PRICE);

                    break;

                case 2:


                    valid = dsc ? (lst->data.YEAR > sig->data.YEAR) : (lst->data.YEAR < sig->data.YEAR);

                    break;

                case 3:

                    valid = dsc ? (lst->data.ARTIST > sig->data.ARTIST) : (lst->data.ARTIST < sig->data.ARTIST);

                    break;

                case 4:

                    valid = dsc ? (lst->data.TITLE > sig->data.TITLE) : (lst->data.TITLE < sig->data.TITLE);

                    break;

            }

            if (valid) {

                temp = lst->data;

                lst->data = sig->data;

                sig->data = temp;

            }

            sig = sig->right;

        }

        lst = lst->right;

    } while (lst->right != NULL);

}




« Última modificación: 23 Febrero 2015, 03:45 am por falconez » En línea

falconez

Desconectado Desconectado

Mensajes: 18



Ver Perfil
Re: Problema al ordenar una Lista Doble(Lectura de XML)
« Respuesta #1 en: 23 Febrero 2015, 03:25 am »

Aquí les adjunto el archivo XML!

https://www.dropbox.com/s/4y7t6ziweqtfoqq/cd_catalog.xml?dl=0


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
AYUDA C#!!! COMO ORDENAR UAN LISTA
.NET (C#, VB.NET, ASP)
neo_angel_xxx 2 10,134 Último mensaje 27 Octubre 2010, 19:43 pm
por [D4N93R]
AYUDA ORDENAR LISTA SIMPLE[C]
Programación C/C++
HRSLASH 0 9,095 Último mensaje 23 Abril 2011, 04:22 am
por HRSLASH
LISTA DOBLE
Programación C/C++
leosansan 4 3,744 Último mensaje 29 Septiembre 2012, 21:33 pm
por leosansan
Cómo tener doble check de confirmación de lectura de correo gmail con ....
Noticias
wolfbcn 0 1,191 Último mensaje 28 Julio 2014, 14:17 pm
por wolfbcn
Problema al ordenar lista c++.
Programación C/C++
Gaspi 6 3,709 Último mensaje 20 Febrero 2015, 22:53 pm
por Gaspi
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines