Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <c++/map>
  4. #include <c++/fstream>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. void swap2(T &t1, T &t2)
  10. {
  11.     T tmp = t1;
  12.     t1 = t2;
  13.     t2 = tmp;
  14. }
  15.  
  16. class Bryla{
  17.     public:
  18.         Bryla(){ };
  19.         virtual double obj() = 0;
  20. };
  21.  
  22. class Szescian : public Bryla{
  23.     private:
  24.         double a;
  25.     public:
  26.     Szescian(double a) : a(a){ };
  27.  
  28.     double obj() override
  29.     {
  30.         return a*a*a;
  31.     }
  32.  
  33. };
  34.  
  35. class Kula : public Bryla{
  36. private:
  37.     double r;
  38. public:
  39.     Kula(double r) : r(r) {};
  40.     double obj() override
  41.     {
  42.         double PI = 3.14;
  43.         return 4/3*PI*r*r*r;
  44.     }
  45. };
  46.  
  47. class ReadFile{
  48. private:
  49.     string fName;
  50.     fstream plik;
  51.     map<string, int> wyrazy;
  52. public:
  53.     ReadFile(const string & f) : fName(f){};
  54.     void readWords()
  55.     {
  56.         plik.open(fName,ios::in);
  57.         if(!plik.good())
  58.             throw runtime_error("Nie mozna odczytac pliku");
  59.         else
  60.         {
  61.             string tmp;
  62.             while(!plik.eof())
  63.             {
  64.                 plik >> tmp;
  65.                 auto it = wyrazy.find(tmp);
  66.                 if(it == wyrazy.end())
  67.                 {
  68.                     wyrazy.insert(make_pair(tmp,1));
  69.                 }
  70.                 else
  71.                 {
  72.                     it->second++;
  73.                 }
  74.             }
  75.         }
  76.     }
  77. };
  78.  
  79. int main()
  80. {
  81.     int a, b;
  82.     a = 5;
  83.     b = 3;
  84.     cout << a <<  " " << b << endl;
  85.     swap2(a,b);
  86.     cout << a << " " << b << endl;
  87.  
  88.  
  89.     vector<Bryla*> bryly(2);
  90.     bryly[0] = new Kula(5);
  91.     bryly[1] = new Szescian(10);
  92.     double objsum = 0;
  93.     for(const auto& b : bryly)
  94.     {
  95.         objsum += b->obj();
  96.     }
  97.     double objAvg = objsum / bryly.size();
  98.     cout << objAvg << endl;
  99.  
  100.     ReadFile rf("test.txt");
  101.     rf.readWords();
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement