I need help with a structure problem

[b][red]This message was edited by golfshark12 at 2003-4-30 15:48:4[/red][/b][hr]
I'm looking to figure out this problem:
Define a structure called DATE with three fields, each of which is an integer: one for the month, one for the day and one for the year, and write a function called increment that accepts a date structure, increments the date by one day and returns the new date. If the
date is the last day of the month, then the month field also must be changed. If the month is 12 (December) and the day is 31, then the value of the year field must also be changed. (Remember a year is a leap year if it is evenly divisible by 4 but not by 100 or it is evenly
divisible by 400.) Finally, build a complete program that reads a particular date and prints the next date.




Comments

  • : [b][red]This message was edited by golfshark12 at 2003-4-30 15:48:4[/red][/b][hr]
    : I'm looking to figure out this problem:
    : Define a structure called DATE with three fields, each of which is an integer: one for the month, one for the day and one for the year, and write a function called increment that accepts a date structure, increments the date by one day and returns the new date. If the
    : date is the last day of the month, then the month field also must be changed. If the month is 12 (December) and the day is 31, then the value of the year field must also be changed. (Remember a year is a leap year if it is evenly divisible by 4 but not by 100 or it is evenly
    : divisible by 400.) Finally, build a complete program that reads a particular date and prints the next date.

    I'm not sure if this will help any, but I wrote this for an app I was working on, and thought it may help you: possibly save you the trouble of writing out a bunch of code, and maybe give you a lead on a way to approach your problem.
    [code]
    // int FillDates():
    // Action: Fills the NewDate combo with dates in mm/dd/yy format, and returns the index of the current date
    int FillDates() {
    int DM[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 30, 31, 31 };
    char *Date = new char[10];
    char *Text = new char[3];
    apstring Final, thDate, M, D, Y;
    int curM, curD, curY;
    int curI, temp;
    _strdate( Date );
    thDate = Date;

    Text[0] = Date[0];
    Text[1] = Date[1];
    curM = AtoI( Text, 2 );
    Text[0] = Date[3];
    Text[1] = Date[4];
    curD = AtoI( Text, 2 );
    Text[0] = Date[6];
    Text[1] = Date[7];
    curY = AtoI( Text, 2 );

    for ( int m = 0; m < 12; m++ ) {
    itoa( m + 1, Text, 10 );
    M = Text;
    if ( m + 1 < 10 ) {
    M += " ";
    M[1] = M[0];
    M[0] = '0';
    }
    for ( int d = 0; d < DM[m]; d++ ) {
    itoa( d + 1, Text, 10 );
    D = Text;
    if ( d + 1 < 10 ) {
    D += " ";
    D[1] = D[0];
    D[0] = '0';
    }
    if ( m + 1 < curM ) { // Next year
    itoa( curY + 1, Text, 10 );
    Y = Text;
    if ( curY + 1 < 10 ) {
    Y += " ";
    Y[1] = Y[0];
    Y[0] = '0';
    }
    }
    if ( m + 1 == curM ) {
    if ( d + 1 < curD ) { // Next year
    itoa( curY + 1, Text, 10 );
    Y = Text;
    if ( curY + 1 < 10 ) {
    Y += " ";
    Y[1] = Y[0];
    Y[0] = '0';
    }
    } else { // This year
    itoa( curY, Text, 10 );
    Y = Text;
    if ( curY < 10 ) {
    Y += " ";
    Y[1] = Y[0];
    Y[0] = '0';
    }
    }
    }
    if ( m + 1 > curM ) { // This year
    itoa( curY, Text, 10 );
    Y = Text;
    if ( curY < 10 ) {
    Y += " ";
    Y[1] = Y[0];
    Y[0] = '0';
    }
    }

    Final = M + "/" + D + "/" + Y;

    // Do something w/ the date, not necessarily this...
    temp = SendMessage( NewDate, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)Final.c_str() );
    if ( Final == thDate )
    curI = temp;
    }
    }

    delete Date;
    delete Text;

    return curI;
    }
    [/code]
    P.S. : Just noticed this, I used a default 29 days for February, not taking into account leap years. make sure you change the DM array so you don't encounter problems later.
    ~[red]PhoeniX[/red]

  • : : [b][red]This message was edited by golfshark12 at 2003-4-30 15:48:4[/red][/b][hr]
    : : I'm looking to figure out this problem:
    : : Define a structure called DATE with three fields, each of which is an integer: one for the month, one for the day and one for the year, and write a function called increment that accepts a date structure, increments the date by one day and returns the new date. If the
    : : date is the last day of the month, then the month field also must be changed. If the month is 12 (December) and the day is 31, then the value of the year field must also be changed. (Remember a year is a leap year if it is evenly divisible by 4 but not by 100 or it is evenly
    : : divisible by 400.) Finally, build a complete program that reads a particular date and prints the next date.
    :
    : I'm not sure if this will help any, but I wrote this for an app I was working on, and thought it may help you: possibly save you the trouble of writing out a bunch of code, and maybe give you a lead on a way to approach your problem.
    : [code]
    : // int FillDates():
    : // Action: Fills the NewDate combo with dates in mm/dd/yy format, and returns the index of the current date
    : int FillDates() {
    : int DM[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 30, 31, 31 };
    : char *Date = new char[10];
    : char *Text = new char[3];
    : apstring Final, thDate, M, D, Y;
    : int curM, curD, curY;
    : int curI, temp;
    : _strdate( Date );
    : thDate = Date;
    :
    : Text[0] = Date[0];
    : Text[1] = Date[1];
    : curM = AtoI( Text, 2 );
    : Text[0] = Date[3];
    : Text[1] = Date[4];
    : curD = AtoI( Text, 2 );
    : Text[0] = Date[6];
    : Text[1] = Date[7];
    : curY = AtoI( Text, 2 );
    :
    : for ( int m = 0; m < 12; m++ ) {
    : itoa( m + 1, Text, 10 );
    : M = Text;
    : if ( m + 1 < 10 ) {
    : M += " ";
    : M[1] = M[0];
    : M[0] = '0';
    : }
    : for ( int d = 0; d < DM[m]; d++ ) {
    : itoa( d + 1, Text, 10 );
    : D = Text;
    : if ( d + 1 < 10 ) {
    : D += " ";
    : D[1] = D[0];
    : D[0] = '0';
    : }
    : if ( m + 1 < curM ) { // Next year
    : itoa( curY + 1, Text, 10 );
    : Y = Text;
    : if ( curY + 1 < 10 ) {
    : Y += " ";
    : Y[1] = Y[0];
    : Y[0] = '0';
    : }
    : }
    : if ( m + 1 == curM ) {
    : if ( d + 1 < curD ) { // Next year
    : itoa( curY + 1, Text, 10 );
    : Y = Text;
    : if ( curY + 1 < 10 ) {
    : Y += " ";
    : Y[1] = Y[0];
    : Y[0] = '0';
    : }
    : } else { // This year
    : itoa( curY, Text, 10 );
    : Y = Text;
    : if ( curY < 10 ) {
    : Y += " ";
    : Y[1] = Y[0];
    : Y[0] = '0';
    : }
    : }
    : }
    : if ( m + 1 > curM ) { // This year
    : itoa( curY, Text, 10 );
    : Y = Text;
    : if ( curY < 10 ) {
    : Y += " ";
    : Y[1] = Y[0];
    : Y[0] = '0';
    : }
    : }
    :
    : Final = M + "/" + D + "/" + Y;
    :
    : // Do something w/ the date, not necessarily this...
    : temp = SendMessage( NewDate, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)Final.c_str() );
    : if ( Final == thDate )
    : curI = temp;
    : }
    : }
    :
    : delete Date;
    : delete Text;
    :
    : return curI;
    : }
    : [/code]
    : P.S. : Just noticed this, I used a default 29 days for February, not taking into account leap years. make sure you change the DM array so you don't encounter problems later.
    : ~[red]PhoeniX[/red]

    P.P.S. : Noticed this too... I had all dates after the current date be a part of the current year, and all of the earlier dates be part of the following year, so that the dates ranged from today till a year from yesterday.

    ~[red]PhoeniX[/red]

  • [b][red]This message was edited by golfshark12 at 2003-4-30 19:9:15[/red][/b][hr]
    Here is what I have so far. Can anyone help me finish it?

    #include
    #include

    struct DATE_1
    {
    int month;
    int day;
    int year;
    };

    void increment_date(DATE_1 &date)

    int main()
    {
    DATE_1 date;

    date.year;
    date.day;
    date.month;

    switch(month)
    {
    case1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
    if(date.day < 31)
    {
    date.day++;
    }
    else
    {
    date.day = 1;
    date.month++;
    }
    }break;
    case 4: case 6: case 9: case 11:
    {
    if(date.day < 30)
    {
    date.day++;
    }
    else
    {
    date.day = 1;
    date.month++;
    }
    }break;
    case 2:
    {
    if(((date.year % 4) == 0) && ((date.year % 100) != 0)
    {
    if((date.year % 400) == 400)
    }
    if(date.day < 28)
    {
    date.day++;
    }
    else
    {
    date.day = 1;
    date.month++;
    }
    return 0;
    }

  • : [b][red]This message was edited by golfshark12 at 2003-4-30 15:48:4[/red][/b][hr]
    : I'm looking to figure out this problem:
    : Define a structure called DATE with three fields, each of which is an integer: one for the month, one for the day and one for the year, and write a function called increment that accepts a date structure, increments the date by one day and returns the new date. If the
    : date is the last day of the month, then the month field also must be changed. If the month is 12 (December) and the day is 31, then the value of the year field must also be changed. (Remember a year is a leap year if it is evenly divisible by 4 but not by 100 or it is evenly
    : divisible by 400.) Finally, build a complete program that reads a particular date and prints the next date.
    :
    :
    :
    :
    :
    [code]
    typedef struct
    {
    int m_iDay;
    int m_iMonth;
    int m_iYear;
    } DATETAG;

    void IncrementDate (DATETAG& date)
    {
    int iMaxDays = 31;
    int iMonth = date.m_iMonth;

    if ((iMonth==4) || (iMonth==6) || (iMonth==9) || (iMonth==11)) {
    iMaxDays--; [b]// 30 days for these months[/b]
    }
    else {
    if (iMonth==2) {
    int iYear = date.m_iYear;

    iMaxDays = 28;
    if (((iYear%4 == 0) && (iYear%100 != 0)) || (iYear%400 == 0)) {
    iMaxDays++; [b]// Leaping![/b]
    }
    }
    }

    [b]// And now adding a day:[/b]
    if (++(date.m_iDay) > iMaxDays) {
    date.m_iDay=1;
    if (++(date.m_iMonth) > 12) {
    date.m_iMonth=1;
    date.m_iYear++;
    }
    }
    }

    int main (int argc, char* argv[])
    {
    DATETAG aDate = { 13, 5, 2000 };

    for (int i=0; i<1024; i++) {
    IncrementDate (aDate);
    _asm nop [b]// Stop here in debugger and see how structure changes[/b]
    }
    return 0;
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories