1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <tuple>
using namespace std;
int dates[3];
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
string userInput1, userInput2;
tuple <int, int, int> splitString(string userInput){
vector<int> vect;
stringstream ss(userInput);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == '/')
ss.ignore();
}
for (i=0; i< vect.size(); i++){
dates[0] = vect.at(0);
dates[1] = vect.at(1);
dates[2] = vect.at(2);
}
return make_tuple(dates[0], dates[1], dates[2]);
}
int calcAge(){
auto [day1, month1, year1] = splitString(userInput1);
auto [day2, month2, year2] = splitString(userInput2);
int days_delta = abs(day2-day1);
int months_delta = abs((month2-month1) * (months[month2-month1]));
int years_delta = abs((year2-year1) * 365);
return abs(days_delta + (years_delta - months_delta));
}
int main (){
cout<<"Enter the first date in the following format 'dd/mm/yyyy': ";
cin >> userInput1;
cout << "Enter the second date in the following format 'dd/mm/yyyy': ";
cin >> userInput2;
cout << "You are " << calcAge() << " days old" << endl;
return 0;
}
|