Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Reading Data From a File and Formatting Output in C++ file is composed of bunch of neighborhoods. a neighborhoob starts with a ; and finishes

Reading Data From a File and Formatting Output in C++

file is composed of bunch of neighborhoods.

a neighborhoob starts with a ; and finishes with a ;

Each neighborhood is composed of a number of households

Each household has an ID (first int) a revenue (int) and number of members (int) separated by spaces

the main goal is to compute the revenue average of each neighborhood. I need help to read the inpu properly si I can create objects out of them and then store in a dynamic array of neighborhood

File to read from : data.txt

Neighborhood1

;

1041 12180 4

1062 13240 3

6217 10000 2

9280 6200 1

;

Neighborhood2

;

1227 24180 6

1060 70240 1

;

household.h

#if !defined(__FOYER_H__) #define __FOYER_H__ #include

class Household{ public: Household(); Household(int id, int revenue, int nbrMembre); // getters int getId(); int getRevenue(); int getNbrMembre(); // setters void setId(int _id); void setRevenue(int _revenue); void setMembre(int _nbrMembre); private: int id; int revenue; int nbrMembre;

friend std::ostream& operator << (std::ostream&, const Household&); friend std::istream& operator >> (std::istream&, Household&); };

#endif

neighborhood.h

#if !defined(__NEIGHBORHOOD_H__) #define __NEIGHBORHOOD_H__ #include #include #include #include "household.h" #include "tableau.h"

using namespace std;

class Quartier{ public: Neighborhood(); Neighborhood(std::string nom, Tableau _listFoyer); Household f;

private: Tableau listFoyer; std::string nom;

friend std::ostream& operator << (std::ostream&, const Neighberhood&); friend std::istream& operator >> (std::istream&, Neighborhood&);

}; #endif

tableau.h

#if !defined(__TABLEAU_H__) #define __TABLEAU_H__

template class Tableau { public: Tableau(int cap=8); Tableau(const Tableau&); ~Tableau();

void add(const T& element); // la fin //void inserer_fin(const T& element){ajouter(element);} bool empty() const {return nbElements==0;} void toEmpty(); int size() const {return nbElements;}

void insert(const T& element, int index=0); void remove(int index=0); void remove_last(); int find(const T& element) const; bool contain(const T& element) const;

T& operator[] (int index); const T& operator[] (int index) const;

Tableau& operator = (const Tableau& autre);

bool operator == (const Tableau& autre) const;

private: T* elements; int cap; int nbElements;

};

#include

template Tableau::Tableau(int c) { cap = c; nbElements = 0; elements = new T[cap]; }

template Tableau::Tableau(const Tableau& other) { cap = other.nbElements; nbElements = other.nbElements; elements = new T[cap]; for(int i=0;i

template Tableau::~Tableau() { delete[] elements; //elements = NULL; // cela est optionnel }

template void Tableau::add(const T& item){ if(nbElements >= cap){ if(cap==0) cap=2; cap *= 2; T* temp = new T[cap]; for(int i=0;i

template void Tableau::insert(const T& element, int index){ assert(index<=nbElements); if(nbElements >= cap){ if(cap==0) cap=2; cape *= 2; T* temp = new T[cap]; for(int i=0;iindex;i--) elements[i] = elements[i-1]; elements[index] = element; nbElements++; } }

template void Tableau::remove_last(){ assert(nbElements>0); nbElements--; }

template void Tableau::remove(int index){ assert(nbElements>0); nbElements--; for(int i=index;i

template int Tableau::find(const T& element) const{ for(int i=0;i

template bool Tableau::contain(const T& element) const{ return find(element)!=-1; }

template void Tableau::toEmpty(){ nbElements = 0; }

template T& Tableau::operator[] (int index){ assert(index

template const T& Tableau::operator[] (int index) const{ assert(index

template Tableau& Tableau::operator = (const Tableau& other){ if(this==&other) return *this;

nbElements = other.nbElements; if(capacite

template bool Tableau::operator == (const Tableau& other) const{ if(this == &other) return true; if(nbElements != other.nbElements) return false; for(int i=0;i

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What are the classifications of Bank?

Answered: 1 week ago