Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
85
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. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. class NumarComplex {
  8. private:
  9.     float r;
  10.     float i;
  11. public:
  12.     NumarComplex() {}
  13.     NumarComplex(float r, float i)
  14.         : r(r)
  15.         , i(i)
  16.     {}
  17.  
  18.     friend ostream& operator<< (ostream& out, const NumarComplex& c);
  19. };
  20.  
  21. ostream& operator<< (ostream &out, const NumarComplex &c)
  22. {
  23.     out << c.r << " + " << c.i << "i\n";
  24.     return out;
  25. }
  26.  
  27. class A {
  28. private:
  29.     int ri; // random integer
  30.     float rf; // random float;
  31.     NumarComplex rc; //random complex
  32. public:
  33.     A(){
  34.         srand(time(NULL));
  35.         ri = rand();
  36.         rf = (float)(RAND_MAX) / (float)rand();
  37.         rc = NumarComplex((float)(RAND_MAX) / (float)rand(), (float)(RAND_MAX) / (float)rand());
  38.  
  39.     }
  40.  
  41.     int getRi() {
  42.         return ri;
  43.     }
  44.  
  45.     float getRf() {
  46.         return rf;
  47.     }
  48.  
  49.     NumarComplex getRc() {
  50.         return rc;
  51.     }
  52.  
  53.     template <typename T>
  54.     T getRandom(T t) {
  55.             return t;
  56.     }
  57. };
  58.  
  59. int main() {
  60.     A a;
  61.    
  62.     srand(time(NULL));
  63.     int random_type = (int)(rand() % 3);
  64.     if (random_type == 0)
  65.         cout << "Nr intreg: " << a.getRandom<int>(a.getRi());
  66.     else if (random_type == 1)
  67.         cout << "Nr real: " << a.getRandom<float>(a.getRf());
  68.     else if (random_type == 2)
  69.         cout << "Nr complex: " << a.getRandom<NumarComplex>(a.getRc());
  70.    
  71.     return 0;
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement