2013-05-02 23 views
-2

的代碼是在這裏否定不增加以及

#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

+0

請不要吝嗇,這是一個簡單的意外。我的意思是Visual Studio 2012.我也使用C++控制檯應用程序。 – McBroColi 2013-05-02 00:09:12

+1

隨意使用該「編輯」鏈接來解決您的問題。 – 2013-05-02 00:09:53

+0

爲什麼這麼消極? – 2013-05-02 01:37:28

回答

3

-3 -3 。

(-1)-squared加4平方是17

確保您打印-3和17之間的換行符

4

所以我看到你做同樣的事情:http://ideone.com/jaGeXx

1 + -4 = -317

由這些線在代碼創建的:

cout << ans1 << " + " << ans2 << " = " << ans3; 
cout << result << "\n"; 

第一生成

1 + -1 = -3

第二生成

你沒有要求任何分隔換行符或空格。所以你的程序高興地將它們發送到屏幕上,並且彼此相鄰。

+0

我錯過了。我很抱歉,在轉向StackOverflow之前,我應該誠實地看待這一點。 – McBroColi 2013-05-02 00:15:40