Language Learning 1: Date display, c

Source: Internet
Author: User

Write a function that requires entering the year, month, day, hour, minute, second, and output the next second of the year, month, day, hour, minute, second. For example, if you enter December 31, 2004 00:00:00, the output is January 1, 2005.

// Year. Month. Day. Hour. Minute. Second

# Include <stdio. h>

Void nexttime (int * year, int * month, int * data, int * hour, int * minute, int * Second)
{
Int Day;
(* Second) ++; // second plus 1
If (* Second> = 60) // After 60 s, add 1 to the minute and return to zero in seconds.
{
* Second = 0;
(* Minute) ++;
If (* minute> = 60) // After 60 Minutes, add 1 to the hour and return to zero in minutes.
{
* Minute = 0;
(* Hour) ++;
If (* hour> = 24) // after 24 hours, the date plus 1, and the hour returns to zero.
{
* Hour = 0;
(* Data) ++;
 

// Determine the number of days per month
Switch (* month)
{

// The number of months in 31 days
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
Day = 31;
Break;

// The number of days in March February, 28 days in the leap year, and 29 days in the remaining
Case 2:
If (* Year % 400 = 0 & * Year % 100! = 0
& * Year % 4 = 0) // determines the leap year.
{
Day = 29;
}
Else
{
Day = 28;
}
Break;

// The remaining 30 days
Default:
Day = 30;
Break;
}
If (* Data> day) // If * Data ++ is greater than the day value calculated above, 1 is added to the month and the date is zero.
{
* Data = 1;
(* Month) ++;
If (* month> 12) // after January 1, December, the year is incremented by one, and the month is reset to zero.
{
* Month = 1;
(* Year) ++;
}
}
}
}
}
}

// Main Function
Void main ()
{
Int year, month, Data, hour, minute, second;
Printf ("Please input the time :");
Scanf ("% d", & year, & month, & Data, & hour, & minute, & second );
Nexttime (& year, & month, & Data, & hour, & minute, & second );
Printf ("the result: % d-% d: % d", year, month, Data, hour, minute, second );}

 

The biggest mistake is not to recognize: * The calculation level ratio is smaller than ++, so it is always the original value at the beginning.

Second, use scanf () to input time. Pay attention to the writing of functions.

I don't know how to write in C ++ if I use iostream. h library function input and output? (Can this be done for CIN <year <month <data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.