// 函式1, 判斷兩個點是否在一條水平線上。BOOL isLevel(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x == point[i + 1].x) && (point[i].y != point[i + 1].y)) { return YES; } i++; } return NO;}// 函式2,判斷兩個點是否在一條垂直線上。BOOL isVertical(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x != point[i + 1].x) && (point[i].y == point[i + 1].y)) { return YES; } i++; } return NO;}// 函式3,判斷兩個點是否相等BOOL isEqual(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x == point[i + 1].x) && (point[i].y == point[i + 1].y)) { return YES; } i++; } return NO; }在main函式中的呼叫為: MyPoint point1, point2, point[2] = {point1, point2}; // 這裡是定義的結構體變數和結構體陣列 for (int i = 0; i < 2; i++) { point[i].x = arc4random() % 11; // 產生隨機數 point[i].y = arc4random() % 11; printf("point[%d].x = %d, point[%d].y = %d\n", i, i, point[i].x, point[i].y); } if (isLevel(point, 2) == YES) { printf("這兩個點在一條水平線上\n"); } else { printf("這兩個點不在一條水平線上\n"); } if (isVertical(point, 2) == YES) { printf("這兩個點在一條垂直線上\n"); } else { printf("這兩個點不在一條垂直線上\n"); } if (isEqual(point, 2) == YES) { printf("這兩個點相等\n"); } else { printf("這兩個點不相等\n"); }
// 函式1, 判斷兩個點是否在一條水平線上。BOOL isLevel(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x == point[i + 1].x) && (point[i].y != point[i + 1].y)) { return YES; } i++; } return NO;}// 函式2,判斷兩個點是否在一條垂直線上。BOOL isVertical(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x != point[i + 1].x) && (point[i].y == point[i + 1].y)) { return YES; } i++; } return NO;}// 函式3,判斷兩個點是否相等BOOL isEqual(MyPoint point[], int n) { int i = 0; while (i < n) { if ((point[i].x == point[i + 1].x) && (point[i].y == point[i + 1].y)) { return YES; } i++; } return NO; }在main函式中的呼叫為: MyPoint point1, point2, point[2] = {point1, point2}; // 這裡是定義的結構體變數和結構體陣列 for (int i = 0; i < 2; i++) { point[i].x = arc4random() % 11; // 產生隨機數 point[i].y = arc4random() % 11; printf("point[%d].x = %d, point[%d].y = %d\n", i, i, point[i].x, point[i].y); } if (isLevel(point, 2) == YES) { printf("這兩個點在一條水平線上\n"); } else { printf("這兩個點不在一條水平線上\n"); } if (isVertical(point, 2) == YES) { printf("這兩個點在一條垂直線上\n"); } else { printf("這兩個點不在一條垂直線上\n"); } if (isEqual(point, 2) == YES) { printf("這兩個點相等\n"); } else { printf("這兩個點不相等\n"); }