【问题标题】:How to take inputs from array and input it into an equation in C++?如何从数组中获取输入并将其输入到 C++ 中的方程中?
【发布时间】:2017-07-08 09:47:41
【问题描述】:

我的目标是尝试创建一个以百分比表示成绩并将其乘以权重值(小数形式或百分比形式)的程序。等式基本上是:

总成绩 = (grade1*weightInDecimal1)+(grade2*weightInDecimal2)+(grade3*weightInDecimal3)+...

总成绩 = (grade1*weight%1)+(grade2*weight%2)+(grade3*weight%3)+...


有没有办法存储输入,然后在以后的代码中调用它?或者可能是更有效的方法?

我也想尝试制作一个动态数组。我想制作一个程序,询问用户他们有多少分配,并根据它制作一个数组。这样它就不会停留在 4 个任务上

#include <string>
#include <iostream>
using namespace std;
int main() {
    int numbers[4][2];
    for(int i=0;i<4;i++)
    {
       cout<<"Grade #"<<i<<endl;
       cin>>numbers[i][0];
       cout<<"Weight for grade #"<<i<<":"<<endl;
       cin>>numbers[i][1];
    }

    for (int i = 0; i<4; i++)
    {
        cout << "|" << numbers[i][0]*numbers[i][1]<< "|";
    }
    system ("PAUSE");
return 0;
}

【问题讨论】:

  • 为什么不使用std::vector 而不是数组?然后,您可以根据需要动态添加任意数量的元素。

标签: c++ arrays sorting c++11 input


【解决方案1】:

这就是结构的用途

#include <string>
#include <iostream>
#include <array>
using namespace std;

struct entry {
    int grade;
    int weight;
    int gradeWeight; //grade*weight 
};

int main() {
    array<entry,4> numbers;
    for(int i=0;i<numbers.max_size();i++)
    {
       cout<<"Grade #"<<i<<endl;
       cin>>numbers[i].grade;
       cout<<"Weight for grade #"<<i<<":"<<endl;
       cin>>numbers[i].weight;
    }

    for (int i = 0; i<numbers.max_size(); i++)
    {
        numbers[i].gradeWeight = numbers[i].grade*numbers[i].weight;
        cout << "|" << numbers[i].gradeWeight << "|";
    }
    system ("PAUSE");
    return 0;
}

这样你也可以通过增加数组大小来增加数字的数量。

【讨论】:

  • 你应该#include &lt;array&gt;
  • 非常感谢。在发布之前应该测试代码^^
【解决方案2】:

避免使用数组(动态或其他)的原因有很多。例如,请参阅 Stroustrup 的常见问题解答条目 What's wrong with arrays? 正如 Greg 在 cmets 中所建议的那样,如果您使用像 std::vector 这样的容器,您很可能会编写质量更好的代码。

如果您可以在分配容器之前计算或输入容器的大小,则可以(如果您愿意)将该大小传递给构造函数...

auto syze{ 0 };
auto initialValue{ 0 };

// calculate or input syze
. . .

// dynamically allocate an "array"
// of ints and an "array" of floats
std::vector<int> grades(syze, initialValue);
std::vector<float> weights(syze, initialValue);

另一方面,如果您更喜欢使用动态增长的容器来保存数据,您也可以这样做...

// dynamically allocate an empty "array"
// of ints and an empty "array" of floats
std::vector<int> grades;
std::vector<float> weights;

while (...condition...)
{
    std::cin >> g;   // input a grade ...
    std::cin >> w;   // ... and a weight

    // grow the containers by adding 
    // one item at the end of each
    grades.emplace_back(g);
    weights.emplace_back(w);
}

更新

我应该指出如何从两个向量计算结果。通过使用 STL 的 &lt;numeric&gt; 标头中的 std::inner_product,您只需多行代码即可计算您的总成绩。请注意,在下面的代码中,最后一个参数是 0.0(而不仅仅是 0),因此 std::inner_product 返回 double 而不是 int。这避免了 float 值被截断为 int 的任何风险(并避免了来自编译器的一些非常难看的警告)。

auto overallGrade = std::inner_product(grades.begin(), grades.end(), weights.begin(), 0.0);

【讨论】:

    【解决方案3】:

    正如其他人所指出的,如果您询问用户他们有多少分配,std::array 是一个错误的容器,因为它的维度在编译时是固定的。

    和其他人一样,我不赞成使用直接内存分配,而是使用std::vector 来管理它。

    我只是建议使用reserve()std::vector 的方法),当你知道有多少分配时。

    以下是一个完整的示例,使用 std::vectorint 的一对,std::vectorstd::pair&lt;int, int&gt; 的单个 std::vector

    #include <utility>
    #include <vector>
    #include <iostream>
    
    int main()
     {
       int                              valG, valW;
       std::size_t                      dim;
       std::vector<std::pair<int, int>> vec; 
    
       std::cout << "How many grade/weight couples? ";
       std::cin  >> dim;
    
       vec.reserve(dim);
    
       for ( auto i = 0U ; i < dim ; ++i )
        {
          std::cout << "Grade #" << i << "? " << std::endl;
          std::cin  >> valG;
          std::cout << "Weight for grade #" << i << "? " << std::endl;
          std::cin  >> valW;
    
          vec.emplace_back(valG, valW);
        }
    
        for ( auto const & p : vec )
           std::cout << '|' << (p.first * p.second) << '|';
    
        std::cout << std::endl;
    
        return 0;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      相关资源
      最近更新 更多