代码在这里
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main ()
{
cout << 1 + -4 << "\n";
signed int x1; //(x1, ...
signed int y1; //(x1, y1)...
signed int x2; //... (x2, ...
signed int y2; // ... (x2, y2)
signed int ans1;
signed int ans2;
signed int ans3;
signed int result;
cout << "X1: ";
cin >> x1;
cout << "\nY1: ";
cin >> y1;
cout << "\nX2: ";
cin >> x2;
cout << "\nY2: ";
cin >> y2;
cout << "\n(" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << ")\n";
result = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1);
cout << "X2 - X1 = " << x2 - x1 << "\n";
cout << "Y2 - Y1 = " << y2 - y1 << "\n";
ans1 = x2 - x1;
ans2 = y2 - y1;
ans3 = ans1 + ans2;
cout << ans1 << " + " << ans2 << " = " << ans3;
cout << result << "\n";
return 0;
}
我一直在尝试为一个简单的方程制作一个求解器。简单来说,它是 (x2 - x1)(squared) + (y2 - y1)(squared) - 因为它们是不同的数字 - 想想坐标。(4, 3), (6, 2)
问题是,例如,当我输入 8, 6, 9, 2 (结果是 (8, 6), (9, 2) 时,它得出的答案比我在纸上解决时的错误。我继续按顺序将其 cout << 步骤,它说 1 + -4 是 -317。我很困惑,因为这当然离正确答案还很远。那么有什么问题呢?它确实打破了当我从开头的整数中删除有符号时。
我正在使用运行 Windows 8 的 Visual Studio Express 2012。