コード例 #1
0
RationalNumberCollection* rncUpdateSize(RationalNumberCollection *rncOld, bool increase){
    int newSize;
    if(increase){
        newSize = rncOld->size*2;
        printf("\nthe capacity will be incresed to the double size.\n");
        RationalNumberCollection* rncNew = rncCreate(newSize);
        rncInit(rncNew, newSize, rncOld->nfi, rncOld->totalCount, rncOld->totalUniqueCount, rncOld->rnSum);
        for( int i = 0; i<rncOld->size; i++){
            rncNew->collection[i].rn.denominator = rncOld->collection[i].rn.denominator;
            rncNew->collection[i].rn.numerator = rncOld->collection[i].rn.numerator;
            rncNew->collection[i].count = rncOld->collection[i].count;
        }
        return rncNew;
    }else{
        newSize = rncOld->size/2;
        printf("\nthe capacity will be decresed to the half size.:%i\n", newSize);
        RationalNumberCollection* rncNew = rncCreate(newSize);
        rncInit(rncNew, newSize, rncOld->nfi, rncOld->totalCount, rncOld->totalUniqueCount, rncOld->rnSum);
        for( int i = 0; i<newSize; i++){
            rncNew->collection[i].rn.denominator = rncOld->collection[i].rn.denominator;
            rncNew->collection[i].rn.numerator = rncOld->collection[i].rn.numerator;
            rncNew->collection[i].count = rncOld->collection[i].count;
        }
        return rncNew;
    }


}
コード例 #2
0
RationalNumberCollection* rncCreate(int size) {
    RationalNumberCollection* rnc = (RationalNumberCollection*) malloc(sizeof(RationalNumberCollection));    
    rnc->collection = (CollectionElement*) malloc(size*sizeof(CollectionElement));
    RationalNumber n = {0,1};
    rncInit(rnc, size, 0, 0, 0, n);
    return rnc;
}
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;
    }

}