Advertisement
Stoycho_KK

задача трета

Dec 1st, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main() {
  4.     system("title = Date calculator");
  5.  
  6.     //first date input vars:
  7.     int daysOne = 0; int mountsOne = 0; int yearOne = 0;
  8.     int hoursOne = 0; int minutesOne = 0; int secondsOne = 0;
  9.  
  10.     //second date input vars:
  11.     int daysTwo = 0; int mountsTwo = 0; int yearTwo = 0;
  12.     int hoursTwo = 0; int minutesTwo = 0; int secondsTwo = 0;
  13.  
  14.     //result date vars used for output:
  15.     int resultDays = 0; int resultMounts = 0; int resultYear = 0;
  16.     int resultHours = 0; int resultMinutes = 0; int resultSeconds = 0;
  17.  
  18.     //char to handle dots:
  19.     char noInput = '.';
  20.  
  21.     //float representing days:
  22.     const float DAY = 365.25F;
  23.  
  24.     //inpit of date one:
  25.     std::cin >> daysOne >> noInput >> mountsOne >> noInput >>
  26.         yearOne >> hoursOne >> noInput >>
  27.         minutesOne >> noInput >> secondsOne;
  28.  
  29.     //input of date two:
  30.     std::cin >> daysTwo >> noInput >> mountsTwo >> noInput >>
  31.         yearTwo >> hoursTwo >> noInput >>
  32.         minutesTwo >> noInput >> secondsTwo;
  33.  
  34.     //bools, showing if the year is leap or if the mount has 31 days:
  35.     bool isLeapOne = (yearOne % 400 == 0) || (yearOne % 4 == 0 && yearOne % 100 != 0);
  36.     bool isLeapTwo = (yearTwo % 400 == 0) || (yearTwo % 4 == 0 && yearTwo % 100 != 0);
  37.     bool thirtyOneOne = false;
  38.     bool thirtyOneTwo = false;
  39.  
  40.     //working on the bools:
  41.     if (mountsOne <= 7) {
  42.         if (mountsOne % 2 != 0)
  43.             thirtyOneOne = true;
  44.     }
  45.     else {
  46.         if (mountsOne % 2 == 0)
  47.             thirtyOneOne = true;
  48.     }
  49.  
  50.     //handling invalid output:
  51.     if (mountsTwo <= 7) {
  52.         if (mountsTwo % 2 != 0)
  53.             thirtyOneTwo = true;
  54.     }
  55.     else {
  56.         if (mountsTwo % 2 == 0)
  57.             thirtyOneTwo = true;
  58.     }
  59.  
  60.     if (daysOne == 31) {
  61.         if (!thirtyOneOne) {
  62.             std::cout << "invalid date/time";
  63.             return 0;
  64.         }
  65.     }
  66.     else if (daysOne == 29 && mountsOne == 2) {
  67.         if (!isLeapOne) {
  68.             std::cout << "Invalid date/time";
  69.             return 0;
  70.         }
  71.     }
  72.  
  73.     if (daysTwo == 31) {
  74.         if (!thirtyOneTwo) {
  75.             std::cout << "invalid date/time";
  76.             return 0;
  77.         }
  78.     }
  79.     else if (daysTwo == 29 && mountsTwo == 2) {
  80.         if (!isLeapTwo) {
  81.             std::cout << "Invalid date/time";
  82.             return 0;
  83.         }
  84.     }
  85.  
  86.     //algorithm for finding difference between two dates in
  87.     //years-mounts-days-hours-minutes-seconds:
  88.  
  89.     resultYear = yearOne - yearTwo;
  90.     resultMounts = mountsOne - mountsTwo;
  91.     resultDays = daysOne - daysTwo;
  92.  
  93.     resultHours = hoursOne - hoursTwo;
  94.     resultMinutes = minutesOne - minutesTwo;
  95.     resultSeconds = secondsOne - secondsTwo;
  96.  
  97.     bool flagDays = false;
  98.     bool flagMounts = false;
  99.  
  100.     if (resultMounts < 0)
  101.         flagMounts = true;
  102.  
  103.     if (resultYear < 0) {
  104.         resultYear *= -1; resultMounts *= -1;
  105.  
  106.         if (resultDays < 0)
  107.             flagDays = true;
  108.  
  109.         resultDays *= -1;
  110.     }
  111.  
  112.     if (resultMounts < 0) {
  113.         if (resultYear != 0) {
  114.             resultMounts += 12;
  115.             --resultYear;
  116.         }
  117.         else {
  118.             resultMounts *= -1; resultDays *= -1;
  119.             resultHours *= -1; resultMinutes *= -1; resultSeconds *= -1;
  120.         }
  121.     }
  122.  
  123.     if (resultDays < 0) {
  124.         if (resultMounts > 0) {
  125.             resultDays += 30;
  126.             if (isLeapOne && mountsOne == 2)
  127.                 resultDays -= 1;
  128.             if (thirtyOneOne)
  129.                 resultDays += 1;
  130.             resultMounts--;
  131.         }
  132.         else {
  133.             resultDays *= -1;
  134.             resultHours *= -1; resultMinutes *= -1; resultSeconds *= -1;
  135.         }
  136.     }
  137.  
  138.     if (resultHours < 0) {
  139.         if (flagDays) {
  140.             resultHours *= -1;
  141.             resultMinutes *= -1;
  142.             resultSeconds *= -1;
  143.         }
  144.         else {
  145.             resultDays--;
  146.             resultHours += 24;
  147.         }
  148.     }
  149.  
  150.     if (resultMinutes < 0) {
  151.         if (resultHours != 0) {
  152.             resultHours--;
  153.             resultMinutes += 60;
  154.         }
  155.         else {
  156.             resultMinutes *= -1; resultSeconds *= -1;
  157.         }
  158.     }
  159.  
  160.     if (resultSeconds < 0) {
  161.         if (resultMinutes != 0) {
  162.             resultMinutes--;
  163.             resultSeconds += 60;
  164.         }
  165.         else
  166.             resultSeconds *= -1;
  167.     }
  168.  
  169.     //algorithm used for turning years and mounts into days:
  170.  
  171.     int daysAddition = 0;
  172.  
  173.     switch (mountsOne){
  174.     case 1: daysAddition += 31;
  175.     case 2: daysAddition += 28;
  176.     case 3: daysAddition += 31;
  177.     case 4: daysAddition += 30;
  178.     case 5: daysAddition += 31;
  179.     case 6: daysAddition += 30;
  180.     case 7: daysAddition += 31;
  181.     case 8: daysAddition += 31;
  182.     case 9: daysAddition += 30;
  183.     case 10: daysAddition += 31;
  184.     case 11: daysAddition += 30;
  185.     case 12: daysAddition += 31;
  186.     }
  187.  
  188.     switch (mountsTwo){
  189.     case 1: daysAddition -= 31;
  190.     case 2: daysAddition -= 28;
  191.     case 3: daysAddition -= 31;
  192.     case 4: daysAddition -= 30;
  193.     case 5: daysAddition -= 31;
  194.     case 6: daysAddition -= 30;
  195.     case 7: daysAddition -= 31;
  196.     case 8: daysAddition -= 31;
  197.     case 9: daysAddition -= 30;
  198.     case 10: daysAddition -= 31;
  199.     case 11: daysAddition -= 30;
  200.     case 12: daysAddition -= 31;
  201.     }
  202.  
  203.     if (flagMounts || daysAddition < 0)
  204.         daysAddition = 365 - std::abs(daysAddition);
  205.  
  206.     resultDays += daysAddition;
  207.  
  208.     resultDays += DAY * resultYear;
  209.  
  210.     if (resultYear != 0) {
  211.         //using intervals to handle leap years:
  212.         int intervalOneforHundred = yearOne / 100;
  213.         int intervalTwoforHundred = yearTwo / 100;
  214.  
  215.         int intervalOneforFourHundred = yearOne / 400;
  216.         int intervalTwoforFourHundred = yearTwo / 400;
  217.  
  218.         if (intervalOneforHundred > intervalTwoforHundred)
  219.             resultDays -= intervalOneforHundred - intervalTwoforHundred;
  220.         else
  221.             resultDays -= intervalTwoforHundred - intervalOneforHundred;
  222.  
  223.         if (intervalOneforHundred > intervalTwoforHundred)
  224.             resultDays += intervalOneforFourHundred - intervalTwoforFourHundred;
  225.         else
  226.             resultDays += intervalTwoforFourHundred - intervalOneforFourHundred;
  227.     }
  228.  
  229.     //output:
  230.     if (resultDays == 0) {
  231.         if (resultHours < 10)
  232.             std::cout << "0" << resultHours;
  233.         else
  234.             std::cout << resultHours;
  235.  
  236.         if (resultMinutes < 10)
  237.             std::cout << " : 0" << resultMinutes;
  238.         else
  239.             std::cout << " : " << resultMinutes;
  240.  
  241.         if (resultSeconds < 10)
  242.             std::cout << " : 0" << resultSeconds;
  243.         else
  244.             std::cout << " : " << resultSeconds;
  245.     }
  246.     else {
  247.  
  248.         std::cout << resultDays << " - ";
  249.  
  250.         if (resultHours < 10)
  251.             std::cout << "0" << resultHours;
  252.         else
  253.             std::cout << resultHours;
  254.  
  255.         if (resultMinutes < 10)
  256.             std::cout << " : 0" << resultMinutes;
  257.         else
  258.             std::cout << " : " << resultMinutes;
  259.  
  260.         if (resultSeconds < 10)
  261.             std::cout << " : 0" << resultSeconds;
  262.         else
  263.             std::cout << " : " << resultSeconds;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement