Task C++ Difficult help please

Hello I have a little Problem with this task .
Some I have already implemented in the code:
I will post the task with 2 post because the task ist too long


c)In the class CTimeOfDay, use "overloaded by method" to complete the int operator- (CTimeOfDay rhs), which can be used to determine the number of seconds elapsed between two times.

Implemented this task in the code ?Not sure if its right

d) This task is very difficult for me :Implement the switchedOffAt method and the switchedOff method. Note that using methods that have already been implemented can make the task easier to solve. (In particular, for example, the switchedOff method should simply call the switchedOffAt method with the current time.)
offTime: the off time as seconds since midnight.
* If the value is less than the switch-on time, will
* assumed that the dimmer closest
* Day was turned off (for example, means one
* On time 22:00:00 off at 01:00:00
* that the dimmer was switched on for 3 hours).


My codes:

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

/*
 * CDimmerLogEntry.h
 */

#ifndef CDIMMERLOGENTRY_H_
#define CDIMMERLOGENTRY_H_

#include <iostream>

/**
 * Diese Klasse modelliert ein Einschalten des Dimmers zu einem
 * gegebenen Zeitpunkt, f�r eine bestimmte Dauer und mit einem
 * bestimmten Dimm-Faktor (ein Wert zwischen 0 (ausschlie�lich)
 * und 1 (einschlie�lich)).
 *
 * Um Speicherplatz zu sparen, werden der Einschaltzeitpunkt und
 * die Einschaltdauer mit Attributen vom Typ unsigned short
 * modelliert. Da deren Wertebereich nicht ganz ausreicht, um
 * alle Sekunden eines Tages zu rep�sentieren (24*60*60 > 2^16),
 * werden die Sekundenangaben f�r die Speicherung durch 2 geteilt
 * (und beim Auslesen der Werte wieder mit 2 multipliziert).
 * Die daduch entstehende Ungenauigkeit bei den Zeitangaben
 * ist im Rahmen der vorgesehenen Anwendungsf�lle akzeptabel.
 *
 * Auch der Dimm-Faktor soll effizient gespeichert werden. Der
 * als float �bergebene Wert zwischen 0 und 1 wird daher
 * f�r die Speicherung in einen unsigned short gewandelt.
 * Nutzen Sie den zur Verf�gung stehenden Wertebereich
 * vollst�ndig aus, d.h. speichern Sie den Faktor mit
 * m�glichst hoher Genauigkeit.
 */
class CDimmerLogEntry {
private:
	unsigned short m_startTime;
	unsigned short m_duration;
	unsigned short m_factor;

	/**
	 * Setzt das Attribut m_factor nach entsprechender
	 * Umrechnung des �bergebenen Wertes. Die Methode
	 * wertet auch die Zusicherung (s.u.) aus.
	 */
	void setFactor(float factor);

public:
	/**
	 * Erzeugt einen neuen Log-Eintrag mit der angegebenen
	 * Einschaltzeit, dem angegebenen Dimm-Faktor und einer
	 * Einschaltdauer von 0.
	 *
	 * startTime: die Einschaltzeit als Sekunden seit Mitternacht.
	 *            Der Wert muss kleiner sein als die Anzahl
	 *            Sekunden eines Tages. Ist das nicht der Fall,
	 *            wird statt dessen der Maximalwert (sp�test
	 *            m�glicher Einschaltzeitpunkt an einem Tag)
	 *            genommen.
	 *
	 * factor:    der Dimm-Faktor. Der Wert muss gr��er 0 und
	 *            kleiner gleich 1 sein. Verletzt der angegebene
	 *            Wert diese Bedingung, wird der Wert 1 genommen.
	 */
	CDimmerLogEntry(unsigned int startTime, float factor = 0.5);

	/**
	 * Erzeugt einen neuen Log-Eintrag mit der aktuellen Zeit
	 * als Einschaltzeit, dem angegebenen Dimm-Faktor und einer
	 * Einschaltdauer von 0.
	 *
	 * factor:    der Dimm-Faktor. Der Wert muss gr��er 0 und
	 *            kleiner gleich 1 sein. Verletzt der angegebene
	 *            Wert diese Bedingung, wird der Wert 1 genommen.
	 */
	CDimmerLogEntry(float factor = 0.5);

	/**
	 * Legt die Ausschaltzeit des Dimmers und damit die
	 * Einschaltdauer fest.
	 *
	 * offTime:   die Ausschaltzeit als Sekunden seit Mitternacht.
	 *            Ist der Wert kleiner als die Einschaltzeit, wird
	 *            davon ausgegangen, dass der Dimmer am n�chsten
	 *            Tag ausgeschaltet wurde (z.B. bedeutet bei einer
	 *            Einschaltzeit 22:00:00 ein Ausschalten um 01:00:00
	 *            dass der Dimmer 3 Stunden angeschaltet war).
	 */
	void switchedOffAt(unsigned int offTime);

	/**
	 * Legt die aktuelle Zeit als Ausschaltzeitpunkt fest.
	 */
	void switchedOff();

	/**
	 * Liefert den Einschaltzeitpunkt.
	 */
	unsigned int getStartTime() const;

	/**
	 * Liefert die Einschaltdauer.
	 */
	unsigned int getDuration() const;

	/**
	 * Liefert den Dimm-Faktor.
	 */
	float getFactor() const;

};

#endif /* CDIMMERLOGENTRY_H_ */


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
#include <time.h>
using namespace std;

void CDimmerLogEntry::setFactor(float factor){

	m_factor = factor;
}

CDimmerLogEntry::CDimmerLogEntry(unsigned int startTime, float factor ){

	if(m_factor >= 0 && m_factor<= 1){

		setFactor(factor);
	}
	else{

		setFactor(1);
	}

	if(startTime > 86400) {
		//dann ist schlecht
		m_startTime = 86400;
	} else {
		m_startTime = startTime/2;
	}
}

unsigned int CDimmerLogEntry::getStartTime() const {
	return m_startTime*2;
}

unsigned int CDimmerLogEntry::getDuration() const {
	return m_duration*2;
}

float CDimmerLogEntry::getFactor() const {
	return m_factor;
}



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
/*
 * CTimeStamp.h
 */

#ifndef CTIMEOFDAY_H_
#define CTIMEOFDAY_H_

#include <string>
#include<cmath>

class CTimeOfDay {
private:
	unsigned int m_secsSinceMidnight;

	/**
	 * Hilfsmethode zum Konvertieren eines Ganzzahl-Wertes
	 * in eine Zeichenkette.
	 */
	std::string intToString(unsigned int value);

public:
	/**
	 * Erzeugt eine neue Zeitangabe mit der aktuellen Uhrzeit.
	 */
	CTimeOfDay();

	/**
	 * Erzeugt eine neue Zeitangabe aus der Angabe der
	 * seit Mitternacht vergangenen Sekunden.
	 */
	CTimeOfDay(unsigned int secsSinceMidnight);

	/**
	 * Erzeugt eine neue Zeitangabe aus der Angabe einer
	 * Uhrzeit im 24-Stunden Format ("hh:mm:ss", mit hh=Stunden,
	 * mm=Minuten und ss=Sekunden).
	 */
	CTimeOfDay(std::string timeOfDay);

	/**
	 * Liefert den durch diese Zeitangabe repr�sentierten
	 * Zeitpunkt als Sekunden seit Mitternacht.
	 */
	long getSecsSinceMidgnight();

	/**
	 * Liefert den durch diese Zeitangabe repr�sentierten
	 * Zeitpunkt im 24-Stunden Format ("hh:mm:ss", mit hh=Stunden,
	 * mm=Minuten und ss=Sekunden).
	 */
	std::string asTime();
	int operator-(CTimeOfDay rhs);
};

#endif /* CTIMEOFDAY_H_ */



cpp
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
/*
 * CTimeStamp.cpp
 */

#include "CTimeOfDay.h"

#include <stdio.h>
#include <cstring>
#include <time.h>
using namespace std;

CTimeOfDay::CTimeOfDay() {
	  time_t rawtime;
	  struct tm * timeinfo;

	  time (&rawtime);
	  timeinfo = localtime (&rawtime);
	  m_secsSinceMidnight = timeinfo->tm_hour * 3600
			  + timeinfo->tm_min * 60 + timeinfo->tm_sec;
}

CTimeOfDay::CTimeOfDay(unsigned int secsSinceMidnight) {
	m_secsSinceMidnight = secsSinceMidnight;
}

CTimeOfDay::CTimeOfDay(std::string timeOfDay) {
	unsigned int hours;
	unsigned int minutes;
	unsigned int seconds;
	sscanf(timeOfDay.c_str(), "%02u:%02u:%02u", &hours, &minutes, &seconds);
	m_secsSinceMidnight = hours * 3600 + minutes * 60 + seconds;
}

std::string CTimeOfDay::intToString(unsigned int value) {
	string result = "";
	do {
		result = (char)('0' + value % 10) + result;
		value /= 10;
	} while (value > 0);
	return result;
}

long CTimeOfDay::getSecsSinceMidgnight() {
	return m_secsSinceMidnight;
}

std::string CTimeOfDay::asTime() {
	unsigned int hours = m_secsSinceMidnight/3600;
		unsigned int minutes = m_secsSinceMidnight/60%60 ;
		unsigned int seconds = m_secsSinceMidnight%60;
	// Ersetzen Sie die nachfolgende Anweisung!

		string xhours;
		string xminutes;
		string xseconds;
		if(hours < 10) {
		xhours = "0"+intToString(hours);
		}
		else {
		xhours = intToString(hours);
		}
		if(minutes<10){
		 xminutes = "0"+intToString(minutes);
		}
		else{
			xminutes = intToString(minutes);

		}
		if(seconds< 10){
		 xseconds = "0"+intToString(seconds) ;
		}
		else{
			xseconds = intToString(seconds);
		}


		//string gesamt = "0"+intToString(hours)+intToString(minutes)+intToString(seconds);
		string gesamt = xhours+":"+xminutes+":"+xseconds;
		return gesamt;
}

int CTimeOfDay::operator-(CTimeOfDay rhs){
	unsigned int zeit;

	if(m_secsSinceMidnight < 0){

		zeit = m_secsSinceMidnight*-1;


	}
	zeit = m_secsSinceMidnight - rhs.m_secsSinceMidnight;


}




What should I do in the switchedoffAt Methode?
I dont really understand.

I tried a litte:
1
2
3
4
5
6
void switchedOffAt(unsigned int offTime){
if(m_secsSinceMidnight < m_startTime){
..... i dont know
}
offTime = m_duration -m_startTime;
}


Is that the code they want?

I hope somebody can help me
A consumer is connected to the dimmer. The consumer can be switched on and off with the dimmer. If the consumer is switched on, it works with a power according to the dimming factor set on the dimmer. For example, a connected 100W bulb will actually operate at 100W with a dimming factor of 1.0, but with a dimming factor of 0.75 but only 75W. Each time it is switched on, a new entry is created in the log, in which the switch-on time and the set dimming factor are noted. When the dimmer is switched off, the switch-off time (or the switch-on time) is added to the log entry.
An important piece of information in a log is the time. An efficient way to represent the time is to store the past seconds since midnight in an integer. Unfortunately, this form of time can be interpreted by humans bad. Therefore you will find a prepared class CTimeOfDay in the project. It has an attribute of type unsigned int, which stores the seconds since midnight. The class represents u. a. a constructor that provides a time representation in 24-hour format ("hh: mm: ss", with hh = hours, mm = minutes and ss = seconds) into an object of type CTimeOfDay (and thus in a number of seconds since Midnight) can be converted. Furthermore, the asTime () method can be used to convert the integer value to the usual 24-hour format. For more information, see the class definition comments from CTimeOfDay.


Here the task which is written at the beginnig.

Hope somebody can help me
maybe write a main() and test it, implementing pieces as you need them.

It's unclear what some of these pieces are supposed to do. For example, why are some of these getter methods multiplying private variables by 2?
1
2
3
4
5
6
7
8
9
10
11
unsigned int CDimmerLogEntry::getStartTime() const {
	return m_startTime*2;
}

unsigned int CDimmerLogEntry::getDuration() const {
	return m_duration*2;
}

float CDimmerLogEntry::getFactor() const {
	return m_factor;
}


Where is the stringified version of CDimmerLogEntry , or maybe a Write() method so that one can actually write a log?

Anyway, in switchedOffAt(unsigned int offTime) , I think they just want you to compute m_duration. The start time is already known, as per the two constructors (either set by user or now), though I'm confused why its type is 'unsigned short' rather than a CTimeOfDay.

Feels like you were given base code, but this isn't it, and some of the stuff you added might've been wrong.
a) I have already implemented the task was:
So you can understand my code better
Unfortunately, in the asTime () method, the 24-hour format conversion is not yet implemented (currently the number of seconds since midnight is returned as a string). Correct the method to return a string representing the stored time in 24-hour format. Note that for the hour, minute and second, a leading "0" must be added if the value is less than 10.

This will explain the getter method:
his class models turning the dimmer on
* given time, for a certain duration and with one
* certain dimming factor (a value between 0 (exclusive)
* and 1 (including)).
*
* To save storage space, the on time and
* the duty cycle with attributes of type unsigned short
* modeled. Since their value range is not quite sufficient to
* Repeat every second of every day (24 * 60 * 60> 2 ^ 16),
* the seconds for storage are divided by 2
* (and multiplied by 2 when reading the values).
* The resulting inaccuracy in the timing
* is acceptable within the intended use cases.
*
* The dimming factor should also be stored efficiently. Of the
* value passed between 0 and 1 as a float therefore becomes
* converted to unsigned short for storage.
* Use the available value range
* completely off, i. Save the factor with
* highest possible accuracy.

Ok to my Question:

Anyway, in switchedOffAt(unsigned int offTime) , I think they just want you to compute m_duration. The start time is already known, as per the two constructors (either set by user or now), though I'm confused why its type is 'unsigned short' rather than a CTimeOfDay.

What should I do with duration exactly?





As I said, write an actual test to test the usage of the class.

Lex33 wrote:
Each time it is switched on, a new entry is created in the log, in which the switch-on time and the set dimming factor are noted. When the dimmer is switched off, the switch-off time (or the switch-on time) is added to the log entry.

Perhaps another method needed, especially if you're just reusing the same log entry object
- SwitchOnAt() // with a CTimeOfDay or unsigned int as parameter
- SwitchOn()


Perhaps each of the constructors should have an extra parameter -- a reference to an output stream:
1
2
CDimmerLogEntry(unsigned int startTime, float factor = 0.5, std::ostream& os = std::cout);
CDimmerLogEntry(float factor = 0.5, std::ostream& os = std::cout);


where a private var like output_ gets assigned to the stream. Then you could actually log an entry (to the screen by default, but could be to an output string-stream, an output file stream, etc)
Last edited on
std::ostream& os = std::cout

Why are you doing this in the constructor?
why not?
I didnt learn it at school .
That s why I dont know.

Should I not do this?
void switchedOffAt(unsigned int offTime){
if(m_secsSinceMidnight < m_startTime){
..... i dont know
}
offTime = m_duration -m_startTime;
}
I didnt learn it at school .
That s why I dont know.

I explained what it does in sentence above and below the posted code snippet suggestion. Assignment in method arguments indicates default parameter(s). Hence my words
to the screen by default


Should I not do this?
void switchedOffAt(unsigned int offTime){
if(m_secsSinceMidnight < m_startTime){
..... i dont know
}
offTime = m_duration -m_startTime;
}


you can't access private variable CTimeOfDay::m_secsSinceMidnight inside CDimmerLogEntry::switchedOffAt() method.

Please write a test class main.cpp or similar, with a main(). I'm sure much of your confusion will be cleared up once you start using the class.
Last edited on
main
// GIT-Labor
// main.h

////////////////////////////////////////////////////////////////////////////////
// Header-Dateien
#include <iostream> // Header f�r die Standard-IO-Objekte (z.B. cout, cin)
#include <stdlib.h>
// F�gen Sie hier weitere ben�tigte Header-Dateien der
// Standard-Bibliothek ein z.B.
// #include <string>
#include"CTimeOfDay.h"
using namespace std; // Erspart den scope vor Objekte der
// C++-Standard-Bibliothek zu schreiben
// z.B. statt "std::cout" kann man "cout" schreiben

// Inkludieren Sie hier die Header-Files Ihrer Klassen, z.B.
// #include "CFraction.h"


// Hauptprogramm
// Dient als Testrahmen, von hier aus werden die Klassen aufgerufen
int main (void)
{
// TODO: Tragen Sie hier Ihren Namen, die Matrikelnummer und die Rechnernummer ein
cout << "Name: ..., Matrikelnummer: ..., Rechnernummer: ..." << endl << endl;

// Aufgabe 1:
CTimeOfDay time1("8:30:1");

cout << time1.asTime() << endl;







// Aufgabe 2:



// Zus�tzliche Eintr�ge f�r Aufgabe 2f)
/*
CDimmerLogEntry entry2(39920, 0.5);
entry2.switchedOffAt(40800);
log += entry2;
CDimmerLogEntry entry3(43800, 0.3);
entry3.switchedOffAt(60700);
log += entry3;
*/

return 0;
}


But I have still no idea how I should implement the switch off Method sorry
But how should I implement the switch off Method ?

Thats my Problem
I did implement the SwitchOff() method at the time of my post, which is trickier because it involves converting from the ctime objects. Just now I updated the link (same link) to make a conversion to TimeOfDay and use that time subtraction method (moved calculations to there). I also commented out the DEBUG statements.

Unless you were asking about SwitchOffAt(TimeOfDay stop_time) method -- this one is much simpler. I implemented it just now using the ideas from the other method and updated main() to demo it. Added constness to some of the methods.

As I was saying before, everything becomes much less confusing (at least for me), once you begin exercising the class and visualizing the log entries in std::cout or so.
Last edited on
I would use a date/time class like KDate to simply take care of all the time calculations and time wrapping across days as is being accomplish by your CTimeOfDay class.

Checkout it's documentation: http://www.shankodev.com/Libs/KLib/items/KDate/KDate_Page_20.htm
I have become one tipp of the teacher ,which might would help to implement the Switch off Method :


For each object that represents an entry (copy of the cash register
"CLogEntry") there is a method with the off time in the
Entry can be set ("switchedOffAt"). The method is
described in detail:

/ **
* Sets the switch-off time of the dimmer and thus the
* Duty cycle fixed.
*
* offTime: the off time as seconds since midnight.
* If the value is less than the switch-on time, will
* assumed that the dimmer closest
* Day was turned off (for example, means one
* On time 22:00:00 off at 01:00:00
* that the dimmer was switched on for 3 hours).
* /
void switchedOffAt (unsigned int offTime);


*/
void switchedOff();

That the switch-off time is not a value transferred in the parameter
used but the current time. The method could as well
"switchedOffNow ()", but that would in my opinion the
Make designation unnecessarily long. The method saves the user
the class "CLogEntry" to first have to determine the current time
and then call "switchedOffAt" with this determined time.

The implementation of "switchedOff ()" is obvious: you
call "switchedOffAt" with the current time as an argument



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CDimmerLogEntry::switchedOff()
{
CTimeofDay();
CDimmerLogEntry::switchedoffAt();
}
Bei der 2 Methode :
void CDimmerLogEntry::switchedOffAt(unsigned int offTime)
{
if(m_startTime < offTime){
m_duration = offTime- m_startTime;
m_duration = m_duration/24;
}
if(m_startTime >offTime){
m_duration = -1*(-offTime +m_startTime)+3600;
m_duration = m_duration/24;
}
Of(m_startTime == offTime){
cout << m_startTime << endl;
}


Is this right?
Last edited on
Some Ideas Guys ?
Topic archived. No new replies allowed.