Koragg

Time Converter [C++]

Oct 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.23 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////Time Converter/////////////////////////////////////////////////////////////////////////
  2. //http://www.cplusplus.com/forum/beginner/229109/ --> Find if value is inside an array.
  3. //http://softpixel.com/~cwright/programming/datatypes.c.php --> Fix for very huge numbers as input/output.
  4. //Formulas used to calculate everything are from the internet so some may not be 100% accurate.
  5. //https://stackoverflow.com/questions/32980467/validate-input-using-regex --> Check if input is numeric.
  6. //https://stackoverflow.com/questions/30306993/integer-matching-regex-pattern-not-working --> Check if input is 'int'.
  7. //https://stackoverflow.com/questions/7663709/how-can-i-convert-a-stdstring-to-int --> Convert 'string' to 'int'.
  8. //https://stackoverflow.com/questions/17341870/correct-use-of-stdcout-precision-not-printing-trailing-zeros --> Set number of trailing symbols after decimal point.
  9. //https://stackoverflow.com/questions/18041615/keep-looping-until-user-enters-a-blank-line --> Fix for empty input when user presses only the ENTER key on each step.
  10. //https://stackoverflow.com/questions/2310939/remove-last-character-from-c-string --> Remove last char from string.
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <regex>
  15. #include <iomanip>
  16.  
  17. #define N 11
  18.  
  19. using namespace std;
  20.  
  21. int calculate(string, string, long long int, long long int);
  22.  
  23. int main() {
  24.     cout << "This program can convert between the following time measurements: " << endl;
  25.     cout << endl;
  26.     cout << "milliseconds" << endl;
  27.     cout << "seconds" << endl;
  28.     cout << "minutes" << endl;
  29.     cout << "hours" << endl;
  30.     cout << "days" << endl;
  31.     cout << "weeks" << endl;
  32.     cout << "months" << endl;
  33.     cout << "years" << endl;
  34.     cout << "decades" << endl;
  35.     cout << "centuries" << endl;
  36.     cout << "millennia" << endl;
  37.     cout << endl;
  38.     string arr[N] = { "milliseconds", "seconds", "minutes", "hours", "days", "weeks", "months", "years", "decades", "centuries", "millennia" };
  39.     string from = "";
  40.     do
  41.     {
  42.         cout << "Please enter a measurement from the list from which to convert from: ";
  43.         getline(cin, from);
  44.     } while (find(begin(arr), end(arr), from) == end(arr) || from.empty());
  45.     string to;
  46.     do
  47.     {
  48.         cout << "Please enter a measurement from the list to which to convert to: ";
  49.         getline(cin, to);
  50.     } while (find(begin(arr), end(arr), to) == end(arr) || to.empty());
  51.     long long int value = 0;
  52.     string input = "";
  53.     regex regexValueNumber("^(0|[1-9][0-9]*)$");
  54.     bool isValueANumber = regex_match(input, regexValueNumber);
  55.     do
  56.     {
  57.         cout << "Please input an integer value for the measurement: ";
  58.         getline(cin, input);
  59.         isValueANumber = regex_match(input, regexValueNumber);
  60.     } while (!isValueANumber || input.empty());
  61.     value = stoi(input);
  62.     long long int precision = 0;
  63.     string trailingDigits = "";
  64.     regex regexPrecisionNumberInt("^(0|[1-9][0-9]*)$");
  65.     bool isPrecisionAnInt = regex_match(trailingDigits, regexPrecisionNumberInt);
  66.     do
  67.     {
  68.         cout << "Please input an integer value for the precision: ";
  69.         getline(cin, trailingDigits);
  70.         isPrecisionAnInt = regex_match(trailingDigits, regexPrecisionNumberInt);
  71.     } while (!isPrecisionAnInt || trailingDigits.empty());
  72.     precision = stoi(trailingDigits);
  73.     cout << endl;
  74.     cout << "Converting " << value << " from " << from << " to " << to << " with precision " << precision << "." << endl;
  75.     cout << endl;
  76.     calculate(from, to, value, precision);
  77.     system("pause");
  78.  
  79.     return 0;
  80. }
  81.  
  82. int calculate(string from, string to, long long int value, long long int precision) {
  83.     long double result = 0.0;
  84.     string fromText = "";
  85.     string toText = "";
  86.  
  87.     // Milliseconds to everything else.
  88.     if ((from == "milliseconds") && (to == "seconds")) {
  89.         result = value / 1000.0;
  90.     }
  91.     else if ((from == "milliseconds") && (to == "minutes")) {
  92.         result = value / 60000.0;
  93.     }
  94.     else if ((from == "milliseconds") && (to == "hours")) {
  95.         result = value / 3600000.0;
  96.     }
  97.     else if ((from == "milliseconds") && (to == "days")) {
  98.         result = value / 86400000.0;
  99.     }
  100.     else if ((from == "milliseconds") && (to == "weeks")) {
  101.         result = value / 604800000.0;
  102.     }
  103.     else if ((from == "milliseconds") && (to == "months")) {
  104.         result = value / 2629740000.0;
  105.     }
  106.     else if ((from == "milliseconds") && (to == "years")) {
  107.         result = value / 31556900000.0;
  108.     }
  109.     else if ((from == "milliseconds") && (to == "decades")) {
  110.         result = value / 315360000000.0;
  111.     }
  112.     else if ((from == "milliseconds") && (to == "centuries")) {
  113.         result = value / 3153600000000.0;
  114.     }
  115.     else if ((from == "milliseconds") && (to == "millennia")) {
  116.         result = value / 31536000000000.0;
  117.     }
  118.  
  119.     if (from == "milliseconds") {
  120.         if (value == 1) {
  121.             fromText = "millisecond";
  122.         }
  123.         else {
  124.             fromText = "milliseconds";
  125.         }
  126.         if (round(result) == 1.0) {
  127.             if ((to != "millennia") || (to != "centuries")) {
  128.                 toText = to.substr(0, to.length() - 1);
  129.             }
  130.             if (to == "millennia")
  131.             {
  132.                 toText = "millennium";
  133.             }
  134.             if (to == "centuries") {
  135.                 toText = "century";
  136.             }
  137.         }
  138.         else {
  139.             toText = to;
  140.         }
  141.     }
  142.  
  143.     // Seconds to everything else.
  144.     if ((from == "seconds") && (to == "milliseconds")) {
  145.         result = value * 1000.0;
  146.     }
  147.     else if ((from == "seconds") && (to == "minutes")) {
  148.         result = value / 60.0;
  149.     }
  150.     else if ((from == "seconds") && (to == "hours")) {
  151.         result = value / 3600.0;
  152.     }
  153.     else if ((from == "seconds") && (to == "days")) {
  154.         result = value / (24 * 3600.0);
  155.     }
  156.     else if ((from == "seconds") && (to == "weeks")) {
  157.         result = value / 604800.0;
  158.     }
  159.     else if ((from == "seconds") && (to == "months")) {
  160.         result = value / 2629740.0;
  161.     }
  162.     else if ((from == "seconds") && (to == "years")) {
  163.         result = value / 31556900.0;
  164.     }
  165.     else if ((from == "seconds") && (to == "decades")) {
  166.         result = value / 315360000.0;
  167.     }
  168.     else if ((from == "seconds") && (to == "centuries")) {
  169.         result = value / 3153600000.0;
  170.     }
  171.     else if ((from == "seconds") && (to == "millennia")) {
  172.         result = value / 31536000000.0;
  173.     }
  174.  
  175.     if (from == "seconds") {
  176.         if (value == 1) {
  177.             fromText = "second";
  178.         }
  179.         else {
  180.             fromText = "seconds";
  181.         }
  182.         if (round(result) == 1.0) {
  183.             if ((to != "millennia") || (to != "centuries")) {
  184.                 toText = to.substr(0, to.length() - 1);
  185.             }
  186.             if (to == "millennia")
  187.             {
  188.                 toText = "millennium";
  189.             }
  190.             if (to == "centuries") {
  191.                 toText = "century";
  192.             }
  193.         }
  194.         else {
  195.             toText = to;
  196.         }
  197.     }
  198.  
  199.     // Minutes to everything else.
  200.     if ((from == "minutes") && (to == "milliseconds")) {
  201.         result = value * 60000.0;
  202.     }
  203.     else if ((from == "minutes") && (to == "seconds")) {
  204.         result = value / 0.016667;
  205.     }
  206.     else if ((from == "minutes") && (to == "hours")) {
  207.         result = value / 60.0;
  208.     }
  209.     else if ((from == "minutes") && (to == "days")) {
  210.         result = value * 0.000694;
  211.     }
  212.     else if ((from == "minutes") && (to == "weeks")) {
  213.         result = value / 10.080;
  214.     }
  215.     else if ((from == "minutes") && (to == "months")) {
  216.         result = value / 43829.0;
  217.     }
  218.     else if ((from == "minutes") && (to == "years")) {
  219.         result = value / 525948.3;
  220.     }
  221.     else if ((from == "minutes") && (to == "decades")) {
  222.         result = value / 5259487.66;
  223.     }
  224.     else if ((from == "minutes") && (to == "centuries")) {
  225.         result = value / 52594870.66;
  226.     }
  227.     else if ((from == "minutes") && (to == "millennia")) {
  228.         result = value / 525948700.66;
  229.     }
  230.  
  231.     if (from == "minutes") {
  232.         if (value == 1) {
  233.             fromText = "minute";
  234.         }
  235.         else {
  236.             fromText = "minutes";
  237.         }
  238.         if (round(result) == 1.0) {
  239.             if ((to != "millennia") || (to != "centuries")) {
  240.                 toText = to.substr(0, to.length() - 1);
  241.             }
  242.             if (to == "millennia")
  243.             {
  244.                 toText = "millennium";
  245.             }
  246.             if (to == "centuries") {
  247.                 toText = "century";
  248.             }
  249.         }
  250.         else {
  251.             toText = to;
  252.         }
  253.     }
  254.  
  255.     // Hours to everything else.
  256.     if ((from == "hours") && (to == "milliseconds")) {
  257.         result = value * 3600000.0;
  258.     }
  259.     else if ((from == "hours") && (to == "seconds")) {
  260.         result = (value * 3600000) / 1000.0;
  261.     }
  262.     else if ((from == "hours") && (to == "minutes")) {
  263.         result = value * 60.0;
  264.     }
  265.     else if ((from == "hours") && (to == "days")) {
  266.         result = value * 0.041667;
  267.     }
  268.     else if ((from == "hours") && (to == "weeks")) {
  269.         result = value * 0.005952;
  270.     }
  271.     else if ((from == "hours") && (to == "months")) {
  272.         result = value * 0.001369;
  273.     }
  274.     else if ((from == "hours") && (to == "years")) {
  275.         result = value * 0.000114;
  276.     }
  277.     else if ((from == "hours") && (to == "decades")) {
  278.         result = value / 87600.0;
  279.     }
  280.     else if ((from == "hours") && (to == "centuries")) {
  281.         result = value / 876000.0;
  282.     }
  283.     else if ((from == "hours") && (to == "millennia")) {
  284.         result = value / 8760000.0;
  285.     }
  286.  
  287.     if (from == "hours") {
  288.         if (value == 1) {
  289.             fromText = "hour";
  290.         }
  291.         else {
  292.             fromText = "hours";
  293.         }
  294.         if (round(result) == 1.0) {
  295.             if ((to != "millennia") || (to != "centuries")) {
  296.                 toText = to.substr(0, to.length() - 1);
  297.             }
  298.             if (to == "millennia")
  299.             {
  300.                 toText = "millennium";
  301.             }
  302.             if (to == "centuries") {
  303.                 toText = "century";
  304.             }
  305.         }
  306.         else {
  307.             toText = to;
  308.         }
  309.     }
  310.  
  311.     // Days to everything else.
  312.     if ((from == "days") && (to == "milliseconds")) {
  313.         result = value * 86400000.0;
  314.     }
  315.     else if ((from == "days") && (to == "seconds")) {
  316.         result = value * 86400.0;
  317.     }
  318.     else if ((from == "days") && (to == "minutes")) {
  319.         result = value * 1440.0;
  320.     }
  321.     else if ((from == "days") && (to == "hours")) {
  322.         result = value / 0.041667;
  323.     }
  324.     else if ((from == "days") && (to == "weeks")) {
  325.         result = value * 0.142857;
  326.     }
  327.     else if ((from == "days") && (to == "months")) {
  328.         result = value * 0.032855;
  329.     }
  330.     else if ((from == "days") && (to == "years")) {
  331.         result = value * 0.002738;
  332.     }
  333.     else if ((from == "days") && (to == "decades")) {
  334.         result = value / 3652.42;
  335.     }
  336.     else if ((from == "days") && (to == "centuries")) {
  337.         result = value / 36520.42;
  338.     }
  339.     else if ((from == "days") && (to == "millennia")) {
  340.         result = value / 365200.42;
  341.     }
  342.  
  343.     if (from == "days") {
  344.         if (value == 1) {
  345.             fromText = "day";
  346.         }
  347.         else {
  348.             fromText = "days";
  349.         }
  350.         if (round(result) == 1.0) {
  351.             if ((to != "millennia") || (to != "centuries")) {
  352.                 toText = to.substr(0, to.length() - 1);
  353.             }
  354.             if (to == "millennia")
  355.             {
  356.                 toText = "millennium";
  357.             }
  358.             if (to == "centuries") {
  359.                 toText = "century";
  360.             }
  361.         }
  362.         else {
  363.             toText = to;
  364.         }
  365.     }
  366.  
  367.     // Weeks to everything else.
  368.     if ((from == "weeks") && (to == "milliseconds")) {
  369.         result = value * 604800000.0;
  370.     }
  371.     else if ((from == "weeks") && (to == "seconds")) {
  372.         result = value * 604800.0;
  373.     }
  374.     else if ((from == "weeks") && (to == "minutes")) {
  375.         result = value * 10080.0;
  376.     }
  377.     else if ((from == "weeks") && (to == "hours")) {
  378.         result = value * 168.0;
  379.     }
  380.     else if ((from == "weeks") && (to == "days")) {
  381.         result = value * 7.0;
  382.     }
  383.     else if ((from == "weeks") && (to == "months")) {
  384.         result = value * 0.229985;
  385.     }
  386.     else if ((from == "weeks") && (to == "years")) {
  387.         result = value * 0.019165;
  388.     }
  389.     else if ((from == "weeks") && (to == "decades")) {
  390.         result = value / 521.775;
  391.     }
  392.     else if ((from == "weeks") && (to == "centuries")) {
  393.         result = value / 5210.775;
  394.     }
  395.     else if ((from == "weeks") && (to == "millennia")) {
  396.         result = value / 52100.775;
  397.     }
  398.  
  399.     if (from == "weeks") {
  400.         if (value == 1) {
  401.             fromText = "week";
  402.         }
  403.         else {
  404.             fromText = "weeks";
  405.         }
  406.         if (round(result) == 1.0) {
  407.             if ((to != "millennia") || (to != "centuries")) {
  408.                 toText = to.substr(0, to.length() - 1);
  409.             }
  410.             if (to == "millennia")
  411.             {
  412.                 toText = "millennium";
  413.             }
  414.             if (to == "centuries") {
  415.                 toText = "century";
  416.             }
  417.         }
  418.         else {
  419.             toText = to;
  420.         }
  421.     }
  422.  
  423.     // Months to everything else.
  424.     if ((from == "months") && (to == "milliseconds")) {
  425.         result = value * 2629740000.0;
  426.     }
  427.     else if ((from == "months") && (to == "seconds")) {
  428.         result = value * 2629740.0;
  429.     }
  430.     else if ((from == "months") && (to == "minutes")) {
  431.         result = value * 43829.0;
  432.     }
  433.     else if ((from == "months") && (to == "hours")) {
  434.         result = value * 730.483333;
  435.     }
  436.     else if ((from == "months") && (to == "days")) {
  437.         result = value * 30.436806;
  438.     }
  439.     else if ((from == "months") && (to == "weeks")) {
  440.         result = value * 4.348115;
  441.     }
  442.     else if ((from == "months") && (to == "years")) {
  443.         result = value * 0.083333;
  444.     }
  445.     else if ((from == "months") && (to == "decades")) {
  446.         result = value / 120.0;
  447.     }
  448.     else if ((from == "months") && (to == "centuries")) {
  449.         result = value / 1200.0;
  450.     }
  451.     else if ((from == "months") && (to == "millennia")) {
  452.         result = value / 12000.0;
  453.     }
  454.  
  455.     if (from == "months") {
  456.         if (value == 1) {
  457.             fromText = "month";
  458.         }
  459.         else {
  460.             fromText = "months";
  461.         }
  462.         if (round(result) == 1.0) {
  463.             if ((to != "millennia") || (to != "centuries")) {
  464.                 toText = to.substr(0, to.length() - 1);
  465.             }
  466.             if (to == "millennia")
  467.             {
  468.                 toText = "millennium";
  469.             }
  470.             if (to == "centuries") {
  471.                 toText = "century";
  472.             }
  473.         }
  474.         else {
  475.             toText = to;
  476.         }
  477.     }
  478.  
  479.     // Years to everything else.
  480.     if ((from == "years") && (to == "milliseconds")) {
  481.         result = value * 31556900000.0;
  482.     }
  483.     else if ((from == "years") && (to == "seconds")) {
  484.         result = value * 31556900.0;
  485.     }
  486.     else if ((from == "years") && (to == "minutes")) {
  487.         result = value * 525948.333333;
  488.     }
  489.     else if ((from == "years") && (to == "hours")) {
  490.         result = value * 8765.805556;
  491.     }
  492.     else if ((from == "years") && (to == "days")) {
  493.         result = value * 365.241898;
  494.     }
  495.     else if ((from == "years") && (to == "weeks")) {
  496.         result = value * 52.177414;
  497.     }
  498.     else if ((from == "years") && (to == "months")) {
  499.         result = value * 12.000008;
  500.     }
  501.     else if ((from == "years") && (to == "decades")) {
  502.         result = value / 10.0;
  503.     }
  504.     else if ((from == "years") && (to == "centuries")) {
  505.         result = value / 100.0;
  506.     }
  507.     else if ((from == "years") && (to == "millennia")) {
  508.         result = value / 1000.0;
  509.     }
  510.  
  511.     if (from == "years") {
  512.         if (value == 1) {
  513.             fromText = "year";
  514.         }
  515.         else {
  516.             fromText = "years";
  517.         }
  518.         if (round(result) == 1.0) {
  519.             if ((to != "millennia") || (to != "centuries")) {
  520.                 toText = to.substr(0, to.length() - 1);
  521.             }
  522.             if (to == "millennia")
  523.             {
  524.                 toText = "millennium";
  525.             }
  526.             if (to == "centuries") {
  527.                 toText = "century";
  528.             }
  529.         }
  530.         else {
  531.             toText = to;
  532.         }
  533.     }
  534.  
  535.     // Decades to everything else.
  536.     if ((from == "decades") && (to == "milliseconds")) {
  537.         result = value * 315360000000.0;
  538.     }
  539.     else if ((from == "decades") && (to == "seconds")) {
  540.         result = value * 315360000.0;
  541.     }
  542.     else if ((from == "decades") && (to == "minutes")) {
  543.         result = value * 5259487.66;
  544.     }
  545.     else if ((from == "decades") && (to == "hours")) {
  546.         result = value * 87600.0;
  547.     }
  548.     else if ((from == "decades") && (to == "days")) {
  549.         result = value * 3652.42;
  550.     }
  551.     else if ((from == "decades") && (to == "weeks")) {
  552.         result = value * 521.775;
  553.     }
  554.     else if ((from == "decades") && (to == "months")) {
  555.         result = value * 120.0;
  556.     }
  557.     else if ((from == "decades") && (to == "years")) {
  558.         result = value * 10.0;
  559.     }
  560.     else if ((from == "decades") && (to == "centuries")) {
  561.         result = value / 10.0;
  562.     }
  563.     else if ((from == "decades") && (to == "millennia")) {
  564.         result = value / 100.0;
  565.     }
  566.  
  567.     if (from == "decades") {
  568.         if (value == 1) {
  569.             fromText = "decade";
  570.         }
  571.         else {
  572.             fromText = "decades";
  573.         }
  574.         if (round(result) == 1.0) {
  575.             if ((to != "millennia") || (to != "centuries")) {
  576.                 toText = to.substr(0, to.length() - 1);
  577.             }
  578.             if (to == "millennia")
  579.             {
  580.                 toText = "millennium";
  581.             }
  582.             if (to == "centuries") {
  583.                 toText = "century";
  584.             }
  585.         }
  586.         else {
  587.             toText = to;
  588.         }
  589.     }
  590.  
  591.     // Centuries to everything else.
  592.     if ((from == "centuries") && (to == "milliseconds")) {
  593.         result = value * 3153600000000.0;
  594.     }
  595.     else if ((from == "centuries") && (to == "seconds")) {
  596.         result = value * 3155760000.0;
  597.     }
  598.     else if ((from == "centuries") && (to == "minutes")) {
  599.         result = value * 52594870.66;
  600.     }
  601.     else if ((from == "centuries") && (to == "hours")) {
  602.         result = value * 876000.0;
  603.     }
  604.     else if ((from == "centuries") && (to == "days")) {
  605.         result = value * 36500.0;
  606.     }
  607.     else if ((from == "centuries") && (to == "weeks")) {
  608.         result = value * 5214.2857142857;
  609.     }
  610.     else if ((from == "centuries") && (to == "months")) {
  611.         result = value * 1200.0;
  612.     }
  613.     else if ((from == "centuries") && (to == "years")) {
  614.         result = value * 100.0;
  615.     }
  616.     else if ((from == "centuries") && (to == "decades")) {
  617.         result = value * 10.0;
  618.     }
  619.     else if ((from == "centuries") && (to == "millennia")) {
  620.         result = value / 10.0;
  621.     }
  622.  
  623.     if (from == "centuries") {
  624.         if (value == 1) {
  625.             fromText = "century";
  626.         }
  627.         else {
  628.             fromText = "centuries";
  629.         }
  630.         if (round(result) == 1.0) {
  631.             if ((to != "millennia") || (to != "centuries")) {
  632.                 toText = to.substr(0, to.length() - 1);
  633.             }
  634.             if (to == "millennia")
  635.             {
  636.                 toText = "millennium";
  637.             }
  638.             if (to == "centuries") {
  639.                 toText = "century";
  640.             }
  641.         }
  642.         else {
  643.             toText = to;
  644.         }
  645.     }
  646.  
  647.     // Millennia to everything else.
  648.     if ((from == "millennia") && (to == "milliseconds")) {
  649.         result = value * 31536000000000.0;
  650.     }
  651.     else if ((from == "millennia") && (to == "seconds")) {
  652.         result = value * 31536000000.0;
  653.     }
  654.     else if ((from == "millennia") && (to == "minutes")) {
  655.         result = value * 525948700.66;
  656.     }
  657.     else if ((from == "millennia") && (to == "hours")) {
  658.         result = value * 8760000.0;
  659.     }
  660.     else if ((from == "millennia") && (to == "days")) {
  661.         result = value * 365200.42;
  662.     }
  663.     else if ((from == "millennia") && (to == "weeks")) {
  664.         result = value * 52100.775;
  665.     }
  666.     else if ((from == "millennia") && (to == "months")) {
  667.         result = value * 12000.0;
  668.     }
  669.     else if ((from == "millennia") && (to == "years")) {
  670.         result = value * 1000.0;
  671.     }
  672.     else if ((from == "millennia") && (to == "decades")) {
  673.         result = value * 100.0;
  674.     }
  675.     else if ((from == "millennia") && (to == "centuries")) {
  676.         result = value * 10.0;
  677.     }
  678.  
  679.     if (from == "millennia") {
  680.         if (value == 1) {
  681.             fromText = "millennium";
  682.         }
  683.         else {
  684.             fromText = "millennia";
  685.         }
  686.         if (round(result) == 1.0) {
  687.             if ((to != "millennia") || (to != "centuries")) {
  688.                 toText = to.substr(0, to.length() - 1);
  689.             }
  690.             if (to == "millennia")
  691.             {
  692.                 toText = "millennium";
  693.             }
  694.             if (to == "centuries") {
  695.                 toText = "century";
  696.             }
  697.         }
  698.         else {
  699.             toText = to;
  700.         }
  701.     }
  702.  
  703.     cout << fixed;
  704.     cout << setprecision(precision);
  705.     cout << value << " " << fromText << " = " << result << " " << toText << endl;
  706.  
  707.     return 0;
  708. }
Add Comment
Please, Sign In to add comment