waterlubber

Untitled

Oct 5th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.     bool verbose = false;
  9.     if(argv[0][0] == '-' && argv[0][1] == 'v') {
  10.         verbose = true;
  11.     }
  12.    // verbose=true;
  13.     unsigned short int a=0, b=0, c=0, d=0; //a,b,c,d, our two ratios are a/b and c/d
  14.     unsigned short int best[4] = {}; //best values for these guys
  15.     unsigned short int max = 0; //maximum value to use
  16.     unsigned short int min = 0; //minimum value to use
  17.     double ratioA = 0; //first ratio (A:B)
  18.     double ratioC = 0; //second ratio (C:D)
  19.     double ratio = 0; //final ratio
  20.     double goal = 0; //goal ratio
  21.     double powerR = 0; //target gear ratio from input to output
  22.     double distance = 0; //distance from goal
  23.     double powerDistance = 0; //distance of gear ratio to ideal gear ratio
  24.     double powerRatio = 0; //stored power ratio that was good
  25.     double bestRatio = 0; //best ratio
  26.     double bestDistance = 99999999999; //record distance from goal
  27.     double bestPowerDistance = 0; //record power distance from goal
  28.     double bestPowerRatio = 0;
  29.     double powerWeight = 0;
  30.     double ratioWeight = 0;
  31.     double score = 0; //used when working with power ratios
  32.     //user input time
  33.     do {
  34.         printf("Enter Goal Ratio (>1): ");
  35.         scanf("%lf",&goal);
  36.     }while(goal<=1);
  37.     do {
  38.         printf("Enter Target Gear Ratio or 0: ");
  39.         scanf("%lf", &powerR);
  40.     }while(powerR < 0);
  41.     if(powerR) {
  42.         do {
  43.             printf("Enter weighting for turn ratio: ");
  44.             scanf("%lf",&ratioWeight);
  45.         }while(ratioWeight <= 0);
  46.         powerWeight = 1-ratioWeight;
  47.     }
  48.     do {
  49.         printf("Enter min value: ");
  50.         scanf("%hu", &min);
  51.     } while(min <= 0);
  52.     do {
  53.         printf("Enter max value: ");
  54.         scanf("%hu", &max);
  55.     } while(max <= min);
  56.     printf("Min: %hu Max: %hu Goal: %lf\n",min,max,goal);
  57.    
  58.     //time for big time nested for loops
  59.     for(a = min; a <= max; a++ ) { //for every value of a...
  60.         for(b = min; b <= max; b++) {//create a value of b, forming our first ratio
  61.             for(c = min; c<= max; c++) {//for every first ratio, create another c...
  62.                 for(d = min; d <= max; d++) {//and finally ds for every c. note that this program is O(n^4), so oof basically
  63.                     //okay, so now we have two fractions
  64.                     ratioA = (float)a/b;
  65.                     ratioC = (float)c/d;
  66.                     ratio = ratioA/ratioC;
  67.                     distance = ratio - goal; //let's compare it and see how it works
  68.                    
  69.                     //The following block will find the best overall gear ratio this thing presents
  70.                     if (powerR) { //if power ratio mode is enabled
  71.                         if(fabs(powerR-ratioA) <= fabs(powerR-ratioC)) { //if ratioA is closer than ratioB
  72.                             powerRatio = ratioA;
  73.                             powerDistance = ratioA-powerR;
  74.                         }
  75.                         else {
  76.                             powerRatio = ratioC;
  77.                             powerDistance = ratioC-powerR;
  78.                         }
  79.                     }
  80.  
  81.                     if(verbose) {
  82.                         if(powerR) {
  83.                             printf("Ratio: %hu:%hu over %hu:%hu with value %lf and distance %+lf\n",a,b,c,d,ratio,distance);
  84.                             printf("Power Ratio Value: %lf with distance %+lf\n",powerRatio,powerDistance);
  85.                         }                        
  86.                         else printf("Ratio: %hu:%hu over %hu:%hu with value %lf and distance %+lf\n",a,b,c,d,ratio,distance);
  87.                     }
  88.                     if(powerR) {
  89.                         score = fabs(powerWeight * powerDistance) + fabs(ratioWeight * distance); //weight these appropriately
  90.                         if(score < bestDistance) {
  91.                             bestDistance = score; //this sneaky trick allows me to reuse a variable
  92.                             bestRatio = ratio;
  93.                             bestPowerDistance = powerDistance;
  94.                             bestPowerRatio = powerRatio;
  95.                             best[0] = a;
  96.                             best[1] = b;
  97.                             best[2] = c;
  98.                             best[3] = d;
  99.                             printf("NEW BEST: %hu:%hu over %hu:%hu, Score: %.0lf\n",best[0],best[1],best[2],best[3],(1/score));
  100.                             printf("Value: %lf\tDistance: %+lf\tPRatio: %lf Dist: %+lf\n",bestRatio,bestDistance,powerRatio,powerDistance);
  101.                         }
  102.                     }
  103.                     else if(fabs(distance) < fabs(bestDistance)) { //we have a new record
  104.                         bestDistance = distance;
  105.                         bestRatio = ratio;
  106.                         best[0] = a;
  107.                         best[1] = b;
  108.                         best[2] = c;
  109.                         best[3] = d;
  110.                         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);
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     printf("BEST: %hu:%hu over %hu:%hu with value %.10lf and distance %+.10lf\n",best[0],best[1],best[2],best[3],bestRatio,bestDistance);
  117.     if(powerR) printf("POWER RATIO: %lf with distance %+lf\n",bestPowerRatio,bestPowerDistance);
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment