Stoycho_KK

втора задача от второ домашно КН

Jan 24th, 2021 (edited)
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include<thread>
  3. #include<chrono>
  4.  
  5. const char _LEFT_SYMB = '<';
  6. const char _RIGHT_SYMB = '>';
  7. const char _DEF_FILL = '#';
  8. const char _DEF_EMPTY = '-';
  9. const int _MAX_LEN = 1026;
  10. const int _LENGHT = 100;
  11.  
  12. void initBar(char* bar, int len, char left, char right, char fill) {
  13.     bar[0] = left;
  14.     bar[len] = right;
  15.     for (int i = 0; i < len - 1; i++)
  16.         bar[i] = fill;
  17. }
  18.  
  19. void printBar(char* bar, int len, int speed, char left, char right, bool perc = false) {
  20.     std::cout << left;
  21.     int info = 0;
  22.     auto start = std::chrono::steady_clock::now();
  23.     for (int i = 1; i < len - 1; i++) {
  24.         std::cout << bar[i];
  25.         std::this_thread::sleep_for(std::chrono::milliseconds(speed));
  26.         if (bar[i] == _DEF_FILL && bar[i + 1] == _DEF_EMPTY) {
  27.             if (perc) {
  28.                 int res = ceill((float)(i) / (float)(len - 1) * 100);
  29.                 std::cout << res << "%";
  30.             }
  31.             auto end = std::chrono::steady_clock::now();
  32.             info = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
  33.         }
  34.     }
  35.     std::cout << right;
  36.     std::cout << "\nExecution time: " << info << " second(s)";
  37. }
  38.  
  39. void fillBar(char* bar, int border, char fill) {
  40.     for (int i = 1; i < border+1; i++)
  41.         bar[i] = fill;
  42. }
  43.  
  44. void drawBar(char* bar, float currentProgress,int speed, bool perc = false, int length = 100, char leftSymb = _LEFT_SYMB, char rightSymb = _RIGHT_SYMB, char fillSymb = _DEF_FILL, char emptySymb = _DEF_EMPTY) {
  45.     if (currentProgress > 1 || currentProgress < 0)
  46.         std::cout << "Can't display more than 100%";
  47.     else if (length > 1024)
  48.         std::cout << "Can't display such bar.";
  49.     else {
  50.         length += 2;
  51.         initBar(bar, length, leftSymb, rightSymb, emptySymb);
  52.  
  53.         float borderF = (length - 2) * currentProgress;
  54.  
  55.         int border = borderF;
  56.  
  57.         fillBar(bar, border, fillSymb);
  58.  
  59.         printBar(bar, length, speed, leftSymb, rightSymb, perc);
  60.     }
  61. }
  62.  
  63. void validate(int& number) {
  64.     bool errorMessage = false;
  65.  
  66.     do {
  67.         if (errorMessage)
  68.             std::cout << "Bad input. Please try again.";
  69.  
  70.         std::cin >> number;
  71.  
  72.         bool validateCharInput = !((bool)(std::cin));
  73.         if (validateCharInput) {
  74.             std::cout << "No symbols allowed\n";
  75.             std::cin.clear();
  76.             std::cin.ignore(255, '\n');
  77.         }
  78.  
  79.         errorMessage = true;
  80.     } while (number <= 0);
  81. }
  82.  
  83. int main() {
  84.     int seconds = 0;
  85.     char bar[_MAX_LEN];
  86.  
  87.     std::cout << "Enter seconds: ";
  88.     validate(seconds);
  89.     const float percentage = 0.74;
  90.     float speed = 10*((float)seconds / percentage);
  91.     //speed *= 10;
  92.     drawBar(bar, percentage, (int)speed, true, _LENGHT);
  93. }
Add Comment
Please, Sign In to add comment