/*---------------------------------------------------------------------- This recurses through all dimesnions, thereby skipping the summed one. At the end of the rescusion the actual summing is performed. ----------------------------------------------------------------------*/ static void sumData(pNXDS source, pNXDS target, int sourceDim[], int targetDim[], int targetDimCount, int dimNo, int start, int end, int currentDim){ int i, length; double val, sumVal; /* when we have recursed through all dimensions we actually do the sums... */ if(currentDim == source->rank){ length = end - start; sumVal = getNXDatasetValue(target, targetDim); for(i = 0; i < length; i++){ sourceDim[dimNo] = start + i; val = getNXDatasetValue(source,sourceDim); sumVal += val; } putNXDatasetValue(target, targetDim, sumVal); } else { /* jump over the summed dimension while recursing through the dimensions */ if(currentDim == dimNo){ sumData(source,target,sourceDim, targetDim,targetDimCount, dimNo,start,end,currentDim+1); } else { /* loop over all values of the non summed dimension */ for(i = 0; i < source->dim[currentDim]; i++){ /* the problem here is that we have to jump over the summed dimension here. This why we have to maintain a separate dimension count for the target array. Jumping is done above. */ targetDim[targetDimCount] = i; targetDimCount++; sourceDim[currentDim] = i; sumData(source,target,sourceDim,targetDim,targetDimCount, dimNo,start,end,currentDim+1); targetDimCount--; } } } }
/*-----------------------------------------------------------------------*/ pNXDS sumNXDataset(pNXDS source, int dimNo, int start, int end){ int newDim[NX_MAXRANK], targetDim[NX_MAXRANK], sourceDim[NX_MAXRANK]; pNXDS result = NULL; int i, count; if(dimNo < 0 || dimNo > source->rank-1) { fprintf(stderr,"ERROR: invalid dimension for summing requested"); return NULL; } /* make result dataset with missing summed dimension */ for(i = 0, count = 0; i < source->rank; i++){ if(i != dimNo){ newDim[count] = source->dim[i]; count++; } } result = createNXDataset(source->rank-1, source->type, newDim); if(result == NULL){ fprintf(stderr,"ERROR: out of memory creating result dataset"); return NULL; } sumData(source,result,sourceDim,targetDim,0, dimNo, start, end, 0); return result; }
void main(void) { int i, total, sumData(int); for (i = 2; i < 11; i++) { total = sumData(i); printf(" add (1 to %d)\t=>\t%d\n", i, total); } }
/*----------------------------------------------------------------------------*/ bool KsefBillItem :: assign(QDomElement& element) { mN = element.attribute("N").toInt(); switch(type()) { case Sale: { SaleData *data = saleData(); data->C = element.attribute("C").toInt(); data->CD = element.attribute("CD"); data->NM = element.attribute("NM"); data->PRC = element.attribute("PRC").toInt(); data->Q = element.attribute("Q").toInt(); data->RT = element.attribute("RT").toInt(); data->SM = element.attribute("SM").toInt(); data->TX = element.attribute("TX").toInt(); data->TX2 = element.attribute("TX2").toInt(); break; } case Comment: { CommentData *data = commentData(); data->TXT = element.text(); break; } case Discount: { DiscountData *data = discountData(); data->PR = element.attribute("PR").toInt(); data->S = element.tagName() == "M"; data->SM = element.attribute("SM").toInt(); data->ST = element.attribute("ST").toInt(); data->TR = element.attribute("TR").toInt(); data->TY = element.attribute("TY").toInt(); break; } case Sum: { SumData *data = sumData(); data->CS = element.attribute("CS").toInt(); data->NO = element.attribute("NO").toInt(); data->SE = element.attribute("SE").toInt(); data->SM = element.attribute("SM").toInt(); //YYYYMMDDhhmmss QString t = element.attribute("TS"); if(t.length() == 14) { int Y; int M; int D; int h, m, s; Y = t.left(4).toInt(); M = t.mid(4, 2).toInt(); D = t.mid(6, 2).toInt(); h = t.mid(8, 2).toInt(); m = t.mid(10, 2).toInt(); s = t.mid(12, 2).toInt(); data->TS.setDate(QDate(Y, M, D)); data->TS.setTime(QTime(h, m, s, 0)); } break; } case Tax: { TaxData *data = taxData(); data->DTPR = element.attribute("DTPR").toInt(); data->DTSM = element.attribute("DTSM").toInt(); data->TX = element.attribute("TX").toInt(); data->TXAL = element.attribute("TXAL").toInt(); data->TXPR = element.attribute("TXPR").toInt(); data->TXSM = element.attribute("TXSM").toInt(); data->TXTY = element.attribute("TXTY").toInt(); break; } case SaleFuel: { SaleFuelData *data = saleFuelData(); data->C = element.attribute("C").toInt(); data->CD = element.attribute("CD"); data->KRK = element.attribute("KRK").toInt(); data->NM = element.attribute("NM"); data->OV = element.attribute("OV"); data->PRC = element.attribute("PRC").toInt(); data->PRK = element.attribute("PRK").toInt(); data->Q = element.attribute("Q").toInt(); data->SM = element.attribute("SM").toInt(); data->TX = element.attribute("TX").toInt(); data->TX2 = element.attribute("TX2").toInt(); break; } case Payment: { PaymentData *data = paymentData(); data->NM = element.attribute("NM"); data->RM = element.attribute("RM").toInt(); data->RRN = element.attribute("RRN"); data->SM = element.attribute("SM").toInt(); data->T = element.attribute("T").toInt(); break; } case Receive: { ReseiveData *data = reseiveData(); data->O = element.text() == "O"; data->SM = element.attribute("SM").toInt(); break; } case Deliver: { DeliverData *data = deliverData(); data->C = element.attribute("C").toInt(); data->NM = element.attribute("NM"); data->NR = element.attribute("NR").toInt(); data->OV = element.attribute("OV"); data->Q = element.attribute("Q").toInt(); break; } case PumpCheck: { PumpCheckData *data = pumpCheckData(); data->C = element.attribute("C").toInt(); data->KRK = element.attribute("KRK").toInt(); data->NM = element.attribute("NM"); data->OV = element.attribute("OV"); data->PRK = element.attribute("PRK").toInt(); data->Q = element.attribute("Q").toInt(); break; } case ZTax: { ZTaxData *data = zTaxData(); data->DTI = element.attribute("DTI").toInt(); data->DTO = element.attribute("DTO").toInt(); data->DTPR = element.attribute("DTPR").toInt(); data->SMI = element.attribute("SMI").toInt(); data->SMO = element.attribute("SMO").toInt(); data->TX = element.attribute("TX").toInt(); data->TXAL = element.attribute("TXAL").toInt(); data->TXI = element.attribute("TXI").toInt(); data->TXO = element.attribute("TXO").toInt(); data->TXPR = element.attribute("TXPR").toInt(); data->TXTY = element.attribute("TXTY").toInt(); //YYYYMMDD QString t = element.attribute("TS"); if(t.length() == 8) { int Y; int M; int D; Y = t.left(4).toInt(); M = t.mid(4, 2).toInt(); D = t.mid(6, 2).toInt(); data->TS = QDate(Y, M, D); } break; } case ZReceive: { ZReseiveData *data = zReseiveData(); data->SMI = element.attribute("SMI").toInt(); data->SMO = element.attribute("SMO").toInt(); break; } case ZBillCount: { ZBillCountData *data = zBillCountData(); data->NI = element.attribute("NI").toInt(); data->NO = element.attribute("NO").toInt(); break; } case ZPayment: { ZPaymentData *data = zPaymentData(); data->NM = element.attribute("NM"); data->SMI = element.attribute("SMI").toInt(); data->SMO = element.attribute("SMO").toInt(); data->T = element.attribute("T").toInt(); break; } case ZFuel: { ZFuelData *data = zFuelData(); data->C = element.attribute("C").toInt(); data->IF = element.attribute("IF").toInt(); data->NM = element.attribute("NM"); data->OF = element.attribute("OF").toInt(); data->OFP = element.attribute("OFP").toInt(); data->SF = element.attribute("SF").toInt(); data->SMI = element.attribute("SMI").toInt(); break; } case ZTank: { ZTankData *data = zTankData(); data->C = element.attribute("C").toInt(); data->NR = element.attribute("NR").toInt(); data->SF = element.attribute("SF").toInt(); break; } case ZFPayment: { ZFPaymentData *data = zFPaymentData(); data->C = element.attribute("C").toInt(); data->NM = element.attribute("NM"); data->OF = element.attribute("OF").toInt(); data->SM = element.attribute("SM").toInt(); data->T = element.attribute("T").toInt(); break; } case ZPump: { ZPumpData *data = zPumpData(); data->NP = element.attribute("NP").toInt(); data->NR = element.attribute("NR").toInt(); break; } default: return false; } return true; }