Advertisement
macrofish

alg ucty

Jul 21st, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. const int N=10; //pocet uctu
  7. const int M=100000; //generovani cash
  8. const int P=10;//urok
  9. const int Y=1;//pocet let
  10.  
  11. class Ucet
  12. {
  13. private:
  14.     static int m_Count;//aktualni pocet uctu
  15.     int m_Urok;
  16.     int m_CisloUctu;
  17.     int m_Cash;
  18. public:
  19.     Ucet();
  20.     int Cislo();
  21.     int Urok();
  22.     void Vlozit(int x);
  23.     void Vybrat(int x);
  24.     int AktualniStav();
  25.     void PripsatUrok();
  26.     virtual ~Ucet();
  27. };
  28.  
  29. int Ucet::m_Count=0;//nastaven na 0 defaultne
  30.  
  31.  
  32. class List
  33. {
  34. private:
  35.     int m_items;//pocet items v seznamu
  36.     struct Node
  37.     {
  38.         Ucet *data;
  39.         Node *next;
  40.         Node *prev;
  41.     };
  42.     Node *head;
  43.     Node *current;
  44. public:
  45.     List();
  46.     void Uroceni();
  47.     void Vklad();
  48.     void Vyber();
  49.     void Report();
  50.     bool isEmpty();
  51.     void Insert(Ucet *X);
  52.     virtual ~List();
  53. };
  54.  
  55. List::List()
  56. {
  57.     m_items=0;
  58.     head=current=NULL;
  59. }
  60.  
  61. List::~List()
  62. {
  63.         while (head!=NULL)
  64.         {
  65.                 Node *tmp = head;
  66.                 head=head->next;
  67.                 delete tmp;
  68.         }
  69.         current=NULL;
  70. }
  71.  
  72. bool List::isEmpty()
  73. {
  74.     return head==NULL;
  75. }
  76.  
  77. void List::Insert(Ucet *item)
  78. {
  79.     Node *add=new Node;
  80.     m_items++;
  81.     add->data=item;
  82.     add->next=NULL;
  83.     add->prev=NULL;
  84.     if(head == NULL)
  85.         {
  86.             head=add;
  87.         }
  88.     else
  89.         {
  90.             current->next=add;
  91.             add->prev=current;
  92.         }
  93.     current=add;
  94. }
  95.  
  96. void List::Uroceni()
  97. {
  98.     Node *tmp;
  99.    
  100.     for(tmp=head;tmp!=NULL; tmp=tmp->next)
  101.     {
  102.         cout << "U" <<tmp->data->Cislo() <<"[" << tmp->data->AktualniStav() <<"]" << "[";
  103.         tmp->data->PripsatUrok();
  104.         cout << tmp->data->AktualniStav()<<"]"<<endl;
  105.     }
  106. }
  107.  
  108. void List::Vklad()
  109. {
  110.     int x =rand()%1000+1;
  111.     Node *tmp;
  112.    
  113.     for(tmp=head; tmp!=NULL; tmp=tmp->next)
  114.     {
  115.         cout << "V" <<tmp->data->Cislo() <<"[" << tmp->data->AktualniStav() <<"]" << "[" <<x << "]" << endl;
  116.         tmp->data->Vlozit(x);
  117.     }
  118. }
  119.  
  120. void List::Vyber()
  121. {
  122.     int x =rand()%1000+1;
  123.     Node *tmp;
  124.    
  125.     for(tmp=head; tmp!=NULL; tmp=tmp->next)
  126.     {
  127.         cout << "X" <<tmp->data->Cislo() <<"[" << tmp->data->AktualniStav() <<"]" << "[" <<x << "]" << endl;
  128.         tmp->data->Vybrat(x);
  129.     }
  130. }
  131.  
  132. void List::Report()
  133. {
  134.         if (isEmpty())
  135.         {
  136.                 cout << "list is empty" << endl;
  137.         }
  138.         else
  139.         {
  140.                 cout << "---HEAD---" << endl;
  141.                 Node *tmp;
  142.                 for(tmp=head; tmp!=NULL; tmp=tmp->next)
  143.                 {
  144.                     cout << tmp->data->AktualniStav() << endl;
  145.                 }
  146.                 cout << endl;
  147.         }
  148. }
  149.  
  150. Ucet::Ucet()
  151. {
  152.     m_Cash=rand()%M+1000;
  153.     m_CisloUctu=m_Count+1;
  154.     m_Urok=rand()%P+1;
  155.     m_Count++;
  156.     cout << "G" <<m_CisloUctu<<"["<< m_Urok << "]" << "[" << m_Cash << "]" << endl;
  157. }
  158.  
  159. void Ucet::PripsatUrok()
  160. {
  161.     int x;
  162.     x=m_Cash/100*m_Urok;
  163.     m_Cash+=x;
  164. }
  165.  
  166. int Ucet::Urok()
  167. {
  168.     return m_Urok;
  169. }
  170.  
  171. int Ucet::Cislo()
  172. {
  173.     return m_CisloUctu;
  174. }
  175.  
  176. void Ucet::Vlozit(int x)
  177. {
  178.     m_Cash+=x;
  179. }
  180.  
  181. void Ucet::Vybrat(int x)
  182. {
  183.     if (x<m_Cash)
  184.     {
  185.         m_Cash-=x;
  186.     }
  187.     else
  188.     {
  189.         cout << "nelze vybrat vice, nez je na uctu" << endl;
  190.     }
  191. }
  192.  
  193. int Ucet::AktualniStav()
  194. {
  195.     return m_Cash;
  196. }
  197.  
  198. Ucet::~Ucet()
  199. {
  200.  
  201. }
  202.  
  203.  
  204. int main()
  205. {
  206.     Ucet *p[N];//vytvorime pole N uctu
  207.     for(int i=0;i<N;i++)
  208.     {
  209.         p[i]=new Ucet(); //priradime jim hodnoty
  210.     }
  211.  
  212.     cout << endl;
  213.  
  214.     List L;//vytvorime seznam
  215.  
  216.     for(int i =0;i<N;i++)
  217.     {
  218.         L.Insert(p[i]);//vlozime postupne data z pole uctu do seznamu
  219.     }
  220.  
  221.     int x=Y*12;
  222.    
  223.     while(x>=0)
  224.     {
  225.         if(x%2==1)
  226.         {
  227.             L.Vklad();
  228.         }
  229.         else
  230.         {
  231.             L.Vyber();
  232.         }
  233.         if (x%12==0)
  234.         {
  235.             L.Uroceni();
  236.             cout << " -------- year --------" << endl;
  237.         }
  238.         x--;
  239.     }
  240.    
  241.     for(int i=0;i<N;i++)
  242.     {
  243.         delete p[i]; //smazeme vsechny dynamicky vytvorene ucty
  244.     }
  245.  
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement