2013-02-03 3 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이거나 둘 다 0 인 경우 코드가 손상됩니다.

어떻게이 오류를 해결할 수 있습니까? 예를 들어 점 을 가져 가십시오. i = -1, -1j = 1, -1, k-1, -1. 나는 3 점이 동일 선상인지 찾기 위해 노력하고있다.

+1

내가 말을해야 할 것, 이것은 "창조적 인 형식"사이에서 꽤 높은 거기 위 ... – Mysticial

+0

그냥'0 수표'dy' 추가 ''dy''가 결코''0 '이 될 수 없도록 보장하십시오. –

답변

3

변수가 0인지 확인하고 divide 문을 사용하여 변수를 0으로 설정하는 경우 조건을 설정하십시오.

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