chasnasestra

Акции

Mar 20th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. // vasiot kod za klasite ovde
  6.  
  7. class StockRecord {
  8.  
  9. private:
  10. char id[12];
  11. char ime[50];
  12. float kupenaCena;
  13. float momentalnaCena;
  14. int brAkcii;
  15.  
  16. public:
  17. StockRecord(const char *id="", const char *ime="", float momentalnaCena=0, int brAkcii=0){
  18. strcpy(this->id, id);
  19. strcpy(this->ime, ime);
  20. this->momentalnaCena = momentalnaCena;
  21. this->brAkcii = brAkcii;
  22. }
  23.  
  24. void setNewPrice(double c){
  25. this->kupenaCena = c;
  26. }
  27.  
  28. double value(){
  29. return brAkcii*kupenaCena;
  30. }
  31.  
  32. double profit(){
  33. return brAkcii*(momentalnaCena - kupenaCena);
  34. }
  35.  
  36. friend ostream &operator<< (ostream &o, const StockRecord &rhs){
  37. o << rhs.ime << " " << rhs.brAkcii << " " << rhs.momentalnaCena << " " << rhs.kupenaCena << " " <<
  38. rhs.brAkcii*(rhs.momentalnaCena - rhs.kupenaCena)*-1 << endl; return o;
  39. }
  40. };
  41.  
  42. class Client{
  43.  
  44. private:
  45. char imePrezime[60];
  46. int id;
  47. StockRecord *sr;
  48. int brAkcii;
  49.  
  50. public:
  51. Client(const char *imePrezime="", int id=0){
  52. strcpy(this->imePrezime, imePrezime);
  53. brAkcii = 0;
  54. this->id = id;
  55. }
  56.  
  57. ~Client(){ delete [] sr; }
  58.  
  59. double totalValue(){
  60. double sum=0;
  61. for(int i=0; i<brAkcii; i++){
  62. sum+=sr[i].value();
  63. }
  64. return sum;
  65. }
  66.  
  67. Client& operator+=(const StockRecord &c) {
  68. StockRecord *temp = new StockRecord[brAkcii+1];
  69. for(int i=0; i<brAkcii; i++)
  70. temp[i]=sr[i];
  71. temp[brAkcii]=c;
  72. sr = temp;
  73. brAkcii++;
  74. return *this;
  75. }
  76.  
  77. friend ostream &operator<< (ostream &o, Client &rhs){
  78. o << rhs.id << " " << rhs.totalValue() << endl;
  79. for(int i=0; i<rhs.brAkcii; i++){
  80. cout << rhs.sr[i];
  81. }
  82. return o;
  83. }
  84.  
  85. };
  86.  
  87. // ne menuvaj vo main-ot
  88.  
  89. int main(){
  90.  
  91. int test;
  92. cin >> test;
  93.  
  94. if(test == 1){
  95. double price;
  96. cout << "=====TEST NA KLASATA StockRecord=====" << endl;
  97. StockRecord sr("1", "Microsoft", 60.0, 100);
  98. cout << "Konstruktor OK" << endl;
  99. cin >> price;
  100. sr.setNewPrice(price);
  101. cout << "SET metoda OK" << endl;
  102. }
  103. else if(test == 2){
  104. cout << "=====TEST NA METODITE I OPERATOR << OD KLASATA StockRecord=====" << endl;
  105. char id[12], company[50];
  106. double price, newPrice;
  107. int n, shares;
  108. cin >> n;
  109. for(int i = 0; i < n; ++i){
  110. cin >> id;
  111. cin >> company;
  112. cin >> price;
  113. cin >> newPrice;
  114. cin >> shares;
  115. StockRecord sr(id, company, price, shares);
  116. sr.setNewPrice(newPrice);
  117. cout << sr.value() << endl;
  118. cout << sr;
  119. }
  120. }
  121. else if(test == 3){
  122. cout << "=====TEST NA KLASATA Client=====" << endl;
  123. char companyID[12], companyName[50], clientName[50];
  124. int clientID, n, shares;
  125. double oldPrice, newPrice;
  126. bool flag = true;
  127. cin >> clientName;
  128. cin >> clientID;
  129. cin >> n;
  130. Client c(clientName, clientID);
  131. cout << "Konstruktor OK" << endl;
  132. for(int i = 0; i < n; ++i){
  133. cin >> companyID;
  134. cin >> companyName;
  135. cin >> oldPrice;
  136. cin >> newPrice;
  137. cin >> shares;
  138. StockRecord sr(companyID, companyName, oldPrice, shares);
  139. sr.setNewPrice(newPrice);
  140. c += sr;
  141. if(flag){
  142. cout << "Operator += OK" << endl;
  143. flag = false;
  144. }
  145. }
  146. cout << c;
  147. cout << "Operator << OK" << endl;
  148. }
  149. return 0;
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment