Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef QUICKIO_H
- #define QUICKIO_H
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- #include <cstdlib>
- class QuickIO
- {
- bool echo, extended;
- public:
- QuickIO(){echo = false;}
- void Echo() {echo = true;}
- void DontEcho() {echo = false;}
- bool isEchoing(){return echo;}
- void get(){if(echo) getche(); else getch();}
- QuickIO& operator >> (int& var);
- QuickIO& operator >> (char& var);
- };
- QuickIO& QuickIO::operator >> (int& var)
- {
- do
- if(echo)
- var = getche();
- else
- var = getch();
- while(var==0||var==224);
- return *this;
- }
- QuickIO& QuickIO::operator >> (char& var)
- {
- int p;
- do
- if(echo)
- p = getche();
- else
- p = getch();
- while(p==0||p==224);
- var = (char) p;
- return *this;
- }
- QuickIO qin;
- class Stopwatch
- {
- clock_t proctime, procelapsed;
- bool running;
- public:
- Stopwatch(); //called automatically when a stopwatch object is created
- void start(); //starts the stopwatch
- float stop(); //stops the watch and returns the time
- void reset(); //resets the stopwatch to zero
- float read(); //returns the current time (without stopping the watch)
- };
- //implementation code...
- Stopwatch::Stopwatch()
- {
- procelapsed = 0;
- running = false;
- }
- void Stopwatch::start()
- {
- proctime = clock();
- running = true;
- }
- float Stopwatch::stop()
- {
- running = false;
- clock_t oldproc = proctime;
- proctime = clock();
- procelapsed = proctime - oldproc;
- return float(procelapsed)/CLOCKS_PER_SEC;
- }
- void Stopwatch::reset()
- {
- if(!running)
- procelapsed = 0;
- }
- float Stopwatch::read()
- {
- if(running)
- return float(clock() - proctime + procelapsed)/CLOCKS_PER_SEC;
- else
- return float(procelapsed)/CLOCKS_PER_SEC;
- }
- enum colour {Black, Blue, Green, Aqua, Red, Purple, Yellow, White};
- class ColourController
- {
- HANDLE hout;
- int foreground, background;
- void setConsole(){SetConsoleTextAttribute(hout, foreground + background*16);}
- public:
- ColourController(): foreground(15), background(0) {hout = GetStdHandle(STD_OUTPUT_HANDLE);}
- void setForeground(colour c);
- void setBackground(colour c);
- void setForeBright(bool b);
- void setBackBright(bool b);
- void invert();
- ~ColourController(){setForeground(White); setForeBright(true); setBackground(Black); setBackBright(false);}
- };
- void ColourController::setForeground(colour c)
- {
- if(c < 8)
- foreground = c + (foreground >= 8) * 8;
- setConsole();
- }
- void ColourController::setBackground(colour c)
- {
- if(c < 8)
- background = c + (background >= 8) * 8;
- setConsole();
- }
- void ColourController::setBackBright(bool b)
- {
- if(b && background < 8)
- background += 8;
- else if(!b && background >= 8)
- background -= 8;
- setConsole();
- }
- void ColourController::setForeBright(bool b)
- {
- if(b && foreground < 8)
- foreground += 8;
- else if(!b && foreground >= 8)
- foreground -= 8;
- setConsole();
- }
- void ColourController::invert()
- {
- foreground = foreground ^ background;
- background = background ^ foreground;
- foreground = foreground ^ background;
- setConsole();
- }
- class CursorController
- {
- HANDLE hout;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- COORD c;
- void setConsole(){SetConsoleCursorPosition(hout, c);}
- public:
- CursorController(){hout = GetStdHandle (STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;}
- void setPosition(short x, short y){c.X = x; c.Y = y; setConsole();}
- short getX(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.X;}
- short getY(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.Y;}
- void clearAll();
- };
- void CursorController::clearAll()
- {
- COORD origin = {0,0};
- if(!GetConsoleScreenBufferInfo(hout,&csbi))return;
- DWORD consize = csbi.dwSize.X * csbi.dwSize.Y;
- DWORD charsWritten;
- if(!FillConsoleOutputCharacter( hout,(TCHAR) ' ', consize, origin, &charsWritten ))return;
- if(!FillConsoleOutputAttribute( hout, csbi.wAttributes, consize, origin, &charsWritten ))return;
- SetConsoleCursorPosition(hout, origin);
- }
- class Random
- {
- int mlower, mupper;
- protected:
- int value;
- public:
- Random():mlower(0),mupper(RAND_MAX){time_t seconds;time(&seconds);srand((unsigned int) seconds);}
- void setLimits(double lower, double upper){mlower=lower; mupper=upper;}
- int get();
- int getLast(){return value;}
- };
- int Random::get()
- {
- value = rand()%(mupper-mlower+1)+mlower;
- return value;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement