Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main() {
- system("title = Date calculator");
- //first date input vars:
- int daysOne = 0; int mountsOne = 0; int yearOne = 0;
- int hoursOne = 0; int minutesOne = 0; int secondsOne = 0;
- //second date input vars:
- int daysTwo = 0; int mountsTwo = 0; int yearTwo = 0;
- int hoursTwo = 0; int minutesTwo = 0; int secondsTwo = 0;
- //result date vars used for output:
- int resultDays = 0; int resultMounts = 0; int resultYear = 0;
- int resultHours = 0; int resultMinutes = 0; int resultSeconds = 0;
- //char to handle dots:
- char noInput = '.';
- //float representing days:
- const float DAY = 365.25F;
- //inpit of date one:
- std::cin >> daysOne >> noInput >> mountsOne >> noInput >>
- yearOne >> hoursOne >> noInput >>
- minutesOne >> noInput >> secondsOne;
- //input of date two:
- std::cin >> daysTwo >> noInput >> mountsTwo >> noInput >>
- yearTwo >> hoursTwo >> noInput >>
- minutesTwo >> noInput >> secondsTwo;
- //bools, showing if the year is leap or if the mount has 31 days:
- bool isLeapOne = (yearOne % 400 == 0) || (yearOne % 4 == 0 && yearOne % 100 != 0);
- bool isLeapTwo = (yearTwo % 400 == 0) || (yearTwo % 4 == 0 && yearTwo % 100 != 0);
- bool thirtyOneOne = false;
- bool thirtyOneTwo = false;
- //working on the bools:
- if (mountsOne <= 7) {
- if (mountsOne % 2 != 0)
- thirtyOneOne = true;
- }
- else {
- if (mountsOne % 2 == 0)
- thirtyOneOne = true;
- }
- //handling invalid output:
- if (mountsTwo <= 7) {
- if (mountsTwo % 2 != 0)
- thirtyOneTwo = true;
- }
- else {
- if (mountsTwo % 2 == 0)
- thirtyOneTwo = true;
- }
- if (daysOne == 31) {
- if (!thirtyOneOne) {
- std::cout << "invalid date/time";
- return 0;
- }
- }
- else if (daysOne == 29 && mountsOne == 2) {
- if (!isLeapOne) {
- std::cout << "Invalid date/time";
- return 0;
- }
- }
- if (daysTwo == 31) {
- if (!thirtyOneTwo) {
- std::cout << "invalid date/time";
- return 0;
- }
- }
- else if (daysTwo == 29 && mountsTwo == 2) {
- if (!isLeapTwo) {
- std::cout << "Invalid date/time";
- return 0;
- }
- }
- //algorithm for finding difference between two dates in
- //years-mounts-days-hours-minutes-seconds:
- resultYear = yearOne - yearTwo;
- resultMounts = mountsOne - mountsTwo;
- resultDays = daysOne - daysTwo;
- resultHours = hoursOne - hoursTwo;
- resultMinutes = minutesOne - minutesTwo;
- resultSeconds = secondsOne - secondsTwo;
- bool flagDays = false;
- bool flagMounts = false;
- if (resultMounts < 0)
- flagMounts = true;
- if (resultYear < 0) {
- resultYear *= -1; resultMounts *= -1;
- if (resultDays < 0)
- flagDays = true;
- resultDays *= -1;
- }
- if (resultMounts < 0) {
- if (resultYear != 0) {
- resultMounts += 12;
- --resultYear;
- }
- else {
- resultMounts *= -1; resultDays *= -1;
- resultHours *= -1; resultMinutes *= -1; resultSeconds *= -1;
- }
- }
- if (resultDays < 0) {
- if (resultMounts > 0) {
- resultDays += 30;
- if (isLeapOne && mountsOne == 2)
- resultDays -= 1;
- if (thirtyOneOne)
- resultDays += 1;
- resultMounts--;
- }
- else {
- resultDays *= -1;
- resultHours *= -1; resultMinutes *= -1; resultSeconds *= -1;
- }
- }
- if (resultHours < 0) {
- if (flagDays) {
- resultHours *= -1;
- resultMinutes *= -1;
- resultSeconds *= -1;
- }
- else {
- resultDays--;
- resultHours += 24;
- }
- }
- if (resultMinutes < 0) {
- if (resultHours != 0) {
- resultHours--;
- resultMinutes += 60;
- }
- else {
- resultMinutes *= -1; resultSeconds *= -1;
- }
- }
- if (resultSeconds < 0) {
- if (resultMinutes != 0) {
- resultMinutes--;
- resultSeconds += 60;
- }
- else
- resultSeconds *= -1;
- }
- //algorithm used for turning years and mounts into days:
- int daysAddition = 0;
- switch (mountsOne){
- case 1: daysAddition += 31;
- case 2: daysAddition += 28;
- case 3: daysAddition += 31;
- case 4: daysAddition += 30;
- case 5: daysAddition += 31;
- case 6: daysAddition += 30;
- case 7: daysAddition += 31;
- case 8: daysAddition += 31;
- case 9: daysAddition += 30;
- case 10: daysAddition += 31;
- case 11: daysAddition += 30;
- case 12: daysAddition += 31;
- }
- switch (mountsTwo){
- case 1: daysAddition -= 31;
- case 2: daysAddition -= 28;
- case 3: daysAddition -= 31;
- case 4: daysAddition -= 30;
- case 5: daysAddition -= 31;
- case 6: daysAddition -= 30;
- case 7: daysAddition -= 31;
- case 8: daysAddition -= 31;
- case 9: daysAddition -= 30;
- case 10: daysAddition -= 31;
- case 11: daysAddition -= 30;
- case 12: daysAddition -= 31;
- }
- if (flagMounts || daysAddition < 0)
- daysAddition = 365 - std::abs(daysAddition);
- resultDays += daysAddition;
- resultDays += DAY * resultYear;
- if (resultYear != 0) {
- //using intervals to handle leap years:
- int intervalOneforHundred = yearOne / 100;
- int intervalTwoforHundred = yearTwo / 100;
- int intervalOneforFourHundred = yearOne / 400;
- int intervalTwoforFourHundred = yearTwo / 400;
- if (intervalOneforHundred > intervalTwoforHundred)
- resultDays -= intervalOneforHundred - intervalTwoforHundred;
- else
- resultDays -= intervalTwoforHundred - intervalOneforHundred;
- if (intervalOneforHundred > intervalTwoforHundred)
- resultDays += intervalOneforFourHundred - intervalTwoforFourHundred;
- else
- resultDays += intervalTwoforFourHundred - intervalOneforFourHundred;
- }
- //output:
- if (resultDays == 0) {
- if (resultHours < 10)
- std::cout << "0" << resultHours;
- else
- std::cout << resultHours;
- if (resultMinutes < 10)
- std::cout << " : 0" << resultMinutes;
- else
- std::cout << " : " << resultMinutes;
- if (resultSeconds < 10)
- std::cout << " : 0" << resultSeconds;
- else
- std::cout << " : " << resultSeconds;
- }
- else {
- std::cout << resultDays << " - ";
- if (resultHours < 10)
- std::cout << "0" << resultHours;
- else
- std::cout << resultHours;
- if (resultMinutes < 10)
- std::cout << " : 0" << resultMinutes;
- else
- std::cout << " : " << resultMinutes;
- if (resultSeconds < 10)
- std::cout << " : 0" << resultSeconds;
- else
- std::cout << " : " << resultSeconds;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement