C ++-此条件语句编写正确吗?还是有一种更有效的书写方式?

时间:2018-08-21 18:10:25

标签: c++ multidimensional-array conditional

我有一个试图找到魔术方块的程序。数字的方阵,其中所有行,列,对角线加起来等于相同的数字。

到目前为止,我有一个3x3数组,该数组成功填充了真正的随机数。这样做似乎可以正常工作,但是当我将程序包含在while(true)循环中时,该程序将永远运行,而不会找到魔方,这是因为我的条件语句编写不正确。就是这样:

if (row0 == row1 == row2 == col0 == col1 == col2 == dia1 == dia2) {
    cout << "We have a magic square!" <<  endl;
    cout << troysArray[i][j];
    cout << "";
    break;
} 

整个程序在这里:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//Defining the sum function, which takes 3 integers as arguments.
int addnums(int x,int y,int z){
    int result = x + y + z;
    return result;}

int main() {
    srand(time(0));

//Initial array contents before random numbers are substituted
int troysArray[3][3] = {
{1,3,2},
{4,6,5},
{7,9,8},
};

int i;
int j;

int row0;
int zero_zero;
int zero_one;
int zero_two;

int row1;
int one_zero;
int one_one;
int one_two;

int row2;
int two_zero;
int two_one;
int two_two;

int col0;
int col1;
int col2;

int dia1;
int dia2;
int sum = row0;

while(true) {
for (i = 0;i < 3;i++){
    for (j = 0;j < 3;j++){
        //Generating random numbers between 1-9, with which to populate troysArray.
        troysArray[i][j] = 1 + (rand() % 9);
        cout << troysArray[i][j];
        cout << "";
    //If all the rows,columns, and diagonals are equal,we have a magic square!
    if (row1 == sum && row2 == sum && col0 == sum && col1 == sum && col2 == sum &&
        dia1 == sum && dia2 == sum) {
        cout << "We have a magic square!" << endl;
        cout << troysArray[i][j];
        cout << "";
        break;}
    }
    cout << endl;

}
}
//Adding up row 0 (top row):
zero_zero = troysArray[0][0];
zero_one = troysArray[0][1];
zero_two = troysArray[0][2];
row0 = addnums(zero_zero,zero_one,zero_two);
cout << "The sum of row 0 equals: " << zero_zero + zero_one + zero_two << endl;

//Adding up row 1 (middle row):
one_zero = troysArray[1][0];
one_one = troysArray[1][1];
one_two = troysArray[1][2];
row1 = addnums(one_zero,one_one,one_two);
cout << "The sum of row 1 equals: " << one_zero + one_one + one_two << endl;

//Adding up row 2 (bottom row):
two_zero = troysArray[2][0];
two_one = troysArray[2][1];
two_two = troysArray[2][2];
row2 = addnums(two_zero,two_one,two_two);
cout << "The sum of row 2 equals: " << two_zero + two_one + two_two << endl;

cout << "\n";

//Adding up col 0 (Left):
col0 = addnums(zero_zero,one_zero,two_zero);
cout << "The sum of col 0 equals: " << zero_zero + one_zero + two_zero << endl;

//Adding up col 1 (Middle):
col1 = addnums(zero_one,one_one,two_one);
cout << "The sum of col 1 equals: " << zero_one + one_one + two_one << endl;

//Adding up col 2 (Right):
col2 = addnums(zero_two,one_two,two_two);
cout << "The sum of col 2 equals: " << zero_two + one_two + two_two << endl;

cout << "\n";

//Adding up tL-bR diagonal (dia 1):
dia1 = addnums(zero_zero,one_one,two_two);
cout << "The sum of dia 1 equals: " << zero_zero + one_one + two_two << endl;

//Adding up bL-tR diagonal (dia 2):
dia2 = addnums(two_zero,one_one,zero_two);
cout << "The sum of dia 2 equals: " << zero_two + one_one + two_zero << endl;

return 0;
}

1 个答案:

答案 0 :(得分:5)

Equality运算符无法像您认为的那样在C和C ++中工作。

它是从左到右求值的,因此WebFlux变成a == b == c(a == b) == c

解决此问题的正确方法是分别比较每个值:

(true/false) == c
相关问题