2017-02-19 130 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; 
} 
相關問題