Advertisement
Guest User

Metric System or Go Fuck Your Mother

a guest
Jul 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. double smaller(double a, double b)
  8. {
  9.     if (a < b) return a;
  10.     return b;
  11. }
  12.  
  13. double larger(double a, double b)
  14. {
  15.     if (a > b) return a;
  16.     return b;
  17. }
  18.  
  19. double to_meters(double v, string unit)
  20. {
  21.     double sum = 0;
  22.     if (unit == "m") {
  23.         sum += v;
  24.     } else if (unit == "cm") {
  25.         sum = sum + (v/100);
  26.     } else if (unit == "in") {
  27.         sum = sum + ((v * 2.54)/100);
  28.     } else if (unit == "ft") {
  29.         sum = sum + ((v * 12 * 2.54)/100);
  30.     }
  31.     return sum;
  32. }
  33.  
  34. int main()
  35. {
  36.     double last_smallest = -1, last_largest = -1;
  37.     double amount = 0, cm, in, ft, m, total = 0;//, y;
  38.     vector<string> smallest;
  39.     vector<string> largest;
  40.     vector<string> all;
  41.     vector<double> all_in_meters;
  42.     string unit, smallest_s, largest_s;
  43.     cout << "Enter a scale amount and the scales respective unit: " << "\n";
  44.    
  45.     while (cin >> amount >> unit) {
  46.         amount = amount;
  47.         unit = unit;
  48.        
  49.         if (last_smallest == -1 and last_largest == -1) {
  50.             last_smallest, last_largest = amount;
  51.             smallest.push_back( to_string(amount) + unit );
  52.             largest.push_back( to_string(amount) + unit );
  53.         } else {
  54.             if (smaller(last_smallest, amount) < last_smallest) {
  55.                 last_smallest = amount;
  56.                 smallest_s = (to_string(amount) + unit);
  57.                 smallest.push_back( smallest_s );            
  58.             } else if (larger(last_largest, amount) > last_largest) {
  59.                 last_largest = amount;
  60.                 largest_s = (to_string(amount) + unit);
  61.                 largest.push_back( largest_s );          
  62.             }
  63.         }
  64.  
  65.         if (unit == "cm") {
  66.             cm = amount;
  67.             total += to_meters(amount, unit);
  68.             cout << "There are " << cm << "cm in " << amount << "cm\n";
  69.         } else if (unit == "in") {
  70.             in = amount * 2.54;
  71.             total += to_meters(amount, unit);
  72.             cout << "There are " << in << "cm in " << amount << "in\n";
  73.         } else if (unit == "ft") {
  74.             ft = amount * 12;
  75.             total += to_meters(amount, unit);
  76.             cout << "There are " << ft << "in in " << amount << "ft\n";
  77.         } else if (unit == "m") {  
  78.             total += to_meters(amount, unit);
  79.             cm = amount * 100;
  80.             cout << "There are " << cm << "cm in " << amount << "m\n";
  81.         } else {
  82.             cout << "Did not recognize that unit, your request is rejected. Sorry bro...\n";
  83.         }
  84.  
  85.         all.push_back( to_string(amount) + unit );
  86.         all_in_meters.push_back( to_meters(amount, unit) );
  87.     }
  88.     cout << "Your total inputs turned out to be: " << total << " meters.\n";
  89.     if (largest.size() > 0)
  90.         cout << "The largest input value so far: " << largest[largest.size() - 1] << "\n";
  91.     if (smallest.size() > 0)
  92.         cout << "The smallest input value so far: " << smallest[smallest.size() - 1] << "\n";
  93.     if (all.size() > 0)
  94.         cout << "All inputs in original order: \n";
  95.         for (int i=0; i < all.size(); ++i)
  96.             cout << "Input " << i << "\t" << all[i] << "\n";
  97.     if (all_in_meters.size() > 0)
  98.         cout << "All inputs (converted to meters) in sorted order: \n";
  99.         sort( all_in_meters.begin(), all_in_meters.end() );
  100.         for (int i=0; i < all_in_meters.size(); ++i)
  101.             cout << "Input " << i << "\t" << all_in_meters[i] << "\n";
  102.     // while (cin >> x) {
  103.     // }
  104.  
  105.     // cout << "Enter two integers in sequence: " << "\n";
  106.     // while (cin >> x >> y) {
  107.     //  // cout << "Integers: " << x << " " << y << "\n";
  108.     //  if (x == y) {
  109.     //      cout << "The two numbers entered are equal" << "\n";
  110.     //  } else {
  111.     //      if (larger(x,y) - smaller(x, y) <= 1.0/100) {
  112.     //          cout << "The numbers entered are almost equal" << "\n";
  113.     //      } else {
  114.     //          cout << "The smaller value is: " << smaller(x,y) << "\n";
  115.     //          cout << "The larger value is: " << larger(x,y) << "\n";
  116.     //      }
  117.     //  }
  118.     // }
  119.     // for (int a, b; cin>>a>>b;) {
  120.     //  cout << a << " " << b << "\n";
  121.     // }
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement