1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
int getArrayFree(int ary[11][2],int salesmanID) {
bool found=false; int index=0;
for(int i=0; i<11; i++) {
for(int j=0; j<1; j++) {
if (ary[i][j] == -1) {
index = i; found = true; break;
}
}
if (found) break;
}
return index;
}
int inArray(int ary[11][2],int salesmanID) {
bool found=false; int index=0;
for(int i=0; i<11; i++) {
for(int j=0; j<1; j++) {
if (ary[i][j]==salesmanID) {
index = i; found = true; break;
}
}
if (found) break;
}
return found ? index : -1;
}
int main() {
int salesmanID, charge;
int ary[11][2] = {
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0},
{-1,0}
};
ifstream ifs("test.txt",ifstream::in);
while (ifs >> salesmanID >> charge) {
int index = inArray(ary,salesmanID);
if (index != -1) {
ary[index][1]+=charge;
} else if (index == -1) {
index = getArrayFree(ary,salesmanID);
ary[index][0] = salesmanID;
ary[index][1] = charge;
}
}
ifs.close();
for(int i=0; i<11; i++)
cout << "salesmanID: " << ary[i][0] << " Charge: " <<ary[i][1] << "\n";
return 0;
}
|