首页 > 解决方案 > 如何使用结构用字符串“标记”数组编号?

问题描述

所以我有一个循环,它接受一行格式化输入,其中包含一个名称和几个数字,它执行一个操作来根据这些数字确定总分。在程序结束时,它应该输出最高分数以及获得分数的人的姓名。我有一个计数变量,每次它通过循环时都会增加,以将前一个“总分”的数量计入一个数组。在程序结束时,将包含所有总数的数组从高到低排序,然后输出 scoretotals[0] 以显示最高分数。我的问题是将与该数字对应的名称转换为可以在最后输出的值的最简单方法是什么?

我尝试过制作一个结构,然后将该数组作为结构的一部分,但这会带来很多错误。所以这是我的代码,没有尝试输出与最高分对应的名称

 #include <iostream>
    #include <cmath>
    #include <string>
    #include <fstream>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    using namespace std;

    int main()
    {
        cout << "Name    " << "Diff   " << "Sorted scores                                      " << "Total" << endl;
        struct contestants
        {
            string name;
            double difficulty;
            double score1;
            double score2;
            double score3;
            double score4;
            double score5;
            double score6;
            double score7;
            double score8;
            double score9;
        };
        contestants person;

        ifstream divers ("m6dive.txt");
        int count = 0;



        double scoretotals[50];

        while (divers >> person.name >> person.difficulty >> person.score1 >> person.score2 >> person.score3 >> person.score4 >> person.score5 >> person.score6 >> person.score7 >> person.score8 >> person.score9)
        {


            double scores[9] = { person.score1, person.score2, person.score3, person.score4, person.score5, person.score6, person.score7, person.score8, person.score9 };

            std::sort(scores, scores + 9, std::greater< double >()); //sorts from max to min

            double total = (scores[1] + scores[2] + scores[3] + scores[4] + scores[5] + scores[6] + scores[7]) * person.difficulty; //computes score (total excluding min,max multiplied by score)

            //outputs name, difficulty, scores sorted and total
            cout << person.name << "\t" << std::setprecision(1) << fixed << person.difficulty << "\t" << scores[8] << "\t" << "\t" << scores [7] << " "<<  scores [6] << " " << scores[5] << " " << scores[4] << " " << scores [3] << " " << scores [2] << " " <<scores[1] << " " << scores [0] << "   " << total << endl;


            scoretotals[count] = total;
            count++;
        }

        std::sort(scoretotals, scoretotals + 50, std::greater< double >());
        cout << "Highest score is " << scoretotals[0];

    }

输出:

Name    Diff   Sorted scores                                      Total
Anne    2.0     8.0             8.0 8.5 8.5 9.0 9.0 9.0 9.5 9.5   123.0
Sarah   3.0     8.5             8.5 8.5 8.5 9.0 9.0 9.0 9.5 9.5   186.0
Jon     1.5     6.0             7.0 7.5 7.5 7.5 8.0 8.5 8.5 8.5   81.8
Highest score is 186.0
. . .

标签: c++visual-studio-2017

解决方案


而不是存储和排序scoretotals,存储和排序contestant

如果你添加一个计算函数<,你可以sort()用你自己的结构调用!

struct less_than_key {
    inline bool operator() (const contestant& c1, const contestant& c2)
    {
        //Consider making a different function to calculate totals to simplify this copy/paste!
        double c1_total = (c1.score1 + ... + c1.score9) * c1.difficulty;
        double c2_total = (c2.score1 + ... + c2.score9) * c2.difficulty;

        return (c1_total < c2_total);
    }
};

然后你可以排序:

std::sort(people, people + 50, less_than_key());

之后,就像拉出第一个人并抓住他们的名字和总数一样简单!


推荐阅读