Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

c++ - Find duplicate element in a vector and add the quantity. Code or algorithm are welcome

I have a struct:

struct fruit_t
{
    char fruit_name[MAX_LENGTH];  // name of fruit
    float quantity; // in lbs
    float price;          // price tag of the fruit
};

Here is how I store my data in main

vector<fruit_t> fruit;

ifstream inFile;
int count = 0;
inFile.open("list1.txt");                                      
int i = 0;
while (true){                                     
    fruit.push_back(fruit_t());
    inFile >> fruit[i].fruit_name;
    inFile >> fruit[i].quantity;
    inFile >> fruit[i].price;
    if (inFile.eof() ) break;                           
    i++;
    count++;
}

Then I need to make a program to find duplicate fruit_name. If it is duplicate, add their quantity together Here is my file (I used array_list aka vector to store the data)

//fruit_name       quantity  price
melon_cantaloupe     2.60     2.99
apples_gala          1.80     1.21
bananas              2.88     0.49
oranges_naval        2.63     0.99
apples_gala          3.00     1.21
raspberries          4.76     3.25
apples_gala          1.45     1.21
mango                4.07     1.20
blueberries          3.85     2.50
oranges_honeybell    4.20     1.08
apples_jazz          4.39     2.69
oranges_honeybell    4.22     1.08

This is the output if the program worked:

//fruit_name       quantity  price
apples_gala          6.25     1.21
apple_jazz           4.39     2.69
bananas              2.88     0.49
...
question from:https://stackoverflow.com/questions/65895378/find-duplicate-element-in-a-vector-and-add-the-quantity-code-or-algorithm-are-w

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
vector<fruit_t> cpy; 
for (int i = 0; i < fruit.length(); i++) {
     bool already_entered = false;
       
      for (int j = 0; j < cpy.length(); j++) {
          bool diff = false;
          for (int k = 0; k < MAX_LENGTH; k++) {
             if (cpy[j].fruit_name[k] != fruit[i].fruit_name[k]) {
                   diff = true;
             }
          }
          if (diff == false) {
            already_entered = true;
          }
      }
        
      if (already_entered == true) {
          continue;
      }
       
      float new_quantity = fruit[i].quantity;
      for (int j = i + 1; j < fruit.length(); j++) {
          bool diff = false;
          for (int k = 0; k < MAX_LENGTH; k++) {
             if (fruit[j].fruit_name[k] != fruit[i].fruit_name[k]) {
                   diff = true;
             }
          }
          if (diff == false) {
              new_quantity += fruit[j].quantity;
          }
       }
        
       cpy.push_back(fruit_t());
       for (int k = 0; k < MAX_LENGTH; k++) cpy[cpy.length() - 1].fruit_name[k] = fruit[i].fruit_name[k];
       cpy[cpy.length() - 1].quantity = new_quantity;
       cpy[cpy.length() - 1].price = fruit[i].price;
 }

What this does: We create a new vector that will only have once occurrence of each fruit name.

We loop through our original vector, each time checking if we have already processed the current fruit (we can do this by simply looping through our cpy array). Now, we loop through the rest of the vector with a nested for loop, and add to our new_quantity variable the sum of quantities that appear for that fruit later in our original vector. Finally, we create a new object and store our results in cpy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...