Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #ifndef QUICKIO_H
  2. #define QUICKIO_H
  3.  
  4. #include <conio.h>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <cstdlib>
  8.  
  9. class QuickIO
  10. {
  11. bool echo, extended;
  12. public:
  13. QuickIO(){echo = false;}
  14. void Echo() {echo = true;}
  15. void DontEcho() {echo = false;}
  16. bool isEchoing(){return echo;}
  17. void get(){if(echo) getche(); else getch();}
  18. QuickIO& operator >> (int& var);
  19. QuickIO& operator >> (char& var);
  20. };
  21.  
  22. QuickIO& QuickIO::operator >> (int& var)
  23. {
  24. do
  25. if(echo)
  26. var = getche();
  27. else
  28. var = getch();
  29. while(var==0||var==224);
  30. return *this;
  31. }
  32.  
  33. QuickIO& QuickIO::operator >> (char& var)
  34. {
  35. int p;
  36. do
  37. if(echo)
  38. p = getche();
  39. else
  40. p = getch();
  41. while(p==0||p==224);
  42. var = (char) p;
  43. return *this;
  44. }
  45.  
  46. QuickIO qin;
  47.  
  48. class Stopwatch
  49. {
  50. clock_t proctime, procelapsed;
  51. bool running;
  52. public:
  53. Stopwatch(); //called automatically when a stopwatch object is created
  54. void start(); //starts the stopwatch
  55. float stop(); //stops the watch and returns the time
  56. void reset(); //resets the stopwatch to zero
  57. float read(); //returns the current time (without stopping the watch)
  58. };
  59.  
  60.  
  61. //implementation code...
  62.  
  63. Stopwatch::Stopwatch()
  64. {
  65. procelapsed = 0;
  66. running = false;
  67. }
  68.  
  69. void Stopwatch::start()
  70. {
  71. proctime = clock();
  72. running = true;
  73. }
  74.  
  75. float Stopwatch::stop()
  76. {
  77. running = false;
  78. clock_t oldproc = proctime;
  79. proctime = clock();
  80. procelapsed = proctime - oldproc;
  81. return float(procelapsed)/CLOCKS_PER_SEC;
  82. }
  83.  
  84. void Stopwatch::reset()
  85. {
  86. if(!running)
  87. procelapsed = 0;
  88. }
  89.  
  90. float Stopwatch::read()
  91. {
  92. if(running)
  93. return float(clock() - proctime + procelapsed)/CLOCKS_PER_SEC;
  94. else
  95. return float(procelapsed)/CLOCKS_PER_SEC;
  96. }
  97.  
  98. enum colour {Black, Blue, Green, Aqua, Red, Purple, Yellow, White};
  99.  
  100. class ColourController
  101. {
  102. HANDLE hout;
  103. int foreground, background;
  104. void setConsole(){SetConsoleTextAttribute(hout, foreground + background*16);}
  105. public:
  106. ColourController(): foreground(15), background(0) {hout = GetStdHandle(STD_OUTPUT_HANDLE);}
  107. void setForeground(colour c);
  108. void setBackground(colour c);
  109. void setForeBright(bool b);
  110. void setBackBright(bool b);
  111. void invert();
  112. ~ColourController(){setForeground(White); setForeBright(true); setBackground(Black); setBackBright(false);}
  113. };
  114.  
  115. void ColourController::setForeground(colour c)
  116. {
  117. if(c < 8)
  118. foreground = c + (foreground >= 8) * 8;
  119. setConsole();
  120. }
  121.  
  122. void ColourController::setBackground(colour c)
  123. {
  124. if(c < 8)
  125. background = c + (background >= 8) * 8;
  126. setConsole();
  127. }
  128.  
  129. void ColourController::setBackBright(bool b)
  130. {
  131. if(b && background < 8)
  132. background += 8;
  133. else if(!b && background >= 8)
  134. background -= 8;
  135. setConsole();
  136. }
  137.  
  138. void ColourController::setForeBright(bool b)
  139. {
  140. if(b && foreground < 8)
  141. foreground += 8;
  142. else if(!b && foreground >= 8)
  143. foreground -= 8;
  144. setConsole();
  145. }
  146.  
  147. void ColourController::invert()
  148. {
  149. foreground = foreground ^ background;
  150. background = background ^ foreground;
  151. foreground = foreground ^ background;
  152. setConsole();
  153. }
  154.  
  155. class CursorController
  156. {
  157. HANDLE hout;
  158. CONSOLE_SCREEN_BUFFER_INFO csbi;
  159. COORD c;
  160. void setConsole(){SetConsoleCursorPosition(hout, c);}
  161. public:
  162. CursorController(){hout = GetStdHandle (STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;}
  163. void setPosition(short x, short y){c.X = x; c.Y = y; setConsole();}
  164. short getX(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.X;}
  165. short getY(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.Y;}
  166. void clearAll();
  167. };
  168.  
  169. void CursorController::clearAll()
  170. {
  171. COORD origin = {0,0};
  172. if(!GetConsoleScreenBufferInfo(hout,&csbi))return;
  173. DWORD consize = csbi.dwSize.X * csbi.dwSize.Y;
  174. DWORD charsWritten;
  175. if(!FillConsoleOutputCharacter( hout,(TCHAR) ' ', consize, origin, &charsWritten ))return;
  176. if(!FillConsoleOutputAttribute( hout, csbi.wAttributes, consize, origin, &charsWritten ))return;
  177. SetConsoleCursorPosition(hout, origin);
  178. }
  179.  
  180. class Random
  181. {
  182. int mlower, mupper;
  183. protected:
  184. int value;
  185. public:
  186. Random():mlower(0),mupper(RAND_MAX){time_t seconds;time(&seconds);srand((unsigned int) seconds);}
  187. void setLimits(double lower, double upper){mlower=lower; mupper=upper;}
  188. int get();
  189. int getLast(){return value;}
  190. };
  191.  
  192. int Random::get()
  193. {
  194. value = rand()%(mupper-mlower+1)+mlower;
  195. return value;
  196. }
  197.  
  198. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement