int main(){ char yn[10]; int program_loop = 1; do{ /* *************** ** VARIABLES ** *************** */ char high_deg[6]; //input string; holds the highest degree directly from user char coeffs_list[6*100]; //input string; holds the list of coefficients directly from user int hdnum; //holds the higest degree (int) int loopvar = 1; //if 0; checking loop stops; char * coeff_s; //string to hold each coefficient in the input string int coeffs_c; //counter of the while loop /* ************************** ** HIGHEST DEGREE INPUT ** ************************** */ //loop to inputcheck first input while(loopvar){ high_deg[4] = '\0'; printf("\nEnter the highest degree of the input polynomial: "); fgets(high_deg, 6, stdin); if((high_deg[4]!='\0') && (high_deg[4]!='\n')) while(getchar() != '\n'); if(strchr(high_deg, '.') != NULL){ printf("Error: Degree must be an integer. Try again\n"); continue; } loopvar = isNotNumber(high_deg, &hdnum, 1); } hdnum++; //printf("Number is %d\n", hdnum); int coeffs[hdnum]; //int array of coefficients /* ************************ ** COEFFICIENTS INPUT ** ************************ */ loopvar = 1; //reinitializes loopvar //loop to inputcheck second input while(loopvar){ //gets the numbers; code from http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm coeff_s = NULL; printf("\nEnter %d coefficients starting from the 0th degree.\nSeparate each input from a comma: ", hdnum); fgets(coeffs_list, 6*100, stdin); if(strchr(coeffs_list, '.') != NULL){ printf("Error: Coefficients must be integers. Try again\n"); continue; } //separates string into string with ',' as a delimiter coeff_s = strtok(coeffs_list, ","); coeffs_c = 0; while(coeffs_c < hdnum){ if(coeff_s == NULL){ printf("Error: incomplete coefficients. Try again\n"); break; } else { if(isNotNumber(coeff_s, &coeffs[coeffs_c], 0)){ break; } } coeff_s = strtok(NULL, ","); coeffs_c++; if(coeffs_c >= hdnum){ //at this part, the input getting is finished and validated loopvar = 0; //checks if last coefficient is a zero //apparently you can help the user and consider the 0, and just change the degree /* if(coeffs[coeffs_c-1] == 0){ printf("Error: coefficient of highest degree term can't be 0. Try again\n"); loopvar = 1; } */ if(coeffs[hdnum-1] == 0){ printf("Warning: coefficient of term of highest degree is 0. Program will change the degree of the entire polynomial.\n"); while(coeffs[hdnum-1] == 0){ hdnum--; } } } } } //prints out coeffs for debugging printIntArray(coeffs, hdnum); /* *************************** ** RATIONAL ROOT FINDING ** *************************** */ float zeroes[hdnum]; //list of zeroes int roots_found = 0; //index of zeroes[] to edit fill(zeroes, 0.0, hdnum); //if constant is zero if(coeffs[0] == 0){ int shft; //for shifting the coefficients around while(coeffs[0] == 0){ roots_found++; hdnum--; for(shft = 0; shft < hdnum; shft++){ coeffs[shft] = coeffs[shft+1]; } } printf("Factor: 0\n\n"); } int const_coeff_fc = ((int) sqrt(fabs(coeffs[0]))) * 4; //number of maximum factors of a0. supposedly int const_coeff_f[const_coeff_fc]; //factors of a0 factor(coeffs[0], const_coeff_f, const_coeff_fc); //printIntArray(const_coeff_f, const_coeff_fc); //for checking int highdeg_coeff_ff = 1; int c_ctr = 0; float x,y; printf("Max factors of a0: %d\n", const_coeff_fc); while(highdeg_coeff_ff <= fabs(coeffs[hdnum-1])){ if(coeffs[hdnum-1]%highdeg_coeff_ff == 0){ for(c_ctr=0; c_ctr<const_coeff_fc; c_ctr++){ x = (float) const_coeff_f[c_ctr] / highdeg_coeff_ff; y = polyfxn(coeffs, hdnum, x); /* //Block of output code for debugging printf("***\nx:%lf:\n", x); printf("y:%lf:\n", y); printf("y times 1000000:%lf:\n", y*(1000000)); printf("abs int y times 10-6:%lf:\n", (int) fabs(y*(1000000))); printf("%d / %d\n***\n\n", const_coeff_f[c_ctr], highdeg_coeff_ff); */ if((int) fabs(y*1000000) == 0){ if(!isHere(zeroes, x, roots_found)){ zeroes[roots_found] = x; if(const_coeff_f[c_ctr] % highdeg_coeff_ff != 0) printf("Factor: %d/%d\n\n", const_coeff_f[c_ctr], highdeg_coeff_ff); else printf("Factor: %d\n\n", (int) zeroes[roots_found]); roots_found++; } } } } highdeg_coeff_ff++; //printf("highdeg_coeff_ff:%d:\n", highdeg_coeff_ff); } //end of the root finding loop printf("\n***PRINTING ZEROES FOUND***\n"); printFloatArray(zeroes, roots_found); /* ******************** ** NEW POLYNOMIAL ** ******************** */ do{ printf("Input new polynomial? "); fgets(yn, 10, stdin); if(strcasecmp(yn, "yes\n") == 0){ break; } else if(strcasecmp(yn, "no\n") == 0){ printf("\nTerminating program.\n"); program_loop = 0; break; } else { printf("Please enter only 'yes' or 'no'.\n"); } }while(1); }while(program_loop); }
/** * @brief Prints current engine configuration to human-readable console. */ void printConfiguration(engine_configuration_s *engineConfiguration, engine_configuration2_s *engineConfiguration2) { scheduleMsg(&logger, getConfigurationName(engineConfiguration)); scheduleMsg(&logger, "configurationVersion=%d", getGlobalConfigurationVersion()); for (int k = 0; k < FUEL_LOAD_COUNT; k++) { // print("line %d (%f): ", k, engineConfiguration->fuelKeyBins[k]); // for (int r = 0; r < FUEL_RPM_COUNT; r++) { // print("%f ", engineConfiguration->fuelTable[k][r]); // } // print("\r\n"); } printFloatArray("RPM bin: ", engineConfiguration->fuelRpmBins, FUEL_RPM_COUNT); printFloatArray("Y bin: ", engineConfiguration->fuelLoadBins, FUEL_LOAD_COUNT); printFloatArray("CLT: ", engineConfiguration->cltFuelCorr, CLT_CURVE_SIZE); printFloatArray("CLT bins: ", engineConfiguration->cltFuelCorrBins, CLT_CURVE_SIZE); printFloatArray("IAT: ", engineConfiguration->iatFuelCorr, IAT_CURVE_SIZE); printFloatArray("IAT bins: ", engineConfiguration->iatFuelCorrBins, IAT_CURVE_SIZE); printFloatArray("vBatt: ", engineConfiguration->battInjectorLagCorr, VBAT_INJECTOR_CURVE_SIZE); printFloatArray("vBatt bins: ", engineConfiguration->battInjectorLagCorrBins, VBAT_INJECTOR_CURVE_SIZE); // appendMsgPrefix(&logger); scheduleMsg(&logger, "rpmHardLimit: %d", engineConfiguration->rpmHardLimit); scheduleMsg(&logger, "rpmMultiplier=%f", engineConfiguration->rpmMultiplier); scheduleMsg(&logger, "tpsMin: %d", engineConfiguration->tpsMin); scheduleMsg(&logger, "tpsMax: %d", engineConfiguration->tpsMax); scheduleMsg(&logger, "timingMode: %d", engineConfiguration->timingMode); scheduleMsg(&logger, "fixedModeTiming: %d", (int) engineConfiguration->fixedModeTiming); scheduleMsg(&logger, "ignitionOffset=%f", engineConfiguration->ignitionOffset); scheduleMsg(&logger, "injectionOffset=%f", engineConfiguration->injectionOffset); scheduleMsg(&logger, "crankingChargeAngle=%f", engineConfiguration->crankingChargeAngle); scheduleMsg(&logger, "crankingTimingAngle=%f", engineConfiguration->crankingTimingAngle); scheduleMsg(&logger, "globalTriggerAngleOffset=%f", engineConfiguration->globalTriggerAngleOffset); // scheduleMsg(&logger, "analogChartMode: %d", engineConfiguration->analogChartMode); // scheduleMsg(&logger, "crankingRpm: %d", engineConfiguration->crankingSettings.crankingRpm); scheduleMsg(&logger, "idlePinMode: %d", boardConfiguration->idleValvePinMode); scheduleMsg(&logger, "malfunctionIndicatorPinMode: %d", boardConfiguration->malfunctionIndicatorPinMode); scheduleMsg(&logger, "analogInputDividerCoefficient: %f", engineConfiguration->analogInputDividerCoefficient); scheduleMsg(&logger, "needSecondTriggerInput: %d", engineConfiguration->needSecondTriggerInput); #if EFI_PROD_CODE scheduleMsg(&logger, "idleValvePin: %s", hwPortname(boardConfiguration->idleValvePin)); scheduleMsg(&logger, "fuelPumpPin: mode %d @ %s", boardConfiguration->fuelPumpPinMode, hwPortname(boardConfiguration->fuelPumpPin)); scheduleMsg(&logger, "injectionPins: mode %d", boardConfiguration->injectionPinMode); for (int i = 0; i < engineConfiguration->cylindersCount; i++) { brain_pin_e brainPin = boardConfiguration->injectionPins[i]; scheduleMsg(&logger, "injection %d @ %s", i, hwPortname(brainPin)); } scheduleMsg(&logger, "ignitionPins: mode %d", boardConfiguration->ignitionPinMode); // todo: calculate coils count based on ignition mode for (int i = 0; i < 4; i++) { brain_pin_e brainPin = boardConfiguration->ignitionPins[i]; scheduleMsg(&logger, "ignition %d @ %s", i, hwPortname(brainPin)); } scheduleMsg(&logger, "primary trigger simulator: %s %d", hwPortname(boardConfiguration->triggerSimulatorPins[0]), boardConfiguration->triggerSimulatorPinModes[0]); scheduleMsg(&logger, "secondary trigger simulator: %s %d", hwPortname(boardConfiguration->triggerSimulatorPins[1]), boardConfiguration->triggerSimulatorPinModes[1]); #endif /* EFI_PROD_CODE */ scheduleMsg(&logger, "isInjectionEnabledFlag %d", engineConfiguration2->isInjectionEnabledFlag); // appendPrintf(&logger, DELIMETER); // scheduleLogging(&logger); }