1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#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
|