This question deals with the ability to construct a structure that is able to be easily iterated through.
The problem:
I have four different "boxes":
- Box A: Always has 1 apple and 1 pear
- Box B: Always has 1 orange and 1 pear
- Box C: Always has 1 peach
- Box D: Can have varying amounts of apples and oranges (0 - 10+) but always has 1 pear
Each piece of fruit has a different taste value
I currently process this information by creating a queue of the different taste values and box types then process this information with a lots of if-elses in multiple functions.
Here is an example of one function that seeks to get a total sum of the taste values e.g.
void process_crate(vector<string> boxes, vector<double> taste_value, vector<vector<double> > amount){
unsigned int count = 0; // holds index in taste_value
double sum_taste = 0; // holds taste value
for(unsigned int i = 0; i < boxes.size(); i++){
string active_box = boxies[i];
if(active_box == "A"){ // An apple and a pear
sum_taste += taste_box_A(taste_value[count],taste_value[count+1]); // user preference
count += 2;
}else if(active_box == "B"){ // An orange and a pear
sum_taste += taste_box_B(taste_value[count], taste_value[count+1]); // user preference
count += 2;
}else if(active_box == "C"){ // A peach
sum_taste += taste_box_C(taste_value[count], taste_value[count+1]); // user preference
count += 1;
}else{ // Box D varying apples and oranges, but always a pear
unsigned int apples = amount[i][0];
unsigned int oranges = amount[i][1];
vector<double> apple_tastes(apples);
vector<double> orange_tastes(oranges);
double pear_taste = taste_value[count+apples + oranges];
if(apples != 0){
for(unsigned int j = 0; j<apples; j++){
apple_tastes[j] = taste_value[count+j];
}
count += apples;
}
if(oranges != 0){
for(unsigned int j = 0; j<apples; j++){
orange_tastes[j] = taste_value[count+j];
}
count += oranges;
}
sum_taste += taste_box_D(apple_tastes, orange_tastes, pear_taste); // user preference
count += 1;
}
}
}
I think use of IF-ELSE statements combined with lots of vectors to store the data could be greatly improved.
Any suggestions?
Aucun commentaire:
Enregistrer un commentaire