Tickt wie eine echte Uhr ;)
Michael Planner
main.cpp@0:0717eb51cbef, 2020-04-21 (annotated)
- Committer:
- Aagrus
- Date:
- Tue Apr 21 19:40:00 2020 +0000
- Revision:
- 0:0717eb51cbef
Uhr Programm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Aagrus | 0:0717eb51cbef | 1 | /****************************************************************************** |
Aagrus | 0:0717eb51cbef | 2 | |
Aagrus | 0:0717eb51cbef | 3 | Welcome to GDB Online. |
Aagrus | 0:0717eb51cbef | 4 | GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, |
Aagrus | 0:0717eb51cbef | 5 | C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS |
Aagrus | 0:0717eb51cbef | 6 | Code, Compile, Run and Debug online from anywhere in world. |
Aagrus | 0:0717eb51cbef | 7 | |
Aagrus | 0:0717eb51cbef | 8 | *******************************************************************************/ |
Aagrus | 0:0717eb51cbef | 9 | #include <stdio.h> |
Aagrus | 0:0717eb51cbef | 10 | #include <ctime> |
Aagrus | 0:0717eb51cbef | 11 | //#include <cstdio> |
Aagrus | 0:0717eb51cbef | 12 | |
Aagrus | 0:0717eb51cbef | 13 | //--------------------- Timer -----------------------// |
Aagrus | 0:0717eb51cbef | 14 | class Time |
Aagrus | 0:0717eb51cbef | 15 | { |
Aagrus | 0:0717eb51cbef | 16 | protected: |
Aagrus | 0:0717eb51cbef | 17 | char s = 0; |
Aagrus | 0:0717eb51cbef | 18 | char m = 0; |
Aagrus | 0:0717eb51cbef | 19 | char h = 0; |
Aagrus | 0:0717eb51cbef | 20 | |
Aagrus | 0:0717eb51cbef | 21 | public: |
Aagrus | 0:0717eb51cbef | 22 | void SetTime(char hour, char min, char sec); |
Aagrus | 0:0717eb51cbef | 23 | virtual char AktualHour(); |
Aagrus | 0:0717eb51cbef | 24 | virtual char AktualMin(); |
Aagrus | 0:0717eb51cbef | 25 | virtual char AktualSec(); |
Aagrus | 0:0717eb51cbef | 26 | }; |
Aagrus | 0:0717eb51cbef | 27 | void Time::SetTime(char hour, char min, char sec) |
Aagrus | 0:0717eb51cbef | 28 | { |
Aagrus | 0:0717eb51cbef | 29 | s = sec; |
Aagrus | 0:0717eb51cbef | 30 | m = min; |
Aagrus | 0:0717eb51cbef | 31 | h = hour; |
Aagrus | 0:0717eb51cbef | 32 | } |
Aagrus | 0:0717eb51cbef | 33 | char Time::AktualSec() { return s; } |
Aagrus | 0:0717eb51cbef | 34 | char Time::AktualMin() { return m; } |
Aagrus | 0:0717eb51cbef | 35 | char Time::AktualHour() { return h; } |
Aagrus | 0:0717eb51cbef | 36 | |
Aagrus | 0:0717eb51cbef | 37 | |
Aagrus | 0:0717eb51cbef | 38 | |
Aagrus | 0:0717eb51cbef | 39 | //--------------------- Uhr -----------------------// |
Aagrus | 0:0717eb51cbef | 40 | class Uhr : public Time |
Aagrus | 0:0717eb51cbef | 41 | { |
Aagrus | 0:0717eb51cbef | 42 | private: |
Aagrus | 0:0717eb51cbef | 43 | // Variabeln für Uhr |
Aagrus | 0:0717eb51cbef | 44 | clock_t tick; |
Aagrus | 0:0717eb51cbef | 45 | clock_t startTick; |
Aagrus | 0:0717eb51cbef | 46 | char sh, sm, ss; |
Aagrus | 0:0717eb51cbef | 47 | // Methoden für Uhr |
Aagrus | 0:0717eb51cbef | 48 | void SetTick(); |
Aagrus | 0:0717eb51cbef | 49 | int GetTickDiff(); |
Aagrus | 0:0717eb51cbef | 50 | void SetNewTime(); |
Aagrus | 0:0717eb51cbef | 51 | |
Aagrus | 0:0717eb51cbef | 52 | unsigned long TimeInSec(); |
Aagrus | 0:0717eb51cbef | 53 | |
Aagrus | 0:0717eb51cbef | 54 | public: |
Aagrus | 0:0717eb51cbef | 55 | // Konstruktoren |
Aagrus | 0:0717eb51cbef | 56 | Uhr(Time time); |
Aagrus | 0:0717eb51cbef | 57 | Uhr(char sec, char min, char hour); |
Aagrus | 0:0717eb51cbef | 58 | // Sonstige Variabeln |
Aagrus | 0:0717eb51cbef | 59 | unsigned long TimeSec(); |
Aagrus | 0:0717eb51cbef | 60 | void SetTime(char hour, char min, char sec); |
Aagrus | 0:0717eb51cbef | 61 | char AktualHour(); |
Aagrus | 0:0717eb51cbef | 62 | char AktualMin(); |
Aagrus | 0:0717eb51cbef | 63 | char AktualSec(); |
Aagrus | 0:0717eb51cbef | 64 | }; |
Aagrus | 0:0717eb51cbef | 65 | // private: |
Aagrus | 0:0717eb51cbef | 66 | void Uhr::SetTick() { startTick = clock(); } |
Aagrus | 0:0717eb51cbef | 67 | int Uhr::GetTickDiff() |
Aagrus | 0:0717eb51cbef | 68 | { |
Aagrus | 0:0717eb51cbef | 69 | // Differenz der Startzeit bis jetzt berrechnen |
Aagrus | 0:0717eb51cbef | 70 | tick = clock() - startTick; |
Aagrus | 0:0717eb51cbef | 71 | int sec = (int)(tick/CLOCKS_PER_SEC); |
Aagrus | 0:0717eb51cbef | 72 | return sec; |
Aagrus | 0:0717eb51cbef | 73 | } |
Aagrus | 0:0717eb51cbef | 74 | void Uhr::SetNewTime() |
Aagrus | 0:0717eb51cbef | 75 | { |
Aagrus | 0:0717eb51cbef | 76 | // Holen der Zeitdifferenz und setze vom Startzeitbezug eine neue Zeit |
Aagrus | 0:0717eb51cbef | 77 | int sec = this->GetTickDiff(); |
Aagrus | 0:0717eb51cbef | 78 | unsigned long timeInSec = ((sh*3600) + (sm*60) + ss) + sec; |
Aagrus | 0:0717eb51cbef | 79 | h = (char)(timeInSec / 3600); |
Aagrus | 0:0717eb51cbef | 80 | m = (char)((timeInSec - (h * 3600)) / 60); |
Aagrus | 0:0717eb51cbef | 81 | s = (char)(timeInSec % 60); |
Aagrus | 0:0717eb51cbef | 82 | } |
Aagrus | 0:0717eb51cbef | 83 | unsigned long Uhr::TimeInSec() |
Aagrus | 0:0717eb51cbef | 84 | { |
Aagrus | 0:0717eb51cbef | 85 | unsigned long sec = (h * 3600) + (m * 60) + s; |
Aagrus | 0:0717eb51cbef | 86 | return sec; |
Aagrus | 0:0717eb51cbef | 87 | } |
Aagrus | 0:0717eb51cbef | 88 | |
Aagrus | 0:0717eb51cbef | 89 | |
Aagrus | 0:0717eb51cbef | 90 | // public: |
Aagrus | 0:0717eb51cbef | 91 | Uhr::Uhr(Time time) : Time() |
Aagrus | 0:0717eb51cbef | 92 | { |
Aagrus | 0:0717eb51cbef | 93 | // relative Zeitvariabeln |
Aagrus | 0:0717eb51cbef | 94 | s = time.AktualSec(); |
Aagrus | 0:0717eb51cbef | 95 | m = time.AktualMin(); |
Aagrus | 0:0717eb51cbef | 96 | h = time.AktualHour(); |
Aagrus | 0:0717eb51cbef | 97 | |
Aagrus | 0:0717eb51cbef | 98 | // Festgelegte Startzeit |
Aagrus | 0:0717eb51cbef | 99 | sh = h; |
Aagrus | 0:0717eb51cbef | 100 | sm = m; |
Aagrus | 0:0717eb51cbef | 101 | ss = s; |
Aagrus | 0:0717eb51cbef | 102 | |
Aagrus | 0:0717eb51cbef | 103 | // Zeitablauf starten |
Aagrus | 0:0717eb51cbef | 104 | this->SetTick(); |
Aagrus | 0:0717eb51cbef | 105 | } |
Aagrus | 0:0717eb51cbef | 106 | Uhr::Uhr(char hour, char min, char sec) : Time() |
Aagrus | 0:0717eb51cbef | 107 | { |
Aagrus | 0:0717eb51cbef | 108 | // relative Zeitvariabeln |
Aagrus | 0:0717eb51cbef | 109 | s = sec; |
Aagrus | 0:0717eb51cbef | 110 | m = min; |
Aagrus | 0:0717eb51cbef | 111 | h = hour; |
Aagrus | 0:0717eb51cbef | 112 | |
Aagrus | 0:0717eb51cbef | 113 | // Festgelegte Startzeit |
Aagrus | 0:0717eb51cbef | 114 | sh = h; |
Aagrus | 0:0717eb51cbef | 115 | sm = m; |
Aagrus | 0:0717eb51cbef | 116 | ss = s; |
Aagrus | 0:0717eb51cbef | 117 | |
Aagrus | 0:0717eb51cbef | 118 | // Zeitablauf starten |
Aagrus | 0:0717eb51cbef | 119 | this->SetTick(); |
Aagrus | 0:0717eb51cbef | 120 | } |
Aagrus | 0:0717eb51cbef | 121 | |
Aagrus | 0:0717eb51cbef | 122 | unsigned long Uhr::TimeSec() |
Aagrus | 0:0717eb51cbef | 123 | { |
Aagrus | 0:0717eb51cbef | 124 | // Setzt Zeit und aktuallisiert |
Aagrus | 0:0717eb51cbef | 125 | this->SetNewTime(); |
Aagrus | 0:0717eb51cbef | 126 | this->TimeInSec(); |
Aagrus | 0:0717eb51cbef | 127 | } |
Aagrus | 0:0717eb51cbef | 128 | char Uhr::AktualSec() { this->SetNewTime(); Time::AktualSec(); } |
Aagrus | 0:0717eb51cbef | 129 | char Uhr::AktualMin() { this->SetNewTime(); Time::AktualMin(); } |
Aagrus | 0:0717eb51cbef | 130 | char Uhr::AktualHour() { this->SetNewTime(); Time::AktualHour(); } |
Aagrus | 0:0717eb51cbef | 131 | |
Aagrus | 0:0717eb51cbef | 132 | |
Aagrus | 0:0717eb51cbef | 133 | |
Aagrus | 0:0717eb51cbef | 134 | |
Aagrus | 0:0717eb51cbef | 135 | |
Aagrus | 0:0717eb51cbef | 136 | |
Aagrus | 0:0717eb51cbef | 137 | int main() |
Aagrus | 0:0717eb51cbef | 138 | { |
Aagrus | 0:0717eb51cbef | 139 | // Uhr setzen |
Aagrus | 0:0717eb51cbef | 140 | Uhr uhr(20, 30, 0); |
Aagrus | 0:0717eb51cbef | 141 | |
Aagrus | 0:0717eb51cbef | 142 | //oder |
Aagrus | 0:0717eb51cbef | 143 | Time t; |
Aagrus | 0:0717eb51cbef | 144 | t.SetTime(10, 2, 30); |
Aagrus | 0:0717eb51cbef | 145 | uhr = t; |
Aagrus | 0:0717eb51cbef | 146 | |
Aagrus | 0:0717eb51cbef | 147 | // Ausgabe der Parameter |
Aagrus | 0:0717eb51cbef | 148 | printf("Zeit in Sec: %lu\n", uhr.TimeSec()); |
Aagrus | 0:0717eb51cbef | 149 | printf("Anzahl an Stunden: %d\n", uhr.AktualHour()); |
Aagrus | 0:0717eb51cbef | 150 | printf("Anzahl an Minuten: %d\n", uhr.AktualMin()); |
Aagrus | 0:0717eb51cbef | 151 | printf("Anzahl an Sekunden: %d\n", uhr.AktualSec()); |
Aagrus | 0:0717eb51cbef | 152 | |
Aagrus | 0:0717eb51cbef | 153 | |
Aagrus | 0:0717eb51cbef | 154 | |
Aagrus | 0:0717eb51cbef | 155 | |
Aagrus | 0:0717eb51cbef | 156 | |
Aagrus | 0:0717eb51cbef | 157 | // Uhrausgabe in Sekundentakt [Zeitmäßig leider nicht genau Sekunden] |
Aagrus | 0:0717eb51cbef | 158 | unsigned long x = uhr.TimeSec(); |
Aagrus | 0:0717eb51cbef | 159 | while (1) |
Aagrus | 0:0717eb51cbef | 160 | { |
Aagrus | 0:0717eb51cbef | 161 | while(x == uhr.TimeSec()){} |
Aagrus | 0:0717eb51cbef | 162 | |
Aagrus | 0:0717eb51cbef | 163 | printf("Zeit in Sec: %lu\n", uhr.TimeSec()); |
Aagrus | 0:0717eb51cbef | 164 | x = uhr.TimeSec(); |
Aagrus | 0:0717eb51cbef | 165 | } |
Aagrus | 0:0717eb51cbef | 166 | |
Aagrus | 0:0717eb51cbef | 167 | |
Aagrus | 0:0717eb51cbef | 168 | return 0; |
Aagrus | 0:0717eb51cbef | 169 | } |