-2
Мне нужно использовать сортировку сортировки для сортировки массива структур в порядке убывания суммы. Я застрял на этом в течение часа и ничего не нашел, любая помощь была бы высоко оценена.Как отсортировать массив структур?
Это текстовый файл, я использую
Cola
0.75 20
Ruby Red Blast
1.00 10
Lemon Fizz
0.75 8
Grape Soda
0.90 5
Citrus Flip
0.85 0
Habanero Surprise
0.80 11
Вот мой код
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct soda1{
string name;
double price;
int amount;
};
typedef soda1 soda[6];
void vendingmachine(struct soda1[6]);
int main(){
ifstream inFile;
int SIZE=6;
soda soda;
double profit=0;
inFile.open ("machine.txt");
if (inFile.fail())
{
cout << "Error: Data file could not be opened" << endl;
exit (EXIT_FAILURE);
}
for(int i=0; i < SIZE; i++){
getline(inFile, soda[i].name);
inFile >> soda[i].price;
inFile >> soda[i].amount;
inFile.ignore(100,'\n');
}
cout << "Welcome to the vending machine!" << endl;
cout << endl;
cout << "***************************************************************" << endl;
cout << " " << "Drink Name" << " " << "Price Per Can" << " " << "Number in Machine" << endl;
for(int i=0; i < SIZE; i++){
cout << setw(17) << soda[i].name << setw(16) << soda[i].price << setw(20) << soda[i].amount << endl;
}
cout << "***************************************************************" << endl;
int choice;
cout << "Please press 1-6 for a drink or 0 if you wish to quit: ";
cin >> choice;
if((choice!=0) && (choice!=1) && (choice!=2) && (choice!=3) && (choice!=4) && (choice!=5) && (choice!=6)){
cout << "Error invalid choice" << endl;}
while((choice >=1) && (choice <=6)){
if((choice==1) && (soda[0].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is .25" << endl;
soda[0].amount--;
profit = profit+.25;
}
if((choice==1) && (soda[0].amount==0)){
cout << "Sold Out" << endl;
}
if((choice==2) && (soda[1].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is 0" << endl;
soda[1].amount--;
profit = profit+.25;
}
if((choice==2) && (soda[1].amount==0)){
cout << "Sold Out" << endl;
}
if((choice==3) && (soda[2].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is .25" << endl;
soda[2].amount--;
profit = profit+.25;
}
if((choice==3) && (soda[2].amount==0)){
cout << "Sold Out" << endl;
}
if((choice==4) && (soda[3].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is .10" << endl;
soda[3].amount--;
profit = profit+.25;
}
if((choice==4) && (soda[3].amount==0)){
cout << "Sold Out" << endl;
}
if((choice==5) && (soda[4].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is .15" << endl;
soda[4].amount--;
profit = profit+.25;
}
if((choice==5) && (soda[4].amount==0)){
cout << "Sold Out" << endl;
}
if((choice==6) && (soda[5].amount!=0)){
cout << endl << "Please input $1.00 for your beverage" << endl;
cout << "Change is .20" << endl;
soda[5].amount--;
profit = profit+.25;
}
if((choice==6) && (soda[5].amount==0)){
cout << "Sold Out" << endl;
}
vendingmachine(soda);
cout << "Please press 1-6 for a drink or 0 if you wish to quit: ";
cin >> choice;
}
cout <<"The Profit is: " << profit << endl;
return 0;
}
void vendingmachine(struct soda1 soda[6]){
cout << "***************************************************************" << endl;
cout << " " << "Drink Name" << " " << "Price Per Can" << " " << "Number in Machine" << endl;
for(int i=0; i < 6; i++){
cout << setw(17) << soda[i].name << setw(16) << soda[i].price << setw(20) << soda[i].amount << endl;
}
cout << "***************************************************************" << endl;
}
http://www.cplusplus.com/reference/algorithm/sort/ 'std :: sort' использует предикат, вы можете использовать его для упрощения. – user2485710
Ваш код не имеет ничего общего с сортировкой. Вы должны попытаться реализовать сортировку, а затем скажите нам, если у вас возникнут проблемы, описывающие наблюдаемое поведение и ожидаемое поведение, и показывая только код, относящийся к сортировке .... –