Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- int main(int argc, char **argv)
- {
- bool verbose = false;
- if(argv[0][0] == '-' && argv[0][1] == 'v') {
- verbose = true;
- }
- // verbose=true;
- unsigned short int a=0, b=0, c=0, d=0; //a,b,c,d, our two ratios are a/b and c/d
- unsigned short int best[4] = {}; //best values for these guys
- unsigned short int max = 0; //maximum value to use
- unsigned short int min = 0; //minimum value to use
- double ratioA = 0; //first ratio (A:B)
- double ratioC = 0; //second ratio (C:D)
- double ratio = 0; //final ratio
- double goal = 0; //goal ratio
- double powerR = 0; //target gear ratio from input to output
- double distance = 0; //distance from goal
- double powerDistance = 0; //distance of gear ratio to ideal gear ratio
- double powerRatio = 0; //stored power ratio that was good
- double bestRatio = 0; //best ratio
- double bestDistance = 99999999999; //record distance from goal
- double bestPowerDistance = 0; //record power distance from goal
- double bestPowerRatio = 0;
- double powerWeight = 0;
- double ratioWeight = 0;
- double score = 0; //used when working with power ratios
- //user input time
- do {
- printf("Enter Goal Ratio (>1): ");
- scanf("%lf",&goal);
- }while(goal<=1);
- do {
- printf("Enter Target Gear Ratio or 0: ");
- scanf("%lf", &powerR);
- }while(powerR < 0);
- if(powerR) {
- do {
- printf("Enter weighting for turn ratio: ");
- scanf("%lf",&ratioWeight);
- }while(ratioWeight <= 0);
- powerWeight = 1-ratioWeight;
- }
- do {
- printf("Enter min value: ");
- scanf("%hu", &min);
- } while(min <= 0);
- do {
- printf("Enter max value: ");
- scanf("%hu", &max);
- } while(max <= min);
- printf("Min: %hu Max: %hu Goal: %lf\n",min,max,goal);
- //time for big time nested for loops
- for(a = min; a <= max; a++ ) { //for every value of a...
- for(b = min; b <= max; b++) {//create a value of b, forming our first ratio
- for(c = min; c<= max; c++) {//for every first ratio, create another c...
- for(d = min; d <= max; d++) {//and finally ds for every c. note that this program is O(n^4), so oof basically
- //okay, so now we have two fractions
- ratioA = (float)a/b;
- ratioC = (float)c/d;
- ratio = ratioA/ratioC;
- distance = ratio - goal; //let's compare it and see how it works
- //The following block will find the best overall gear ratio this thing presents
- if (powerR) { //if power ratio mode is enabled
- if(fabs(powerR-ratioA) <= fabs(powerR-ratioC)) { //if ratioA is closer than ratioB
- powerRatio = ratioA;
- powerDistance = ratioA-powerR;
- }
- else {
- powerRatio = ratioC;
- powerDistance = ratioC-powerR;
- }
- }
- if(verbose) {
- if(powerR) {
- printf("Ratio: %hu:%hu over %hu:%hu with value %lf and distance %+lf\n",a,b,c,d,ratio,distance);
- printf("Power Ratio Value: %lf with distance %+lf\n",powerRatio,powerDistance);
- }
- else printf("Ratio: %hu:%hu over %hu:%hu with value %lf and distance %+lf\n",a,b,c,d,ratio,distance);
- }
- if(powerR) {
- score = fabs(powerWeight * powerDistance) + fabs(ratioWeight * distance); //weight these appropriately
- if(score < bestDistance) {
- bestDistance = score; //this sneaky trick allows me to reuse a variable
- bestRatio = ratio;
- bestPowerDistance = powerDistance;
- bestPowerRatio = powerRatio;
- best[0] = a;
- best[1] = b;
- best[2] = c;
- best[3] = d;
- printf("NEW BEST: %hu:%hu over %hu:%hu, Score: %.0lf\n",best[0],best[1],best[2],best[3],(1/score));
- printf("Value: %lf\tDistance: %+lf\tPRatio: %lf Dist: %+lf\n",bestRatio,bestDistance,powerRatio,powerDistance);
- }
- }
- else if(fabs(distance) < fabs(bestDistance)) { //we have a new record
- bestDistance = distance;
- bestRatio = ratio;
- best[0] = a;
- best[1] = b;
- best[2] = c;
- best[3] = d;
- printf("NEW BEST: %hu:%hu over %hu:%hu with value %lf and distance %+lf\n",best[0],best[1],best[2],best[3],bestRatio,bestDistance);
- }
- }
- }
- }
- }
- printf("BEST: %hu:%hu over %hu:%hu with value %.10lf and distance %+.10lf\n",best[0],best[1],best[2],best[3],bestRatio,bestDistance);
- if(powerR) printf("POWER RATIO: %lf with distance %+lf\n",bestPowerRatio,bestPowerDistance);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment