Advertisement
Weper

Object C++

Apr 25th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. struct kolcsonzes
  7. {
  8.     char datum[12];
  9.     char tipus[20];
  10.     int sorszam;
  11.     int ido;
  12. };
  13.  
  14. class kolcsonzo
  15. {
  16. private:   
  17.     kolcsonzes *k;
  18.     int db;
  19. public:
  20.     kolcsonzo(char fnev[]);
  21.     ~kolcsonzo();
  22.     int Getdb();
  23.     int GetMagellan();
  24.     int GetMedian();
  25.     int MedianBevetel();
  26.     int Osszbevetel();
  27.     int LeghosszabbKolcsonzes();
  28.     int LegtobbKolcsonzes();
  29.     void Lista();
  30.     void Kiir();
  31. };
  32.  
  33. kolcsonzo::kolcsonzo(char fnev[])
  34. {
  35.     ifstream be(fnev);
  36.     if(!be)
  37.     {
  38.         cerr<<"hiba";
  39.         exit(-1);
  40.     }
  41.     db=0;
  42.     char sor[80];
  43.     while(!be.eof())
  44.     {
  45.         be.getline(sor,80);
  46.         if(!be.eof()) db++;
  47.     }
  48.     be.clear();
  49.     be.seekg(0,ios::beg);
  50.    
  51.     k=new kolcsonzes[db];
  52.     if(k==0)
  53.     {
  54.         cerr<<"hiba2";
  55.         exit(-2);
  56.     }
  57.     for(int i=0; i<db;i++)
  58.     {
  59.         be>>k[i].datum;
  60.         be>>k[i].tipus;
  61.         be>>k[i].sorszam;
  62.         be>>k[i].ido;
  63.     }
  64.     be.close();
  65. }
  66. kolcsonzo::~kolcsonzo()
  67. {
  68.     if(k!=0)
  69.         delete[]k;
  70. }
  71. int kolcsonzo::Getdb()
  72. {
  73.     return db;
  74. }
  75. int kolcsonzo::GetMagellan()
  76. {
  77.     int m=0;
  78.     for (int i = 0; i<db; i++)
  79.     {
  80.         if(strcmp(k[i].tipus,"magellan")==0)
  81.         {
  82.             m++;
  83.         }
  84.     }
  85.     return m;
  86. }
  87. int kolcsonzo::GetMedian()
  88. {
  89.     int m=0;
  90.     for (int i = 0; i<db; i++)
  91.     {
  92.         if(strcmp(k[i].tipus,"median")==0)
  93.         {
  94.             m++;
  95.         }
  96.     }
  97.     return m;
  98. }
  99. int kolcsonzo::MedianBevetel()
  100. {
  101.     int mbev=0;
  102.     for (int i = 0; i<db; i++)
  103.     {
  104.         if(strcmp(k[i].tipus,"median")==0)
  105.         {
  106.             mbev+=k[i].ido*1000;
  107.         }
  108.     }
  109.     return mbev;
  110. }
  111. int kolcsonzo::Osszbevetel()
  112. {
  113.     int bev=0;
  114.     for (int i = 0; i<db; i++)
  115.     {
  116.         if(strcmp(k[i].tipus,"median")==0)
  117.         {
  118.             bev+=k[i].ido*1000;
  119.         }
  120.         else
  121.         {
  122.             bev+=k[i].ido*1200;
  123.         }
  124.     }
  125.     return bev;
  126. }
  127. int kolcsonzo::LeghosszabbKolcsonzes()
  128. {
  129.     int l=k[0].ido;
  130.     for (int i = 0; i<db; i++)
  131.     {
  132.         if(k[i].ido>l)
  133.         {
  134.             l=k[i].ido;
  135.         }
  136.     }
  137.     return l;
  138. }
  139. int kolcsonzo::LegtobbKolcsonzes()
  140. {
  141.     int max=0, maxsorszam=0, hanyszor=0;
  142.     for (int s = 20; s<31; s++)
  143.     {
  144.         hanyszor=0;
  145.         for (int i = 0; i<db; i++)
  146.         {
  147.             if(k[i].sorszam==s)
  148.             {
  149.                 hanyszor++;;
  150.             }
  151.         }
  152.         if(max<hanyszor)
  153.         {
  154.             maxsorszam=s;
  155.             max=hanyszor;
  156.         }
  157.     }
  158.     return maxsorszam;
  159. }
  160. void kolcsonzo::Lista()
  161. {
  162.     cout <<setw(12)<<"datum"<<setw(20)<<"tipus"<<setw(10)<<"sorszam"<<setw(10)<<"kolcsido"<<endl;
  163.     for (int i = 0; i<db; i++)
  164.     {
  165.         if(k[i].ido>5)
  166.         {
  167.             cout << setw(12)<<k[i].datum<<setw(20)<<k[i].tipus<<setw(10)<<k[i].sorszam<<setw(10)<<k[i].ido<<endl;
  168.         }
  169.     }
  170. }
  171. void kolcsonzo::Kiir()
  172. {
  173.     cout <<setw(14)<<"datum"<<setw(20)<<"tipus"<<setw(10)<<"sorszam"<<setw(10)<<"kolcsido"<<endl;
  174.     for (int i = 0; i<db; i++)
  175.     {
  176.  
  177.         cout << i+1 <<"."<<setw(12)<<k[i].datum<<setw(20)<<k[i].tipus<<setw(10)<<k[i].sorszam<<setw(10)<<k[i].ido<<endl;
  178.     }
  179. }
  180. int main(int argdb, char* argv[])
  181. {
  182.     if(argdb!=2)
  183.     {
  184.         cout<<"hibas programinditas\n";
  185.         return 1;
  186.     }
  187.     kolcsonzo ketkerek(argv[1]);
  188.     cout<<"Az allomanyban levo adatok:\n";
  189.     ketkerek.Kiir();
  190.     cout<<endl;
  191.     cout<<"A kolcsonzesek szama: "<<ketkerek.Getdb()<<endl;
  192.     cout<<"A magellan kolcsonzesek szama: "<<ketkerek.GetMagellan()<<endl;
  193.     cout<<"A median kolcsonzesek szama: "<<ketkerek.GetMedian()<<endl;
  194.     cout<<"A median kerekparokkal szerzett bevetel: "<<ketkerek.MedianBevetel()<<endl;
  195.     cout<<"A kerekparokkal szerzett osszbevetel: "<<ketkerek.Osszbevetel()<<endl;
  196.     cout<<"A leghosszabb kolcsonzes ideje: "<<ketkerek.LeghosszabbKolcsonzes()<<endl;
  197.     cout<<"A legtobbszor kolcsonzott kerekpar sorszama: "<<ketkerek.LegtobbKolcsonzes()<<endl;
  198.     cout<<endl;
  199.     cout<<"Az 5 oranal hosszabb kolcsonzesek:\n";
  200.     ketkerek.Lista();
  201.  
  202.     cout<<endl<<endl;
  203.     cout<<"***************** dinamikus objektum *****************\n\n";
  204.     kolcsonzo *kektura=new kolcsonzo(argv[1]);
  205.     if(kektura==0)
  206.     {
  207.         cerr<<"keves a memoria 2";
  208.         return 2;
  209.     }
  210.     cout<<"Az allomanyban levo adatok:\n";
  211.     kektura->Kiir();
  212.     cout<<endl;
  213.     cout<<"A kolcsonzesek szama: "<<kektura->Getdb()<<endl;
  214.     cout<<"A magellan kolcsonzesek szama: "<<kektura->GetMagellan()<<endl;
  215.     cout<<"A median kolcsonzesek szama: "<<kektura->GetMedian()<<endl;
  216.     cout<<"A median kerekparokkal szerzett bevetel: "<<kektura->MedianBevetel()<<endl;
  217.     cout<<"A kerekparokkal szerzett osszbevetel: "<<kektura->Osszbevetel()<<endl;
  218.     cout<<"A leghosszabb kolcsonzes ideje: "<<kektura->LeghosszabbKolcsonzes()<<endl;
  219.     cout<<"A legtobbszor kolcsonzott kerekpar sorszama: "<<kektura->LegtobbKolcsonzes()<<endl;
  220.     cout<<endl;
  221.     cout<<"Az 5 oranal hosszabb kolcsonzesek:\n";
  222.     kektura->Lista();
  223.     delete kektura;
  224.  
  225.     system("pause");
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement