Thread: C++ Question

  1. #16
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This "human class" can be set to any birth date and it converts the date into timestamp
    Code:
    #define MONTH false
    #define DAY true
    #include <ctime>
    #include <iostream>
    
    class Human{
    public:
        int DayMonthToDay(int day,int month){
            int days[12]={31,28,31,30,31,30,31,31,30,31,30,31},count=0;
            if((getAgeInfo('Y')-1970)%4==0){
                days[1]=29;
            }
            for(int i=1;i<month%13;i++){
                count+=days[i-1];
            }
            count+=day;
            return count;
        }
        int DateDataFromDay(int day,bool dayt){
            int days[12]={31,28,31,30,31,30,31,31,30,31,30,31},i;
            if((getAgeInfo('Y')-1970)%4==0){
                days[1]=29;
            }
            for(i=0;day>days[i];i++){
                day-=days[i];
            }
            if(dayt){ return day; }
            return i+1;
        }
        void setBirthTime(int year,int month,int day,int hour,int minute,int second){
            setAgeInfo('y',year);
            setAgeInfo('d',DayMonthToDay(day,month));
            setAgeInfo('h',hour);
            setAgeInfo('m',minute);
            setAgeInfo('s',second);
        }
        void setAgeInfo(char type,int tmpstamp){
            int temporary;
            switch(type){
                //set the day of birth
            case 'd':
                if(tmpstamp<=366){
                    TimeStamp-=getAgeInfo('D')*3600*24;
                    TimeStamp+=tmpstamp*3600*24/*+((getAgeInfo('Y')-1970)/4)*3600*24*/;
                }
                return;
                //set the hour of birth
            case 'h':
                TimeStamp-=getAgeInfo('H')*3600;
                TimeStamp+=(tmpstamp%24)*3600;
                return;
                //set the minute of birth
            case 'm':
                TimeStamp-=getAgeInfo('H')*60;
                TimeStamp+=(tmpstamp%60)*60;
                return;
                //set the total seconds of birth (since 1970)
            case 'r':
                TimeStamp=tmpstamp;
                return;
                //set the seconds of birth
            case 's':
                TimeStamp-=getAgeInfo('H');
                TimeStamp+=(tmpstamp%60);
                return;
                //set the year of birth
            case 'y':
                temporary=(getAgeInfo('Y')-1970)*(3600*24*365);
                TimeStamp-=(getAgeInfo('Y')-1970)*(3600*24*365)+((getAgeInfo('Y')-1970)/4)*3600*24;
                TimeStamp+=(tmpstamp-1970)*3600*24*365+((tmpstamp-1970)/4)*3600*24;
                return;    
            }
        }
        int getAgeInfo(char type){
            int tmpint;
            switch(type){
                //get the day of birth
            case 'D':
                return (TimeStamp/(3600*24)%365)-TimeStamp/(3600*24*365)/4;
                //get the day of age
            case 'd':
                tmpint=time(NULL)-TimeStamp;
                return (tmpint/(3600*24)%365)-tmpint/(3600*24*365)/4;
                //hours of age
            case 'h':
                tmpint=time(NULL)-TimeStamp;
                return (tmpint%(3600*24))/3600;
                //hour of birth
            case 'H':
                return (TimeStamp%(3600*24))/3600;
                //minutes of age
            case 'm':
                tmpint=time(NULL)-TimeStamp;
                return (tmpint%3600)/60;
                //minutes of birth
            case 'M':
                return (TimeStamp%3600)/60;
                //total seconds of age
            case 'r':
                return tmpint=time(NULL)-TimeStamp;
                //total seconds of birth (since 1970)
            case 'R':
                return TimeStamp;
                //seconds of age
            case 's':
                tmpint=time(NULL)-TimeStamp;
                return tmpint%60;
                //seconds of birth
            case 'S':
                return TimeStamp%60;
                //year of age
            case 'y':
                tmpint=time(NULL)-TimeStamp;
                tmpint=tmpint/(3600*24);
                tmpint+=tmpint/365/4;
                return tmpint/365;
                //year of birth
            case 'Y':
                tmpint=TimeStamp/(3600*24);
                tmpint+=tmpint/365/4;
                return (tmpint/365)+1970;
            default:
                return 0;
            }
        }
    private:
        int TimeStamp;
    };
    
    int main(){
        Human user;
        int year=0,month=0,day=0,hour=0,minute=0,second=0;
        do{
            std::cin.clear();
            std::cout<<"Please enter your year of birth\n";
            std::cin>>year;
        }while(year<1970||year>3000);
        do{
            std::cin.clear();
            std::cout<<"Please enter your month of birth\n";
            std::cin>>month;
        }while(month<1||month>12);
        do{
            std::cin.clear();
            std::cout<<"Please enter your day of birth\n";
            std::cin>>day;
        }while(day<1||day>31);
        user.setBirthTime(year,month,day,hour,minute,second);
        printf("You are %i years, %i months, %i days, %i hours, %i minutes and %i seconds old.\n\n",
        user.getAgeInfo('y'),user.DateDataFromDay(user.getAgeInfo('d'),MONTH),user.DateDataFromDay(user.getAgeInfo('d'),DAY),
        user.getAgeInfo('h'),user.getAgeInfo('m'),user.getAgeInfo('s'));
        std::cin.ignore(std::numeric_limits<int>::max(),'\n');
        return 0;
    }
    This is useless for you maybe, but it is funny
    You can set month, year, seconds, minutes, hours, days, everything separately. And then you can get the timestamp it made
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #17
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    You're using Dev-Cpp aren't you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM