RationalNumberCollection* rncRemove(RationalNumberCollection *c, RationalNumber n) { printf("\n %i/%i will be removed from the storage.\n",n.numerator,n.denominator); int pos = rncFindPosition(c,n); if(pos != -1 && c->collection[pos].count > 1) { c->collection[pos].count--; RationalNumber subtrahend = c->collection[pos].rn; c->rnSum = rnSubtract(c->rnSum,n); c->totalCount--; } else{ if((c->nfi <= c->size/2)) { RationalNumberCollection *cNew = rncUpdateSize(c, false); c = rncDelete(c); c = cNew; } if(pos != -1 && c->collection[pos].count == 1) { for(int i = pos; i < c->nfi-1; i++) { c->collection[i] = c->collection[i+1]; } RationalNumber subtrahend = c->collection[c->nfi-1].rn; c->rnSum = rnSubtract(c->rnSum,n); c->nfi--; c->collection[c->nfi].count = 0; c->collection[c->nfi].rn.numerator = 0; c->collection[c->nfi].rn.denominator = 1; c->totalCount--; c->totalUniqueCount--; }else{ printf("\n %i/%i could not be removed from the storage.\n",n.numerator,n.denominator); } } print(c); return c; }
} END_TEST /********************** Test Subtract Function, complex results *********************/ START_TEST(subtraction_complex_result) { RomanNumber rn1 = newRomanNumber("CL"); RomanNumber rn2 = newRomanNumber("LX"); RomanNumber difference = rnSubtract(rn1, rn2); ck_assert_str_eq("XC", to_string(difference)); ck_assert_int_eq(90, to_a(difference)); } END_TEST
} END_TEST /******************** Test Subtract Function, start simple 10-1=9 *******************/ START_TEST(subtraction_10_minus_1_equals_9_subtractive_result) { RomanNumber rn1 = newRomanNumber("X"); RomanNumber rn2 = newRomanNumber("I"); RomanNumber difference = rnSubtract(rn1, rn2); ck_assert_str_eq("IX", to_string(difference)); ck_assert_int_eq(9, to_a(difference)); } END_TEST
} END_TEST /******************** Test Subtract Function, start simple 10-5=5 *******************/ START_TEST(subtraction_10_minus_5_equals_5) { RomanNumber rn1 = newRomanNumber("X"); RomanNumber rn2 = newRomanNumber("V"); RomanNumber difference = rnSubtract(rn1, rn2); ck_assert_str_eq("V", to_string(difference)); ck_assert_int_eq(5, to_a(difference)); } END_TEST
void rncRemove(RationalNumberCollection *c, RationalNumber rn) { // Wenn rn ungültig -> return if (!rnIsValid(rn)) { return; } // Länge der Collection ermitteln int len = rncTotalUniqueCount(*(&c)); // Wenn Collection leer -> return if (len == 0) { return; } // rn kürzen rn = rnShorten(rn); // Zeiger auf Collection RationalNumber (*ptr)[2] = c->rn; // Falls Bruch bereits vorhanden int i = rncSearch(*(&c), rn, 0, len-1); if (i >= 0) { // Veringere Counter des Bruchs ptr[i][1].numerator--; // Veringere totalCount der Collection c->totalCount--; // Verringere sum der Collection c->sum = rnShorten(rnSubtract(c->sum,rn)); // Wenn Counter anschließend == 0, entferne Bruch aus Collection if (ptr[i][1].numerator == 0) { ptr[i][0].numerator = 0; ptr[i][0].denominator = 0; // Verringere totalUniqueCount der Collection c->totalUniqueCount--; // Collection sortieren wenn Bruch nicht letzter Bruch in der Collection war if (len > 1) { rncSort(*(&c), len); } // Berechne median der Collection rncCalcMedian(*(&c)); } // Berechne average der Collection rncCalcAverage(*(&c)); } }
/* calculates the new values for the members of the given collection, after a rational number was removed */ void rncRemoved(RationalNumberCollection* c, RationalNumber r, bool isZero){ if(isZero){ c->totalUniqueCount--; } c->totalCount--; if(rncTotalCount(c) < 1) { RationalNumber rnZero = { 0,1 }; c->sum = rnZero; c->average = rnZero; } else { c->sum = rnSubtract(c->sum, r); RationalNumber rnDivisor = { c->totalCount, 1 }; c->average = rnDivide(c->sum, rnDivisor); } rncCalcMedian(c); return; }
} END_TEST /******************************************************************************* * Use above API to subtract two roman numbers * ******************************************************************************/ /******************** Test Subtract Function, start simple 2-1=1 *******************/ START_TEST(create_function_to_subtract_two_numbers) { RomanNumber rn1 = newRomanNumber("II"); RomanNumber rn2 = newRomanNumber("I"); RomanNumber difference = rnSubtract(rn1, rn2); ck_assert_str_eq("I", to_string(difference)); ck_assert_int_eq(1, to_a(difference)); } END_TEST
/* reduce the count of the given rational number by one and removes the rational number if the count reaches zero */ void rncRemove(RationalNumberCollection* c, RationalNumber r){ if(!rnIsValid(r) || c->totalUniqueCount==0){ return; }else{ int index = rncExists(c, r); if(index == -1){ return; }else{ RationalNumber rnOne = { 1,1 }; c->rnList[index+1] = rnSubtract(c->rnList[index+1], rnOne); if(c->rnList[index+1].numerator == 0){ rncCleanUp(c,index); rncRemoved(c, r, true); }else{ rncRemoved(c, r, false); } } return; } }