ciao ragazzi ho alcuni problemi con il mio codice che dovrebbe gestire una rubrica telefonica....in particolare ho dei problemi gravi con la funzione per cancellare un contatto sulla quale vi chiedo di illuminarmi totalmente perche non essendo esperto credo proprio ke la mia funzione sia più che sbagliata....e poi ho un lieve problema con la funzione per la stampa della lista ke nello specifico di tutti i contatti che inserisco mi stampa sempre gli ultimi due....vi prego di aiutarmi e correggermi....con questa speranza vi posto il codice....grazie mille...
Codice PHP:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define DIM_numero 10
#define MAX_carattere 100
typedef struct { // DEFINIAMO IL TIPO CONTATTO.
char nome [MAX_carattere];
char cognome [MAX_carattere];
char numero_cell [DIM_numero];
char numero_casa [DIM_numero];
char email [MAX_carattere];
char indirizzo_web [MAX_carattere];
} tipi_contatti;
struct Contatto_in_rubrica;
typedef Contatto_in_rubrica *PuntContatto_in_rubrica;
typedef struct Contatto_in_rubrica {
tipi_contatti CONTATTO;
Contatto_in_rubrica * contattoSuccessivo;
} Contatto_in_rubrica;
PuntContatto_in_rubrica inserisci_in_ordine(PuntContatto_in_rubrica L, tipi_contatti *CONTATTO) { // FUNZIONE PER L'INSERIMENTO DI UN CONTATTO IN RUBRICA.
PuntContatto_in_rubrica succ, prec, new_node;
new_node=(PuntContatto_in_rubrica)malloc(sizeof (struct Contatto_in_rubrica));
if (new_node==NULL) {
printf("Errore nell'allocazione della memoria. \n");
exit (1);
};
new_node->contattoSuccessivo = NULL;
succ=L;
printf("Inserire il NOME del contatto: \n");
scanf("%s", &new_node->CONTATTO.nome);
printf("Inserire il COGNOME del contatto: \n");
scanf("%s", &new_node->CONTATTO.cognome);
printf("Inserire il NUMERO del contatto: \n");
scanf("%s", &new_node->CONTATTO.numero_cell);
printf("Inserire il NUMERO DI CASA del contatto: \n");
scanf("%s", &new_node->CONTATTO.numero_casa);
printf("Inserire l'INDIRIZZO EMAIL del contatto: \n");
scanf("%s", &new_node->CONTATTO.email);
printf("Inserire l'INDIRIZZO DI PAGINA WEB del contatto: \n");
scanf("%s", &new_node->CONTATTO.indirizzo_web);
printf("\n Il contatto è stato inserito in rubrica! \n");
if (succ == NULL) {
return new_node;
};
while (succ->contattoSuccessivo!=NULL) {
succ = succ->contattoSuccessivo;
};
succ->contattoSuccessivo = new_node;
return succ;
while (strncmp (new_node->CONTATTO.nome, succ->CONTATTO.nome, MAX_carattere) > 1) {
prec=succ;
succ=succ->contattoSuccessivo;
};
if (prec==NULL) {
new_node->contattoSuccessivo = succ;
return new_node;
}
else {
new_node->contattoSuccessivo = succ;
prec->contattoSuccessivo = new_node;
return L;
};
};
PuntContatto_in_rubrica cancella_tutto (PuntContatto_in_rubrica L) { // FUNZIONE PER LA CANCELLAZIONE DELLA RUBRICA.
L = L->contattoSuccessivo;
L == NULL;
free (L);
return L;
};
PuntContatto_in_rubrica cancella_un_contatto (PuntContatto_in_rubrica L, tipi_contatti *CONTATTO) { // FUNZIONE PER LA CANCELLAZIONE DI UN CONTATTO DALLA RUBRICA.
PuntContatto_in_rubrica succ, prec, temp;
temp->contattoSuccessivo = NULL;
succ = L;
printf("\n Inserire il NOME da eliminare: \n");
scanf("%s", &temp->CONTATTO.nome);
if (succ == NULL) {
printf("\n La rubrica è vuota oppure il contatto non è presente in rubrica. \n");
return L;
}
while (succ->contattoSuccessivo != NULL) {
succ = succ->contattoSuccessivo;
};
succ->contattoSuccessivo = temp;
return succ;
while (strncmp(temp->CONTATTO.nome, succ->CONTATTO.nome, MAX_carattere) >1) {
prec = succ;
succ = succ->contattoSuccessivo;
};
if (prec == NULL) {
temp->contattoSuccessivo = succ;
free (temp);
printf("\n Il contatto è stato rimosso dalla rubrica! \n");
return L;
}
else {
temp->contattoSuccessivo = succ;
prec->contattoSuccessivo = temp;
free (temp);
printf("\n Il contatto è stato rimosso dalla rubrica! \n");
return L;
};
};
PuntContatto_in_rubrica cerca_contatto (PuntContatto_in_rubrica L, tipi_contatti *CONTATTO) { // FUNZIONE PER LA RICERCA DI UN CONTATTO IN RUBRICA.
PuntContatto_in_rubrica temp;
printf("\n Inserire il NOME da ricercare: \n");
scanf("%s", &temp->CONTATTO.nome);
if (L == NULL) {
printf("\n La rubrica è vuota oppure il contatto non è presente in rubrica. \n");
return L;
};
if (strncmp(temp->CONTATTO.nome, L->CONTATTO.nome, MAX_carattere) != 0) {
L = L->contattoSuccessivo;
}
else {
temp = L;
printf("\n Il contatto è presente in rubrica: \n \n NOME: %s \n COGNOME: %s \n NUMERO DI CELLULARE: %s \n NUMERO DI CASA: %s \n INDIRIZZO DI POSTA ELETTRONICA: %s \n INDIRIZZO DI PAGINA WEB: %s \n", &temp->CONTATTO.nome, &temp->CONTATTO.cognome, &temp->CONTATTO.numero_cell, &temp->CONTATTO.numero_casa, &temp->CONTATTO.email, &temp->CONTATTO.indirizzo_web);
return L;
};
};
void visualizza_rubrica (PuntContatto_in_rubrica L) { // FUNZIONE PER LA VISUALIZZAZIONE DELLA RUBRICA.
PuntContatto_in_rubrica temp;
temp = L;
while (temp != NULL) {
printf("\n NOME: %s\n", temp->CONTATTO.nome);
printf(" COGNOME: %s\n", temp->CONTATTO.cognome);
printf(" NUMERO: %s\n", temp->CONTATTO.numero_cell);
printf(" NUMERO DI CASA: %s\n", temp->CONTATTO.numero_casa);
printf(" INDIRIZZO EMAIL: %s\n", temp->CONTATTO.email);
printf(" INDIRIZZO DI PAGINA WEB: %s\n", temp->CONTATTO.indirizzo_web);
printf("\n __________________________________________________________________ \n");
temp = temp->contattoSuccessivo;
}
if (L == NULL) {
printf("\n La rubrica è priva di contatti. \n");
return;
};
};
int main () { // FUNZIONE PRINCIPALE DELLA RUBRICA.
PuntContatto_in_rubrica L=NULL;
tipi_contatti CONTATTO;
int scelta;
printf("\n §§§ RUBRICA TELEFONICA §§§ \n");
while (scelta != 6) {
printf("\n 1- AGGIUNGI CONTATTO. \n");
printf(" 2- CERCA CONTATTO. \n");
printf(" 3- ELIMINA CONTATTO. \n");
printf(" 4- ELIMINA TUTTA LA RUBRICA. \n");
printf(" 5- VISUALIZZA RUBRICA. \n");
printf(" 6- ESCI. \n");
scanf("%d", &scelta);
switch (scelta) {
case 1 :
L = inserisci_in_ordine (L, &CONTATTO);
break;
case 2 :
L = cerca_contatto (L, &CONTATTO);
break;
case 3 :
L = cancella_un_contatto (L, &CONTATTO);
break;
case 4 :
L = cancella_tutto (L);
break;
case 5 :
visualizza_rubrica (L);
break;
}
}
system("PAUSE");
return 0;
}