First, I have a few notes on style.
Don't do this. By convention, T is used to denote a template parameter when defining simple templates, and more generally as a placeholder for an unknown type (e.g. as in the sentence "suppose there's a function foo() that takes a T ...").
This typedef makes your code much harder to read. Use the more conventional
typedef unsigned long ulong;
.
Also, you're not using a consistent style.
* Indentation should follow code structure.
* Use blank lines, but not at random. Use them to separate distinctly related code sections. More than a single blank line in a row is almost never a good idea, as they spread the code too far apart.
* Use consistent names. Either use CamelCase or underscore_variable_names. IMO it's okay to use different naming styles for different kinds of things. I like to name classes LikeThis (but not likeThis) and functions and variables like_this.
* With the exception of for loop indices, avoid single letter variable names. You should never use 'x' unless it's in a mathematical context, and even then you should be careful to avoid producing code that's difficult to follow.
* This is less important than the other bullet points, but classes should be named like things (e.g. CustomerRecord) and functions should be named like actions (e.g. 'get_customer_id()'), with some exceptions (e.g. 'vincenty_distance()', or 'shortest_path()', but it could be argued that they could be renamed to 'compute_vincenty_distance()' and 'find_shortest_path()'). A class named checkFoodSupply is strange and suggests perhaps poor design.
Here's your code cleaned up. You don't have to follow the particular style I used here, but you should be consistent and clear.
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 113 114 115 116 117 118 119 120
|
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long ulong;
class Condo
{
public:
Condo(unsigned int fridgenumber);//total number of fridges
double foodSupply() const; // returns total amount of food in all fridges, if a fridge has food it counts as 2 food supply.
unsigned int buyFood(); //fills one fridge up
unsigned int eatFood();//empties an entire fridge
private:
unsigned int fridgez;
unsigned int fullFridges;
unsigned int emptyFridges;
};
Condo::Condo(unsigned int fridgenumber)
:
fridgez(fridgenumber),
fullFridges(0),
emptyFridges(fridgenumber)
{}
unsigned int Condo::buyFood()
{
if(fullFridges<fridgez)
{
emptyFridges--;
fullFridges++;
return fullFridges;
}
else
return fullFridges;;
}
double Condo::foodSupply() const
{
return fullFridges*2;
}
unsigned int Condo::eatFood()
{
if(emptyFridges< fridgez)
{
emptyFridges++;
fullFridges--;
return fullFridges;
}
else
return fullFridges;
}
struct foodAmount
{
//! food amount at time of test.
double food;
ulong time;
};
class checkFoodSupply
{
public:
// Condo's being checked
checkFoodSupply(const std::vector<Condo>& Condos);
double recordFood(ulong nT);//record amount of food currently being contained by all the condo's and record it for later
//record of food records
std::vector<foodAmount> records() const;
private:
std::vector<Condo> CondoSupply;
std::vector<foodAmount> recordsP;
};
checkFoodSupply::checkFoodSupply(const std::vector<Condo>& Condos):
CondoSupply(Condos)
{}
double checkFoodSupply::recordFood(ulong nT) {
double totalSupply = 0.0;
for (auto & condo : CondoSupply) // for each condo in CondoSupply
{
totalSupply+=condo.foodSupply();
}
recordsP.push_back( {totalSupply,nT});
return totalSupply;
}
std::vector<foodAmount> checkFoodSupply::records() const
{
return recordsP;
}
int main()
{
unsigned int myCondo1Time = 0;
unsigned int fridgeNum = 10;
Condo myCondo(fridgeNum);
Condo myCondo1(11);
Condo myCondo2(12);
std::vector<Condo> CondoList= {myCondo,myCondo1,myCondo2};
checkFoodSupply CondoTest(CondoList);
std::vector<foodAmount> records;
records = CondoTest.records();
ulong t;
for(t = 0; t < 10; t++)
{
CondoTest.recordFood(t);
}
myCondo1Time = records[1].time;
std::cout<<myCondo1Time;
}
|