如何从传感器中获取值到向量的; isensor>?

发布于 2025-01-31 19:09:20 字数 2632 浏览 1 评论 0原文

我必须使用此UML图制作程序 实施房屋的监视系统。

生成温度,湿度和压力的值是随机的。 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
enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文