Lucio_Vero
16-05-2013, 10:55
PER FAVORE POTETE RISPONDERMI, GRAZIE.
Buongiorno, sono nuovo di qui e volevo innanzitutto dire quanto questo forum mi sia stato di aiuto per alcuni dettagli riguardo il linguaggio di programmazione C/C++ e colgo l'occasione per chiedere un aiuto. Sto facendo un programma appunto in C che gestisce una lista di prodotti e, oltre al programma che vedete, vorrei aggiungere la sezione "CalcolaTotale" che tramite un'apposita funzione float CalcolaTotale che restituisce la somma di tutti i prodotti. Il problema e' che non riesco a capire come poter fare la somma di prezzi di prodotti diversi. Il programma e' questo:
/*-----------------------------------------------------------------------------
Nome del programma: lista_C.cpp
Autore:
Data: 16/05/2013
Descrizione:
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct LISTA
{
char NOME_PRODOTTO[30];
float PREZZO;
int QUANTITA;
};
bool AcquisisciLista();
void LeggiLista();
bool EsportaHTML();
bool ModificaLista();
float CalcolaTotale();
int StampaMenu();
//------------------------------- main ----------------------------------------
int main()
{
int scelta;
float somma;
bool t;
do
{
scelta = StampaMenu();
switch(scelta)
{
case 1: t = AcquisisciLista();
if(t == true) printf("\nProdotto inserito correttamente \1\n");
else printf("\nErrore durante l'inserimento del prodotto\n");
break;
case 2: LeggiLista();
break;
case 3: t = ModificaLista();
if(t == true) printf("\nLista modificata correttamente \1\n");
else printf("\nErrore durante la modifica del file 'lista.dat'\n");
break;
case 4: somma = CalcolaTotale();
printf("Totale: %f", somma);
break;
case 5: t = EsportaHTML();
if(t == true) printf("\nLista in HTML esportata correttamente \1\n");
else printf("\nErrore durante l'esportazione in HTML del file 'lista.dat'\n");
break;
default: if(scelta == 0) printf("Grazie per aver usato questo programma \1\n");
else printf("\nScelta non disponibile\n");
}
}while(scelta!=0);
return 0;
}
/*---------------------------------------------------------------- Acquisisci Lista ----------------------------------------------------------------*/
bool AcquisisciLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "ab");
bool t;
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.txt'\n");
t = false;
}
else
{
printf("\nInserisci nome prodotto: "); scanf("%s", L.NOME_PRODOTTO);
printf("Inserisci prezzo: "); scanf("%f", &L.PREZZO);
printf("Inserisci la quantita': "); scanf("%d", &L.QUANTITA);
fwrite(&L, sizeof(LISTA), 1, f);
t = true;
}
fclose(f);
return t;
}
/*---------------------------------------------------------------- Leggi Lista ----------------------------------------------------------------*/
void LeggiLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "rb");
if(f == NULL) printf("Errore durante l'apertura del file 'lista.txt'\n");
else
{
while(fread(&L, sizeof(LISTA), 1, f))
{
printf("\nNome prodotto: %s \n", L.NOME_PRODOTTO);
printf("Prezzo: %f \n", L.PREZZO);
printf("Quantita': %d \n", L.QUANTITA);
}
}
fclose(f);
}
/*---------------------------------------------------------------- Modifica Lista ----------------------------------------------------------------*/
bool ModificaLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "r+b");
char NOME_PRODOTTO[30];
bool t;
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.txt'\n");
t = false;
}
else
{
printf("\nInserisci nome prodotto da modificare: "); scanf("%s", NOME_PRODOTTO);
while(fread(&L, sizeof(LISTA),1, f))
{
if(strcmp(L.NOME_PRODOTTO, NOME_PRODOTTO) == 0)
{
fseek(f, -1*sizeof(LISTA), SEEK_CUR);
printf("\nInserisci nome prodotto: "); scanf("%s", L.NOME_PRODOTTO);
printf("Inserisci prezzo: "); scanf("%f", &L.PREZZO);
printf("Inserisci la quantita': "); scanf("%d", &L.QUANTITA);
fwrite(&L, sizeof(LISTA), 1, f);
fclose(f);
}
}
t = true;
}
return t;
}
/*---------------------------------------------------------------- Esporta HTML ----------------------------------------------------------------*/
bool EsportaHTML()
{
LISTA L;
FILE *f, *f1;
bool t;
f = fopen("lista.dat", "rb");
f1 = fopen("lista_html.html", "w");
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.dat'\n");
return 0;
}
if(f1 == NULL)
{
printf("Errore durante l'apertura del file 'lista_html.html'\n");
return 0;
}
if(f != NULL && f1 != NULL)
{
fprintf(f1, "<html>\n");
fprintf(f1, " <head>\n");
fprintf(f1, " <title> Lista </title>\n");
fprintf(f1, " <body>\n");
fprintf(f1, " <body bgcolor = \"DEEPSKYBLUE\">\n");
fprintf(f1, " <font type = \" Comic Sans MS\">\n");
fprintf(f1, " <center> <h1> <u> Lista </u> </h1> </center>");
fprintf(f1, " <div align = center>\n");
fprintf(f1, " <table width = \"40\" height = \"20\" border = \"1\">\n");
fprintf(f1, " <tr>\n");
fprintf(f1,"<td> <b> NOME PRODOTTO </b> </td> <td> <b> PREZZO </b> </td> <td> <b> QUANTITA' </b> </td>");
while(fread(&L, sizeof(LISTA), 1, f))
{
fprintf(f1, " <tr>\n");
fprintf(f1, " <td> <center> %s </center> </td>\n", L.NOME_PRODOTTO);
fprintf(f1, " <td> <center> %f </center> </td>\n", L.PREZZO);
fprintf(f1, " <td> <center> %d </center> </td>\n", L.QUANTITA);
fprintf(f1, " </tr>\n");
}
fprintf(f1, " </tr>");
fprintf(f1, " </table>\n");
fprintf(f1, " </font>\n");
fprintf(f1, " </div>\n");
fprintf(f1, " </body>\n");
fprintf(f1, "</html>\n");
t = true;
}
fclose(f);
fclose(f1);
system("start lista_html.html");
return t;
}
/*---------------------------------------------------------------- Stampa Menu ----------------------------------------------------------------*/
int StampaMenu()
{
int scelta;
printf("\n---- MENU -----\n");
printf("1. Acquisisci lista\n");
printf("2. Stampa lista\n");
printf("3. Modifica lista\n");
printf("4. Calcola totale\n");
printf("5. Esporta lista in HTML\n");
printf("0. Esci\n");
printf("Scelta -> "); scanf("%d", &scelta);
return scelta;
}
Se volete vederlo come su un compilatore, andate qui: http://nopaste.info/bf4fd91527.html
Cortesemente chiedo un vostro parere e un vostro aiuto. Buon pranzo per chi sta per mangiare e grazie in anticipo :)
Buongiorno, sono nuovo di qui e volevo innanzitutto dire quanto questo forum mi sia stato di aiuto per alcuni dettagli riguardo il linguaggio di programmazione C/C++ e colgo l'occasione per chiedere un aiuto. Sto facendo un programma appunto in C che gestisce una lista di prodotti e, oltre al programma che vedete, vorrei aggiungere la sezione "CalcolaTotale" che tramite un'apposita funzione float CalcolaTotale che restituisce la somma di tutti i prodotti. Il problema e' che non riesco a capire come poter fare la somma di prezzi di prodotti diversi. Il programma e' questo:
/*-----------------------------------------------------------------------------
Nome del programma: lista_C.cpp
Autore:
Data: 16/05/2013
Descrizione:
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct LISTA
{
char NOME_PRODOTTO[30];
float PREZZO;
int QUANTITA;
};
bool AcquisisciLista();
void LeggiLista();
bool EsportaHTML();
bool ModificaLista();
float CalcolaTotale();
int StampaMenu();
//------------------------------- main ----------------------------------------
int main()
{
int scelta;
float somma;
bool t;
do
{
scelta = StampaMenu();
switch(scelta)
{
case 1: t = AcquisisciLista();
if(t == true) printf("\nProdotto inserito correttamente \1\n");
else printf("\nErrore durante l'inserimento del prodotto\n");
break;
case 2: LeggiLista();
break;
case 3: t = ModificaLista();
if(t == true) printf("\nLista modificata correttamente \1\n");
else printf("\nErrore durante la modifica del file 'lista.dat'\n");
break;
case 4: somma = CalcolaTotale();
printf("Totale: %f", somma);
break;
case 5: t = EsportaHTML();
if(t == true) printf("\nLista in HTML esportata correttamente \1\n");
else printf("\nErrore durante l'esportazione in HTML del file 'lista.dat'\n");
break;
default: if(scelta == 0) printf("Grazie per aver usato questo programma \1\n");
else printf("\nScelta non disponibile\n");
}
}while(scelta!=0);
return 0;
}
/*---------------------------------------------------------------- Acquisisci Lista ----------------------------------------------------------------*/
bool AcquisisciLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "ab");
bool t;
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.txt'\n");
t = false;
}
else
{
printf("\nInserisci nome prodotto: "); scanf("%s", L.NOME_PRODOTTO);
printf("Inserisci prezzo: "); scanf("%f", &L.PREZZO);
printf("Inserisci la quantita': "); scanf("%d", &L.QUANTITA);
fwrite(&L, sizeof(LISTA), 1, f);
t = true;
}
fclose(f);
return t;
}
/*---------------------------------------------------------------- Leggi Lista ----------------------------------------------------------------*/
void LeggiLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "rb");
if(f == NULL) printf("Errore durante l'apertura del file 'lista.txt'\n");
else
{
while(fread(&L, sizeof(LISTA), 1, f))
{
printf("\nNome prodotto: %s \n", L.NOME_PRODOTTO);
printf("Prezzo: %f \n", L.PREZZO);
printf("Quantita': %d \n", L.QUANTITA);
}
}
fclose(f);
}
/*---------------------------------------------------------------- Modifica Lista ----------------------------------------------------------------*/
bool ModificaLista()
{
LISTA L;
FILE *f = fopen("lista.dat", "r+b");
char NOME_PRODOTTO[30];
bool t;
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.txt'\n");
t = false;
}
else
{
printf("\nInserisci nome prodotto da modificare: "); scanf("%s", NOME_PRODOTTO);
while(fread(&L, sizeof(LISTA),1, f))
{
if(strcmp(L.NOME_PRODOTTO, NOME_PRODOTTO) == 0)
{
fseek(f, -1*sizeof(LISTA), SEEK_CUR);
printf("\nInserisci nome prodotto: "); scanf("%s", L.NOME_PRODOTTO);
printf("Inserisci prezzo: "); scanf("%f", &L.PREZZO);
printf("Inserisci la quantita': "); scanf("%d", &L.QUANTITA);
fwrite(&L, sizeof(LISTA), 1, f);
fclose(f);
}
}
t = true;
}
return t;
}
/*---------------------------------------------------------------- Esporta HTML ----------------------------------------------------------------*/
bool EsportaHTML()
{
LISTA L;
FILE *f, *f1;
bool t;
f = fopen("lista.dat", "rb");
f1 = fopen("lista_html.html", "w");
if(f == NULL)
{
printf("Errore durante l'apertura del file 'lista.dat'\n");
return 0;
}
if(f1 == NULL)
{
printf("Errore durante l'apertura del file 'lista_html.html'\n");
return 0;
}
if(f != NULL && f1 != NULL)
{
fprintf(f1, "<html>\n");
fprintf(f1, " <head>\n");
fprintf(f1, " <title> Lista </title>\n");
fprintf(f1, " <body>\n");
fprintf(f1, " <body bgcolor = \"DEEPSKYBLUE\">\n");
fprintf(f1, " <font type = \" Comic Sans MS\">\n");
fprintf(f1, " <center> <h1> <u> Lista </u> </h1> </center>");
fprintf(f1, " <div align = center>\n");
fprintf(f1, " <table width = \"40\" height = \"20\" border = \"1\">\n");
fprintf(f1, " <tr>\n");
fprintf(f1,"<td> <b> NOME PRODOTTO </b> </td> <td> <b> PREZZO </b> </td> <td> <b> QUANTITA' </b> </td>");
while(fread(&L, sizeof(LISTA), 1, f))
{
fprintf(f1, " <tr>\n");
fprintf(f1, " <td> <center> %s </center> </td>\n", L.NOME_PRODOTTO);
fprintf(f1, " <td> <center> %f </center> </td>\n", L.PREZZO);
fprintf(f1, " <td> <center> %d </center> </td>\n", L.QUANTITA);
fprintf(f1, " </tr>\n");
}
fprintf(f1, " </tr>");
fprintf(f1, " </table>\n");
fprintf(f1, " </font>\n");
fprintf(f1, " </div>\n");
fprintf(f1, " </body>\n");
fprintf(f1, "</html>\n");
t = true;
}
fclose(f);
fclose(f1);
system("start lista_html.html");
return t;
}
/*---------------------------------------------------------------- Stampa Menu ----------------------------------------------------------------*/
int StampaMenu()
{
int scelta;
printf("\n---- MENU -----\n");
printf("1. Acquisisci lista\n");
printf("2. Stampa lista\n");
printf("3. Modifica lista\n");
printf("4. Calcola totale\n");
printf("5. Esporta lista in HTML\n");
printf("0. Esci\n");
printf("Scelta -> "); scanf("%d", &scelta);
return scelta;
}
Se volete vederlo come su un compilatore, andate qui: http://nopaste.info/bf4fd91527.html
Cortesemente chiedo un vostro parere e un vostro aiuto. Buon pranzo per chi sta per mangiare e grazie in anticipo :)