Advertisement
newvol

student code

Dec 29th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <cstring>  
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Student {  
  9. private:  
  10.     char name[51]{};
  11.     char surname[51]{};
  12.     int oldhp;
  13.     int oldenergy;
  14.     int oldiq;
  15.     int oldsanity;
  16.     int oldexcitement;
  17.     void change_field(int& field, int delta)
  18.     {
  19.         field += delta;
  20.         if (field > 100) {
  21.             field = 100;
  22.         }
  23.         if (field < 0) {
  24.             field = 0;
  25.         }
  26.     }
  27.     void change_params(int hp, int energy, int iq, int sanity, int excitement) {
  28.         change_field(oldhp, hp);
  29.         change_field(oldenergy, energy);
  30.         change_field(oldiq, iq);
  31.         change_field(oldsanity, sanity);
  32.         change_field(oldexcitement, excitement);
  33.     }
  34. public:
  35.     Student() {
  36.         cin >> name >> surname;
  37.         oldhp = 100;
  38.         oldenergy = 100;
  39.         oldiq = 20;
  40.         oldsanity = 100;
  41.         oldexcitement = 50;
  42.     }
  43.     Student(char* name, char *surname) {
  44.         oldhp = 100;
  45.         oldenergy = 100;
  46.         oldiq = 20;
  47.         oldsanity = 100;
  48.         oldexcitement = 50;
  49.     }
  50.  
  51.     bool isAlive() {  
  52.         if (oldhp > 0) return true;
  53.         else return false;
  54.     }
  55.     void show() {  
  56.         cout << name << " " << surname << ": HP = " << setw(3) << setfill('0') << hp
  57.             << ", Energy = " << setw(3) << setfill('0') << energy
  58.             << ", IQ = " << setw(3) << setfill('0') << iq
  59.             << ", Sanity = " << setw(3) << setfill('0') << sanity
  60.             << ", Excitement = " << setw(3) << setfill('0') << excitement << ".";
  61.         if (!is_alive()) {
  62.             cout << " Game over.";
  63.         }
  64.         cout << endl;
  65.     }
  66.  
  67.     void eat() {
  68.         if (!isAlive()) return;
  69.         else change_params(1, 7, -1, 0, -2);
  70.     }
  71.  
  72.     void wait() {
  73.         if (!isAlive()) return;
  74.         else change_params(1, -3, 0, 0, -3);
  75.     }
  76.  
  77.     void study() {
  78.         if (!isAlive()) return;
  79.         else change_params(-2, -4, 5, -5, -2);
  80.     }
  81.  
  82.     void sleep() {
  83.         if (!isAlive()) return;
  84.         else change_params(2,-2, 0, 7, 0);
  85.     }
  86.  
  87.     void watchTV() {
  88.         if (!isAlive()) return;
  89.         else change_params(-2, -3, -3, 1, 5);
  90.     }
  91. };
  92.  
  93.  
  94. int main() {
  95.     Student student;
  96.    
  97.     int cmdCount;
  98.     cin >> cmdCount;
  99.     string cmd;
  100.     for (int i = 0; i < cmdCount; i++) {
  101.         cin >> cmd;
  102.         if (cmd == "Eat") student.eat();
  103.         else if (cmd == "Wait") student.wait();
  104.         else if (cmd == "Study") student.study();
  105.         else if (cmd == "Sleep") student.sleep();
  106.         else if (cmd == "Watch TV") student.watchTV();
  107.         student.show();
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement