compiti 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // programma_scontrino.cpp : Defines the entry point for the console application.
  2. //
  3. /*
  4. scrivere un programma che efettui e stampi gli scontrini
  5. -nome, prodotto prezzo unitario, e quantià.
  6. 1)l'utente seleziona la voce di menù nuvo scontrino che riciede l'insermento del prodotto della quantità e continia ad aggiungere elemnti
  7. 2)stampa lo scontrino
  8. deallocale la lista.
  9. */
  10. #include "stdafx.h"
  11. #include <iostream>
  12. #define LEN 20
  13. using namespace std;
  14. typedef struct {
  15. char nome_prodotto[LEN + 1];
  16. int prezzo = 0;
  17. int quantia = 0;
  18. }SCONTRINO;
  19. typedef struct NODO {
  20. SCONTRINO scontrino; //dati
  21. struct NODO*next; //punta all latro nodo
  22. }NODO;
  23. NODO*alloca_scontrino();
  24. void leggi(NODO*n);
  25. void scontrino(NODO*n);
  26. NODO*ultimo_nodo(NODO*n);
  27. void stampa(NODO*n);
  28. void dealloca(NODO*n);
  29. bool lista_vuota(NODO*n);
  30. void menù();
  31. int main()
  32. {
  33. menù();
  34. return 0;
  35. }
  36. void menù() {
  37. NODO*testa = 0;
  38. int scelta = 0;
  39. do {
  40. system("cls");
  41. printf(" menù\n");
  42. printf("0.esci\n");
  43. printf("1.carica\n");
  44. printf("2.stampa\n");
  45. scanf_s("%d", &scelta, sizeof(int));
  46. switch (scelta) {
  47. case 0:
  48. printf("arriverci e lasciate la mancia \n");
  49. system("pause");
  50. break;
  51. case 1:
  52. if (lista_vuota(testa) == true) {
  53. scontrino(testa);
  54. }
  55. else {
  56. printf("la lista è piena\n");
  57. }
  58. system("pause");
  59. break;
  60. case 2:
  61. stampa(testa);
  62. system("pause");
  63. break;
  64. default:
  65. printf("valore non valido\n");
  66. break;
  67. }
  68. } while (scelta != 0);
  69. dealloca(testa);
  70. }
  71. NODO*alloca_scontrino(){
  72. NODO*n;
  73. n = (NODO*)malloc(sizeof(NODO));//allocazzione memoria
  74. if (n == NULL) {
  75. printf("ERRORE!!!!!\n");
  76. return 0;
  77. }
  78. n->next = NULL; // puntatore
  79. return n;
  80. }
  81. void leggi(NODO*n) {
  82. printf("inserisci il nome de prodotto\n");
  83. scanf_s("%s", n->scontrino.nome_prodotto, LEN + 1);
  84. printf("inserisci prezzo\n");
  85. scanf_s("%d", n->scontrino.prezzo, sizeof(int));
  86. printf("inserisci quantià\n");
  87. scanf_s("%d", n->scontrino.quantia, sizeof(int));
  88. }
  89. void scontrino(NODO*n) {
  90. NODO*nuovo;//nuovo
  91. NODO*ultimo;
  92. int scelta = 0;
  93. do{
  94. system("cls");
  95. printf("0.esci\n");
  96. printf("1.inserisci prodotto\n");
  97. scanf_s("%d", &scelta, sizeof(int));
  98. switch (scelta) {
  99. case 0:
  100. printf("arrivederci\n");
  101. break;
  102. case 1:
  103. ultimo = ultimo_nodo(n);
  104. nuovo = alloca_scontrino();
  105. leggi(nuovo);
  106. ultimo->next = nuovo;// il puntatore dell ultimo viene ripuntato alla ultimo elemento definitivo
  107. }
  108. } while (scelta != 0);
  109. }
  110. NODO*ultimo_nodo(NODO*n) {
  111. NODO*p;
  112. p = n;
  113. while ((p->next) != NULL) {
  114. p = p->next;
  115. }
  116. return p;
  117. }
  118. void stampa(NODO*n) {
  119. NODO*p;
  120. int cont=0;
  121. p = n;
  122. while (p != NULL) {
  123. printf("NODO: %d\n nome %s\n, prezzo %d\n,quantità %d\n ", cont, p->scontrino.nome_prodotto, p->scontrino.prezzo, p->scontrino.quantia);
  124. p = p->next;
  125. cont++;
  126. }
  127. }
  128. void dealloca(NODO*n) {
  129. NODO*p;
  130. while (n != NULL) {
  131. p = n;
  132. n = p->next;
  133. free(p);
  134. p = NULL;
  135. }
  136. }
  137. bool lista_vuota(NODO*n) {
  138. if (n == NULL) {
  139. return true;
  140. }
  141. else {
  142. return false;
  143. }
  144. }