底片不能很好地叠加

Negatives not adding well

本文关键字:叠加 很好 不能      更新时间:2023-10-16

代码在这里

#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)(平方)+ (y2 - y1)(平方)这些是不同的数字,想想坐标。(4,3), (6,2)

问题是,举个例子,当我输入8,6,9,2(结果是(8,6),(9,2)它会得到错误的答案,而不是我在纸上解决它。我把它做得很好<<这些步骤是有顺序的,题目说1 + -4 = -317。我很困惑,因为这当然不是正确答案。那么到底出了什么问题呢?当我从开始的整数中删除符号时,它会断开。

我使用的是Visual Studio Express 2012,运行Windows 8。

我看到了和你一样的东西:http://ideone.com/jaGeXx

1 + -4 = -317

是由代码中的两行创建的:

cout << ans1 << " + " << ans2 << " = " << ans3;
cout << result << "n";
第一个生成

1 + -1 = -3

第二个生成
17

你没有要求任何分隔换行符或空格。所以你的程序很高兴地将它们并排发送到屏幕上。

-3 = -3

(-1)²+ 4²= 17

确保在-3到17之间打印一个换行符