如何从传感器中获取值到向量的; isensor>?
生成温度,湿度和压力的值是随机的。 createroom
将创建一个文本文件,其中传感器的值在其中,updateroom
将将值添加到文件中,readroom
将在屏幕上显示文件的内容和deleteroom
将删除文件。我被困在updateroom
上,因为我不知道如何从传感器中获取vector< iroom> gt;传感器
,然后将其放入文件中。
这是我到目前为止所做的:
//ISensor.h
#ifndef ISensor_H
#define ISensor_H
class ISensor {
public:
virtual double readSensor() const = 0;
};
#endif
//Temperature.h
#ifndef Temperature_H
#define Temperatur_H
#include<iostream>
#include "ISensor.h"
class Temperature : public ISensor
{
public:
double readSensor() const {
return generateTemperature();
}
private:
double generateTemperature()const {
int temperature;
srand((int)time(NULL));
temperature = -40 + (rand() % 120);
return temperature;
}
};
#endif
//Humidity.h
#ifndef Humidity_H
#define Humidity_H
#include <iostream>
#include "ISensor.h"
class Humidity :public ISensor
{
public:
double readSensor()const {
return generateHumidity();
}
private:
double generateHumidity()const {
int humidity;
srand((int)time(NULL));
humidity = 1 + (rand() % 100);
return humidity;
}
};
#endif
//Presure.h
#ifndef Presure_H
#define Presure_H
#include <iostream>
#include "ISensor.h"
class Presure :public ISensor
{
public:
double readSensor()const {
return generatePresure();
}
private:
double generatePresure()const {
int presure;
srand((int)time(0));
presure = 300+ (rand() % 800);
return presure;
}
};
#endif
//IRoom.h
#ifndef IROOM_H
#define IROOM_H
class IRoom {
public:
virtual void readRoom() const = 0;
virtual void createRoom() const = 0;
virtual void updateRoom() const = 0;
virtual void deleteRoom() const = 0;
};
#endif // !IROOM_H
//Living.h
#ifndef LIVING_H
#define LIVING_G
#include"IRoom.h"
#include"ISensor.h"
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
class Living :public IRoom {
public:
void createRoom() const{
ofstream Myfile("Living.txt");
}
void updateRoom()const {
ofstream out;
out.open("Livinf.txt", ios::app);
}
void deleteRoom() const{
remove("Living.txt");
}
void readRoom() const{
}
private:
vector<ISensor> sensors;
};
#endif
I have to make a program using this UML diagram
to implement the monitoring system of a house.
The value of the generate Temperature, Humidity and Pressure are random. CreateRoom
will create a text file where the value of the sensors goes, updateRoom
will add the values to the file, readRoom
will show on the screen the content of the file and deleteRoom
will delete the file. I am stuck at updateRoom
because I don't know how to get the value from the sensors in to the vector<IRoom> sensor
and then to put it in the file.
This is what I have done so far:
//ISensor.h
#ifndef ISensor_H
#define ISensor_H
class ISensor {
public:
virtual double readSensor() const = 0;
};
#endif
//Temperature.h
#ifndef Temperature_H
#define Temperatur_H
#include<iostream>
#include "ISensor.h"
class Temperature : public ISensor
{
public:
double readSensor() const {
return generateTemperature();
}
private:
double generateTemperature()const {
int temperature;
srand((int)time(NULL));
temperature = -40 + (rand() % 120);
return temperature;
}
};
#endif
//Humidity.h
#ifndef Humidity_H
#define Humidity_H
#include <iostream>
#include "ISensor.h"
class Humidity :public ISensor
{
public:
double readSensor()const {
return generateHumidity();
}
private:
double generateHumidity()const {
int humidity;
srand((int)time(NULL));
humidity = 1 + (rand() % 100);
return humidity;
}
};
#endif
//Presure.h
#ifndef Presure_H
#define Presure_H
#include <iostream>
#include "ISensor.h"
class Presure :public ISensor
{
public:
double readSensor()const {
return generatePresure();
}
private:
double generatePresure()const {
int presure;
srand((int)time(0));
presure = 300+ (rand() % 800);
return presure;
}
};
#endif
//IRoom.h
#ifndef IROOM_H
#define IROOM_H
class IRoom {
public:
virtual void readRoom() const = 0;
virtual void createRoom() const = 0;
virtual void updateRoom() const = 0;
virtual void deleteRoom() const = 0;
};
#endif // !IROOM_H
//Living.h
#ifndef LIVING_H
#define LIVING_G
#include"IRoom.h"
#include"ISensor.h"
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
class Living :public IRoom {
public:
void createRoom() const{
ofstream Myfile("Living.txt");
}
void updateRoom()const {
ofstream out;
out.open("Livinf.txt", ios::app);
}
void deleteRoom() const{
remove("Living.txt");
}
void readRoom() const{
}
private:
vector<ISensor> sensors;
};
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论