Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <exception>
- #include <new>
- #include <iostream>
- #include <fstream>
- #include <cstdint>
- #include <cinttypes>
- #include <cstdlib>
- #include <cstring>
- #include <cmath>
- #include <chrono>
- #include <thread>
- #include <mutex>
- using namespace std;
- #include "ltoa.h"
- class Indicator {
- private:
- mutable mutex editor;
- bool running, killed, started, clearit, clearondeath;
- thread* threader;
- long complete, total;
- unsigned speed;
- char* buf;
- uint8_t prev_length;
- const char* motion = "|[/-\\]+[/-\\]|[/+\\]+[/+\\]";
- uint8_t place/* 0-23 */;
- void clear_buf() {
- for (uint8_t i = 0; i < 71; i++)
- buf[i] = '\0';
- }
- void clear_out() {
- for (uint8_t i = 0; i < prev_length; i++) putchar(' ');
- putchar('\r');
- fflush(stdout);
- prev_length = 0;
- clearit = false;
- }
- #define mlock lock_guard<mutex> abcd(this->editor)
- void run() {
- chrono::milliseconds speedv;
- long percentage;
- uint8_t string_place;
- char *buf2 = buf + 2, *bufs;
- //
- while (true) {
- {
- mlock;
- if (clearit) clear_out();
- /** 2 {spin}" " + 20 percent number + 3 "% (" + 20 complete + 3 " / " + 20 total + 3 ").\0*/
- if (running) {
- clear_buf();
- //
- if (place >= 24) place = 0;
- buf[0] = motion[place++];
- buf[1] = ' ';
- //
- percentage = ((double) complete * 100.0d) / total;
- ltoa(percentage, buf2, 10);
- string_place = 2 + (uint8_t) strlen(buf2);
- //
- buf[string_place++] = '%';
- buf[string_place++] = ' ';
- buf[string_place++] = '(';
- //
- bufs = buf + string_place;
- ltoa(complete, bufs, 10);
- string_place += (uint8_t) strlen(bufs);
- //
- buf[string_place++] = ' ';
- buf[string_place++] = '/';
- buf[string_place++] = ' ';
- //
- bufs = buf + string_place;
- ltoa(total, bufs, 10);
- string_place += (uint8_t) strlen(bufs);
- //
- buf[string_place++] = ')';
- buf[string_place++] = '.';
- buf[string_place] = '\0'; // length
- //
- printf("%s", buf);
- if (prev_length > string_place) {
- string_place = prev_length - string_place;
- for (uint8_t i = 0; i < string_place; i++)
- putchar(' ');
- }
- putchar('\r');
- fflush(stdout);
- prev_length = (uint8_t) strlen(buf);
- }
- if (killed) break;
- speedv = chrono::milliseconds(speed);
- }
- this_thread::sleep_for(speedv);
- }
- mlock;
- if (clearondeath) clear_out();
- }
- public:
- Indicator():
- running(false),
- killed(false),
- started(false),
- clearit(false),
- clearondeath(true),
- complete(0l),
- total(1l),
- speed(500u),
- prev_length(0),
- place(0) {
- threader = nullptr;
- buf = nullptr;
- //
- buf = new (nothrow) char[71] /* 2 {spin}" " + 20 percent number + 3 "% (" + 20 complete + 3 " / " + 20 total + 3 ").\0*/;
- threader = new (nothrow) thread(&Indicator::run, this);
- if (buf) clear_buf();
- }
- ~Indicator() {
- if (threader) {
- stop();
- {
- mlock;
- killed = true;
- }
- threader->join();
- delete threader;
- }
- delete[] buf;
- }
- bool check_construction() const { return threader && buf; }
- bool hide() {
- mlock;
- bool r = clearit;
- clear_out();
- return r;
- }
- void start() { mlock; running = true; started = true; }
- void stop() { mlock; running = false; started = false; clearit = true; }
- void resume() { mlock; running = true; }
- void pause() { mlock; running = false; }
- bool is_paused() { mlock; return !running; }
- bool is_started() { mlock; return started; }
- long get_complete() const { mlock; return complete; }
- long get_total() const { mlock; return total; }
- unsigned get_speed() const { mlock; return speed; }
- bool get_cod() const { mlock; return clearondeath; }
- void set_complete(long x) { mlock; if (x >= 0) complete = x; }
- void make_completion(long x = 1l) { mlock; if (x > 0) complete += x; }
- void set_total(long x) { mlock; if (x > 0) total = x; }
- void set_speed(unsigned x) { mlock; if (x > 0) speed = x; }
- void set_cod(bool cod) { mlock; clearondeath = cod; }
- #undef mlock
- static Indicator* new_indicator() {
- Indicator* tmp = new (nothrow) Indicator();
- if (tmp && tmp->check_construction()) return tmp;
- delete tmp;
- return nullptr;
- }
- };
- uint8_t bitsof(long x) {
- uint8_t bits = 0;
- while (x) {
- bits++;
- x >>= 1;
- }
- return bits;
- }
- long fsqrt(long x) {
- long rvalue = 0;
- long nthbit = 1 << (bitsof(x) / 2);
- long tmp, tmpd;
- while (nthbit) {
- tmpd = rvalue + nthbit;
- tmp = x / tmpd;
- if (tmpd > tmp) {
- nthbit >>= 1;
- continue;
- }
- rvalue = tmpd;
- }
- return rvalue;
- }
- int main() {
- long input;
- do {
- input = 0;
- printf("Input a long int. above 0: ");
- fflush(stdout);
- scanf("%ld", &input);
- fflush(stdin);
- } while(input <= 0);
- Indicator* spinner = Indicator::new_indicator();
- ofstream log("log.txt");
- if (spinner) {
- if (log.is_open()) {
- long* squares = new (nothrow) long[input];
- if (squares) {
- for (long i = 1; i <= input; i++)
- squares[i - 1] = i * i;
- bool error;
- long errors = 0, rmin, rmax, sqrtv;
- const long up = input - 1;
- const long last_square = squares[up];
- spinner->set_total(last_square);
- spinner->set_speed(2000);
- spinner->set_cod(false);
- spinner->start();
- for (long i = 0; i < up;) {
- rmin = squares[i++];
- rmax = squares[i];
- for (long j = rmin; j < rmax; j++) {
- sqrtv = fsqrt(j);
- error = sqrtv != i;
- if (error) errors++;
- log << "\\|(" << j << ") = " << sqrtv << " (" << rmin << ")?\n";
- spinner->make_completion();
- }
- spinner->set_speed(300u + (1500.0d * i) / last_square);
- }
- spinner->stop();
- spinner->hide();
- if (errors) cerr << "Errors: " << errors << endl;
- else cout << "No errors!" << endl;
- delete[] squares;
- } else cerr << "Memory (squares) error" << endl;
- log.close();
- } else cerr << "File error" << endl;
- delete spinner;
- } else cerr << "The spinner can't spin" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement