크기에 관계없이 문제에 대한 알고리즘을 일반화하려는 데 문제가 있습니다. 코드는 내가 사용했던 테스트 문제에서 작동하지만 일부 배열의 길이를 수동으로 삽입해야했습니다. 다음으로, 두 개의 변수로 입력 파일의 길이를 읽으려고 시도했지만, 모든 코드에서 사용할 수는 없지만 일부 파일에서는 사용할 수 없습니다. 나는 그것이 매우 어리 석음이라고 생각하지만 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
당신은'전역 변수가 필요한 가변 길이 배열 '을 작성했습니다. 다른 함수에서도 필요합니다 .' 이 상황을 처리하는 훨씬 더 좋은 방법은 필요한 배열을 각 함수에 인수로 전달하는 것입니다. 그것은 전역 변수에 대한 필요성을 피하고, 함수 서명은 (다른 장점들과 마찬가지로) 의존성을 명확하게 표현합니다. –