出于某种原因,何时
SIZEOF_A = 6 SIZEOF_B = 4 set A = {0, 1, 2, 3, 4, 5, 6} set B = {1, 10, 11, 12}
交叉点都很棘手(1048832
)。
这是为什么?
#include "usefunc.h" /* #define SIZEOF_A 2 #define SIZEOF_B 4 */ typedef enum { a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z } set; void dispSet(set numbers[], int size_numbers) { int i; printf("[ "); for (i = 0; i < size_numbers-1; i++) { printf("%d, ", numbers[i]); } printf("%d ]", numbers[size_numbers-1]); printf("\n"); } int in_arr(int A, set B[], int B_size) { int res = 0; int counter; for (counter = 0; counter < B_size; counter++) { if (A == B[counter]) { res = 1; break; } } return res; } int arr_in_arr(set smaller[], int smaller_size, set bigger[], int bigger_size) { int res = 1; int counter; for (counter = 0; counter < smaller_size; counter++) { if (in_arr(smaller[counter], bigger, bigger_size)) { continue; } else { res = 0; break; } } return res; } int size_c(set arr1[], int arr1_size, set arr2[], int arr2_size) { int i; int newsize = 0; for (i = 0; i < arr1_size; i++) { if (!in_arr(arr1[i], arr2, arr2_size)) { newsize++; } } for (i = 0; i < arr2_size; i++) newsize++; return newsize; } int size_d(set arr1[], int arr1_size, set arr2[], int arr2_size) { int i; int SIZEOF_D = 0; for (i = 0; i < arr1_size; i++) if (in_arr(arr1[i], arr2, arr2_size)) SIZEOF_D++; return SIZEOF_D; } int Join(set arr1[], int arr1_size, set arr2[], int arr2_size, set arr3[], int arr3_size) { int i, j = 0; for (i = 0; i < arr1_size; i++) arr3[i] = arr1[i]; for (i = 0; i < arr2_size; i++) { if (!in_arr(arr2[i], arr3, arr3_size)) { arr3[j+arr1_size] = arr2[i]; j++; } } } int Intersection(set arr1[], int arr1_size, set arr2[], int arr2_size, set arr3[], int arr3_size) { int i, j = 0; for (i = 0; i < arr1_size; i++) { if (in_arr(arr1[i], arr2, arr2_size)) arr3[j] = arr1[i]; j++; } } int getSet(set arr[], int size) { int i; printf("Starting with a=0, enter a number that corresponds to a letter.\n"); for (i = 0; i < size; i++) { printf("NUM: "); arr[i] = GetInteger(); } } int main () { printf("size of a? "); int SIZEOF_A = GetInteger(); printf("size of b? "); int SIZEOF_B = GetInteger(); set A[SIZEOF_A]; printf("\nA:\n"); getSet(A, SIZEOF_A); set B[SIZEOF_B]; printf("B:\n"); getSet(B, SIZEOF_B); int SIZEOF_C = size_c(A, SIZEOF_A, B, SIZEOF_B); int SIZEOF_D = size_d(A, SIZEOF_A, B, SIZEOF_B); printf("size of D: %d", SIZEOF_D); set C[SIZEOF_C]; set D[SIZEOF_D]; Join(A, SIZEOF_A, B, SIZEOF_B, C, SIZEOF_C); Intersection(A, SIZEOF_A, B, SIZEOF_B, D, SIZEOF_D); printf("\nFor the sets\n"); printf("A: "); dispSet(A, SIZEOF_A); printf("B: "); dispSet(B, SIZEOF_B); printf("\n"); printf("union A,B: "); dispSet(C, SIZEOF_C); printf("intersection A,B: "); dispSet(D, SIZEOF_D); printf("%s\n\n", (arr_in_arr(A, SIZEOF_A, B, SIZEOF_B) == 1)?"B contains A":"B does not contain A"); }
答案 0 :(得分:2)
在交点中,您的if语句应为:
if (in_arr(arr1[i], arr2, arr2_size)) {
arr3[j] = arr1[i];
j++;
}
除非找到匹配项,否则不要增加j。