任意のサイズの問題に対してアルゴリズムを一般化しようとして問題が発生しました。 私が使用したテストの問題のコードが動作していますが、いくつかの配列の長さを手動で挿入する必要がありました。次に、私は2つの変数で入力ファイルの長さを読み込もうとしましたが、私はすべてのコードでは使用できませんが、いくつかの部分では使用できません。私はそれが非常にばかげたことだと思うが、私はC++には本当に新しく、助けを求めたい。 は、ここでは、コードの一部です:中ファイルの長さに応じた可変長配列C++
#include <fstream>
#include <iostream>
#include <time.h>
using namespace std;
struct node{
int last_prod;
int last_slot;
float ZL;
float ZU;
float g;
bool fathomed;
node *next;
node *padre;
node *primofiglio;
};
clock_t start, end;
double cpu_time_used;
int l=0;
int cont_slot=0;
int cont_prod=0;
float temp_cont;
float distanze[360]; // dichiarazione variabili
int slot[111];
int slot_cum[111];
float COIp[111];
int domanda[111];
float Zb=9999999999999999;
float LowerBound(struct node *n);
float UpperBound(struct node *n);
float h(struct node *l,struct node *n);
void creasottolivello(struct node *n);
void fathRule2(struct node *n);
void fathRule3(struct node *n);
void stampaRisultati(struct node *n, ofstream &f);
int unFathomedNodes(struct node *n);
void append(struct node* temp, struct node* n);
void ricercaOttimo(struct node *n, ofstream &f);
void calcoloBounds(struct node *n);
int main(){
start = clock();
ifstream contdist_file ("/Users/MarcoBi/Desktop/TESI di LAUREA/Xcode/dati/distanze.txt" ); // conteggio dati input
if (!contdist_file.is_open()) { //conta righe file slot
}
else {
for(int i=0; !contdist_file.eof(); i++){
contdist_file >> temp_cont;
cont_slot++;
}
}
ifstream contslot_file ("/Users/MarcoBi/Desktop/TESI di LAUREA/Xcode/dati/slot.txt");
if (!contslot_file.is_open()) { //conta righe file prodotti
}
else {
for(int i=0; !contslot_file.eof(); i++){
contslot_file >> temp_cont;
cont_prod++;
}
}
....
あなたが見ることができるように、メイン()私はcont_prodとcont_slot変数に入力ファイルのな長さを数えるが、その後、私は、変数の宣言でそれらを使用することはできません。私が必要とする可変長配列は、他の関数でも必要なグローバル変数でなければなりません。また、cont_prodとcont_slotは、グローバル変数である必要があります。なぜなら、いくつかの関数でローカル変数宣言でそれらを必要とするからです。 は、ここで私は、それらを使用する必要がある機能の一つである:cont_prodは
float LowerBound(struct node *n){ //funzione LowerBound
int S[111];
int Sp=0;
float d[111];
float dmin[111];
float D;
float LB;
for(int i=n->last_prod;i<111;i++){
Sp=Sp+slot[i];
}
for(int i=0;i<111;i++){ //Calcolo S_pigreco
S[i]=0;
}
if(n->last_prod==0){ //condizione necessaria per nodo radice
S[0]=slot[0];
for(int i=n->last_prod +2;i<111;i++){
for(int j=n->last_prod +1;j<=i;j++){
S[j]=S[j-1]+slot[j];
}
}
}
else{
for(int i=n->last_prod +1;i<111;i++){
for(int j=n->last_prod;j<=i;j++){
S[j]=S[j-1]+slot[j];
}
}
}
S[110]=S[109] + slot[110];
//calcolo somma distanze da slot j+1 a q
for(int i=0;i<111;i++){
d[i]=0;
}
for(int j=n->last_prod;j<111;j++){
for(int i=n->last_slot; i < n->last_slot +S[j]; i++){
d[j]=d[j]+distanze[i];
}
}
//calcolo dmin_pigreco
for(int i=n->last_prod; i<111; i++){
dmin[i]= d[i]/S[i];
}
D=0;
for(int i=n->last_prod; i<111; i++){
D=D+dmin[i]*domanda[i];
}
LB=n->g+2*D;
return LB;
}
111で、360がcont_slotです。 私はXcodeのMacでプログラミングしていますが、可変長配列はファイルスコープで宣言できないと言います。これはグローバル変数として意味すると思います。 これをどのように管理できますか?
これは膨大なコードです。あなたの質問に不可欠ではないすべてを削除してください。 – celtschk
あなたは '私が必要とする可変長配列はグローバル変数でなければなりません。他の関数でも必要です。 'この状況を処理するもっと良い方法は、必要な配列を引数として各関数に渡すことです。これはグローバル変数の必要性を避け、関数シグネチャは(他の優れた利点の中でも)依存関係を明確に表現します。 –