Advertisement
Smudla

CPP CV_3

Oct 22nd, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Pole{
  7.     int* pole;
  8.     size_t velikost;
  9. public:
  10.     Pole(size_t pocet){
  11.         if (pocet < 1){
  12.             throw exception("Rozmer pole musi byt vetsi nez 1!");
  13.         }
  14.         velikost = pocet;
  15.         pole = new int[velikost];
  16.         for (size_t i = 0; i < velikost; i++)
  17.         {
  18.             pole[i] = 0;
  19.         }
  20.     }
  21.  
  22.     Pole(string str){
  23.         if (str.length() == 0){
  24.             throw exception("Retezec je prazdny!");
  25.         }
  26.         velikost= str.length();
  27.         pole = new int[velikost];
  28.         for (size_t i = 0; i < velikost; i++)
  29.         {
  30.             pole[i] = 0;
  31.         }
  32.         for (size_t i = 0; i < velikost; i++) {
  33.             if (str.at(i) < '0' || str[i] > '9'){
  34.                 throw exception("Cislo neni v rozsahu 1-9");
  35.             }
  36.             pole[i] =str.at(i)-'0';
  37.         }
  38.     }
  39.  
  40.     const int& operator[](size_t index) const{
  41.         if (index >=velikost){
  42.             throw exception("Index mimo pole hombre!");
  43.         }
  44.         return pole[index];
  45.     }
  46.  
  47.  
  48.     int& operator[](size_t index){
  49.         if (index >= velikost){
  50.             throw exception("Index mimo pole hombre!");
  51.         }
  52.         return pole[index];
  53.     }
  54.  
  55.     ~Pole(){
  56.         delete[] pole;
  57.     }
  58. };
  59.  
  60.  
  61.  
  62.  
  63.  
  64. int main(){
  65.     Pole p1(3);
  66.     Pole p2("5555");
  67.     printf("Pole: %d %d %d \n", p1[0], p1[1], p1[2]);
  68.     printf("Pole: %d %d %d %d \n", p2[0], p2[1], p2[2], p2[3]);
  69.  
  70.     system("pause");
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement