2017-02-19 127 views
0

我的目标是尝试创建一个以百分比为等级并将其乘以其权重值(以十进制或百分比形式)的程序。的公式为基本上是:如何从数组中获取输入并将其输入到C++中的等式中?

总体等级=(grade1 * weightInDecimal1)+(grade2 * weightInDecimal2)+(grade3 * weightInDecimal3)+ ...

总体等级=(grade1 *重量% 1)+(grade2 *重量%2)+(grade3 *重量%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; 
} 
+1

一个std::vectorintstd::vector,为什么不使用'的std :: VECTOR'不是数组的?然后,您可以动态添加任意数量的元素。 –

回答

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

你应该'#include ' – Olipro

+0

非常感谢。在发布之前应该测试代码^^ –

0

有很多原因,以避免使用阵列(动态或其它)。例如见Stroustrup的FAQ条目What's wrong with arrays?格雷戈在评论中暗示,如果你使用的容器一样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的<numeric>标头中的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); 
0

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

正如其他人,我discurage直接使用的内存分配,但使用的std::vector来管理它。

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

下面是一个完整的示例使用,而不是几个单一的std::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; 
} 
相关问题