Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. /** gen_array3.hpp **/
  2. template <class T, int s = 50, class E = std::range_error>
  3. class Array {
  4.  
  5. public:
  6.     T& operator[](int i);
  7.     const T& operator[](int i) const;
  8. };
  9.  
  10. /** gen_array3_main.cpp **/
  11. #include <iostream>
  12. #include <stdexcept>
  13. #include <string>
  14. #include "gen_array3.hpp"           // sablon
  15.  
  16. using namespace std;
  17.  
  18. class BajVan {
  19. public:
  20.     BajVan(const string&) {}
  21. };
  22.  
  23.  
  24. /// TESZT 1.
  25. void test_1() {
  26.     Array<int> arr;                 // default (50) elemû int tömb
  27.     int idx;
  28.     int val;
  29.     cout << "default (50) elemu int tomb" << endl;
  30.     while ( cin >> idx >> val) {
  31.       arr[idx] = val;
  32.       cout << "arr[" << idx << "]=" << arr[idx] << endl;
  33.     }
  34. }
  35.  
  36. /// TESZT 2
  37. void test_2() {
  38.     Array<double, 100> arr;         // 100 elemû double tömb
  39.     int idx;
  40.     double val;
  41.     cout << "100 elemu double tomb" << endl;
  42.     while ( cin >> idx >> val) {
  43.       arr[idx] = val;
  44.       cout << "arr[" << idx << "]=" << arr[idx] << endl;
  45.     }
  46. }
  47.  
  48. /// TESZT 3
  49. void test_3() {
  50.     Array<string, 11, BajVan> arr;  // 11 elemû string saját kivétellel
  51.     int idx;
  52.     string val;
  53.     cout << "11 elemu string saját kivetellel" << endl;
  54.     while ( cin >> idx >> val) {
  55.       arr[idx] = val;
  56.       cout << "arr[" << idx << "]=" << arr[idx] << endl;
  57.     }
  58. }
  59.  
  60. /// Fõprogram a standard inputról olvas egy egész számot, majd
  61. /// meghivja az annak megfelelõ tesztesetet.
  62. /// A további inputot és outputot a teszteset kezeli.
  63. int main() {
  64.     try {
  65.       int nr;
  66.       cin >> nr;        // hanaydik teszt eset
  67.       switch (nr) {
  68.         case 1:
  69.             test_1();   // default (50) elemû int tömb
  70.         break;
  71.  
  72.         case 2:
  73.             test_2();   // 100 elemû double tömb
  74.         break;
  75.  
  76.         case 3:
  77.             test_3();   // 11 elemû string saját kivételosztállyal
  78.         break;
  79.       }
  80.     // kivétel elkapása
  81.     } catch (exception& e) {
  82.         cerr << e.what() << endl;
  83.     } catch (BajVan&) {
  84.         cerr << "Sajat kivetel jott" << endl;
  85.     } catch (...) {
  86.         cerr << "*** Nagy baj van! ****" << endl;
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement