Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora
Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora
WF-1000X M6 è la sesta generazione di auricolare in-ear sviluppata da Sony, un prodotto che punta a coniugare facilità di utilizzo con una elevata qualità di riproduzione dei contenuti audio e una cura nella riduzione del rumore ambientale che sia da riferimento
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI
Snowflake ha presentato diverse novità per la sua piattaforma legate all'intelligenza artificiale. Quella forse più eclatante è una collaborazione con OpenAI, ma non mancano diverse nuove funzionalità che rendono la piattaforma più flessibile e in grado di rispondere meglio alle esigenze in continuo cambiamento delle aziende
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI
Con velocità teoriche fino a 11 Gbps, gestione tramite app intelligente e protezione avanzata dei dispositivi, Roamii BE Pro porta il Wi‑Fi 7 tri‑band nelle abitazioni più esigenti. Un sistema Wi-Fi Mesh proposto da MSI allo scopo di garantire agli utenti una rete fluida e continua capace di sostenere streaming 8K, gaming competitivo e le applicazioni moderne più esigenti in termini di banda
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 17-02-2011, 15:21   #1
number15
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 425
[C]Aiuto per progetto algoritmi

Ciao a tutti, son alle prese con un progetto per l'università, un clone del Bunga Bunga dell'università di Insubria postato qua

Il mio è questo.

Come struttura sto usando un albero RB.
Posto parte del main:
Codice:
int main(void){
   char azione[10]; /*inizio stringa, mi indica l'azione da fare- vedere come migliorarlo */
   
   bigInt hash_name = 0; /* per salvare l'hash del nome */
   
   /* per salvare i dati passati quando azione = IN */
   char name[MAXWORD]; 
   char sex; 
   int money, age, height, weight;
   float hair, build;
   char days[MAXWORD]; 
   
   /* inizializzo albero*/
   rbtree *invitati = createrbtree();
   
   /* Puntatori a struttura nodo */
   Invitato *p;
   
  /* Lettura input ed esecuzione comandi */
   do {
      scanf("%s", azione);
      
      if (strcmp(azione, "in") == 0) { /* azione input inserimento */
         scanf("%s %c %d %d %d %d %f %f %s", name, &sex, &money, &age, &height, &weight,
                                    &hair, &build, days);
                                    
         /* calcola key intera per il nome */
         hash_name = hash(name);
      
         if (!search(invitati, hash_name)) { /* se non esiste un invitato di quel nome*/
            rbinsert(invitati, hash_name, name, sex, money, age, height, weight, hair, build, days);
         } /* chiusura if search*/
         
         else { /*  aggiorno invitato*/
            p = search(invitati, hash_name);
            update_invitato(p, hash_name, name, sex, money, age, height, weight, hair, build, days);
         }
      } /* chiusura if in */

      
      else if (strcmp(azione, "stampa") == 0) {  /* azione input stampa invitato */  
         scanf("%s", name);
         hash_name = hash(name);
         p = search(invitati, hash_name); /* cerco invitato*/

         if (p)
            stampa_invitato(p);
         
         else 
            printf("Non ci sono invitati di nome %s\n", name);
      }
      
      else if (strcmp(azione, "out") == 0) {/* azione input estrometti */
         scanf("%s", name);
         hash_name = hash(name);
         p = search(invitati, hash_name); /* cerco invitato*/
         rbdelete(invitati, p);
      }
      /* fa casino, stampa quello cancellato e non un altro... NON ORDINA*/
      else if (strcmp(azione, "invitati") == 0) {/* azione input invitati */
         p = treemin(invitati);
         printf("(\n");
         while(p) {
            printf("%s\n", p->i);
            p = treesucc(invitati, p);
         } /* end of while */
         printf(")\n");
      }
Il codice dell'algorimto rb è questo:
Codice:
/* CREATE RB TREE */
rbtree *createrbtree(void) {
    rbtree *p = malloc(sizeof(rbtree));

    if(!p) {
        fprintf(stderr,"Errore di allocazione A\n");
            exit(-1);
    }
    
    p->root = malloc(sizeof(Invitato));
    
    if(!p->root) {
        fprintf(stderr,"Errore di allocazione B\n");
            exit(-2);
    }
    
    p->nil = p->root;
    p->nil->sx = p->nil->dx = p->nil->up = p->nil;
    p->nil->c = black;
    return p;
}

/* SORT */
void inord(Invitato *p, Invitato *nil, void (*op)(Invitato *))
{
    if(p != nil) {
            inord(p->sx,nil,op);
        (*op)(p);
            inord(p->dx,nil,op);
    }
}


/* SORT BY KEY */
void inorder(rbtree *p, void (*op)(Invitato *))
{
    inord(p->root, p->nil, op);
}


/* SEARCH */
Invitato *search(rbtree *r, key k)
{
    Invitato *p = r->root;

    while(p != r->nil && k != p->v)
        p = k < p->v ? p->sx : p->dx;
    return p == r->nil ? NULL : p;
}


Invitato *rbtmin(Invitato *p, Invitato *nil)
{
  for(;p->sx != nil;p = p->sx);
  return p;
}


Invitato *rbtmax(Invitato *p, Invitato *nil)
{
    for(;p->dx != nil;p = p->dx);
    return p;
}

/* MIN */
Invitato *treemin(rbtree *r)
{
    return rbtmin(r->root,r->nil);
}

/* MAX */
Invitato *treemax(rbtree *r)
{
    return rbtmax(r->root,r->nil);
}

/* NODO SUCESSIVO */
Invitato *treesucc(rbtree *r, Invitato *q)
{
    Invitato *qq;

    if(q->dx != r->nil)
        return rbtmin(q->dx,r->nil);
    qq = q->up; 
    while(qq != r->nil && q == qq->dx) {
        q = qq;
        qq = qq->up;
    }
    return qq == r->nil ? NULL : qq;
}


/* NODO PRECEDENTE */
Invitato *treepred(rbtree *r, Invitato *q)
{
    Invitato *qq;

    if(q->sx != r->nil)
        return rbtmax(q->sx,r->nil);
    qq = q->up; 
    while(qq != r->nil && q == qq->sx) {
        q = qq;
        qq = qq->up;
    }
    return qq == r->nil ? NULL : qq;
}


void sxrotate(rbtree *r, Invitato *x)
{
    Invitato *y = x->dx;
    
    x->dx = y->sx;
    if(y->sx != r->nil)
        y->sx->up = x;
    y->up = x->up;
    if(x->up == r->nil)
        r->root = y;
    else
        if(x == x->up->sx)
            y->up->sx = y;
        else
            y->up->dx = y;
    y->sx = x;
    x->up = y;
}


void dxrotate(rbtree *r, Invitato *x)
{
    Invitato *y = x->sx;
    
    x->sx = y->dx;
    if(y->dx != r->nil)
        y->dx->up = x;
    y->up = x->up;
    if(x->up == r->nil)
        r->root = y;
    else
        if(x == x->up->dx)
            y->up->dx = y;
        else
            y->up->sx = y;
    y->dx = x;
    x->up = y;
}               


Invitato *simpleinsert(rbtree *tree, key hash_name, nome name, sesso sex, 
                       denaro money, eta age, altezza height, peso weight,
                       capelli hair, costituzione build, presenza days)
{
    Invitato *q = malloc(sizeof(Invitato));
    Invitato *r = tree->root;
    Invitato *s = tree->nil;

    

    q->i = malloc((strlen(name)+1)*sizeof(char));
 //   q->s = malloc((strlen(sesso)+1)*sizeof(enum));
//    q->d = malloc((strlen(money)+1)*sizeof(char));
//    q->e = malloc((strlen(age)+1)*sizeof(char));
//    q->h = malloc((strlen(height)+1)*sizeof(char));
//    q->w = malloc((strlen(weigth)+1)*sizeof(char));
//    q->cap = malloc((strlen(hair)+1)*sizeof(char));
//    q->b = malloc((strlen(build)+1)*sizeof(char));
    q->p = malloc((strlen(days)+1)*sizeof(char));

    if(!q) { 
        fprintf(stderr,"Errore di allocazione C\n");
            exit(-4);
    }
    q->v = hash_name;
    strcpy(q->i, name);
    q->s = sex;  
    q->d = money;  
    q->e = age;  
    q->h = height;  
    q->w = weight;  
    q->cap = hair;  
    q->b = build;  
    strcpy(q->p, days);
    q->sx = q->dx = tree->nil;
    q->c = red;
    while(r != tree->nil) {
        s = r;
        r = hash_name < r->v ? r->sx : r->dx;
    }
    q->up = s;
    if(s == tree->nil)
        return tree->root = q;
    if(hash_name < s->v)
        s->sx = q;
    else
        s->dx = q;
    return q;
}


void rbinsert(rbtree *tree, key hash_name, nome name, sesso sex, denaro money, 
              eta age, altezza height, peso weight, capelli hair, 
              costituzione build, presenza days)
{
    Invitato *x = simpleinsert(tree, hash_name, name, sex, money, 
                                 age, height, weight, hair, build, days);
    Invitato *y;

    while(x != tree->root && x->up->c == red) {
        if(x->up == x->up->up->sx) {                 /* caso L */
            y = x->up->up->dx;
            if(y->c == red) {
                x->up->c = black;                  /* caso 1L */
                y->c = black;                      /* caso 1L */
                x->up->up->c = red;                /* caso 1L */
                x = x->up->up;                     /* caso 1L */
            } else {
                if(x == x->up->dx)              /* caso 2L */
                    sxrotate(tree,x = x->up);  /* caso 2L */              
                x->up->c = black;                  /* caso 3L */
                x->up->up->c = red;                /* caso 3L */
                dxrotate(tree,x->up->up);       /* caso 3L */
            }
        } else {                                       /* caso R */
            y = x->up->up->sx;
            if(y->c == red) {
                x->up->c = black;                  /* caso 1R */
                y->c = black;                      /* caso 1R */
                x->up->up->c = red;                /* caso 1R */
                x = x->up->up;                     /* caso 1R */
            } else {
                if(x == x->up->sx)               /* caso 2R */
                    dxrotate(tree,x = x->up); /* caso 2R */                  
                x->up->c = black;                  /* caso 3R */
                x->up->up->c = red;                /* caso 3R */
                sxrotate(tree,x->up->up);        /* caso 3R */
            }
        }
    }
    tree->root->c = black;
}


/* FIXUP RBTREE */
void fixup(rbtree *tree, Invitato *x)
{
    Invitato *w;

    while(x != tree->root && x->c == black) {
        if(x == x->up->sx) {                                    /* caso L */
            if((w = x->up->dx)->c == red) {
                w->c = black;                                 /* caso 1L */
                x->up->c = red;                               /* caso 1L */
                sxrotate(tree,x->up);                       /* caso 1L */
                w = x->up->dx;                             /* caso 1L */
            }
            if(w->sx->c == black && w->dx->c == black) {
                w->c = red;                                   /* caso 2L */
                x = x->up;                                    /* caso 2L */
            } else {
                if(w->dx->c == black) {
                    w->sx->c = black;                     /* caso 3L */
                    w->c = red;                             /* caso 3L */
                    dxrotate(tree,w);                    /* caso 3L */
                    w = x->up->dx;                       /* caso 3L */
                }
                w->c = x->up->c;                              /* caso 4L */
                x->up->c = black;                             /* caso 4L */
                w->dx->c = black;                          /* caso 4L */
                sxrotate(tree,x->up);                       /* caso 4L */
                x = tree->root;                               /* caso 4L */
            }
        } else {                                                  /* caso R */
            if((w = x->up->sx)->c == red) {                   
                w->c = black;                                 /* caso 1R */
                x->up->c = red;                               /* caso 1R */
                dxrotate(tree,x->up);                      /* caso 1R */
                w = x->up->sx;                              /* caso 1R */
            }
            if(w->dx->c == black && w->sx->c == black) {
                w->c = red;                                   /* caso 2R */
                x = x->up;                                    /* caso 2R */
            } else {
                if(w->sx->c == black) {
                    w->dx->c = black;                    /* caso 3R */
                    w->c = red;                             /* caso 3R */
                    sxrotate(tree,w);                     /* caso 3R */
                    w = x->up->sx;                        /* caso 3R */
                }
                w->c = x->up->c;                              /* caso 4R */
                x->up->c = black;                             /* caso 4R */
                w->sx->c = black;                           /* caso 4R */
                dxrotate(tree,x->up);                      /* caso 4R */
                x = tree->root;                               /* caso 4R */
            }
        }
    }
    x->c = black;
}


void rbdelete(rbtree *tree, Invitato *q)
{
    Invitato *r, *s;

    if(q->sx == tree->nil || q->dx == tree->nil)
        r = q;
    else
        r = treesucc(tree,q);
    s = r->sx != tree->nil ? r->sx : r->dx;
    s->up = r->up;
    if(r->up == tree->nil)
        tree->root = s;
    else
        if(r == r->up->sx)
            r->up->sx = s;
        else
            r->up->dx = s;
    if(r != q)
        q->v = r->v;
    if(r->c == black)
        fixup(tree, s);     
    free(r);
}

L'inserimento, aggiornamento, cancellazione e stampa funzionano.
mi fa casino invece il caso 'invitati'.
Dovrei stamparli in ordine alfabeti.
A volte va, a volte no e inoltre se elimino un invitato (out invitato), capita che facendo 'invitati' me lo mostri ancora, mentre non ne compare più un altro.
La conferma dell'eliminazione però ce l'ho in quanto facendo 'stampa invitato' mi dice che non esiste.

Ho anche un problema con la malloc che ho commentato.
Poi altri dubbi li posto più avanti.
Se poteste aiutarmi ve ne sarei molto grato.
number15 è offline   Rispondi citando il messaggio o parte di esso
Old 18-02-2011, 12:24   #2
number15
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 425
number15 è offline   Rispondi citando il messaggio o parte di esso
Old 19-02-2011, 12:14   #3
number15
Senior Member
 
Iscritto dal: Apr 2007
Messaggi: 425
Proprio nessuno?
number15 è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Sony WF-1000X M6: le cuffie in-ear di riferimento migliorano ancora Sony WF-1000X M6: le cuffie in-ear di riferiment...
Snowflake porta l'IA dove sono i dati, anche grazie a un accordo con OpenAI Snowflake porta l'IA dove sono i dati, anche gra...
Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo MSI Sistema Mesh Roamii BE Pro: il Wi-Fi 7 secondo M...
Recensione HUAWEI Mate X7: un foldable ottimo, ma restano i soliti problemi Recensione HUAWEI Mate X7: un foldable ottimo, m...
Nioh 3: souls-like punitivo e Action RPG Nioh 3: souls-like punitivo e Action RPG
Starship Troopers: Ultimate Bug Wars, to...
Il razzo spaziale europeo Ariane 6, per ...
Oracle Fusion Cloud Applications si pote...
OHB Italia svilupperà un satellit...
Fortinet: "Ora abbiamo una chance d...
Linux Mint chiude con gli aggiornamenti ...
Compressori portatili auto in sconto su ...
Durante il lancio della missione USSF-87...
Dopo il ritiro di Intel da Magdeburgo, l...
Xiaomi 15T scende a 388€ su Amazon: 12GB...
MSI Afterburner: arriva il monitoraggio ...
Missione cinese Chang'e-6: confermata l'...
Addio esenzione sotto i 150 euro: l'UE i...
Allarme riavvii su Windows 11 dopo il ri...
Dynatrace Intelligence: osservabilit&agr...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 18:30.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v