Share Pastebin
Guest
Public paste!

practicas punteros

By: a guest | Mar 16th, 2010 | Syntax: C++ | Size: 14.51 KB | Hits: 35 | Expires: Never
Copy text to clipboard
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. #define _n <<char(-92)<< //la letra ñ.
  8.  
  9. const int MAX_TITULO = 100;
  10. const int MAX_GENERO = 50;
  11. const int MAX_SIPNOSIS = 1024;
  12. const int MAX_PELICULAS = 1024;
  13. const int MAX_BUFF = 1024;
  14.  
  15. typedef unsigned short USHORT;
  16.  
  17.  
  18.  
  19. struct pelicula {
  20.     char titulo[MAX_TITULO];
  21.     char genero[MAX_GENERO];
  22.     char sipnosis[MAX_SIPNOSIS];
  23.     USHORT anno;
  24. };
  25.  
  26. struct videoteca {
  27.     pelicula lista[MAX_PELICULAS];
  28.     USHORT cnt;
  29. };
  30. /**************************************** P R O T O T I P O S **********************************/
  31.  
  32. int salir();
  33. void iniciarVideoteca(videoteca *pVid);
  34. void finalizarVideoteca(videoteca *pVid);
  35. void asignarPelicula(pelicula *pPeli, const char *titulo);
  36. void asignarPelicula(pelicula *pPeli, const char *genero);
  37. void asignarPelicula(pelicula *pPeli, const USHORT anno);
  38. void asignarPelicula(pelicula *pPeli, const char *sipnosis);
  39. bool saveVideoteca(videoteca *pVid, const char *nombreArchivo, bool overwrite=false);
  40. USHORT loadVideoteca(videoteca *pVid, const char *nombreArchivo);
  41. void ordenarVid(videoteca *pVid);
  42. void iniciarPelicula(pelicula *pPeli);
  43. void AddPeli(videoteca *pVid, pelicula *pPeli);
  44. void moverPelis(videoteca *pVid, USHORT posPeli1, USHORT posPeli2);
  45. void borrarPeli(videoteca *pVid, const pelicula *pPeli, const USHORT opBorrar);
  46. USHORT BuscaPeli(USHORT *temp, videoteca *pVid, USHORT modoBusqueda, pelicula *CampoPeli);
  47. void imprimirResultados(const videoteca *pVid, USHORT *listaResultados, USHORT resultados);
  48. void imprimirVideoteca(const videoteca *pVid, const USHORT modo, pelicula *campoPeli);
  49. bool VidLLena(const videoteca *pVid);
  50. bool VidVacia(const videoteca *pVid);
  51.  
  52. /******************************************  M A I N  *****************************************/
  53. int main() {
  54.     USHORT op = 0, op2 = 0, resultados = 0;
  55.     USHORT tmpList[MAX_PELICULAS];
  56.     char buffer[MAX_BUFF];
  57.     videoteca videot;
  58.     iniciarVideoteca(&videot);
  59.     pelicula temp;
  60.  
  61.  while(op != 7) {
  62.  system("cls");
  63.  cout << " -------------------------------------------" << endl;
  64.  cout << "|| Videotech || Organizador de Videotecas. ||" << endl;
  65.  cout << " -------------------------------------------" << endl;
  66.  cout << "(1) A"_n"adir Pelicula." << endl;
  67.  cout << "(2) Buscar Pelicula." << endl;
  68.  cout << "(3) Mostrar Videoteca." << endl;
  69.  cout << "(4) Eliminar Pelicula." << endl;
  70.  cout << "(5) Guardar Videoteca." << endl;
  71.  cout << "(6) Cargar Videoteca." << endl;
  72.  cout << "(7) Salir." << endl;
  73.  cin >> op;
  74.  cin.get();
  75.  
  76. switch (op) {
  77.     case 1:////
  78.     cout << "Introduzca el Titulo: " << endl;
  79.  
  80.     cin.getline(temp.titulo, MAX_TITULO);
  81.     cout << "Introduzca el Genero: " << endl;
  82.     cin.getline(temp.genero, MAX_GENERO);
  83.     cout << "Introduzca el A"_n"o de estreno: " << endl;
  84.     cin >> temp.anno;
  85.     cin.get();
  86.     cout << "Introduzca la Sipnosis de la Pelicula: " << endl;
  87.     cin.getline(temp.sipnosis, MAX_SIPNOSIS);
  88.  
  89.     AddPeli(&videot, &temp);
  90.     break;
  91.     case 2:////
  92.     system("cls");
  93.     op2 = 0;
  94.     while(op2 != 1 && op2 != 2 && op2 != 3) {
  95.     cout << "Seleccione un Metodo de Busqueda: " << endl;
  96.     cout << "(1) Por Titulo." << endl;
  97.     cout << "(2) Por Genero." << endl;
  98.     cout << "(3) Por A"_n"o." << endl;
  99.     cin >> op2;
  100.     cin.get();
  101.     }
  102.     switch(op2) {
  103.     case 1:
  104.     cout << "Introduzca el Titulo a buscar:" << endl;
  105.     iniciarPelicula(&temp);
  106.     cin.getline(temp.titulo, MAX_TITULO);
  107.     resultados = BuscaPeli(tmpList, &videot, op2, &temp);
  108.     imprimirResultados(&videot, tmpList, resultados);
  109.     break;
  110.     case 2:
  111.     cout << "Introduzca el Genero a buscar:" << endl;
  112.     iniciarPelicula(&temp);
  113.     cin.getline(temp.genero, MAX_GENERO);
  114.     resultados = BuscaPeli(tmpList, &videot, op2, &temp);
  115.     imprimirResultados(&videot, tmpList, resultados);
  116.     break;
  117.     case 3:
  118.         cout << "Introduzca el A"_n"o a buscar:" << endl;
  119.     iniciarPelicula(&temp);
  120.     cin >> temp.anno;
  121.     cin.get();
  122.     resultados = BuscaPeli(tmpList, &videot, op2, &temp);
  123.     imprimirResultados(&videot, tmpList, resultados);
  124.     break;
  125.     }
  126.     break;
  127.     case 3:///////////////////////////////////////////////////////
  128.     cout << "Mostrar orden por: " << endl;
  129.     cout << "(1) Titulo." << endl;
  130.     cout << "(2) Genero." << endl;
  131.     cout << "(3) A"_n"o" << endl;
  132.     cin >> op2;
  133.     cin.get();
  134.     switch (op2) {
  135.         case 1:
  136.     ordenarVid(&videot);
  137.     imprimirVideoteca(&videot, op2, &temp);
  138.     break;
  139.     case 2:
  140.     iniciarPelicula(&temp);
  141.     cout << "Introduzca el Genero: " << endl;
  142.     cin.getline(temp.genero, MAX_GENERO);
  143.     imprimirVideoteca(&videot, op2, &temp);
  144.     break;
  145.     case 3:
  146.     iniciarPelicula(&temp);
  147.     cout << "Introduzca el A"_n"o" << endl;
  148.     cin >> temp.anno;
  149.     cin.get();
  150.     imprimirVideoteca(&videot, op2, &temp);
  151.  
  152.     break;
  153.      system("pause");
  154.         }
  155.  
  156.     break;
  157.     case 4://///////////////////////////////////////////////////////
  158.     op2 = 0;
  159.     cout << "Seleccion el criterio para Borrar: " << endl;
  160.     cout << "(1) Por Titulo" << endl;
  161.     cout << "(2) Por A"_n"o" << endl;
  162.     cout << "(3) Por Genero" << endl;
  163.     cin >> op2;
  164.     cin.get();
  165.     switch(op2) {
  166.         case 1:
  167.         cout << "Introduzca el Titulo: " << endl;
  168.         cin.getline(temp.titulo, MAX_TITULO);
  169.         borrarPeli(&videot, &temp, op2);
  170.         break;
  171.         case 2:
  172.         cout << "Introduzca el Genero: " << endl;
  173.         cin.getline(temp.genero, MAX_GENERO);
  174.         borrarPeli(&videot, &temp, op2);
  175.         break;
  176.         case 3:
  177.         cout << "Introduzca el A"_n"o: " << endl;
  178.         cin >> temp.anno;
  179.         cin.get();
  180.         borrarPeli(&videot, &temp, op2);
  181.         break;
  182.     }
  183.     case 5:////////////////////////////////////////////////////////////////////////////
  184. cout << "Introduzca el nombre de la Videoteca: " << endl;
  185.     cin.getline(buffer, MAX_BUFF);
  186.     while(!saveVideoteca(&videot, buffer)) {
  187.         do {
  188.         cout << "Desea sobreescribir la Videoteca: " << buffer << "?" << endl;
  189.         cout << "(1) Si." << endl;
  190.         cout << "(2) No." << endl;
  191.         op2=0;
  192.         cin >> op2;
  193.         cin.get();
  194.         if(op2 == 1) { saveVideoteca(&videot, buffer, true); }
  195.         if(op2 == 2) {
  196.             cout << "Introduzca el nuevo nombre de la Videoteca: " << endl;
  197.             cin.getline(buffer, MAX_BUFF);
  198.             saveVideoteca(&videot, buffer);
  199.         }
  200.         } while(op2 != 1 && op2 != 2);
  201.         }
  202.  
  203.     break;
  204.     case 6:
  205.     cout << "Introduzca el nombre de la Videoteca" << endl;
  206.     cin.getline(buffer, MAX_BUFF);
  207.     loadVideoteca(&videot, buffer);
  208.     break;
  209.     case 7://////////////////////////////////////////////////////////////////////////////
  210.     salir();
  211.     break;
  212.  
  213.  }
  214.  }
  215.  
  216.  
  217. return 0;
  218.  
  219. }
  220. /************************************************ End Of Main *********************************************/
  221.  
  222. /*****************************  R U T I N A S   *************************************/
  223. void iniciarVideoteca(videoteca *pVid) {
  224.     pVid->cnt = 0;
  225. }
  226.  
  227. void finalizarVideoteca( videoteca *pVid )  {
  228.     }
  229.  
  230. void iniciarPelicula(pelicula *pPeli) {
  231.  pPeli->titulo[0] = 0;
  232.  pPeli->genero[0] = 0;
  233.  pPeli->anno = 0;
  234.  pPeli->sipnosis[0] = 0;
  235. }
  236.  
  237. void asignarTitulo(pelicula *pPeli, const char *titulo) {
  238.     strncpy(pPeli->titulo, titulo, MAX_TITULO);
  239. }
  240. void asignarGenero(pelicula *pPeli, const char *genero) {
  241.     strncpy(pPeli->genero, genero, MAX_GENERO);
  242. }
  243. void asignarAnno(pelicula *pPeli, const USHORT anno) {
  244.     pPeli->anno = anno;
  245. }
  246. void asignarSipnosis(pelicula *pPeli, const char *sipnosis) {
  247.     strncpy(pPeli->sipnosis, sipnosis, MAX_SIPNOSIS);
  248. }
  249. /******************************************** G U A R D A R   V I D E O T E C A  ****************************************/
  250. bool saveVideoteca(videoteca *pVid, const char *nombreArchivo, bool overwrite) {
  251. USHORT i=0;
  252. ifstream varchivo(nombreArchivo);
  253.     if(!varchivo) {
  254.     ofstream varchivo(nombreArchivo);
  255.     while(i<pVid->cnt) {
  256.         varchivo << pVid->lista[i].titulo << endl;
  257.         varchivo << pVid->lista[i].genero << endl;
  258.         varchivo << pVid->lista[i].anno << endl;
  259.         varchivo << pVid->lista[i].sipnosis << endl;
  260.         i++;
  261.     }
  262.     return true;
  263.    varchivo.close();
  264.     }
  265.     else {
  266.         if(overwrite) {
  267.             ofstream varchivo(nombreArchivo);
  268.     while(i<pVid->cnt) {
  269.         varchivo << pVid->lista[i].titulo << endl;
  270.         varchivo << pVid->lista[i].genero << endl;
  271.         varchivo << pVid->lista[i].anno << endl;
  272.         varchivo << pVid->lista[i].sipnosis << endl;
  273.         i++;
  274.     }
  275.     return true;
  276.         } else {
  277.         cout << "El archivo ya existe" << endl;
  278.         system("pause");
  279.         return false;
  280.  
  281.         }
  282.     }
  283. varchivo.close();
  284. }
  285.  
  286. /******************************************** C A R G A R   V I D E O T E C A  ****************************************/
  287. USHORT loadVideoteca(videoteca *pVid, const char *nombreArchivo) {
  288. USHORT i=0;
  289. iniciarVideoteca(pVid);
  290.     ifstream varchivo(nombreArchivo);
  291.     if(!varchivo) {
  292.          cout << "Error al abrir el archivo" << endl;
  293.     return 1;
  294.     }
  295.     while(!varchivo.eof()) {
  296.         varchivo.getline(pVid->lista[i].titulo, MAX_TITULO);
  297.         varchivo.getline(pVid->lista[i].genero, MAX_GENERO);
  298.         varchivo >> pVid->lista[i].anno;
  299.         varchivo.get();
  300.         varchivo.getline(pVid->lista[i].sipnosis, MAX_SIPNOSIS);
  301.         pVid->cnt++;
  302.         i++;
  303.  
  304.     }
  305. varchivo.close();
  306. }
  307.  
  308.  
  309. /********************************************** O R D E N A R   V I D E O T E C A  ************************************/
  310. void ordenarVid(videoteca *pVid) {
  311.     pelicula temp;
  312.     USHORT ialfab;
  313.  
  314.         for(USHORT i = 0; i<pVid->cnt; i++) {
  315.             for(USHORT i2 = 0; i2<pVid->cnt-2; i2++) {
  316.                 if(strcmp(pVid->lista[i2].titulo, pVid->lista[i2+1].titulo) > 0) {
  317.                     temp = pVid->lista[i2];
  318.                     pVid->lista[i2] = pVid->lista[i2+1];
  319.                     pVid->lista[i2+1] = temp;
  320.  
  321.  
  322.             }
  323.             }
  324.  
  325. }
  326. }
  327. /******************************************** I M P R I M I R   V I D E O T E C A   ************************************/
  328. void imprimirVideoteca(const videoteca *pVid, const USHORT modo, pelicula *campoPeli) {
  329. switch(modo) {
  330.     case 1:
  331. for(USHORT i = 0; i<pVid->cnt; i++) {
  332.     cout << pVid->lista[i].titulo << endl;
  333.     cout << pVid->lista[i].genero << endl;
  334.     cout << pVid->lista[i].anno << endl;
  335.     cout << pVid->lista[i].sipnosis << endl;
  336.     cout << "----------------------------" << endl;
  337. }
  338. break;
  339. case 2:
  340. for(USHORT i = 0; i<pVid->cnt; i++) {
  341. if(!strcmp(pVid->lista[i].genero, campoPeli->genero)) {
  342.      cout << pVid->lista[i].titulo << endl;
  343.     cout << pVid->lista[i].genero << endl;
  344.     cout << pVid->lista[i].anno << endl;
  345.     cout << pVid->lista[i].sipnosis << endl;
  346.     cout << "----------------------------" << endl;
  347. }
  348. }
  349.  
  350. break;
  351. case 3:
  352. for(USHORT i = 0; i<pVid->cnt; i++) {
  353. if(pVid->lista[i].anno == campoPeli->anno) {
  354.      cout << pVid->lista[i].titulo << endl;
  355.     cout << pVid->lista[i].genero << endl;
  356.     cout << pVid->lista[i].anno << endl;
  357.     cout << pVid->lista[i].sipnosis << endl;
  358.     cout << "----------------------------" << endl;
  359. }
  360. }
  361. break;
  362. }
  363. }
  364.  
  365. /********************************************** A Ñ A D I R   P E L I C U L A *****************************************/
  366. void AddPeli(videoteca *pVid, pelicula *pPeli) {
  367.  
  368. if(!VidLLena(pVid)) {
  369.     pVid->lista[pVid->cnt] = *pPeli;
  370.     pVid->cnt++;
  371. }
  372. }
  373. /******************************************** B U S C A R   P E L I C U L A S  *****************************************/
  374. USHORT BuscaPeli(USHORT *tmpList, videoteca *pVid, USHORT modoBusqueda, pelicula *CampoPeli) {
  375. USHORT b = 0;
  376.  
  377. switch (modoBusqueda) {
  378.  case 1:
  379.  for(USHORT i=0; i<pVid->cnt; i++) {
  380.      if(!strcmp(pVid->lista[i].titulo, CampoPeli->titulo)) {
  381.     tmpList[b] = i;
  382.     b++;
  383.      }
  384.  }
  385.  break;
  386.  case 2:
  387.  for(USHORT i=0; i<pVid->cnt; i++) {
  388.      if(!strcmp(pVid->lista[i].genero, CampoPeli->genero)) {
  389.     tmpList[b] = i;
  390.     b++;
  391.      }
  392.  }
  393.  break;
  394.  case 3:
  395.  for(USHORT i=0; i<pVid->cnt; i++) {
  396.      if(pVid->lista[i].anno == CampoPeli->anno) {
  397.     tmpList[b] = i;
  398.     b++;
  399.      }
  400.  }
  401. }
  402.  
  403. return b;
  404. }
  405. /*************************************** M O V E R   P E L I C U L A  **********************************************/
  406. void moverPelis(videoteca *pVid, USHORT posPeli1, USHORT posPeli2) {
  407.     pelicula temp;
  408.  
  409.     temp = pVid->lista[posPeli1];
  410.     pVid->lista[posPeli1] = pVid->lista[posPeli2];
  411.     pVid->lista[posPeli2] = temp;
  412. }
  413. /*************************************** E L I M I N A R     P E L I C U L A *****************************************/
  414. void borrarPeli(videoteca *pVid, const pelicula *pPeli, const USHORT opBorrar) {
  415.     switch (opBorrar) {
  416.         case 1:
  417.     for(USHORT i = 0; i<MAX_PELICULAS; i++) {
  418.         if(!strcmp(pVid->lista[i].titulo, pPeli->titulo)) {
  419.             iniciarPelicula(&pVid->lista[i]);
  420.             moverPelis(pVid, i, pVid->cnt);
  421.             pVid->cnt--;
  422.         }
  423.     }
  424.     break;
  425.     case 2:
  426.     for(USHORT i = 0; i<MAX_PELICULAS; i++) {
  427.         if(!strcmp(pVid->lista[i].genero, pPeli->genero)) {
  428.             iniciarPelicula(&pVid->lista[i]);
  429.             moverPelis(pVid, i, pVid->cnt);
  430.             pVid->cnt--;
  431.         }
  432.     }
  433.    break;
  434.    case 3:
  435. for(USHORT i = 0; i<MAX_PELICULAS; i++) {
  436.         if(pVid->lista[i].anno == pPeli->anno) {
  437.             iniciarPelicula(&pVid->lista[i]);
  438.             moverPelis(pVid, i, pVid->cnt);
  439.             pVid->cnt--;
  440.         }
  441.     }
  442.     break;
  443.   }
  444. }
  445. /*************************************** I M P R I M I R   R E S U L T A D O S ******************************************/
  446. void imprimirResultados(const videoteca *pVid, USHORT *listaResultados, USHORT resultados) {
  447.  for(USHORT i=0;i<resultados;i++) {
  448.      cout << resultados << "  Resultados encontrados en " << pVid->cnt << "  Peliculas totales." << endl;
  449.  
  450.          cout << pVid->lista[listaResultados[i]].titulo << endl;
  451.          cout << pVid->lista[listaResultados[i]].genero << endl;
  452.          cout << pVid->lista[listaResultados[i]].anno << endl;
  453.          cout << pVid->lista[listaResultados[i]].sipnosis << endl;
  454.          system("pause");
  455.  
  456.  }
  457. }
  458. /********************************************** C O M P R O B A C I O N E S *********************************************/
  459. bool VidLLena(const videoteca *pVid) {
  460.  return pVid->cnt == MAX_PELICULAS;
  461.  
  462. }
  463. bool VidVacia(const videoteca *pVid) {
  464.  
  465.   return pVid->cnt == 0;
  466. }
  467.  
  468. int salir() {
  469.     return 0;
  470. }