C++ - 这个条件语句写得正确吗?还是有更有效的写法?

C++ - Is this conditional statement written correctly? Or is there a more effective way to write it?

本文关键字:有效 条件 语句 C++      更新时间:2023-10-16

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

到目前为止,我有一个 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;
}

相等运算符在 C 中不起作用,并且C++您认为的方式。

它是从左到右评估的,因此a == b == c变得(a == b) == c(true/false) == c

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

int sum = row0;
if (row1 == sum && row2 == sum && ...) { ... }

更新:

您可以像这样创建自己的多值比较器:

template <class Arg, class... Args>
bool all_equal(Arg&& arg, Args&&... args) {
static_assert(sizeof...(args), "At least 2 arguments expected");
return (arg == args && ...);
}
// Example
if (all_equal(row0, row1, ...)) {}