2013-02-03 58 views
0

我的代碼:被零除錯誤,當坡度爲0

#include "stdafx.h" 
#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <vector> 
using namespace std; 
typedef pair<int,int> point; 
int numTorres, numEdificios; 
vector<point> towervec; 
vector<point> skyvec; 
typedef vector<point>::const_iterator tower_iter; 
typedef vector<point>::const_iterator sky_iter; 

int noofpaths = 0; 

int findslope(const point & i, const point & j, const point & k) { 
    int dx, dy, dx1, dy1; 
    dx = j.first - i.first; 
    dy = j.second - i.second; 

    int a = dy/dx; 
    dx1 = k.first - i.first; 
    dy1 = k.second - i.second; 

    int b = dy1/dx1; 

    if(a != b) { 


     int length1 = (j.first - i.first) * (j.first - i.first) + (j.second - i.second) * (j.second - i.second); 
     int length2 = (k.first - i.first) * (k.first - i.first) + (k.second - i.second) * (k.second - i.second); 
     if(length1 < length2) { 
      noofpaths++; 
     } 
    } 
    return noofpaths; 
} 

int main() { 
    int T; 
    int n, m; 
    cout << "enter the test cases" << endl; 
    cin >> T; 
    while(T--) { 
     cout << "towers and skyscrapers" << endl; 
     cin >> n >> m; 


     for(int i = 0; i < n; i++) { 
      point p; 
      cin >> p.first >> p.second; 
      towervec.push_back(p); 
      skyvec.push_back(p); 

     } 
     for(int i = 0; i < m; i++) { 
      point p; 
      cin >> p.first >> p.second; 
      skyvec.push_back(p); 
     } 



     for(tower_iter i = towervec.begin(); i != towervec.end(); i++) { 
      for(tower_iter j = i + 1; j != towervec.end(); j++) { 
       for(sky_iter k = skyvec.begin(); k != skyvec.end(); k++) { 

        int dx, dy; 

        noofpaths = findslope(*i, *j, *k); 
        cout << noofpaths; 
       } 
      } 
     } 
    } 
    return 0; 
} 

如何零錯誤克服鴻溝。如果dx = 0或者dy = 0或者兩者都等於零,代碼就會中斷。

如何解決這個錯誤..例如取點 i = -1,-1 j = 1,-1,k-1,-1。試圖找出3點是否共線。

+1

我不得不說,這個排名相當靠前間 「創意格式化」 ...... – Mysticial

+0

只需添加一個檢查'dy'爲'0 '或者確保'dy'永遠不能是'0'。 –

回答

3

設置是否檢查變量是否爲0的條件,從而將divide語句的變量設置爲0。

0

使用叉積,對於平行向量它是零。

#include <iostream> 
#include <utility> 

using namespace std; 

typedef pair<int,int> point; 

int SegmentsAreParallel(const point & i, const point & j, const point & k) 
{ 
    int dx1, dy1, dx2, dy2; 

    dx1 = j.first - i.first; 
    dy1 = j.second - i.second; 

    dx2 = k.first - i.first; 
    dy2 = k.second - i.second; 

    return dx1 * dy2 - dy1 * dx2 == 0; 
} 

int main() 
{ 
    point p1(0, 0); 
    point p2(1, 1); 
    point p3(2, 2); 
    point p4(-1, -1); 
    point p5(1, -1); 
    point p6(0, 10); 
    point p7(0, 100); 
    point p8(10, 0); 
    point p9(100, 0); 

    cout << SegmentsAreParallel(p1, p2, p3) << endl; 
    cout << SegmentsAreParallel(p1, p2, p4) << endl; 
    cout << SegmentsAreParallel(p1, p2, p5) << endl; 
    cout << SegmentsAreParallel(p1, p6, p7) << endl; 
    cout << SegmentsAreParallel(p1, p8, p9) << endl; 
    cout << SegmentsAreParallel(p1, p6, p9) << endl; 

    return 0; 
} 

輸出(ideone):

1 
1 
0 
1 
1 
0