如何从数组中获取输入并将其输入到C ++中的等式中?

时间:2017-02-19 00:08:29

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

我的目标是尝试创建一个程序,该程序采用百分比等级并将其与权重值相乘(以十进制形式或百分比形式)。等式基本上是:

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

整体成绩=(1级*重量%1)+(2级*重量%2)+(3级*重量%3)+ ...

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

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

var options = { contentSettings: { contentType: 'application/pdf'}};
fileService.createFileFromLocalFile(shareName, directoryName, fileName, imageToUpload, options, function (error) {
    if (error) {
      callback(error);
    } else {

3 个答案:

答案 0 :(得分:0)

这是

的结构
#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;
}

这样,您也可以通过增加数组大小来增加数量。

答案 1 :(得分:0)

避免使用数组(动态或其他)有很多原因。例如,参见Stroustrup常见问题解答条目What's wrong with arrays?正如Greg在评论中建议的那样,如果您使用像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的std::inner_product标题中的<numeric>,仅使用一行代码来计算总体成绩。请注意,在下面的代码中,最后一个参数是0.0(而不仅仅是0),以便std::inner_product返回double而不是int。这样可以避免float值被截断为int的任何风险(并避免编译器发出一些非常难看的警告)。

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

答案 2 :(得分:0)

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

与其他人一样,我讨论了使用直接内存分配,但使用std::vector来管理它。

当您知道有多少作业时,我建议您使用reserve()std::vector的方法)。

以下是一个完整示例,使用std::vector int std::vectorstd::pair<int, int> #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; }

Set Objective - $I$387
To - Max
By Changing Variable Cells - $F$2:$F$201
Subject to the Constraints - $F$2:$F$201 = binary
- $H$387 <= 493.45
- $J$387 = 2
- $K$387 >= 4
- $L$387 >= 5
- $M$387 <= 2
- $N$387 <= 2
- $O$387 <= 2
- $P$387 <= 18