CodeSnippets: Difference between revisions

From FlowerHouseWiki
 
(15 intermediate revisions by the same user not shown)
Line 3: Line 3:


== C++ ==
== C++ ==
{| class="mw-collapsible mw-collapsed wikitable"
<div class="toccolours mw-collapsible mw-collapsed" style="width:80%;">
|+ style=white-space:nowrap | Code for animated shell progress bare like: [===>  ]50%
Code for animated shell progress bare like: [===>  ]50%
|-
<div class="mw-collapsible-content">
| das
‎<syntaxhighlight lang="C++" line>
|}
#include <iostream>
#include <unistd.h> // for sleep()


{| role="presentation" class="mw-collapsible"
using namespace std;
|-
 
* Code for animated shell progress bare like: [===>   ]50%  
void DrawProgressBar(int len, double percent) {
|-
  string progress;
<syntaxhighlight lang="nginx" line>
  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>
</div>
</div>

Latest revision as of 21:53, 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;
    }
}