// Copyright 2011 #include #include #include "./Intersect.h" // ___________________________________________________________________________ int parseSetOfIntegers(const char* str, int integers[MAX_LEN]) { int i = 0; // amount of integers written to integers[MAX_LEN] if (str[0] != '{') return 0; const char* p = str+1; // while i < MAX_LEN and *p between '0' and '9' do while (( i < MAX_LEN ) && ( *p < 58 ) && ( *p > 47) ) { // parse (i+1)-th integer to target-array and increase i integers[i] = atoi(p); i++; // go to the next komma and increase p to point to the next digit // (will break if the input is not in the specified format) while ( 47 < *p && *p < 58 ) p++; if (*p != ',') break; p++; } if ( *p == '}' ) return i; return 0; } // ___________________________________________________________________________ int intersectTwoSetsOfIntegers( int setA[MAX_LEN], int setB[MAX_LEN], int setALength, int setBLength, int results[MAX_LEN]) { int n = 0; // amount of integers written to results[] for (int i = 0; i < setALength; i++) { for (int j = 0; j < setBLength; j++) { if (setA[i] == setB[j]) { results[n] = setA[i]; n++; } } } return n; }