CodeSnippets: Difference between revisions

From FlowerHouseWiki
Line 7: Line 7:
<div class="mw-collapsible-content">
<div class="mw-collapsible-content">
<syntaxhighlight lang="C++" line>
<syntaxhighlight lang="C++" line>
dad
#include <iostream>
#include <unistd.h> // for sleep()
 
using namespace std;
 
void DrawProgressBar(int len, double percent) {
  string progress;
  for (int i = 0; i < (len-1); ++i) {
    if (i < static_cast<int>(len * percent))
    {
      progress += "=";
    }
    else if((i == static_cast<int>(len * percent)))
    {
        progress += ">";
    }
    else
    {
      progress += " ";
    }
  }
  cout << "\r[" << progress << "] " << (static_cast<int>(100 * percent)) << "%" << std::flush;
}
 
int main()
{
    double i = 0.01;
    for (;;) {
        sleep(1);
        DrawProgressBar(100, 0.99);
        i += 0.05;
    }
}
 
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>
</div>
<div class="toccolours mw-collapsible" style="width:auto; overflow:auto;">
<div style="font-weight:bold;line-height:1.6;">Code for animated shell progress bare like: [===>  ]50%</div>
<div class="mw-collapsible-content">
This text is collapsible. {{Lorem}}
</div></div>
<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
This text is collapsible. {{Lorem}}
</div>
</div>

Revision as of 21:52, 17 December 2021

C

C++

Code for animated shell progress bare like: [===> ]50%

#include <iostream>
#include <unistd.h> // for sleep()

using namespace std;

void DrawProgressBar(int len, double percent) {
  string progress;
  for (int i = 0; i < (len-1); ++i) {
    if (i < static_cast<int>(len * percent))
    {
      progress += "=";
    }
    else if((i == static_cast<int>(len * percent)))
    {
        progress += ">";
    }
    else
    {
      progress += " ";
    }
  }
  cout << "\r[" << progress << "] " << (static_cast<int>(100 * percent)) << "%" << std::flush;
}

int main()
{
    double i = 0.01;
    for (;;) {
        sleep(1);
        DrawProgressBar(100, 0.99);
        i += 0.05;
    }
}