Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Questo è custom_warehouse.cpp
- ___________________________________________________________________________
- #include "custom_warehouse.h"
- void create_warehouse(warehouse& a, int& r)
- {
- cout << "Definire il numero di prodotti da stoccare in magazzino: ";
- cin >> r;
- cin.ignore();
- a = new product[r];
- for(int i = 0, j = 1; i < r; i++, j++)
- {
- cout << endl << "Elemento " << j << endl;
- insert_product_data(*(a + i));
- }
- }
- int find_product(c_string key, warehouse a, int r)
- {
- bool found = false;
- int i = 0;
- do
- {
- if(strcmp((*(a + i)).code, key) == 0) //* Avrei potuto usare anche l'operatore di accesso
- found = true; //* agli array [] in questo modo: strcmp(a[i].code, key)
- else //* ma ho preferito utilizzare la sintassi dei puntatori
- i++;
- }while(!found && i < r);
- if(found)
- return i;
- else
- return -1;
- }
- int delete_product(warehouse a, int& r, c_string code)
- {
- int pos = find_product(code, a, r);
- if(pos < 0)
- return 1;
- else
- {
- for(int i = pos, j = pos + 1; j < r; i++, j++)
- a[i] = a[j];
- r--;
- return 0;
- }
- }
- void show_warehouse(warehouse a, int r)
- {
- cout << endl << "Prodotti presenti in magazzino: ";
- for(int i = 0, j = 1; i < r; i++, j++)
- {
- cout << endl << "Elemento " << j << endl;
- show_product_data(*(a + i));
- }
- }
- void copy_warehouse(warehouse a, warehouse& b, int r_a, int& r_b)
- {
- r_b = 0;
- b = new product[r_a];
- for(int i = 0; i < r_a; i++)
- {
- b[i] = a[i];
- r_b++;
- }
- }
- void destroy_warehouse(warehouse a)
- {
- delete[] a;
- }
- ___________________________________________________________________________
- Questo è custom_warehouse.h
- ___________________________________________________________________________
- #ifndef CUSTOM_WAREHOUSE_H_INCLUDED
- #define CUSTOM_WAREHOUSE_H_INCLUDED
- #include "custom_product.h"
- #include <cstring>
- typedef product* warehouse;
- void create_warehouse(warehouse& a, int& r);
- int find_product(c_string key, warehouse a, int r);
- int delete_product(warehouse a, int& r, c_string code);
- void show_warehouse(warehouse a, int r);
- void copy_warehouse(warehouse a, warehouse& b, int r_a, int& r_b);
- void destroy_warehouse(warehouse a);
- #endif // CUSTOM_WAREHOUSE_H_INCLUDED
- ___________________________________________________________________________
- Questo è custom_product.cpp
- ___________________________________________________________________________
- #include "custom_product.h"
- /* Questa funzione ha problemi di "robustezza": un input scorretto del campo .code
- * compromette il funzionamento dell'intero programma. Ho provato, senza successo
- * ad utilizzare cin.getline() in abbinamento con cin.ignore() */
- void insert_product_data(product& a)
- {
- cout << "Inserisci il codice prodotto: ";
- cin.getline(a.code, 6);
- cout << "Inserisci una descrizione: ";
- cin.getline(a.description, 100);
- cout << "Inserisci la quantita' presente in magazzino: ";
- cin >> a.qty;
- cin.ignore();
- }
- void show_product_data(product a)
- {
- cout << "Codice: " << a.code << endl
- << "Descrizione: " << a.description << endl
- << "Quantita': " << a.qty << endl;
- }
- ___________________________________________________________________________
- Questo è custom_product.h
- ___________________________________________________________________________
- #ifndef CUSTOM_PRODUCT_H_INCLUDED
- #define CUSTOM_PRODUCT_H_INCLUDED
- #include <iostream>
- using namespace std;
- typedef char c_string[100];
- struct product
- {
- c_string code;
- c_string description;
- int qty;
- };
- void insert_product_data(product& a);
- void show_product_data(product a);
- #endif // CUSTOM_PRODUCT_H_INCLUDED
- ___________________________________________________________________________
- Questo è main.cpp (solo per testare)
- ___________________________________________________________________________
- #include <iostream>
- #include "custom_product.h"
- #include "custom_warehouse.h"
- using namespace std;
- int main()
- {
- warehouse mag1 = 0,mag2 = 0;
- int riemp1 = 0, riemp2 = 0;
- c_string key;
- create_warehouse(mag1, riemp1);
- cout << endl << "Inserisci il codice da cercare: ";
- cin >> key;
- int pos = find_product(key, mag1, riemp1);
- if(pos < 0)
- cout << "La ricerca non ha prodotto risultati" << endl;
- else
- cout << "Il prodotto si trova nella posizione " << pos << endl;
- cout << endl << "Inserire il codice dell'elemento da eliminare: ";
- cin >> key;
- delete_product(mag1, riemp1, key);
- show_warehouse(mag1, riemp1);
- copy_warehouse(mag1, mag2, riemp1, riemp2);
- show_warehouse(mag2, riemp2);
- destroy_warehouse(mag1);
- destroy_warehouse(mag2);
- return 0;
- }
Add Comment
Please, Sign In to add comment