簡體   English   中英

為什么function只打印最后一個項目的結果?

[英]Why does the function prints the results of the last project only?

我正在嘗試創建一個 function 來計算每個項目的.net present worth(NPW),具有最高 NPW 的項目將是我們的選擇。

該程序適用於一個項目,如果我輸入多個項目,它只會打印最后一個項目結果,重復次數與項目數量一樣多。

代碼:

#include <iostream>
#include <cmath>
using namespace std;

void npwMethod(int nproj,int *pv,int *n, int *oc, int *ai,int *sv ,float marr){
    float npw[nproj], sum[nproj];
    
    for(int i=1;i<=nproj;i++){
        
        for(int j=1;j<=n[i];j++){
            sum[i]+=(oc[j]*(pow(1+marr,-j)));
        }
        for(int j=1;j<=n[i];j++){
            sum[i]+=(ai[j]*(pow(1+marr,-j)));
        }
        npw[i]=pv[i]+sv[i]*(pow(1+marr,-n[i]))+sum[i];
    }
    for(int i=1;i<=nproj;i++){
        cout<<"NPW"<<i<<"="<<npw[i]<<endl;
    }
}

int main() {
    int nProj,methodNum, *OC, *AI;
    float MARR;
    
    cout<<"Enter the number of projects please:";
    cin>>nProj;
    
    int *PV=new int[nProj], *N=new int[nProj], *SV=new int [nProj];     
    
    for(int i=1;i<=nProj;i++){
        cout<<"------Project"<<i<<"------\n";
        cout<<"PV"<<i<<":";
        cin>>PV[i];
        cout<<"n"<<i<<":";
        cin>>N[i];
        
        OC=new int[N[i]], AI=new int [N[i]];
        
        for(int j=1;j<=N[i];j++){
            cout<<"OC"<<j<<":";
            cin>>OC[j];
        }
        for(int j=1;j<=N[i];j++){
            cout<<"AI"<<j<<":";
            cin>>AI[j];
        }
        
        cout<<"SV"<<i<<":";
        cin>>SV[i];
                
    }
    
    cout<<"-------------------\nMARR:";
    cin>>MARR;
    MARR=MARR/100;
    
    cout<<"------RESULTS------\n";
    npwMethod(nProj, PV, N, OC, AI, SV, MARR);
}

輸入:

Enter the number of projects please:2
------Project1------
PV1:-1000
n1:2
OC1:-200
OC2:-300
AI1:0
AI2:0
SV:2000
------Project2------
PV1:-1000
n1:2
OC1:-200
OC2:-300
AI1:500
AI2:0
SV:2000
-------------------
MARR:10

預計 output:

NPW1:223.14
NPW2:677.686

有人可以幫忙解決這個問題嗎?

您的問題歸結為以下幾行:

    int* OC;
    for(int i=1; i <= nProj; i++){
        OC = new int[N[i]];
        for(int j=1; j <= N[i]; j++){
            cout << "OC" << j << ":";
            cin >> OC[j];
        }
    }
    
    npwMethod(nProj, PV, N, OC, AI, SV, MARR);

使用OC = new int[N[i]] ,您可以覆蓋上一個項目的 OC 值。 人工智能也是如此。 您需要您的 OC 是 arrays 的數組。當您使用std::vector時,這會容易得多。 然后你可以這樣做:

int main() {
    std::vector<std::vector<int>> OCs;
    for(int i=1; i <= nProj; i++){
        OCs.emplace_back();
        OC = OCs.back();
        for(int j=1; j <= N[i]; j++){
            cout << "OC" << j << ":";
            int oc;
            cin >> oc;
            OC.push_back(oc);
        }
    }
    
    npwMethod(nProj, PV, N, OCs, AI, SV, MARR);
}

void npwMethod(..., std::vector<std::vector<int>> const& oc, ...) {
    for(int i=1; i<=nproj; i++){
        for(int j=1 ;j<=n[i]; j++){
            sum[i] += oc[i][j]*...;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM