int rncSearch(RationalNumberCollection *c, RationalNumber rn, int min, int max) { // Zeiger auf Collection RationalNumber (*ptr)[2] = c->rn; // Wenn Collection leer if (max < min) { return -1; } // Mitte der Collection ermitteln int mid = (min + max) / 2; // Wenn rn in der unteren Teil-Collection if (rnLessThan(rn,ptr[mid][0])) { return rncSearch(*(&c), rn, min, mid-1); } // Wenn rn in der oberen Teil-Collection else if (rnLessThan(ptr[mid][0],rn)) { return rncSearch(*(&c), rn, mid+1, max); } // rn wurde gefunden else { return mid; } }
int bin_search(RationalNumberCollection *c, RationalNumber n){ int mitte; int links = 0; int rechts = c->unique -1; if(c->unique == 0){ return -1; } for(;;){ mitte = ((rechts+links)/2); //bereich wird halbiert if(rechts < links){ return -1; } // n gefunden ? if(rnEqual(c->liste[mitte].bruch,n)){ return mitte; } if(rnLessThan(n,c->liste[mitte].bruch)){ rechts = mitte-1; } else { links = mitte+1; } } return -1; }
int rncFindIndex(RationalNumberCollection* c, RationalNumber n) { int von = 0; int bis = c->nfi-1; while(von <= bis) { int mitte = von + (bis-von) / 2; if(rnLessThan(c->collection[mitte].rn,n)) { von = mitte + 1; } else if(rnLessThan(n,c->collection[mitte].rn)) { bis = mitte - 1; } else { return mitte; } } return von; }
int main() { printf("Performing unit tests for RationalNumber..."); fflush(stdout); /* Part 1 - RationalNumber data type */ RationalNumber n1 = { 3, 4 }, n2 = { 6, 4 }, n3 = { 3, 2 }, n4 = { -9, -6 }, n5 = { 9, -6 }, n6 = { 9, 4 }, n0 = { 0, 4 }, nn = { 4, 0 }; assert( rnIsValid(n0) ); assert( !rnIsValid(nn) ); assert( rnEqual( n2, n3) ); assert( rnEqual( rnAdd(n1,n1), n2) ); assert( rnEqual( n2,n4) ); assert( !rnEqual( n4,n5) ); assert( rnLessThan( n5,n3) ); RationalNumber t1 = rnAdd(n1,n2); RationalNumber t2 = rnDivide(n3,n3); RationalNumber t3 = rnDivide(n2,n2); RationalNumber t4 = rnDivide(n6,n0); assert( rnEqual(t1, n6) ); assert( rnEqual(t2, t3) ); assert( !rnIsValid(t4) ); printf(" successful!\n"); return 0; }
void rncSort(RationalNumberCollection *c, int len) { // Temporäre Collection erstellen RationalNumberCollection temp; rncInit(&temp); // Zeiger auf Collection RationalNumber (*ptr)[2] = c->rn; // Zeiger auf temporäre Collection RationalNumber (*tempptr)[2] = temp.rn; // Variablen zum Speichern von Indexpositionen int tempIndex = 0; int minIndex = 0; // Solange sich Brüche in der Collection c befinden while (minIndex != -1) { minIndex = -1; // Suche kleinsten Bruch aus der Collection c int minCount; for (int i=0; i<len; i++) { if (ptr[i][1].numerator == 0) { continue; } if (minIndex == -1 || rnLessThan(ptr[i][0],ptr[minIndex][0])) { minIndex = i; minCount = ptr[i][1].numerator; } } // Wenn kleinsten Bruch gefunden if (minIndex != -1) { // Übertrage den gefunden kleinsten Bruch und dessen Vorkommen in die Collection temp tempptr[tempIndex][0] = ptr[minIndex][0]; tempptr[tempIndex][1].numerator = minCount; // Erhöhe den totalUniqueCount der Collection temp temp.totalUniqueCount++; // Entferne den gefunden kleinsten Bruch und dessen Vorkommen aus der Collection c ptr[minIndex][0].numerator = 0; ptr[minIndex][0].denominator = 0; ptr[minIndex][1].numerator = 0; // Zeige auf den nächsten Index der Collection temp tempIndex++; } } // Übertrage alle Brüche und ihre Vorkommen der Collection temp in die Collection c int tempLen = rncTotalUniqueCount(&temp); for (int j=0; j<tempLen; j++) { ptr[j][0] = tempptr[j][0]; ptr[j][1].numerator = tempptr[j][1].numerator; } }