如何对结构数组进行排序?

时间:2013-12-04 22:58:15

标签: c++ sorting struct selection

我需要使用选择排序按数量降序排序结构数组。 我已经被困在这一小时并且没有找到任何东西,任何帮助都会非常感激。

这是我正在使用的文本文件

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;

}

1 个答案:

答案 0 :(得分:1)

您可以使用std::sort标题中的<algorithm>

std::sort(&soda[0], &soda[6], 
    [](soda1 left, soda1 right) { 
        return left.amount > right.amount; // > = descending, < = ascending
    });

或者,如果你没有lambda函数:

bool sortSoda(soda1 left, soda1 right) {
    return left.amount > right.amount; // > = descending, < = ascending
}

//... in int main()
std::sort(&soda[0], &soda[6], sortSoda);