Advertisement
emacuervo

Untitled

Dec 4th, 2023
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. void cargarCadena(char *palabra, int tam){
  7.     int i=0;
  8.     fflush(stdin);
  9.     for (i=0; i<tam; i++){
  10.         palabra[i]=cin.get();
  11.         if (palabra[i]=='\n'){
  12.             break;
  13.         }
  14.     }
  15.     palabra[i]='\0';
  16.     fflush(stdin);
  17. }
  18.  
  19. class Fecha{
  20. private:
  21.     int dia,mes, anio;
  22. public:
  23.     void Cargar(){
  24.         cin>>dia;
  25.         cin>>mes;
  26.         cin>>anio;
  27.     }
  28.     void Mostrar(){
  29.         cout<<dia<<"/";
  30.         cout<<mes<<"/";
  31.         cout<<anio<<endl;
  32.     }
  33.     int getDia(){return dia;}
  34.     int getMes(){return mes;}
  35.     int getAnio(){return anio;}
  36.  
  37.     void setDia(int d){dia=d;}
  38.     void setMes(int m){mes=m;}
  39.     void setAnio(int a){anio=a;}
  40.  
  41. };
  42.  
  43. class Partidos{
  44.     private:
  45.         int codPartido;
  46.         int sede;
  47.         int codLocal;
  48.         int codVisitante;
  49.         Fecha fechaRealizacion;
  50.         char nombreArbitro[50];
  51.         bool estado;
  52.  
  53.     public:
  54.         Partidos(int codPart = 0){
  55.             codPartido = codPart;
  56.         }
  57.  
  58.         int getCodPartido(){return codPartido;}
  59.         int getSede(){return sede;}
  60.         int getCodLocal(){return codLocal;}
  61.         int getCodVisitante(){return codVisitante;}
  62.         Fecha getFechaRealizacion(){return fechaRealizacion;}
  63.         const char* getNombreArbitro(){return nombreArbitro;}
  64.         bool getEstado(){return estado;}
  65.  
  66.         void setCodPartido(int cp){codPartido = cp;}
  67.         void setSede(int s){sede = s;}
  68.         void setCodLocal(int cl){codLocal = cl;}
  69.         void setCodVisitante(int cv){codVisitante = cv;}
  70.         void setFechaRealizacion(Fecha f){fechaRealizacion = f;}
  71.         void setNombreArbitro(const char* na){strcpy(nombreArbitro, na);}
  72.         void setEstado(bool e){estado = e;}
  73.  
  74.         bool Cargar(){
  75.             cout<< "CODIGO PARTIDO: ";
  76.             cin>> codPartido;
  77.             cout<< "SEDE: ";
  78.             cin>> sede;
  79.             cout<< "CODIGO EQUIPO LOCAL: ";
  80.             cin>> codLocal;
  81.             cout<< "CODIGO EQUIPO VISITANTE: ";
  82.             cin>> codVisitante;
  83.             cout<< "FECHA DE REALIZACION: ";
  84.             fechaRealizacion.Cargar();
  85.             cout<< "NOMBRE DEL ARBITRO: ";
  86.             cin>> nombreArbitro;
  87.             estado = true;
  88.  
  89.             return true;
  90.         }
  91.  
  92.         void Mostrar(){
  93.             cout<< codPartido << endl;
  94.             cout<< sede << endl;
  95.             cout<< codLocal << endl;
  96.             cout<< codVisitante << endl;
  97.             fechaRealizacion.Mostrar();
  98.             cout<< nombreArbitro << endl;
  99.             cout<< estado << endl;
  100.         }
  101. };
  102.  
  103.  
  104. class ArchivoPartido{
  105.     private:
  106.         char nombre[30];
  107.     public:
  108.         ArchivoPartido(const char *n){
  109.             strcpy(nombre, n);
  110.         }
  111.         bool grabarRegistro(Partidos reg){
  112.             FILE * archivo = fopen(nombre, "ab");
  113.             if (archivo == NULL){
  114.                 return false;
  115.             }
  116.             Partidos registro;
  117.             bool exito = registro.Cargar();
  118.             if (exito){
  119.                 fwrite(&registro, sizeof registro, 1, archivo);
  120.                 cout << "REGISTRO ESCRITO CON EXITO"<<endl;
  121.             }
  122.             fclose(archivo);
  123.             return true;
  124.         }
  125.         bool mostrarRegistros(){
  126.             FILE * archivo = fopen(nombre, "rb");
  127.             if (archivo == NULL)
  128.             {
  129.                 cout << "ERROR ABRIENDO EL ARCHIVO en MOSTRAR\n";
  130.                 return false;
  131.             }
  132.             Partidos leer;
  133.             while(fread(&leer, sizeof leer, 1, archivo)==1)
  134.             {
  135.                 if (leer.getEstado()) leer.Mostrar();
  136.             }
  137.             fclose(archivo);
  138.             return true;
  139.         }
  140.  
  141.         int buscarCodigo (int codigo) {
  142.             FILE * archivo = fopen(nombre, "rb");
  143.             if (archivo == NULL) {
  144.                 cout << "ERROR ABRIENDO EL ARCHIVO BUSCAR JUGADOR \n";
  145.                 return -1;
  146.             }
  147.             Partidos leer;
  148.             int pos = 0;
  149.             while (fread(&leer, sizeof leer, 1, archivo)) {
  150.                 if (leer.getCodPartido() == codigo && leer.getEstado()) {
  151.                     leer.Mostrar();
  152.                     fclose(archivo);
  153.                     return pos;
  154.                 }
  155.                 pos++;
  156.             }
  157.             cout << "NO SE ENCONTRO EL EQUIPO CON EL CODIGO INGRESADO \n";
  158.             fclose(archivo);
  159.             return -2;
  160.         }
  161.         bool bajaLogica() {
  162.             int codigo;
  163.             cout << "INGRESAR CODIGO DEL EQUIPO: ";
  164.             cin >> codigo;
  165.             int pos = buscarCodigo(codigo);
  166.             if (pos > -1) {
  167.                 FILE * archivo = fopen(nombre, "r+b");
  168.                 if (archivo == NULL) {
  169.                     cout << "ERROR ABRIENDO EL ARCHIVO \n";
  170.                     return false;
  171.                 }
  172.                 Partidos leer;
  173.                 fseek(archivo, sizeof leer * pos, SEEK_SET);
  174.                 fread(&leer, sizeof leer, 1, archivo);
  175.  
  176.                 leer.setEstado(false);
  177.                 fseek(archivo, sizeof leer * pos, SEEK_SET);
  178.                 if (fwrite(&leer, sizeof leer, 1, archivo)) {
  179.                     fclose(archivo);
  180.                     cout << "REGISTRO ELIMINADO CON EXITO \n";
  181.                     return true;
  182.                 }
  183.                 fclose(archivo);
  184.                 cout << "ERROR DE ARCHIVO, INTENTA DE NUEVO\n";
  185.                 return false;
  186.             }
  187.             return false;
  188.         }
  189.  
  190.         Partidos leerRegistro(int pos){
  191.             Partidos reg;
  192.             reg.setCodLocal(-1);
  193.             FILE *p;
  194.             p=fopen(nombre, "rb");
  195.             if(p==NULL) return reg;
  196.             fseek(p, sizeof(Partidos)*pos,0);
  197.             fread(&reg, sizeof reg,1, p);
  198.             fclose(p);
  199.             return reg;
  200.         }
  201.         int contarRegistros(){
  202.             FILE *p;
  203.             p=fopen(nombre, "rb");
  204.             if(p==NULL) return -1;
  205.             fseek(p, 0,2);
  206.             int tam=ftell(p);
  207.             fclose(p);
  208.             return tam/sizeof(Partidos);
  209.         }
  210. };
  211.  
  212. void punto3(){
  213.     ArchivoPartido archPart("Partidos.dat");
  214.     int cantPart = archPart.contarRegistros();
  215.     Partidos regPart;
  216.    
  217.     Fecha fechaNueva;
  218.     cout<< "INGRESAR FECHA: ";
  219.     fechaNueva.Cargar();
  220.  
  221.     if (cantPart <= 0){
  222.         cout<< "No hay registros" << endl;
  223.         return;
  224.     }
  225.    
  226.     int cont=0;
  227.  
  228.     for (int x=0; x<cantPart; x++){
  229.         regPart = archPart.leerRegistro(x);
  230.         if  (fechaNueva < regPart.getFechaRealizacion().getAnio() && regPart.getEstado()){
  231.             cont++
  232.         }
  233.     }
  234.    
  235.     Partidos *v;
  236.    
  237.     v=new Partidos[cont];
  238.     if(v==NULL) return;
  239.    
  240.     int pos=0;
  241.    
  242.     for (int x=0; x<cantPart; x++){
  243.         regPart = archPart.leerRegistro(x);
  244.         if (regPart.getEstado()){
  245.             v[pos] = regPart;
  246.             pos++;
  247.         }
  248.     }
  249.  
  250.     for (int x=0; x<cont; x++){
  251.         v[x].Mostrar();
  252.     }
  253.  
  254.     delete[] v;
  255. }
  256.  
  257.  
  258.  
  259. int main (void){
  260.     int opc;
  261.  
  262.     ArchivoPartido archPartidos("Partidos.dat");
  263.  
  264.     while(true){
  265.         system("cls");
  266.         cout<<"MENU ALUMNOS "<<endl;
  267.         cout<<"********************** "<<endl;
  268.         cout<<"1. AGREGAR REGISTRO "<<endl;
  269.         cout<<"2. BAJA LOGICA "<< endl;
  270.         cout<<"3. MOSTRAR REGISTROS "<<endl;
  271.         cout<<"0. SALIR DEL PROGRAMA "<<endl;
  272.         cout<<"********************** "<<endl;
  273.         cout<<"OPCION "<<endl;
  274.         cin>>opc;
  275.         system("cls");
  276.         switch(opc){
  277.             case 1:
  278.                 archPartidos.grabarRegistro(opc);
  279.                 break;
  280.  
  281.             case 2:
  282.                 archPartidos.bajaLogica();
  283.                 break;
  284.             case 3:
  285.                 punto3();
  286.                 break;
  287.             case 0:
  288.                 return 0;
  289.                 break;
  290.  
  291.         }
  292.         system("pause");
  293.         system("cls");
  294.     }
  295.  
  296.  
  297.   return 0;
  298. }
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement