无论我如何尝试交换结构或“玩家数据”,它似乎都不会在程序末尾排序。
该分配要求我调用打印功能中的大多数功能。
任何在深夜之前可以提供帮助的人将成为==英雄。 预先感谢。
#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;
}
}
没有错误,或者在输出末尾也没有排序:/
答案 0 :(得分:1)
希望这对您有帮助(气泡排序):
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;
}
}
}
}