Advertisement
xerpi

PRO2 gesto vtable

Mar 4th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2014 Sergi Granell
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22.  
  23. #include "Cjt_estudiants.hpp"
  24.  
  25.  
  26. void redondear_e_a(Estudiant & est);
  27. /* Pre: est tiene nota */
  28. /* Post: est pasa a tener su nota original redondeada */
  29.  
  30. #define opcio_func_declr(n) \
  31.     void opcio_##n(Cjt_estudiants &cjt);
  32.    
  33. #define opcio_nom(n) \
  34.     opcio_##n
  35.  
  36. opcio_func_declr(1);
  37. opcio_func_declr(2);
  38. opcio_func_declr(3);
  39. opcio_func_declr(4);
  40. opcio_func_declr(5);
  41.  
  42. typedef void (*vtable_entry)(Cjt_estudiants &cjt);
  43. vtable_entry vtable[] = {
  44.     opcio_nom(1),
  45.     opcio_nom(2),
  46.     opcio_nom(3),
  47.     opcio_nom(4),
  48.     opcio_nom(5),
  49. };
  50.  
  51. int main()
  52. {
  53.     Cjt_estudiants cjt;
  54.     cout << "Escriu la mida del conjunt i els elements" << endl;
  55.    
  56.     cjt.llegir_cjt_estudiants();
  57.     int opcio = 0;
  58.     while (opcio != -6) {
  59.         cout << "Escriu una opcio: " << endl;
  60.         opcio = readint();
  61.         if (opcio < 0 && opcio > -6) {
  62.             vtable[-opcio-1](cjt);
  63.         }
  64.     }
  65. }
  66.  
  67.  
  68. void opcio_1(Cjt_estudiants &cjt)
  69. {
  70.     cout << "Escriu un estudiant:" << endl;
  71.     Estudiant e;
  72.     e.llegir_estudiant();
  73.     cjt.afegir_estudiant(e);
  74. }
  75.  
  76. void opcio_2(Cjt_estudiants &cjt)
  77. {
  78.     int dni = readint();
  79.     if (cjt.existeix_estudiant(dni)) {
  80.         Estudiant e = cjt.consultar_estudiant(dni);
  81.         if (e.te_nota())
  82.             cout << "El estudiant " << dni << " te nota " << e.consultar_nota() << endl;
  83.         else
  84.             cout << "El estudiant " << dni << " no te nota" << endl;
  85.     } else {
  86.         cout << "El estudiant " << dni << " no existeix" << endl;
  87.     }
  88. }
  89. void opcio_3(Cjt_estudiants &cjt)
  90. {
  91.     cout << "Escriu el DNI i la nova nota: ";
  92.     int dni = readint();
  93.     double nota = readdouble();
  94.     if (cjt.existeix_estudiant(dni)) {
  95.         Estudiant e = cjt.consultar_estudiant(dni);
  96.         if (e.te_nota())
  97.             e.modificar_nota(nota);
  98.         else
  99.             e.afegir_nota(nota);
  100.         cjt.modificar_estudiant(e);
  101.     } else {
  102.         cout << "El estudiant " << dni << " no existeix" << endl;
  103.     }
  104. }
  105. void opcio_4(Cjt_estudiants &cjt)
  106. {
  107.     for (int i = 1; i <= cjt.mida(); ++i) {
  108.         Estudiant e = cjt.consultar_iessim(i);
  109.         if (e.te_nota()) {
  110.             redondear_e_a(e);
  111.             cjt.modificar_iessim(i, e);
  112.         }
  113.     }
  114. }
  115. void opcio_5(Cjt_estudiants &cjt)
  116. {
  117.     cjt.escriure_cjt_estudiants();
  118. }
  119.  
  120.  
  121. void redondear_e_a(Estudiant & est)
  122. {
  123.   est.modificar_nota(((int) (10. * (est.consultar_nota() + 0.05))) / 10.0);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement