vendredi 10 septembre 2021

I need advice on how to write a printing function c++

so i made this program , it's made of 2 classes (the third one is just for date ) ,all it does is , creating a detector wich has x cord , y cord , an id, and it's related to a list of surveys. the surveys re made of , a date (wich i created with a class) ,and a double that means cO2. now the program works fine but there is a thing idk how to show , in output my program just show the adding function where it says " id numb x is adding this survey to his list : date :12-1... co2: xx " now i want to show in output all the survey that are related to a single detector but i have no idead on how to achive this(was thinking about iterate the list but than how can i add the character info to the numbers? ) . my idead is " Detector 1 : Survey Date:12-xx-xx co2:xx.xx _____ Survey: Date:13--xx-xx co2:xx.x______ Detector 2: Survey Date :14.-xx-xx co2: xx;

this for all the survey that i store into a list attached to a detector

Here is the code :


//DICHIARAZIONE CLASSE DATA
#ifndef DATA_H
#define DATA_H
#include <array>
#include <sstream>
#include <string>
#include <stdexcept>

class Data{
    friend Data operator+(int, const Data&);
    friend Data operator+(const Data&, int);
    public:
        Data(int d=1, int m=1, int y=1900);
        ~Data() = default;
        Data(const Data&);
        Data& operator=(const Data&);
        Data& operator+=(unsigned int);
        void setData(int, int, int);
        static bool annoBisestile(int);
        bool fineMese(int) const;
        int getGiorno() const;
        int getMese() const;
        int getAnno() const;
        std::string toString() const;
    private:
        unsigned int day;
        unsigned int month;
        unsigned int year;
        static const std::array<int, 13> days;
};
bool operator<(const Data&, const Data&);
bool operator==(const Data&, const Data&);

//DEFINIZIONE FUNZIONI MEMBRO DELLA CLASSE DATA

const std::array<int, 13> Data::days{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Data operator+(int numdays, const Data& dd){
    Data newdate = dd;
    int temp = dd.day + numdays;
    while(temp > newdate.days[newdate.month]){
        temp = temp - newdate.days[newdate.month];
        newdate.month++;
        if(newdate.month > 12){
            newdate.year++;
            newdate.month -= 12;
        }
    }
    newdate.day = temp;
    return newdate;
}

Data operator+(const Data& dd, int numdays){
    Data newdate = dd;
    int temp = dd.day + numdays;
    while(temp > newdate.days[newdate.month]){
        temp = temp - newdate.days[newdate.month];
        newdate.month++;
        if(newdate.month > 12){
            newdate.year++;
            newdate.month -= 12;
        }
    }
    newdate.day = temp;
    return newdate;
}

Data::Data(int dd, int mm, int yy){
    setData(dd, mm, yy);
}

Data::Data(const Data &d){
    setData(d.getGiorno(), d.getMese(), d.getAnno());
}

Data& Data::operator=(const Data &d){
    setData(d.getGiorno(), d.getMese(), d.getAnno());
    return *this;
}

void Data::setData(int dd, int mm, int yy){
    if(mm>=1 && mm<=12){
        month = mm;
    } else{
        throw std::invalid_argument{"Month must be 1-12"};
    }
    if(yy>=1900 && yy<=2100){
        year = yy;
    } else{
        throw std::invalid_argument{"Year must be >= 1900 and <= 2100"};
    }
    if((month==2 && annoBisestile(year) && dd>=1 && dd<=29)||(dd>=1 && dd<=days[month])){
        day = dd;
    } else{
        throw std::invalid_argument{"Days is out of range for current month and year"};
    }
}

Data& Data::operator+=(unsigned int additionalDays){
    int temp = day + additionalDays;
    while(temp > days[month]){
        temp = temp - days[month];
        month++;
        if(month > 12){
            year++;
            month -= 12;
        }
    }
    day = temp;
    return *this;
}

bool Data::annoBisestile(int testYear){
    return (testYear%400==0 || (testYear%100!=0 && testYear%4==0));
}

bool Data::fineMese(int testDay) const{
    if(month==2 && annoBisestile(year)) return testDay==29;
    else return testDay == days[month];
}

int Data::getGiorno() const{return day; }
int Data::getMese() const{return month; }
int Data::getAnno() const{return year; }

std::string Data::toString() const{
    std::ostringstream out;
    out <<getGiorno() <<"-" <<getMese() <<"-" <<getAnno();
    return out.str();
}

bool operator<(const Data& d1, const Data& d2){
    if(d1.getAnno()<d2.getAnno()){
        return true; 
    } else if(d1.getAnno()==d2.getAnno()){
        if(d1.getMese()<d2.getMese()){
            return true;
        } else if(d1.getMese()==d2.getMese()){
            if(d1.getGiorno()<d2.getGiorno()) return true;
        }
    }
    return false;
}

bool operator==(const Data& d1, const Data& d2){
    if(d1.getGiorno()==d2.getGiorno() && d1.getMese()==d2.getMese() && d1.getAnno()==d2.getAnno()){
        return true;
    } else{
        return false;
    }
}

//FINE DEFINIZIONE FUNZIONI MEMBRO DELLA CLASSE DATA

#endif

//FINE DICHIARAZIONE CLASSE DATA

//header dei rilevamenti
//dichiarazione unica
#ifndef EVAMENTO_H
#define EVAMENTO_H
//includo librerie e strumenti 
#include"Data.h"
#include<sstream>
#include<string>
//elementi namespace std
using std::string;
using std::ostringstream;
using std::endl;

//definizione classe



class Rilevamento
{
    public:
    Rilevamento(); //costruttore di default 
    Rilevamento(Data dd,double qpmm) : d{dd},qpm{qpmm} {} //costruttore
    Rilevamento(const Rilevamento& riv) //costruttore copia
    {
        d=riv.d;
        qpm=riv.qpm;
    };

    ~Rilevamento() {};
    
   Data& getData() 
   {
       return d;
   }

    string ToString()  //funzione di stampa 

    {
        ostringstream out;
        out<<"Data: "<<d.toString()<<endl;
        out<<"qta co2: "<<qpm<<endl;
        return out.str();
    }

    private:
    Data d; //data rilevamento
    double qpm; //qta co2
};

bool operator<(Rilevamento &r1, Rilevamento &r2)
{
    if(r1.getData()<r2.getData()) return true;
    return false;
}
#endif
//header dei rilevatori
//dichiarazione unica
#ifndef EVATORE_H
#define EVATORE_H
//includo i rilevamenti per la lista 
#include"evamento.h"
//librerie e strumenti 
#include<iostream>
#include<string>
#include<sstream>
#include<list>
#include<algorithm>

using std::string;
using std::ostringstream;
using std::list;
using std::endl;
using std::sort;
using std::cout;



class Rilevamento; //dichiarata per poter essere usata come membro privato

//dichiarazione classe
class Rilevatore
{
    public:
    Rilevatore();//costruttore di default 
    Rilevatore(int ids,int x,int y,list<Rilevamento> rl) :id_station{ids},coord_x{x},coord_y{y},listarilev{rl} {} //costruttore
    ~Rilevatore() {}; //distruttore
    Rilevatore(const Rilevatore& riv)  //copy costructor
    {
        id_station=riv.id_station;
        coord_x=riv.coord_x;
        coord_y=riv.coord_y;
        listarilev=riv.listarilev;
    };

    string ToString() //funzione di stampa
    {
        ostringstream out;
        out<<"ID_Station: "<<id_station<<endl;
        out<<"Coord X: "<<coord_x<<endl;
        out<<"Coord Y: "<<coord_y<<endl;
        return out.str();
    }

   void addMeasurement(Rilevamento& riv,Rilevatore& ore)
   {
       cout<<"Rilevatore con id :" << ore.id_station<<endl;
       cout<<"sta aggiungendo questo rilevamento: " <<endl<<riv.ToString();
       listarilev.push_back(riv);
       listarilev.sort(); //sfrutta l'overload dell operatore < per funzionare
   }

    private:
    int id_station;
    int coord_x;
    int coord_y;
    list<Rilevamento> listarilev;
};
#endif
//driver dei rilevamenti e rilevatori
//definizione unica 
#ifndef DRIVERRIL_CPP
#define DRIVERRIL_CPP
//includo le due classi 
#include"evatore.h"
#include"evamento.h"
//librerie e strumenti
#include<string>
#include<vector>
#include<list>
#include<iostream>

using std::vector;
using std::list;
using std::string;
using std::cout;


int main()
{
//dichiaro le strutture che conterrano i dati 
vector<Rilevatore> evatorevec;
list<Rilevamento> l1;
list<Rilevamento> l2;

//definisco degli oggetti 
Data d1(01,02,2000), d2(02,03,2007) ,d3(05,10,1997),d4(12,07,2003),d5(28,02,2021),d6(21,04,2010);
Rilevamento m1(d1,12.00);
Rilevamento m2(d2,25.2);
Rilevamento m3(d3,10.5);
Rilevamento m4(d4,40.6);
Rilevamento m5(d5,20.6);
Rilevamento m6(d6,12.75);

//inserisco i rilevamenti nelle apposite liste
l1.push_back(m1);
l1.push_back(m2);
l2.push_back(m4);
l2.push_back(m6);


Rilevatore r1(1,12,27,l1);
Rilevatore r2(2,30,40,l2);

evatorevec.push_back(r1);
evatorevec.push_back(r2);

r1.addMeasurement(m3,r1);
r2.addMeasurement(m5,r2);

}

#endif
#MAKEFILE
CC = g++
CFLAGS = -std=c++11 -pedantic -Wall
DEPS = evatore.h evamento.h Data.h
OBJ = driverril.o

%.o: %.cpp $(DEPS)
    $(CC) -c $< $(CFLAGS)
Driver: $(OBJ)
    $(CC) -o $@ $^

ty to all of u .

Aucun commentaire:

Enregistrer un commentaire