|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Sep 2003
Città: Torino
Messaggi: 752
|
[Help] strano problema Dev-cpp
Ciao a tutti, allora non ho mai avuto/visto questo problema:
ho fatto un programma in c, la compilazione non da errori, ma quando lo lancio sembra fare una marea di calcoli, e non mi mostra a video l'output che dovrebbe. Ok, allora faccio il debug per vedere dove ho sbagliato...ma NON ho sbagliato!!! Dal debug funziona tutto a meraviglia! Faccio semplicemente "step successivo" dall'inizio alla fine e fila tutto liscio come l'olio. Che è successo ![]() ![]() ![]() Grazie a tutti come sempre ![]()
__________________
ubuntulinux | Windows 7
Trattato con: enghel | thunder01 | char66 | siemens | topogatto | ::tony design | alcol | mammabell | uazzamerican | niko0 | oldfield | |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2775
|
Posta il codice, così vediamo.
|
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Sep 2003
Città: Torino
Messaggi: 752
|
Quote:
Codice:
#include <stdio.h> #include <string.h> #include <stdlib.h> /*controllare che non ne manchi qualcuno*/ #define input "input.txt" #define MAXNOME 31 typedef struct bst_t { char *nome; struct bst_t *sinistra; struct bst_t *destra; } bst; /*prototipi*/ bst* cercaposto(bst *,char *,short int *); void stampaalbero(bst *); short soluzione(bst *,char *,char *,short *,short *); short visitoinordine(bst *,char *); int main(void) { bst *testa=NULL,*tmp,*posto; FILE *f; char buffer[MAXNOME], K1[MAXNOME], K2[MAXNOME]; int i; short int dir=-1; /*0->sx 1->dx _ per inserimento nel bst*/ short trov1=0,trov2=0; printf("Esercitazione 5 - APA - BST\n"); /*SEZIONE LETTURA DEL FILE*/ if ( (f=fopen(input,"r"))== NULL ) { printf("Errore apertura file.Esco.\n"); return 0; } /*azzero il buffer*/ for(i=0;i<MAXNOME;i++) buffer[i]='0'; /*tengo conto che i nomi possono contenere degli spazi*/ while(!feof(f)) { if(testa==NULL) { fscanf(f,"%s",buffer); if( (testa=(bst*)calloc(1,sizeof(bst)))==NULL) { printf("Errore allocazione memoria.\n"); return; } if( (testa->nome=(char*)calloc(strlen(buffer),sizeof(char)))==NULL) { printf("Errore allocazione memoria.\n"); return; } testa->sinistra=testa->destra=NULL; strcpy(testa->nome,buffer); } else { fscanf(f,"%s",buffer); if( (tmp=(bst*)calloc(1,sizeof(bst)))==NULL ) { printf("Errore allocazione memoria.\n"); return; } if( (tmp->nome=(char*)calloc(strlen(buffer),sizeof(char)))==NULL) { printf("Errore allocazione memoria.\n"); return; } tmp->sinistra=tmp->destra=NULL; strcpy(tmp->nome,buffer); posto=cercaposto(testa,tmp->nome,&dir); /*CERCO LA POSIZIONE CORRETTA IN CUI INSERIRE IL NUOVO DATO.*/ if (posto==NULL) free(tmp); if (dir==1) posto->destra=tmp; else if (dir==0) posto->sinistra=tmp; } } fclose(f); /*FINE SEZIONE*/ printf("Elenco delle chiavi caricate:\n"); stampaalbero(testa); /*SEZIONE LETTURA DELLE CHIAVI TRA CUI CERCARE.*/ printf("Inserisci le due chiavi agli estremi dell'intervallo che vuoi visualizzare.\n"); /*Leggo K1 e K2, controllo che l'input sia corretto.*/ do{ printf("Inseriscile in ordine alfabetico!\n"); do { printf("K1: "); fgets(buffer,MAXNOME,stdin); fflush(stdin); } while ( (sscanf(buffer,"%s",K1)) != 1 ); do { printf("K2: "); fgets(buffer,MAXNOME,stdin); fflush(stdin); } while ( (sscanf(buffer,"%s",K2)) != 1 ); } while ( strcmp(K1,K2)>=0 ); /*FINE SEZIONE*/ /*CALCOLO LE CHIAVI COMPRESE TRA K1 e K2*/ if ( soluzione(testa,K1,K2,&trov1,&trov2)==0 ) { printf("Esecuzione interrotta.\n"); if(trov1==0) printf("La chiave k1 non esiste.\n"); else if(trov2==0) printf("La chiave k2 non esiste.\n"); printf("Premi un tasto per uscire.\n"); getchar(); return 0; } /*CONLCUDO*/ else { printf("Esecuzione terminata con successo, premi un tasto per uscire.\nBuona giornata!\n"); getchar(); return 0; } } bst* cercaposto(bst *rad,char *nome,short int *direzione) { bst *tmp; if(rad!=NULL) { if(strcmp(rad->nome,nome)==0) return NULL; else if(strcmp(rad->nome,nome)<0) { if (rad->destra!=NULL) tmp=cercaposto(rad->destra,nome,direzione); else { *direzione=1; return rad; } } else { if (rad->sinistra!=NULL) tmp=cercaposto(rad->sinistra,nome,direzione); else { *direzione=0; return rad; } } return tmp; } } void stampaalbero(bst *rad) /*stampa in-order*/ { if(rad!=NULL) { stampaalbero(rad->sinistra); printf("%s\n",rad->nome); stampaalbero(rad->destra); } return; } short soluzione(bst *rad,char *k1,char *k2,short *trovato1,short *trovato2) /*trovo e stampo le chiavi richieste*/ { if(rad!=NULL) { if( strcmp(k1,rad->nome)==0 ) { *trovato1=1; printf("%s\n",rad->nome); *trovato2=visitoinordine(rad->destra,k2); return *trovato2; } else if( strcmp(k1,rad->nome)<0) soluzione(rad->sinistra,k1,k2,trovato1,trovato2); else soluzione(rad->destra,k1,k2,trovato1,trovato2); if (*trovato1==1 && *trovato2==0 && strcmp(rad->nome,k2)!=0 ) { printf("%s\n",rad->nome); *trovato2=visitoinordine(rad->destra,k2); return *trovato2; } else if(strcmp(rad->nome,k2)==0) printf("%s\n",rad->nome); } if(*trovato1==0 || *trovato2==0) return 0; else return 1; } short visitoinordine(bst *t,char *k2) { int a; if(t!=NULL) { a=visitoinordine(t->sinistra,k2); if(strcmp(t->nome,k2)<0) { printf("%s\n",t->nome); a=visitoinordine(t->destra,k2); } else if(strcmp(t->nome,k2)==0) { printf("%s\n",t->nome); return 1; } /* else a=visitoinordine(t->sinistra,k2);*/ return a; } else return 0; } Devo cercare di visitare in modo efficiente l'albero (cioè di non visitarlo tutto, altrimenti è facile (ma inefficiente) ![]()
__________________
ubuntulinux | Windows 7
Trattato con: enghel | thunder01 | char66 | siemens | topogatto | ::tony design | alcol | mammabell | uazzamerican | niko0 | oldfield | Ultima modifica di Ciocco@256 : 15-04-2006 alle 17:24. |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2775
|
A me funziona.. L'unica cosa che ho dovuto fare (perchè altrimenti non mi compilava) è stata aggiungere degli zeri dopo alcuni "return".. Per fare il test ho creato un file io e ci ho scritto un po' di caxxate, forse tu usi un file molto + grande, non so..
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 13:56.