Está en la página 1de 18

Centro Universitario de Ciencias Exactas e Ingenierías

UDG
Estructuras de datos

La anidación estructural: Registros con Arreglos,


Arreglos de Registros y Arreglos de Objetos

Alumno: Ceja Zapata Carlos Alberto


Profesor: Dr. Gutiérrez Hernández Alfredo
Sección D05
Introduccion

Para esta actividad se debía de hacer un inventario en el cual debíamos de


registrar los productos, cada uno de ellos con su precio por mayoreo y menudeo,
además debíamos crear variedad de clases las cuales eran las siguientes:

 La fecha
 El producto
 La colección de productos
 El menú

Al empezar a realizar el programa e resulto la primera parte sencilla ya que ya


tenía el código de la fecha funcionando, además de que al crear la clase para los
productos no era nada complicado, después de que fui avanzando con la
elaboración del programa se me dificulto mucho las cosas, ya que al hacer la
colección de productos me resultaba muy difícil que me compilara, intente
entenderlo con algunos videos pero en ninguno me solucionaba mis dudas,
después de ello pedí ayuda a varios compañeros que tenían el mismo problema
hasta que logramos solucionarlo aunque no sé si fue de la manera correcta.
Código fuente “main.cpp”

1 #include <iostream>
2
3 #include "fecha.h"
4 #include "menu.h"
5 #include "producto.h"
6 #include "coleccion.h"
7
8 using namespace std;
9
10 void IgresarProducto();
11 void extraerProducto();
12 void vender();
13
14 Menu menu1;
15 Fecha fecha;
16 Coleccion colecion;
17 Producto producto;
18
19 int main()
20 {
21 int opcion;
22 colecion.inicializar();
23
24 do
25 {
26 system("cls");
27 menu1.imprimirMenu();
28 opcion=menu1.getOpcion();
29 switch(opcion)
30 {
31 case 1:
32 system("cls");
33 IgresarProducto();
34 system("pause");
35 break;
36 case 2:
37 system("cls");
38 vender();
39 system("pause");
40 break;
41 case 3:
42 system("cls");
43 extraerProducto();
44 system("pause");
45 break;
46 case 4:
47 system("cls");
48 colecion.imprimirColeccion();
49 producto.imprimirFecha();
50 system("pause");
51 break;
52 default:
53 break;
54 }
55 }
56 while (opcion!=5);
57 return 0;
58 }
59 void IgresarProducto()
60 {
61 char codigoBarra[13];
62 char nombre[40];
63 float peso, precioMenudeo, precioMayor;
64 int existence,dia,mes,anio;
65 cout<<"Ingresa el dia: "<<endl;
66 cin>>dia;
67 cout<<"Ingresa el mes: "<<endl;
68 cin>>mes;
69 cout<<"Ingresa el año: "<<endl;
70 cin>>anio;
71 producto.setObtenerFecha(dia,mes,anio);
72 system("cls");
73 cout<<"Ingresa el codigo: ";
74 cin>>codigoBarra;
75 fflush(stdin);
76 cout << "Ingresa el nombre: ";
77 cin.getline(nombre,40);
78 cout << "Ingresa peso: ";
79 cin >> peso;
80 cout << "Ingresa el precio de mayoreo: ";
81 cin >> precioMayor;
82 cout << "Ingresa el precio de menudeo: ";
83 cin >> precioMenudeo;
84 cout << "Ingresa los productos existentes: ";
85 cin >> existence;
86
colecion.setColeccion(codigoBarra,nombre,peso,precioMenudeo,precioMayor,existence
);
87 }
88
89 void vender()
90 {
91 int resultado;
92 float exist;
93 char codigoBarra[13];
94 int validacion;
95 cout<<"Ingresa el codigo del producto: ";
96 cin>>codigoBarra;
97 fflush(stdin);
98 validacion = colecion.existente(codigoBarra);
99 if(validacion == -1)
100 {
101 cout<<"No hay productos disponibles."<<endl;
102 }
103 else
104 {
105 cout<<"Ingrese la cantidad de productos que desea vender ";
106 cin>>exist;
107 resultado = colecion.actualizar(exist, validacion);
108 if(!resultado)
109 {
110 cout<<"No hay productos"<<endl;
111 }
112 else
113 {
114 cout<<"listo"<<endl;
115 }
116 }
117 }
118 void extraerProducto()
119 {
120 float existentes;
121 char codigoBarra[13];
122 int validacion;
123 cout << "ingrese el codigo del producto: ";
124 cin>>codigoBarra;
125 fflush(stdin);
126 validacion = colecion.existente(codigoBarra);
127 if(validacion == -1)
128 {
129 cout<<"That code does not exist"<<endl;
130 }
131 else
132 {
133 cout<<"Ingresa el nuevo producto: ";
134 cin>>existentes;
135 colecion.recorrerProducto(existentes, validacion);
136 cout<<"actualizado"<<endl;
137 }
138 }
139
Código fuente fecha.cpp

1 #include "fecha.h"
2 int Fecha::getYear()
3 {
4 return year;
5 }
6
7 int Fecha::getMonth()
8 {
9
10 return month;
11 }
12
13 int Fecha::getDay()
14 {
15
16 return day;
17 }
18
19 void Fecha::setYear(const int& y)
20 {
21
22 year=y;
23 }
24
25 void Fecha::setMonth(const int& m)
26 {
27 month=m;
28
29 }
30
31 void Fecha::setDay(const int& d)
32 {
33 day=d;
34
35 }
36
37
Código fuente produto.cpp

1 #include "producto.h"
2 #include "fecha.h"
3 using namespace std;
4
5 char *Producto::getCodigo()
6 {
7 return codigoBarra;
8 }
9
10 char *Producto::getNombre()
11 {
12 return nombre;
13 }
14
15 float Producto::getPeso()
16 {
17 return peso;
18 }
19
20
21 float Producto::getPrecioMay()
22 {
23 return precioMay;
24
25 }
26
27 float Producto::getPrecioMen()
28 {
29 return precioMen;
30 }
31
32 int Producto::getExistente()
33 {
34 return existente;
35 }
36
37 void Producto::setCodigo(const char *c)
38 {
39 strcpy(codigoBarra,c);
40 }
41
42 void Producto::setNombre(const char *n)
43 {
44 strcpy(nombre,n);
45 }
46
47 void Producto::setPeso(const float& p)
48 {
49 peso=p;
50 }
51
52 void Producto::setPrecioMay(const float& pm)
53 {
54 precioMay=pm;
55 }
56
57 void Producto::setPrecioMen(const float& pme)
58 {
59 precioMen=pme;
60 }
61
62 void Producto::setExistente(const int& e)
63 {
64 existente=e;
65 }
66 Fecha Producto::getObtenerFecha()
67 {
68 return (obtenerF);
69 }
70
71 void Producto::setObtenerFecha(const int& d, const int& m , const int& a)
72 {
73 obtenerF.setDay(d);
74 obtenerF.setMonth(m);
75 obtenerF.setYear(a);
76 }
77
78 void Producto::imprimirFecha()
79 {
80 cout<<" dia "<<" mes "<<"anio "<<endl;
81
cout<<obtenerF.getDay()<<"\t"<<obtenerF.getMonth()<<"\t"<<obtenerF.getYear()<<end
l;
82 }
83
84
85
86
Código fuente de menu.cpp

1 #include "menu.h"
2
3 int Menu::getOpcion()
4 {
5 return opcion;
6 }
7
8 void Menu::imprimirMenu()
9 {
10 cout<<" _______________________"<<endl;
11 cout << "|1.- Registrar producto |" << endl;
12 cout << "|2.- Veneder producto |"<< endl;
13 cout << "|3.- Quitar producto |" << endl;
14 cout << "|4.- Mostrar todo |"<<endl;
15 cout << "|5.- Salir |" << endl;
16 cout << "|_______________________|"<<endl;
17 cout << "Ingresa una opcion: ";
18 cin >> opcion;
19
20 }
Código fuente de coleccion.cpp

1 #include "coleccion.h"
2 #include "producto.h"
3 #include "fecha.h"
4
5
6 using namespace std;
7
8 void Coleccion::inicializar()
9 {
10 ultimo=0;
11 }
12
13 int Coleccion::getUltimo()
14 {
15 return ultimo;
16 }
17
18 int Coleccion::existente(const char* e)
19 {
20 char codigoBarra[13];
21 bool estado=true;
22 for (int i=(0); i<ultimo; i++)
23 {
24 strcpy(codigoBarra,coleccionDeProducto[i].getCodigo());
25 for(int j=(0); j<13; ++j)
26 {
27 if(codigoBarra[j]!=e[j])
28 {
29 estado=false;
30 }
31 }
32 if (estado)
33 {
34 return i;
35 }
36 }
37 return -1;
38 }
39
40 int Coleccion::actualizar(const int& salida, const int& i)
41 {
42 int exist,resultado;
43 exist=coleccionDeProducto[i].getExistente();
44 resultado=exist-salida;
45 if (resultado<0)
46 {
47 return false;
48 }
49 else
50 {
51 coleccionDeProducto[i].setExistente(resultado);
52 return true;
53 }
54 }
55
56 void Coleccion::recorrerProducto(const int& salida, const int& i)
57 {
58 coleccionDeProducto[i].setExistente(salida);
59
60 }
61
62 void Coleccion::imprimirColeccion()
63 {
64 int j;
65 char codigoBarra[13],nombre[40];
66 for (int i=(0); i<ultimo; i++)
67 {
68 strcpy(codigoBarra,coleccionDeProducto[i].getCodigo());
69 strcpy(nombre,coleccionDeProducto[i].getNombre());
70 cout<<"Producto: "<<i+1<<endl;
71 cout<<"Codigo: ";
72 for (j=0; j<13; j++)
73 {
74 cout<<codigoBarra[j];
75 }
76 cout<<"\n";
77 cout<<"Nombre: ";
78 for (j=0; j<(signed)strlen(nombre); j++)
79 {
80 cout<<nombre[j];
81 }
82 cout<<"\n";
83 cout<<"Peso: "<<coleccionDeProducto[i].getPeso()<<endl;
84 cout<<"Precio por mayoreo:
"<<coleccionDeProducto[i].getPrecioMay()<<endl;
85 cout<<"Precio por menudeo:
"<<coleccionDeProducto[i].getPrecioMen()<<endl;
86 cout<<"Piezas en existencias:
"<<coleccionDeProducto[i].getExistente()<<endl;
87
cout<<"_________________________________________________________"<<endl;
88 }
89 }
90
91 void Coleccion::setColeccion(const char* c, const char* n, const float& p,
const float& pm, const float& pme, const int& e)
92 {
93 coleccionDeProducto[ultimo].setCodigo(c);
94 coleccionDeProducto[ultimo].setNombre(n);
95 coleccionDeProducto[ultimo].setPeso(p);
96 coleccionDeProducto[ultimo].setPrecioMay(pm);
97 coleccionDeProducto[ultimo].setPrecioMen(pme);
98 coleccionDeProducto[ultimo].setExistente(e);
99 ultimo=ultimo+1;
100 }
Código fuente de colección.h

1 #ifndef COLECCION_H_INCLUDED
2 #define COLECCION_H_INCLUDED
3
4 #include <iostream>
5 #include <cstdlib>
6 #include <cstring>
7 #include <string>
8
9 #include "producto.h"
10
11 class Coleccion{
12 private:
13 Producto coleccionDeProducto[800];
14 int ultimo;
15 public:
16 void inicializar();
17 int getUltimo();
18 int existente(const char*);
19 int actualizar(const int &, const int &);
20 void recorrerProducto(const int&,const int&);
21 void imprimirColeccion();
22 void setColeccion(const char*, const char*, const float&,
const float&, const float&, const int&);
23 };
24 #endif // COLECCION_H_INCLUDED
Código fuente de producto.h

1 #ifndef PRODUCTO_H_INCLUDED
2 #define PRODUCTO_H_INCLUDED
3
4 #include <iostream>
5 #include <cstdlib>
6 #include <string.h>
7
8 #include "fecha.h"
9
10 #define TAM_MAX 13
11 #define TAM_NOMBRE 40
12
13 using namespace std;
14
15 class Producto{
16 private:
17 Fecha obtenerF;
18 char codigoBarra[TAM_MAX];
19 char nombre[TAM_NOMBRE];
20 float peso;
21 float precioMay;
22 float precioMen;
23 int existente;
24 public:
25 Fecha getObtenerFecha();
26 char *getCodigo();
27 char *getNombre();
28 float getPeso();
29 float getPrecioMay();
30 float getPrecioMen();
31 int getExistente();
32
33 void imprimirFecha();
34 void setObtenerFecha(const int&,const int&,const int&);
35 void setCodigo(const char* );
36 void setNombre(const char* );
37 void setPeso(const float&);
38 void setPrecioMay(const float&);
39 void setPrecioMen(const float&);
40 void setExistente(const int&);
41 };
42
43 #endif // PRODUCTO_H_INCLUDED
Codigo Fuente de menu.h

1 #ifndef MENU_H_INCLUDED
2 #define MENU_H_INCLUDED
3
4 #include <iostream>
5 #include <cstdlib>
6 #include <stdio.h>
7
8 #include "coleccion.h"
9
10 class Menu{
11 private:
12 int opcion;
13 public:
14 int getOpcion();
15
16 void imprimirMenu();
17 };
18
19 #endif // MENU_H_INCLUDED

Código fuente de fecha.h

1 #ifndef FECHA_H_INCLUDED
2 #define FECHA_H_INCLUDED
3
4 #include <iostream>
5 #include <cstdlib>
6
7 using namespace std;
8
9
10 class Fecha
11 {
12 private:
13 int year;
14 int month;
15 int day;
16 public:
17 int getYear();
18 int getMonth();
19 int getDay();
20
21 void setYear(const int&);
22 void setMonth(const int&);
23 void setDay(const int&);
24 };
25 #endif // FECHA_H_INCLUDED
ejecución satisfactoria del programa

Ilustración 1 Menú

Ilustración 2 Ingreso de la fecha

Ilustración 3 lista de un producto antes de vender


Ilustración 4 Venta del producto

Ilustración 5 lista de productos, y los productos que sobraron después de la venta del primero
Ilustración 6 impresión de la fecha

Ilustración 7 Registro de nuevo articulo

Ilustración 8 Mostrando el articulo agregado


Ilustración 9 Actualizando la cantidad nueva de artículos

Ilustración 10 mostrando los la actualización

También podría gustarte