kellex

C++ Clock

Jul 18th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <ctime>
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <functional>
  6. #include <windows.h>
  7.  
  8. using namespace std;
  9.  
  10. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  11. COORD CursorPosition;
  12.  
  13. void Draw(int style, int col, int row, int length,int amount, bool fill, int sw );
  14. void gotoXY(int x, int y );
  15. void gotoXY(int x, int y, string text);
  16. void setcursor(bool visible, DWORD size);
  17.  
  18. int main()
  19. {
  20.     setcursor(0,0);
  21.     int hours, minutes, seconds, t;
  22.     struct tm * timeinfo;
  23.     bool Military_Time = false;
  24.  
  25.     string AM_PM;
  26.  
  27.     string Segment[11][10]={
  28.         {" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
  29.         {"\x1E     \x1E","      \x1E","      \x1E","      \x1E","\x1E     \x1E","\x1E      ","\x1E      ","      \x1E","\x1E     \x1E","\x1E     \x1E"},
  30.         {"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
  31.         {"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
  32.         {"\x1F     \x1F","      \x1F","      \x1F","      \x1F","\x1F     \x1F","\x1F      ","\x1F      ","      \x1F","\x1F     \x1F","\x1F     \x1F"},
  33.         {"       ","       ", " \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
  34.         {"\x1E     \x1E","      \x1E","\x1E      ","      \x1E","      \x1E","      \x1E","\x1E     \x1E","      \x1E","\x1E     \x1E","      \x1E"},
  35.         {"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
  36.         {"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
  37.         {"\x1F     \x1F","      \x1F","\x1F      ","      \x1F","      \x1F","      \x1F","\x1F     \x1F","      \x1F","\x1F     \x1F","      \x1F"},
  38.         {" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "}
  39.     };
  40.     string Colon[11] = {"       ","       ","       ","   \xFE   ","       ","       ","       ","   \xFE   ","       ","       ","       "};
  41.     Draw(2,2,5,76,15,0,2);
  42.     gotoXY(26,11,"Pressing the \"M\" key");
  43.     gotoXY(26,13,"toggles Military Time");
  44.     Sleep(2000);
  45.     Draw(1,4,6,72,13,1,0);
  46.    
  47.     gotoXY(73,8,"HR");
  48.     do
  49.     {
  50.         time_t rawtime;
  51.         time ( &rawtime );
  52.         timeinfo = localtime( &rawtime );
  53.         hours = timeinfo->tm_hour;
  54.         seconds = timeinfo->tm_sec;
  55.         minutes = timeinfo->tm_min;
  56.  
  57.         if (_kbhit())
  58.       {
  59.          char ch = _getch();
  60.          ch = toupper(ch);
  61.          if (ch == 'M')  
  62.          {
  63.            Military_Time = !Military_Time;
  64.            ch = ' ';
  65.          }
  66.       }
  67.         if( Military_Time)
  68.         {
  69.             AM_PM = "  ";
  70.             gotoXY(73,7,"24");
  71.         }
  72.         else
  73.         {
  74.         AM_PM = "AM";
  75.         gotoXY(73,7,"12");
  76.         if ( hours >= 12)
  77.             AM_PM = "PM";
  78.         if ( hours == 0 )
  79.             hours = 12;
  80.         if ( hours > 12)
  81.             hours -=12;
  82.         }
  83.  
  84.         for(t=0;t<11;t++)
  85.         {
  86.             gotoXY(6,7+t);
  87.             cout  << Segment[t][hours/10] << "   " << Segment[t][hours%10] << Colon[t] << Segment[t][minutes/10] << "   " << Segment[t][minutes%10] << Colon[t] << Segment[t][seconds/10] << "   " << Segment[t][seconds%10];
  88.         }
  89.        
  90.         gotoXY(73,17, AM_PM);
  91.         Sleep(1000);
  92.     } while (true);
  93. }
  94.  
  95. void Draw(int style, int col, int row, int length,int amount, bool fill, int sw )
  96. {
  97.     // Draws a 1 or 2 line box
  98.     int a;
  99.     if ( sw >4)
  100.         sw = 4;
  101.     style=(style-1)*6;
  102.     char box[24];
  103.     char shdw[5];
  104.  
  105.     box[0] = '\xDA';
  106.     box[1] = '\xBF';
  107.     box[2] = '\xC0';
  108.     box[3] = '\xD9';
  109.     box[4] = '\xB3';
  110.     box[5] = '\xC4';
  111.     box[6] = '\xC9';
  112.     box[7] = '\xBB';
  113.     box[8] = '\xC8';
  114.     box[9] = '\xBC';
  115.     box[10] = '\xBA';
  116.     box[11] = '\xCD';
  117.     box[12] = '\xD5';//  +
  118.     box[13] = '\xB8';//  +
  119.     box[14] = '\xD4';//  +
  120.     box[15] = '\xBE';//  +
  121.     box[16] = '\xB3';//  ¦
  122.     box[17] = '\xCD';//  -
  123.     box[18] = '\xD6';//  +
  124.     box[19] = '\xB7';//  +
  125.     box[20] = '\xD3';//  +
  126.     box[21] = '\xBD';//  +
  127.     box[22] = '\xBA';// ¦
  128.     box[23] = '\xC4';// -
  129.     shdw[1] = '\xB0';// ¦
  130.     shdw[2] = '\xB1';// ¦
  131.     shdw[3] = '\xB2';// ¦
  132.     shdw[4] = '\xDB';// ¦
  133.     char tl,tr,bl,br,side,edge,shadow;
  134.     tl = box[style];
  135.     tr = box[style+1];
  136.     bl = box[style+2];
  137.     br = box[style+3];
  138.     side = box[style+4];
  139.     edge = box[style+5];
  140.     shadow = shdw[sw];
  141.     string Line(length-2,edge);
  142.     string Shadow(length,shadow);
  143.     string Fill(length-2, ' ');
  144.     gotoXY(col,row);
  145.     cout << tl << Line << tr;
  146.     for (a = 1; a <amount-1;a++)
  147.     {
  148.         gotoXY(col,row+a);
  149.         cout << side;
  150.         if  (fill)
  151.             cout << Fill;
  152.         else       
  153.             gotoXY(col+length-1,row+a);
  154.         cout << side;
  155.         if (sw)
  156.             cout << shadow;
  157.     }
  158.     gotoXY(col,(amount+row)-1);
  159.     cout << bl << Line << br;
  160.     if (sw)
  161.     {
  162.         cout << shadow;
  163.         gotoXY(col+1,row+amount , Shadow );
  164.     }
  165. }
  166.  
  167. void gotoXY(int x, int y)
  168. {
  169.     CursorPosition.X = x;
  170.     CursorPosition.Y = y;
  171.     SetConsoleCursorPosition(console,CursorPosition);
  172. }
  173.  
  174. void gotoXY(int x, int y, string text)
  175. {
  176.  
  177.     CursorPosition.X = x;
  178.     CursorPosition.Y = y;
  179.     SetConsoleCursorPosition(console,CursorPosition);
  180.     cout << text;
  181. }
  182.  
  183. void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
  184. {
  185.     if(size == 0)
  186.     {
  187.         size = 20;  // default cursor size
  188.     }
  189.     CONSOLE_CURSOR_INFO lpCursor;  
  190.     lpCursor.bVisible = visible;
  191.     lpCursor.dwSize = size;
  192.     SetConsoleCursorInfo(console,&lpCursor);
  193. }
Advertisement
Add Comment
Please, Sign In to add comment