简体   繁体   English

如何在这个结构数组中正确排序?

[英]How do I properly sort in this struct array?

No matter how I try to swap the structs or the "player data" it won't seem to sort at the end of the program.无论我如何尝试交换结构或“玩家数据”,它似乎都不会在程序结束时排序。

The assignment requires to call almost all the functions within the print function.赋值需要调用打印函数中的几乎所有函数。

Any late night people that could help before its due would be == heros.任何可以在到期之前提供帮助的深夜人都是 == 英雄。 thanks in advance.提前致谢。

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;



const int MAX_QBS = 100;

struct qbType{
    string firstname,lastname;
    int attempts,completions,yardsPassing,touchdowns,interceptions;
    double completionPercentage,rating;
};



//Function Prototypes
void read_stats(qbType qbRoster[],int &qbCount);
void print_stats(qbType qbRoster[],int qbCount);
void compute_comp_percentage(qbType qbRoster[],int qbCount);
void sort_by_name(qbType qbRoster[],int qbCount);
void sort_by_rating(qbType qbRoster[],int qbCount);
void compute_rating(qbType qbRoster[],int qbCount);



//**************MAIN PROGRAM*************************
int main()
{

    int qbCount = 0;
    qbType qbRoster[MAX_QBS];

    sort_by_name(qbRoster,qbCount);

    sort_by_rating(qbRoster,qbCount);


    print_stats(qbRoster,qbCount);
}
//Function Name:        read_stats
//Purpose:              reads file data into array of structs and count number of players
//Incoming:             qbRoster[], &qbCount
//Outgoing:             void
void read_stats(qbType qbRoster[],int &qbCount)
{
    ifstream infile;
    infile.open("football-in.txt");
    if(infile.fail())
    {
        cout<< "ERROR opening input file"<<endl;
        exit(1);
    }
    while(!infile.eof())
    {   
        getline(infile,qbRoster[qbCount].firstname);
        getline(infile,qbRoster[qbCount].lastname);
        infile >> ws;
        infile >> qbRoster[qbCount].attempts;
        infile >> qbRoster[qbCount].completions;
        infile >> qbRoster[qbCount].yardsPassing;
        infile >> qbRoster[qbCount].touchdowns;
        infile >> qbRoster[qbCount].interceptions;
        infile >>ws;
        qbCount ++;
    }
    infile.close();
}
//Function Name:        print_stats
//Purpose:              prints stats from array of structs into table
//Incoming:             qbRoster[] , int qbcount
//Outgoing:             void
void print_stats(qbType qbRoster[],int qbCount)
{
    read_stats(qbRoster,qbCount);
    compute_comp_percentage(qbRoster,qbCount);
    compute_rating(qbRoster,qbCount);

    cout << endl;
    cout << "Quarterback";
    cout << setw(17) << right << "Att.";
    cout << setw(17) << "Comp.";
    cout <<setw(17)<<"Pct.";
    cout <<setw(17)<<"Yards";
    cout << setw(17)<<"TD";
    cout <<setw(17)<<"Int.";
    cout <<setw(17)<<"Rating"<<endl;

    for(int i = 0; i < qbCount;i++)
        {

            cout <<qbRoster[i].firstname << qbRoster[i].lastname;
            cout <<setw(17)<<right<<qbRoster[i].attempts;
            cout <<setw(17)<<qbRoster[i].completions;
            cout <<setw(17)<<setprecision(2)<<fixed<<qbRoster[i].completionPercentage;
            cout <<setw(17)<<qbRoster[i].yardsPassing;
            cout <<setw(17)<<qbRoster[i].touchdowns;
            cout <<setw(17)<<qbRoster[i].interceptions;
            cout <<setw(17)<<setprecision(2)<<fixed<<qbRoster[i].rating<< endl;


        }
}
//Function Name:        compute_comp_percentage
//Purpose:              computes the completion percentage
//Incoming:             qbRoster[], int qbcount
//Outgoing:             void
void compute_comp_percentage(qbType qbRoster[],int qbCount)
{

    for(int i = 0;i<qbCount;i++)
    {
        qbRoster[i].completionPercentage = 1.0*qbRoster[i].completions / qbRoster[i].attempts;
    }

}
//Function Name:        compute_rating
//Purpose:              computes the total rating
//Incoming:             qbRoster[], int qbcount
//Outgoing:             void
void compute_rating(qbType qbRoster[],int qbCount)
{
    for(int i = 0; i<qbCount;i++)
    {

        qbRoster[i].rating= ((static_cast<double>(qbRoster[i].completions)*3)+static_cast<double>(qbRoster[i].yardsPassing) + (static_cast<double>(qbRoster[i].touchdowns)*10))/(static_cast<double>(qbRoster[i].attempts) +(8* static_cast<double>(qbRoster[i].interceptions)));

    }
}
//Function Name:        sort_by_name
//Purpose:              sorts by last name in ascending order
//Incoming:             qbRoster[], int qbcount
//Outgoing:             void
void sort_by_name(qbType qbRoster[],int qbCount)
{

    struct qbType temp;
    int j=0,i=0,minIndex=0;
    j = i + 1;
    for(int i=0;i< qbCount;i++)
        {

        if(qbRoster[i].lastname > qbRoster[j].lastname)
            {
            temp = qbRoster[i];


            qbRoster[i] = qbRoster[j];


            qbRoster[j] = temp;

            }
}
}

//Function Name:        sort_by_rating
//Purpose:              sorts by last name in descending order
//Incoming:             qbRoster[], int qbcount
//Outgoing:             void

void sort_by_rating(qbType qbRoster[],int qbCount)
{
    struct qbType temp;
    int j=0, i=0;
    j = i +1;
    for(int i = 0;i<qbCount;i++)
        if(qbRoster[i].rating < qbRoster[j].rating)
            {
                temp = qbRoster[i];
                qbRoster[i] = qbRoster[j];
                qbRoster[j] = temp;
            }


}

No errors just no sorting at the end of output either.没有错误只是在输出结束时也没有排序。

Hope this helps (bubblesort):希望这有帮助(冒泡排序):

void sort_by_name(qbType qbRoster[], int qbCount)
{
    struct qbType temp;
    (int j = 0; j < qbCount; j++) 
    {
       for (int i = 1; i < (qbCount - j); i++) 
       {
           if(qbRoster[i-1].lastname > qbRoster[i].lastname) 
          {
              temp = qbRoster[i];
              qbRoster[i] = qbRoster[i-1];
              qbRoster[i-1] = temp;
          }
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM