Advertisement
amarek

OOP 1. kolokvij (23.01.2019.)

Nov 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <fstream>
  6.  
  7. #define DG 0.0
  8. #define GG 1.0
  9.  
  10. using namespace std;
  11.  
  12. // 1. zadatak
  13. template <typename Type>
  14. double Percentage(Type arr[], int n, int a, int b) {
  15.     int count = 0;
  16.     for (int i = 0; i < n; i++) {
  17.         if (arr[i] > a && arr[i] < b) {
  18.             count++;
  19.         }
  20.     }
  21.  
  22.     return (double)count / (double)n * 100;
  23. }
  24.  
  25. // 2. zadatak
  26. class Money {
  27.     friend bool operator<(const Money&, const int);
  28.     friend bool operator>(const Money&, const int);
  29. private:
  30.     int mAmount;
  31. public:
  32.     Money() : mAmount(0) {}
  33.     Money(int amount) : mAmount(amount) {}
  34. };
  35.  
  36. bool operator<(const Money& lhs, const int rhs) {
  37.     return lhs.mAmount < rhs;
  38. }
  39.  
  40. bool operator>(const Money& lhs, const int rhs) {
  41.     return lhs.mAmount > rhs;
  42. }
  43.  
  44. int main() {
  45.     Money* monies = new Money[3];
  46.     monies[0] = Money(5);
  47.     monies[1] = Money(7);
  48.     monies[2] = Money(3);
  49.  
  50.     cout << Percentage(monies, 3, 4, 10) << "%" << endl;
  51.  
  52.     return 0;
  53. }
  54.  
  55. // 3. zadatak
  56. class Enemy {
  57.     friend class IllegalEnemyException;
  58. protected:
  59.     std::string mName;
  60.     int mHealth;
  61.     int mAttackDamage;
  62. public:
  63.     Enemy();
  64.     Enemy(std::string, int);
  65.     int getAttackDamage() const { return mAttackDamage; }
  66.     int getHealth() const { return mHealth; }
  67.     virtual void heal(int amount);
  68.     virtual void takeDamage(int amount) = 0;
  69. };
  70.  
  71. Enemy::Enemy() : mName(""), mHealth(0), mAttackDamage(0) {}
  72.  
  73. Enemy::Enemy(std::string name, int attackdamage) : mName(name), mHealth(100), mAttackDamage(attackdamage) {}
  74.  
  75. void Enemy::heal(int amount) {
  76.     if (mHealth == 0) { return; }
  77.     mHealth += amount;
  78. }
  79.  
  80. // 4. zadatak
  81. class ArmoredEnemy : protected Enemy{
  82. private:
  83.     double mHitRatio;
  84.     int mArmor;
  85. public:
  86.     ArmoredEnemy();
  87.     ArmoredEnemy(double, int, std::string, int);
  88.     virtual void takeDamage(int amount);
  89. };
  90.  
  91. ArmoredEnemy::ArmoredEnemy() : mHitRatio(0), mArmor(0), Enemy() {}
  92.  
  93. ArmoredEnemy::ArmoredEnemy(double hitratio, int armor, std::string name, int attackdamaage) : mHitRatio(hitratio), mArmor(armor), Enemy(name, attackdamaage) {}
  94.  
  95. // 5. zadatak
  96. void ArmoredEnemy::takeDamage(int amount) {
  97.     double damage = DG + (float)rand() * (GG - DG) / (float)RAND_MAX;
  98.  
  99.     if (damage < mHitRatio) {
  100.         return;
  101.     }
  102.  
  103.     for (int i = 0; i < amount; i++) {
  104.         if (mArmor != 0) {
  105.             mArmor -= 1;
  106.         }
  107.         else if (mHealth != 0) {
  108.             mHealth -= 1;
  109.         }
  110.         else {
  111.             return;
  112.         }
  113.     }
  114. }
  115.  
  116. // 6. zadatak
  117. class IllegalEnemyException : public std::runtime_error {
  118. public:
  119.     IllegalEnemyException(std::string);
  120. };
  121.  
  122. IllegalEnemyException::IllegalEnemyException(std::string message) : std::runtime_error(message) {}
  123.  
  124. // 7. zadatak
  125. // ...
  126.  
  127. // 8. zadatak
  128. // ...
  129.  
  130. // 9. zadatak - C#
  131. interface ITaxable
  132. {
  133.     int GetTaxPercentage();
  134.     double CalculateAmount();
  135.     double CalculateTaxAmount();
  136. }
  137.  
  138. // 10. zadatak - C#
  139. class Flour : ITaxable
  140. {
  141.     public int TaxPercentage { get; private set; }
  142.     public int QuantityKg { get; private set; }
  143.     public int PricePerKg { get; private set; }
  144.  
  145.     public Flour(int taxPercentage, int quantityKg, int pricePerKg)
  146.     {
  147.         TaxPercentage = taxPercentage;
  148.         QuantityKg = quantityKg;
  149.         PricePerKg = pricePerKg;
  150.     }
  151.  
  152.     public int GetTaxPercentage()
  153.     {
  154.         return this.TaxPercentage;
  155.     }
  156.  
  157.     public double CalculateAmount()
  158.     {
  159.         return (this.QuantityKg * this.PricePerKg) + (this.QuantityKg * this.PricePerKg * (this.TaxPercentage / 100));
  160.     }
  161.  
  162.     public double CalculateTaxAmount()
  163.     {
  164.         return (this.QuantityKg * this.PricePerKg * (this.TaxPercentage / 100));
  165.     }
  166. }
  167.  
  168. // 11. zadatak - C#
  169. // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement