Guest User

Untitled

a guest
Jan 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. Questo è custom_warehouse.cpp
  2. ___________________________________________________________________________
  3. #include "custom_warehouse.h"
  4.  
  5. void create_warehouse(warehouse& a, int& r)
  6. {
  7.     cout << "Definire il numero di prodotti da stoccare in magazzino: ";
  8.     cin >> r;
  9.     cin.ignore();
  10.     a = new product[r];
  11.     for(int i = 0, j = 1; i < r; i++, j++)
  12.     {
  13.         cout << endl << "Elemento " << j << endl;
  14.         insert_product_data(*(a + i));
  15.     }
  16. }
  17.  
  18. int find_product(c_string key, warehouse a, int r)
  19. {
  20.     bool found = false;
  21.     int i = 0;
  22.     do
  23.     {
  24.         if(strcmp((*(a + i)).code, key) == 0)           //* Avrei potuto usare anche l'operatore di accesso
  25.             found = true;                               //* agli array [] in questo modo: strcmp(a[i].code, key)
  26.         else                                            //* ma ho preferito utilizzare la sintassi dei puntatori
  27.             i++;
  28.     }while(!found && i < r);
  29.     if(found)
  30.         return i;
  31.     else
  32.         return -1;
  33. }
  34.  
  35. int delete_product(warehouse a, int& r, c_string code)
  36. {
  37.     int pos = find_product(code, a, r);
  38.     if(pos < 0)
  39.         return 1;
  40.     else
  41.     {
  42.         for(int i = pos, j = pos + 1; j < r; i++, j++)
  43.             a[i] = a[j];
  44.         r--;
  45.         return 0;
  46.     }
  47. }
  48.  
  49. void show_warehouse(warehouse a, int r)
  50. {
  51.     cout << endl << "Prodotti presenti in magazzino: ";
  52.     for(int i = 0, j = 1; i < r; i++, j++)
  53.     {
  54.         cout << endl << "Elemento " << j << endl;
  55.         show_product_data(*(a + i));
  56.     }
  57. }
  58.  
  59. void copy_warehouse(warehouse a, warehouse& b, int r_a, int& r_b)
  60. {
  61.     r_b = 0;
  62.     b = new product[r_a];
  63.     for(int i = 0; i < r_a; i++)
  64.     {
  65.         b[i] = a[i];
  66.         r_b++;
  67.     }
  68. }
  69.  
  70. void destroy_warehouse(warehouse a)
  71. {
  72.     delete[] a;
  73. }
  74. ___________________________________________________________________________
  75.  
  76. Questo è custom_warehouse.h
  77. ___________________________________________________________________________
  78. #ifndef CUSTOM_WAREHOUSE_H_INCLUDED
  79. #define CUSTOM_WAREHOUSE_H_INCLUDED
  80.  
  81. #include "custom_product.h"
  82. #include <cstring>
  83.  
  84. typedef product* warehouse;
  85.  
  86. void create_warehouse(warehouse& a, int& r);
  87.  
  88. int find_product(c_string key, warehouse a, int r);
  89.  
  90. int delete_product(warehouse a, int& r, c_string code);
  91.  
  92. void show_warehouse(warehouse a, int r);
  93.  
  94. void copy_warehouse(warehouse a, warehouse& b, int r_a, int& r_b);
  95.  
  96. void destroy_warehouse(warehouse a);
  97.  
  98. #endif // CUSTOM_WAREHOUSE_H_INCLUDED
  99. ___________________________________________________________________________
  100.  
  101. Questo è custom_product.cpp
  102. ___________________________________________________________________________
  103.  
  104. #include "custom_product.h"
  105.  
  106. /* Questa funzione ha problemi di "robustezza": un input scorretto del campo .code
  107.  * compromette il funzionamento dell'intero programma. Ho provato, senza successo
  108.  * ad utilizzare cin.getline() in abbinamento con cin.ignore() */
  109.  
  110. void insert_product_data(product& a)
  111. {
  112.     cout << "Inserisci il codice prodotto: ";
  113.     cin.getline(a.code, 6);
  114.     cout << "Inserisci una descrizione: ";
  115.     cin.getline(a.description, 100);
  116.     cout << "Inserisci la quantita' presente in magazzino: ";
  117.     cin >> a.qty;
  118.     cin.ignore();
  119. }
  120.  
  121. void show_product_data(product a)
  122. {
  123.     cout << "Codice: " << a.code << endl
  124.          << "Descrizione: " << a.description << endl
  125.          << "Quantita': " << a.qty << endl;
  126. }
  127. ___________________________________________________________________________
  128.  
  129. Questo è custom_product.h
  130. ___________________________________________________________________________
  131.  
  132. #ifndef CUSTOM_PRODUCT_H_INCLUDED
  133. #define CUSTOM_PRODUCT_H_INCLUDED
  134.  
  135. #include <iostream>
  136.  
  137. using namespace std;
  138.  
  139. typedef char c_string[100];
  140.  
  141. struct product
  142. {
  143.     c_string code;
  144.     c_string description;
  145.     int qty;
  146. };
  147.  
  148. void insert_product_data(product& a);
  149. void show_product_data(product a);
  150.  
  151. #endif // CUSTOM_PRODUCT_H_INCLUDED
  152. ___________________________________________________________________________
  153.  
  154. Questo è main.cpp (solo per testare)
  155. ___________________________________________________________________________
  156. #include <iostream>
  157. #include "custom_product.h"
  158. #include "custom_warehouse.h"
  159.  
  160. using namespace std;
  161.  
  162. int main()
  163. {
  164.     warehouse mag1 = 0,mag2 = 0;
  165.     int riemp1 = 0, riemp2 = 0;
  166.     c_string key;
  167.     create_warehouse(mag1, riemp1);
  168.     cout << endl << "Inserisci il codice da cercare: ";
  169.     cin >> key;
  170.     int pos = find_product(key, mag1, riemp1);
  171.     if(pos < 0)
  172.         cout << "La ricerca non ha prodotto risultati" << endl;
  173.     else
  174.         cout << "Il prodotto si trova nella posizione " << pos << endl;
  175.     cout << endl << "Inserire il codice dell'elemento da eliminare: ";
  176.     cin >> key;
  177.     delete_product(mag1, riemp1, key);
  178.     show_warehouse(mag1, riemp1);
  179.     copy_warehouse(mag1, mag2, riemp1, riemp2);
  180.     show_warehouse(mag2, riemp2);
  181.     destroy_warehouse(mag1);
  182.     destroy_warehouse(mag2);
  183.     return 0;
  184. }
Add Comment
Please, Sign In to add comment