Precedente :: Successivo |
Autore |
Messaggio |
sunrise Comune mortale
Registrato: 02/02/15 20:11 Messaggi: 1 Residenza: milano
|
Inviato: 02 Feb 2015 20:27 Oggetto: esercizio C liste di liste |
|
|
ho questo programma che mi da dei problemi, tra l'altro gli inserimenti della funzione "inserisci" non mi funzionano tutte correttamente
vi copio il codice
grazie a tutti
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
typedef enum{PRIMO, SECONDO, DOLCE} classe;
struct ingrediente{
char nome[20];
int qta;
char u_m;
struct ingrediente *next;
};
struct ricetta{
char nome[20];
int tempo;
classe tipo;
struct ricetta *next;
struct ingrediente *ingr;
};
void inserisci(struct ricetta **L);
int insIngrediente(struct ingrediente **L);
void visualizza(struct ricetta *L);
int main(){
struct ricetta *lista=NULL;
char opzione;
do{
printf("scegli opzione I-V-F: ");
scanf("%c", &opzione);
fflush(stdin);
switch (opzione) {
case 'I':
inserisci(&lista);
break;
case 'V':
visualizza(lista);
break;
case 'F':
printf("\nFINE");
break;
default: printf("\nopzione non supportata");
break;
}
} while(opzione!='F');
return 0;}
void inserisci(struct ricetta **L){
struct ricetta *nuovo=NULL;
char classe;
nuovo=(struct ricetta*)malloc(sizeof(struct ricetta));
printf("\nnome ricetta: "); scanf("%s", nuovo->nome);
printf("\ntempo: "); scanf("%d", &nuovo->tempo);
printf("\nPrimo Secondo Contorno: ");
scanf("%c", &classe);
fflush(stdin);
if(classe=='P') nuovo->tipo=PRIMO;
else if(classe=='S') nuovo->tipo=SECONDO;
else if(classe=='D') nuovo->tipo=DOLCE;
//inserisci ingrediente e collega elemento alla lista
nuovo->ingr=NULL;
while ((insIngrediente(&nuovo->ingr))==0);
}
int insIngrediente(struct ingrediente **L)
{
int opzione;
printf("\ninserire ingrediente? 0 si 1 no: ");
scanf("%d", &opzione);
if(opzione==1) return -1;
struct ingrediente *nuovo=NULL;
nuovo=(struct ingrediente*)malloc(sizeof(struct ingrediente));
nuovo->next=NULL;
printf("\ninserisci nome");
scanf("%s", nuovo->nome);
printf("\ninserisci quantita'");
scanf("%d", &nuovo->qta);
fflush(stdin);
printf("inserisci unità misura K G H");
scanf("%c", &nuovo->u_m);
fflush(stdin);
nuovo->next=*L;
*L=nuovo;
return 0;
}
void visualizza(struct ricetta *L)
{
while (L!=NULL) {
printf("\n NOME: %s, TEMPO: %d:", L->nome, L->tempo);
if(L->tipo==PRIMO) printf("TIPO: primo");
else if(L->tipo==SECONDO) printf("TIPO: secondo");
else if(L->tipo==DOLCE) printf("TIPO: dolce");
while (L->ingr!=NULL) {
printf("\nNOME: %s, QTA: %d %c", L->ingr->nome, L->ingr->qta, L->ingr->u_m);
L->ingr=L->ingr->next;
}
L=L->next;
}
} |
|
Top |
|
|
SverX Supervisor Macchinisti
Registrato: 25/03/02 11:16 Messaggi: 11566 Residenza: Tokelau
|
Inviato: 05 Feb 2015 14:01 Oggetto: |
|
|
Non penserai mica che ti facciamo il compito noi, no? |
|
Top |
|
|
|
|
Non puoi inserire nuovi argomenti Non puoi rispondere a nessun argomento Non puoi modificare i tuoi messaggi Non puoi cancellare i tuoi messaggi Non puoi votare nei sondaggi
|
|