123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // programma_scontrino.cpp : Defines the entry point for the console application.
- //
- /*
- scrivere un programma che efettui e stampi gli scontrini
- -nome, prodotto prezzo unitario, e quantià.
- 1)l'utente seleziona la voce di menù nuvo scontrino che riciede l'insermento del prodotto della quantità e continia ad aggiungere elemnti
- 2)stampa lo scontrino
- deallocale la lista.
- */
- #include "stdafx.h"
- #include <iostream>
- #define LEN 20
- using namespace std;
- typedef struct {
- char nome_prodotto[LEN + 1];
- int prezzo = 0;
- int quantia = 0;
- }SCONTRINO;
- typedef struct NODO {
- SCONTRINO scontrino; //dati
- struct NODO*next; //punta all latro nodo
- }NODO;
- NODO*alloca_scontrino();
- void leggi(NODO*n);
- void scontrino(NODO*n);
- NODO*ultimo_nodo(NODO*n);
- void stampa(NODO*n);
- void dealloca(NODO*n);
- bool lista_vuota(NODO*n);
- void menù();
- int main()
- {
- menù();
- return 0;
- }
- void menù() {
- NODO*testa = 0;
- int scelta = 0;
- do {
- system("cls");
- printf(" menù\n");
- printf("0.esci\n");
- printf("1.carica\n");
- printf("2.stampa\n");
- scanf_s("%d", &scelta, sizeof(int));
- switch (scelta) {
- case 0:
- printf("arriverci e lasciate la mancia \n");
- system("pause");
- break;
- case 1:
- if (lista_vuota(testa) == true) {
- scontrino(testa);
- }
- else {
- printf("la lista è piena\n");
- }
- system("pause");
- break;
- case 2:
- stampa(testa);
- system("pause");
- break;
- default:
- printf("valore non valido\n");
- break;
- }
- } while (scelta != 0);
- dealloca(testa);
- }
- NODO*alloca_scontrino(){
- NODO*n;
- n = (NODO*)malloc(sizeof(NODO));//allocazzione memoria
- if (n == NULL) {
- printf("ERRORE!!!!!\n");
- return 0;
- }
- n->next = NULL; // puntatore
- return n;
- }
- void leggi(NODO*n) {
- printf("inserisci il nome de prodotto\n");
- scanf_s("%s", n->scontrino.nome_prodotto, LEN + 1);
- printf("inserisci prezzo\n");
- scanf_s("%d", n->scontrino.prezzo, sizeof(int));
- printf("inserisci quantià\n");
- scanf_s("%d", n->scontrino.quantia, sizeof(int));
- }
- void scontrino(NODO*n) {
- NODO*nuovo;//nuovo
- NODO*ultimo;
- int scelta = 0;
- do{
- system("cls");
- printf("0.esci\n");
- printf("1.inserisci prodotto\n");
- scanf_s("%d", &scelta, sizeof(int));
- switch (scelta) {
- case 0:
- printf("arrivederci\n");
- break;
- case 1:
- ultimo = ultimo_nodo(n);
- nuovo = alloca_scontrino();
- leggi(nuovo);
- ultimo->next = nuovo;// il puntatore dell ultimo viene ripuntato alla ultimo elemento definitivo
- }
- } while (scelta != 0);
- }
- NODO*ultimo_nodo(NODO*n) {
- NODO*p;
- p = n;
- while ((p->next) != NULL) {
- p = p->next;
- }
- return p;
- }
- void stampa(NODO*n) {
- NODO*p;
- int cont=0;
- p = n;
- while (p != NULL) {
- printf("NODO: %d\n nome %s\n, prezzo %d\n,quantità %d\n ", cont, p->scontrino.nome_prodotto, p->scontrino.prezzo, p->scontrino.quantia);
- p = p->next;
- cont++;
- }
- }
- void dealloca(NODO*n) {
- NODO*p;
-
- while (n != NULL) {
- p = n;
- n = p->next;
- free(p);
- p = NULL;
- }
- }
- bool lista_vuota(NODO*n) {
- if (n == NULL) {
- return true;
- }
- else {
- return false;
- }
- }
|