简体   繁体   中英

How do i set x to be a negative value if x is positive, and if x is positive how do i set it to be negative. c++

I am making an AI/algorithm to find a checker pattern in a 2x2 square. I need them to be negative for hidden_1 and hidden_3, but positive for the other 2.

#include <iostream>
#include <math.h>
#include <string.h>  
    
using namespace std;
    
int main (){
    int x;
    int y;
    int a;
    int b;
    int x1 = -x;
    int y1 = -y;
    int a1 = -a;
    int b1 = -b;
    cout << "Is there a square in the top left?\n1 for Yes/-1 for No\n";
    cin >> x;
    cout << "Is there a square in the bottom right?\n1 for Yes/-1 for No\n";
    cin >> y;
    cout << "Is there a square in the bottom left?\n1 for Yes/-1 for No\n";
    cin >> a;
    cout << "Is there a square in the top right?\n1 for Yes/-1 for No\n";
    cin >> b;


    int hidden_1 << x1 + y1 + -1;
    int hidden_2 << x + y + -1;
    int hidden_3 << a1 + b1 + -1;
    int hidden_4 << a + b + -1;

You initialised x1, y1, a1, b1 with negatives of variables that weren't read. Try declare them after you read x, y, a, b.

#include <iostream>
#include <math.h>
#include <string.h>  

using namespace std;

int main () {
  int x;
  int y;
  int a;
  int b;

  cout << "Is there a square in the top left?\n1 for Yes/-1 for No\n";
  cin >> x;
  cout << "Is there a square in the bottom right?\n1 for Yes/-1 for No\n";
  cin >> y;
  cout << "Is there a square in the bottom left?\n1 for Yes/-1 for No\n";
  cin >> a;
  cout << "Is there a square in the top right?\n1 for Yes/-1 for No\n";
  cin >> b;

  int x1 = -x;
  int y1 = -y;
  int a1 = -a;
  int b1 = -b;

  int hidden_1 = x1 + y1 + -1;
  int hidden_2 = x + y + -1;
  int hidden_3 = a1 + b1 + -1;
  int hidden_4 = a + b + -1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM