Advertisement
CristianIglesias1

TP Structs, completo

Apr 10th, 2020
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. ///Funciones
  2. #ifndef FUNCIONES_H_INCLUDED
  3. #define FUNCIONES_H_INCLUDED
  4.  
  5. using namespace std;
  6. int ValidarId(producto *reg,int id_av ){
  7. int i=0;
  8. for(i=0;i<tam;i++)
  9. {
  10.     if(id_av==reg[i].id)
  11.     {return i;};
  12. }
  13. return 0;
  14. };
  15. void MostrarUno(producto *reg, int pos)
  16. {
  17.     cout<<"--------------------"<<endl;
  18.         cout<<"|id="<<reg[pos].id<<"|"<<endl;
  19.         cout<<"|nombre="<<reg[pos].nombre<<"|"<<endl;
  20.         cout<<"|precio= $"<<reg[pos].precio<<"|"<<endl;
  21.         cout<<"|stock actual="<<reg[pos].stock<<"|"<<endl;
  22.         if(reg[pos].stockm>reg[pos].stock)cout<<"|OJO QUE QUEDA MENOS QUE EL MINIMO"<<reg[pos].stockm<<"|"<<endl;
  23.         cout<<"--------------------"<<endl;
  24. }
  25. void CargaProd(struct producto *reg, int cont)
  26. {
  27.     cout<<"Bueno, cargar el producto."<<endl;
  28.        int pos=cont-1, id_av;
  29.         cout<<"ID: ";
  30.         cin>>id_av;
  31.         //validar
  32.         if(ValidarId(reg,id_av)==0){
  33.         reg[pos].id=id_av;
  34.         cout<<"Ahora carga el nombre del item"<<endl;
  35.         cin>>reg[pos].nombre;cout<<endl;
  36.         cout<<"Precio: $";
  37.         cin>>reg[pos].precio;cout<<endl;
  38.         cout<<"Stock: ";
  39.         cin>>reg[pos].stock;
  40.         cout<<"Stock minimo?: ";
  41.         cin>>reg[pos].stockm;
  42.     }
  43.     else{
  44.         cout<<"el articulo ya existe brodi, capaz querés  editarlo ;)"<<endl;
  45.     };
  46. }
  47. void EditarProd(producto *reg)
  48. {
  49.     int id_av,pos;
  50.     cout<<"Bueno, querés editar algo. Vamos a ello!"<<endl;
  51.     cout<<"ingresa el id del producto que querés editar. ";
  52.     cout<<"ID: ";
  53.     cin>>id_av;cout<<endl;
  54.     pos=ValidarId(reg,id_av);
  55.     if(pos>0){
  56.         cout<<"Ahora carga el nombre del item"<<endl;
  57.         cin>>reg[pos].nombre;cout<<endl;
  58.         cout<<"Precio: $";
  59.         cin>>reg[pos].precio;cout<<endl;
  60.         cout<<"Stock: ";
  61.         cin>>reg[pos].stock;
  62.         cout<<"Stock minimo?: ";
  63.         cin>>reg[pos].stockm;
  64.     }else
  65.     {cout<<"el item no existe, intentá denuevo"<<endl;return;}
  66. }
  67. void MostrarxId(producto *reg)
  68. {
  69.     int id_av,pos;
  70. cout<<"Bueno, veamos que producto especifico buscás..."<<endl;
  71. cout<<"ID: ";
  72. cin>>id_av;
  73. pos=ValidarId(reg,id_av);
  74. MostrarUno(reg,pos);
  75.  
  76. }
  77.  
  78. void MostrarTodo(producto *reg,int cont)
  79. {
  80.     for(int i=0;i<cont;i++)
  81.     {   cout<<"--------------------"<<endl;
  82.         cout<<"|id="<<reg[i].id<<"|"<<endl;
  83.         cout<<"|nombre="<<reg[i].nombre<<"|"<<endl;
  84.         cout<<"|precio= $"<<reg[i].precio<<"|"<<endl;
  85.         cout<<"|stock actual="<<reg[i].stock<<"|"<<endl;
  86.         if(reg[i].stockm>reg[i].stock)cout<<"|OJO QUE QUEDA MENOS QUE EL MINIMO"<<reg[i].stockm<<"|"<<endl;
  87.         cout<<"--------------------"<<endl;
  88.     }
  89. }
  90. void  OrdenXID(producto *reg, int cont){
  91.  int i,j;
  92.  producto aux;
  93.  for(i=0;i<cont;i++){
  94.      for(j=0;j<cont-1;j++){
  95.          if(reg[j+1].id<reg[j].id){
  96.              aux=reg[j];
  97.              reg[j]=reg[j+1];
  98.              reg[j+1]=aux;}}}}
  99.  
  100. #endif // FUNCIONES_H_INCLUDED
  101.  
  102. #include <iostream>
  103. #include <cstdlib>
  104. #include <cstring>
  105. #include <clocale>///para que permitan los "ñ" o tildes
  106. using namespace std;
  107. struct producto{
  108. int stock;
  109. int stockm;
  110. int id;
  111. char nombre[30];
  112. float precio;};
  113. const int tam=10;
  114. #include"funciones.h"
  115.  
  116. int main()
  117. {   setlocale(LC_ALL,"spanish");///necesario para que permitan los ñ y tildes.
  118.     cout << "hola denuevo!" << endl;
  119.     int opcion=-1,contProd=0;
  120.     producto reg[tam];
  121.     while(opcion!=0)
  122.     {   system("pause");
  123.         system("cls");
  124.         cout<<"---------------------------------Menú-----------------------------------------"<<endl;
  125.         cout<<"----------------------------1 - Cargar Productos------------------------------"<<endl;
  126.         cout<<"----------------------------2 - Editar Producto x id--------------------------"<<endl;
  127.         cout<<"----------------------------3 - Listar productos x ID-------------------------"<<endl;
  128.         cout<<"----------------------------4 - Listar productos Ordenados por ID-------------"<<endl;
  129.         cout<<"----------------------------5 - Listar productos Ordenados por precio---------"<<endl;
  130.         cout<<"----------------------------0 - Salir-----------------------------------------"<<endl;
  131.         cout<<"------------------------------------------------------------------------------"<<endl;
  132.         cin>>opcion;
  133.     switch (opcion){
  134.     case 1:{
  135.         contProd++;
  136.     system("cls");
  137.     if(contProd==10){cout<<"ya llegaste al maximo de articulos bro"<<endl;}
  138.     CargaProd(reg,contProd);
  139.     }break;
  140.     case 2:{
  141.     system("cls");
  142.     EditarProd(reg);
  143.     }break;
  144.     case 3:{
  145.         system("cls");
  146.         MostrarxId(reg);
  147.     }break;
  148.     case 4:{
  149.     system("cls");
  150.     cout<<"así se ve ahora"<<endl;
  151.     MostrarTodo(reg,contProd);
  152.     system("pause");
  153.     cout<<"Lo ordenamos... "<<endl;
  154.     OrdenXID(reg,contProd);
  155.     cout<<"y queda así"<<endl;
  156.     MostrarTodo(reg, contProd);
  157.  
  158.     }break;
  159.     case 5:{}break;
  160.     case 0:{return 0;}break;
  161.     default:{cout<<"opcion no valida Rey"<<endl;}break;
  162.     }
  163.     }
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement