c++ - 如何使用结构将 "tag"数组编号与字符串一起使用?

标签 c++ visual-studio-2017

所以我有一个循环,它接受一行具有名称和几个数字的格式化输入,它执行一个操作来根据这些数字确定总分。在程序结束时,它应该输出最高分数以及获得分数的人的名字。我有一个计数变量,每次通过循环时都会增加,以将先前“总分”的数量计入数组。在程序结束时,将包含所有总分的数组从高到低排序,然后输出 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
. . .

最佳答案

而不是存储和排序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());

之后,就像拉出第一个人并捕获他们的名字和总分一样简单!

关于c++ - 如何使用结构将 "tag"数组编号与字符串一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529535/

相关文章:

对象或静态中的 C++ 方法

c++ - 理解 C++ 中的模板

uwp - .net native 编译器 2.0.2 无法构建应用程序。无法加载程序集 System.Data.dll

multithreading - Visual Studio 2015+修改所有断点的条件

asp.net-mvc - AzureKeyVault ConfigurationBuilder 中的 connectionString 属性是什么样的?

c++ - Visual Studio 2017 控制台应用程序 : precompiled header

c++ - 对函数类型的右值引用

C++ 头文件 - 将它们放在一个目录中或合并为树结构?

c++ - 小字符串优化(调试与 Release模式)

c++ - 内联变量被多次初始化