来自txt的C ++ Magic Square:仅读取第一个方块

时间:2015-12-06 20:56:34

标签: c++ c++11

这与我提出的另一个问题有关,但现在我已经走得更远了。我正在尝试基于文本文件输入创建Magic Square程序。该程序将读取第一个方块,但然后打印出其他人的乱码。如果存在单个数字,则应该为其后面的正方形设置参数。 所以这是文本文件中的一个例子:

3      <- this indicates the rows&columns
4 9 2  <- row 1
3 5 7  <- row 2
8 1 6  <- row 3
5      <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20

这是我到目前为止的程序。 我正在尝试遵循赋值指令,这就是为什么我有二维数组和函数设置我的方式

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int SIZE = 20;

void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);

int main()
{
    int n;
    int square[SIZE][SIZE];
    ifstream inputFile;
    inputFile.open("Prog2Input.txt");

    while (inputFile >> n)
    {
        readSquare(n, square);
        printSquare(n, square);
        if (checkMagic(n, square))
        {
            cout << "Magic Square" << endl;
        }
        else
        {
            cout << "NOT Magic Square" << endl;
        }
        system("pause");
    }

    system("pause");
    return 0;
}

void readSquare(int n, int square[][SIZE]) {
    ifstream inf("Prog2Input.txt");
    int x;
    for (int i = 0; i<n; i++) {
        for (int j = 0; j<n; j++) {
            inf >> x;
            square[i][j] = x;
        }
    }
}

void printSquare(int n, int square[][SIZE]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << square[i][j] << " ";
        }
        cout << endl;
    }
}
bool checkMagic(int n, int square[][SIZE]) {
    int total = ((1 + n*n) / 2)*n;
    for (int i = 0; i < n; i++) {
        if (sumRow(i, n, square) != total) {
            return false;
        }
    }
    for (int i = 0; i<n; i++) {
        if (sumColumn(i, n, square) != total) {
            return false;
        }
    }
    if (sumDiagonal1(n, square) != total) {
        return false;
    }
    if (sumDiagonal2(n, square) != total) {
        return false;
    }
    return true;
}

int sumRow(int row, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[row][i];
    }
    return sum;
}

int sumColumn(int col, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][col];
    }
    return sum;
}

int sumDiagonal1(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][i];
    }
    return sum;
}

int sumDiagonal2(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][(n - i) - 1];
    }
    return sum;
}

1 个答案:

答案 0 :(得分:0)

全部,感谢您帮助我们解决此问题。我和你的教授一起发表你的评论,制作下面的代码。这将处理由初始大小整数指定的未知数量的幻方矩阵。 首先显示示例文本文件:

3      <- this indicates the rows&columns
4 9 2  <- row 1
3 5 7  <- row 2
8 1 6  <- row 3
5      <- start of next square. rows&columns
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20

现在是代码的最终产品:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int SIZE = 20;
ifstream inf;

void readSquare(int, int[][SIZE]);
void printSquare(int, int[][SIZE]);
bool checkMagic(int, int[][SIZE]);
int sumRow(int, int, int[][SIZE]);
int sumColumn(int, int, int[][SIZE]);
int sumDiagonal1(int, int[][SIZE]);
int sumDiagonal2(int, int[][SIZE]);

int main()
{
    int n;
    int square[SIZE][SIZE];
    inf.open("Prog2Input.txt");

    while (inf >> n)
    {
        cout << "Matrix Size: " << n << endl;
        readSquare(n, square);
        printSquare(n, square);
        if (checkMagic(n, square))
        {
            cout << "Magic Square" << endl;
        }
        else
        {
            cout << "NOT Magic Square" << endl;
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

void readSquare(int n, int square[][SIZE]) {
    int x;
    for (int i = 0; i<n; i++) {
        for (int j = 0; j<n; j++) {
            inf >> x;
            square[i][j] = x;
        }
    }
}

void printSquare(int n, int square[][SIZE]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << square[i][j] << " ";
        }
        cout << endl;
    }
}
bool checkMagic(int n, int square[][SIZE]) {
    int total = ((1 + n*n) / 2)*n;
    for (int i = 0; i < n; i++) {
        if (sumRow(i, n, square) != total) {
            return false;
        }
    }
    for (int i = 0; i<n; i++) {
        if (sumColumn(i, n, square) != total) {
            return false;
        }
    }
    if (sumDiagonal1(n, square) != total) {
        return false;
    }
    if (sumDiagonal2(n, square) != total) {
        return false;
    }
    return true;
}

int sumRow(int row, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[row][i];
    }
    return sum;
}

int sumColumn(int col, int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][col];
    }
    return sum;
}

int sumDiagonal1(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][i];
    }
    return sum;
}

int sumDiagonal2(int n, int square[][SIZE]) {
    int sum = 0;
    for (int i = 0; i<n; i++) {
        sum += square[i][(n - i) - 1];
    }
    return sum;
}