使用输入文件进行冒泡排序

问题描述 投票:0回答:0
//prototypes

//header

void headerOutput(/*ofstream& fout*/);
int highest(float W1Sales[], int i, float W2Sales[], float W3Sales[], float W4Sales[], int highestW);

//detail 
void detail(/*ofstream& fout,*/ string name[], int i, int emplyeeID[], float W1Sales[], float W2Sales[], float W3Sales[], float W4Sales[], int highestW, float totalSales, string prizeNote);

//footer
void footer(/*ofstream& fout,*/ float W1Sum, int numScoutGirls, float W2Sum, float W3Sum, float W4Sum);

//prize 
void prizeOutput(float totalSales, string prizeNote);

//total sales
int returnTotal(float totalSales, float W1Sales[], int i, float W2Sales[], float W3Sales[], float W4Sales[]);
int main() {
//Declare Employee ID,Name,Email,Week1 Sales,Week2 Sales,Week3 Sales,Week4 Sales
    int choice; //choice for the menu
    bool quit = false; //for user to quit the menu if desired
    int emplyeeID[46];
    string name[46], email, oldHeader; //Declared old header as placeholder for the holder
    int numScoutGirls = 0; //scoutgirl counter
    float W1Sales[46], W2Sales[46], W3Sales[46], W4Sales[46]; //W stands for week
    int i = 0;
    int highestW = 0;//placeholder for the highest value
    float W1Sum = 0;
    float W2Sum = 0;
    float W3Sum = 0;
    float W4Sum = 0;
    int listSize = 46; //declares length of the list in girl scout data
    string prizeNote;
    float totalSales;
    int j = 0;
    ifstream fin; //Declared input file
    ofstream fout;//declared output file
//1.Open Files
    fin.open("gsdata.txt");
    fout.open("sales_report.txt");

//File error check
    if (fin){
        cout << "Processing...\n" << endl;
    }
    else{
        cout << "Error." << endl;
    }

    
//2.Remove Header
getline(fin, oldHeader);
    
//3.Add new header (Employee ID,Name,Email,Week1 Sales,Week2 Sales,Week3 Sales,Week4 Sales)
    headerOutput();
//4.Loop through data Employee ID,Name,Email,Week1 Sales,Week2 Sales,Week3 Sales,Week4 Sales
while(!fin.eof()){
//5.Input Values
    numScoutGirls++;
    fin >> emplyeeID[i];
    fin.ignore();
    getline(fin, name[i], ',');
    fin.ignore(100,  '$');
    fin >> W1Sales[i];
    fin.ignore(',', '$');
    fin >> W2Sales[i];
    fin.ignore(',', '$');
    fin >> W3Sales[i];
    fin.ignore(',', '$');
    fin >> W4Sales[i];
    fin.ignore(100, '\n');
            
    
    

//6.Calculations
//Compare values to find the highest sale week
     highestW = highest(W1Sales, i, W2Sales, W3Sales, W4Sales, highestW);

//Calculate total sales
    totalSales = returnTotal(totalSales,  W1Sales, i, W2Sales, W3Sales, W4Sales);//Adding up all weeks to find the total
//Use a loop to determine who gets a prize
    string prizeNote;
    if (totalSales > 450.00){
        prizeNote = "****Prize****";
    }
    
    W1Sum += W1Sales[i];
    W2Sum += W2Sales[i];
    W3Sum += W3Sales[i];
    W4Sum += W4Sales[i];
    


 // Bubble Sort
 for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (emplyeeID[j] > emplyeeID[j + 1]) {
                swap(emplyeeID[j], emplyeeID[j + 1]);
                swap(name[j], name[j + 1]);
                swap(W1Sales[j], W1Sales[j + 1]);
                swap(W2Sales[j], W2Sales[j + 1]);
                swap(W3Sales[j], W3Sales[j + 1]);
                swap(W4Sales[j], W4Sales[j + 1]);
            }
        }
    }
//output
detail(/*ofstream& fout,*/ name, i, emplyeeID, W1Sales, W2Sales, W3Sales, W4Sales, highestW, totalSales, prizeNote);
}
//8.Footer
footer(/*fout,*/ W1Sum, numScoutGirls, W2Sum,  W3Sum, W4Sum);

    
//9.Close Files
    fin.close();
    fout.close();

    return 0;
}

//****************************************************************************************************************************************************************************************************************************************************//

//Functions (Only after code is finished)



//highest function

int highest(float W1Sales[], int i, float W2Sales[], float W3Sales[], float W4Sales[], int highestW){
    if(W1Sales[i] > W2Sales[i] && W1Sales[i] > W3Sales[i] && W1Sales[i] >  W4Sales[i]){
        highestW = 1;
    }
    
    if(W2Sales[i] > W1Sales[i] && W2Sales[i] > W3Sales[i] && W2Sales[i] >  W4Sales[i]){
        highestW = 2;
    }
    if(W3Sales[i] > W1Sales[i]  && W3Sales[i] > W2Sales[i] && W3Sales[i] >  W4Sales[i]){
        highestW = 3;
    }
    if(W4Sales[i] > W1Sales[i] && W4Sales[i] > W2Sales[i] && W4Sales[i] >  W3Sales[i]){
        highestW = 4;
    }
    return highestW;
}

int returnTotal(float totalSales, float W1Sales[], int i, float W2Sales[], float W3Sales[], float W4Sales[]){
    totalSales = W1Sales[i] + W2Sales[i] + W3Sales[i] + W4Sales[i];
    return totalSales;
}
//output header function
void headerOutput(/*ofstream& fout*/){
        cout << left << "Name" << right << setw(36) << "EID" << setw(8) <<  "W1" << setw(8) << "W2" << setw(8) << "w3" << setw(8) << "W4" << setw(15) << "High" << setw(8) << "Total" << setw(15) << "Note\n" << endl; 
}

//function to output detail
void detail(/*ofstream& fout,*/ string name[], int i, int emplyeeID[], float W1Sales[], float W2Sales[], float W3Sales[], float W4Sales[], int highestW, float totalSales, string prizeNote){
        
cout <<left <<  setfill('.') << setw(25) << name[i] << fixed << setprecision(2) << right << setw(15) <<  emplyeeID[i] << setfill(' ') <<  setw(9) << W1Sales[i] << setw(8) << W2Sales[i] << setw(8) << W3Sales[i] << setw(8) << W4Sales[i] << setw(12) << highestW << setw(11) << totalSales << setw(9) << setw(22) << prizeNote  << endl;
    
}
//function for footer
void footer(/*ofstream& fout,*/ float W1Sum, int numScoutGirls, float W2Sum, float W3Sum, float W4Sum){
        cout << '\n' <<  setw(23) << "Average" << right <<setfill('.') << setw(26) << W1Sum / numScoutGirls << setfill(' ') << setw(8) << W2Sum / numScoutGirls << setw(8) << W3Sum / numScoutGirls << setw(8) << W4Sum /numScoutGirls <<  endl; 
    cout << "\nNumber of girl scouts:  " << numScoutGirls << endl;
}
void prizeOutput(float totalSales, string prizeNote){
    if (totalSales >= 450.00){
        prizeNote = "****Prize****";
    }
    else{
        prizeNote = " ";
    }
}

我正在尝试根据员工 ID 和姓名按升序对列表进行排序。

大多数情况下,我需要帮助来理解冒泡排序以及如何在这种情况下应用它。 youtube 上的大多数教程都包含用户输入或已定义的数组,而不包含输入文件。我很难用输入文件来理解这一点。我理解它是这样的:

int x[9] {1, 5, 6, 3, 2, 7, 4, 10, 2};

但是当你合并输入文件时我很挣扎。

提前谢谢你!

下面是我的代码:

我尝试过的事情导致用长数字代替员工 ID,名称不符合他们的观点员工 ID 和核心转储。

这是我的输入文件:

员工 ID、姓名、电子邮件、第 1 周销售额、第 2 周销售额、第 3 周销售额、第 4 周销售额

397,Lev B. Kennedy,[email protected],95.63 美元,59.22 美元,54.10 美元,182.66 美元

604,Keane C. Landry,[email protected],$54.23,$79.15,$70.50,$72.59

597,Jameson M. Cooley,[email protected],$174.73,$68.78,$97.00,$83.07

759,Colorado I. Duke,[email protected],$88.19,$52.64,$51.00,$81.04

672,Chanda L. Valentine,[email protected],67.39 美元,81.79 美元,79.06 美元,243.18 美元

939,Chava A. Farley,[email protected],$87.11,$151.06,$87.74,$142.91

688,Clio K. Beasley,[email protected],$70.36,$76.61,$51.03,$238.74

401,Keelie T. Holman,[email protected],53.51 美元,82.86 美元,62.52 美元,127.53 美元

471,Jacqueline Z. Fulton,[email protected],$82.92,$74.58,$55.53,$84.14

903,Andrew C. Lawson,[email protected],$155.11,$63.50,$94.03,$132.24

503,Petra N. Chandler,[email protected],$78.28,$70.46,$99.68,$81.92

266,Ciaran N. Langley,[email protected],$74.55,$84.19,$94.48,$128.46

790,Brooke D. Bennett,[email protected],$96.59,$52.24,$86.31,$239.56

185,Harriet Q. Pitts,[email protected],55.83 美元,83.62 美元,79.12 美元,187.45 美元

326,Grace A. Valencia,[email protected],$83.87,$82.51,$62.82,$112.05

877,Kenyon E. Dickson,[email protected],$99.28,$55.27,$76.85,$97.12

875,Wynne Q. Larson,[email protected],$55.83,$55.73,$63.76,$223.46

584,MacKenzie H. Garner,[email protected],50.71 美元,73.40 美元,61.92 美元,248.29 美元

821,Stacy K. Salinas,[email protected],$98.01,$93.23,$88.07,$186.98

797,Hayley V. Stuart,[email protected],$87.01,$157.95,$99.44,$107.03

695,Cherokee X. Huffman,[email protected],61.01 美元,84.03 美元,77.87 美元,143.20 美元

472,Amethyst D. Blake,[email protected],85.84 美元,82.86 美元,51.96 美元,194.31 美元

237,Gary Y. Randall,[email protected],$68.07,$87.36,$56.74,$247.64

506,Wynne T. Vasquez,[email protected],$87.50,$90.35,$191.59,$87.09

264,Yoshio J. Levine,[email protected],$51.17,$87.82,$89.21,$140.89

901,Uta B. Mcintyre,[email protected],$51.58,$86.02,$81.11,$138.37

322,Tanner X. Prince,[email protected],$94.20,$57.22,$68.21,$164.63

123,Cailin M. Shepherd,[email protected],$81.15,$59.77,$159.15,$160.46

543,Gregory V. Clayton,[email protected],54.35 美元,90.50 美元,82.44 美元,236.43 美元

752,Wylie H. Rollins,[email protected],$94.19,$98.26,$98.48,$146.00

972,Meghan W. Combs,[email protected],$96.96,$52.71,$62.09,$224.58

218,Kennedy K. Andrews,[email protected],$99.65,$60.97,$71.96,$222.33

441,Cody Q. Morrison,[email protected],61.08 美元,64.47 美元,98.66 美元,194.38 美元

189,Benjamin X. Buckner,[email protected],$58.28,$72.64,$51.49,$142.72

803,Keelie N. Dennis,[email protected],$90.87,$79.25,$156.11,$106.07

625,Zorita D. Meyers,[email protected],$63.42,$76.81,$60.60,$207.82

545,Stewart Z. Rowland,[email protected],71.98 美元,52.68 美元,77.36 美元,154.20 美元

307,阿拉丁 A.福斯特,[email protected],74.99 美元,72.84 美元,82.39 美元,113.55 美元

265,Barry Z. Glass,[email protected],$56.80,$74.73,$52.05,$249.42

526,Abbot N. Humphrey,[email protected],79.44 美元,92.65 美元,77.56 美元,70.54 美元

174,Justina N. Wong,[email protected],67.33 美元,93.52 美元,98.67 美元,102.62 美元

151,Marvin W. Pittman,[email protected],$65.52,$71.46,$60.82,$114.63

687,Adena X. Richardson,[email protected],$50.47,$192.72,$82.83,$151.54

164,Briar P. Merritt,[email protected],$97.54,$86.10,$87.72,$161.33

684,Adele X. Goodman,[email protected],62.57 美元,65.44 美元,61.76 美元,218.01 美元

495,Zeus E. McCullough,[email protected],$260.82,$70.42,$77.08,$211.48

c++ arrays function file sorting
© www.soinside.com 2019 - 2024. All rights reserved.