0

我正在研究一个解决银行家算法的项目。我从定义可用数组、分配资源矩阵和最大资源的文件中读取了输入。输入文件的结构如下:

3 3 2

0 1 0
2 0 0
3 0 2
2 1 1
0 0 2

7 5 3
3 2 2
9 0 2
2 2 2
4 3 3

我在矩阵中读取的代码是:

// get input for available resources
for (int r = 0; r < R; ++r) {
    std::string numStr;
    char in;
    do {
        input.get(in);
        numStr += in;
    } while (in != ' ');
    avail_[r] = std::stoi(numStr);
}

// get input for allocated vector
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::string numStr;
        char in;
        do {
            input.get(in);
            numStr += in;
        } while (in != ' ');
        alloc_[p][r] = std::stoi(numStr);
    }
}

// get input for max
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::string numStr;
        char in;
        do {
            input.get(in);
            numStr += in;
        } while (in != ' ');
        max_[p][r] = std::stoi(numStr);
    }
}

我有循环在读取矩阵后输出它们的值。输出是

Available:
3 3 2 
Allocated:
1 0 0 
0 0 2 
1 1 0 
2 5 3 
2 2 0 

它在读取分配矩阵后停止,甚至没有正确读取。我假设我使用 std::ifstream.get() 错误,但我不明白如何。

我发现如果我将所有数据放在 1 行中,它会正确读取分配矩阵,但我的程序在尝试读取最大矩阵时仍然停止(即使我使用了与分配矩阵相同的代码)最大读数)。

4

1 回答 1

0

请看我的评论// CHANGE HERE

const int R = 3;
const int P = 5;
int avail_[3];
int alloc_[5][3];
int max_[5][3];
std::string line;
    
// get input for available resources
for (int r = 0; r < R; ++r) {
    // std::string numStr;
    // char in;
    // do {
    //     input.get(in);
    //     numStr += in;
    // } while (in != ' ');
    // avail_[r] = std::stoi(numStr);

    // CHANGE HERE: read int directly from input
    if (!(input >> avail_[r])) {
        std::cout << "Error reading\n";
        return 1;
    }
}
// CHANGE HERE: read the blank line
//std::getline(input, line);
// get input for allocated vector
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        // std::string numStr;
        // char in;
        // do {
        //     input.get(in);
        //     numStr += in;
        // } while (in != ' ');
        // alloc_[p][r] = std::stoi(numStr);

        // CHANGE HERE: read integer directly from input
        if (!(input >> alloc_[p][r])) {
            std::cout << "Error reading\n";
            return 1;
        }
    }
}
// CHANGE HERE: read the blank line
//std::getline(input, line);
// get input for max
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        // std::string numStr;
        // char in;
        // do {
        //     input.get(in);
        //     numStr += in;
        // } while (in != ' ');
        // max_[p][r] = std::stoi(numStr);

        // CHANGE HERE: read integer directly from input
        if (!(input >> max_[p][r])) {
            std::cout << "Error reading\n";
            return 1;
        }
    }
}

std::cout << "Available\n";
for (int r = 0; r < R; r++)
{
    std::cout << avail_[r] << " ";
}
std::cout << endl;

std::cout << "Allocated\n";
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::cout << alloc_[p][r] << " ";
    }
    std::cout << std::endl;
}

std::cout << "Max\n";
for (int p = 0; p < P; ++p) {
    for (int r = 0; r < R; ++r) {
        std::cout << max_[p][r] << " ";
    }
    std::cout << std::endl;
}
于 2021-11-30T04:25:16.297 回答