Home > Back-end >  The main function how to invoke this method
The main function how to invoke this method

Time:09-26

#include
# include "stdio.h"
using namespace std;

Struct MyDate//define a structure, storage date
{
Int m_year;//the year
Int m_month;//in
Int m_day;//day
};

MyDate computePrevDate (MyDate date)//calculation on the date of the day
{
MyDate prev=new MyDate ();//will be assigned to the date of today yesterday
Prev. M_year=date. M_year;
Prev. M_month=date. M_month;
Prev. M_day=date. M_day;
Int lastday=28;//a month late date
If (date. M_month % 2==1 & amp; & The date, m_month & lt; 8) | | (date. M_month % 2==0 & amp; & The date, m_month & gt;=8))
Lastday=31;
If (date. M_month==4 | | date. M_month==6 | | date. 9 m_month==| | date. M_month==11)
Lastday=30;

In February the else {//
If (date. M_year % 4==0 & amp; & The date m_year % 100!=0) | | date. M_year %==0)//400 is a leap year
Lastday=29;
The else//is not a leap year
Lastday=28;
}

//if the date is the first day of this month, the day is the last day of last month, otherwise it is ordinary date
If (date. M_day==1)//early date
{
Prev. M_day=lastday;
If (date. M_month==1)//at the beginning of the year date
{
Prev. M_month=12;
Prev. M_year -;
}
The else
{//early date
Prev. M_month -;
}
}
The else//ordinary date
{
Prev. M_day -;
}
Return prev.
}



Void main {
}

CodePudding user response:

You have a BUG this code, the code logic to change, first computePrevDate function
MyDate prev=new MyDate ();
This is a mistake, change to be MyDate prev. Can,

As for the main function in the call, can be like this:
 void main () 
{
MyDate d={2016, 1, 1};
MyDate pd=computePrevDate (d);

Printf (" Prev Date: % d % d - % d ", pd. M_year, pd, m_month, pd. M_day);

getchar();
}

Add your code algorithm has a problem, so the result will be wrong, to change yourself,
  • Related