Calculate the day after tomorrow in C + + and Java, respectively, code

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <cstring>using namespace Std;bool checknum (char c) {if ( C >= ' 0 ' && C <= ' 9 ') return True;return false;} BOOL Checkstring (String str) {if (Checknum (str[0]) &&checknum (str[1]) &&checknum (str[2]) && Checknum (Str[3]) && (str[4]== '-') &&checknum (str[5]) &&checknum (str[6]) && (str[7]== '-') &&checknum (Str[8]) &&checknum (str[9])) return True;return false;   int main () {string Str;int year;int month;int day;while (cin>>str) {int flag = 1;   if (!checkstring (str)) {flag=0;  cout<< "The date format you entered does not match the requirements" <<endl<<endl; }else{year= (str[0]-' 0 ') *1000+ (str[1]-' 0 ') *100+ (str[2]-' 0 ') *10+ (str[3]-' 0 '); month= (str[5]-' 0 ') *10+ (str[6]-' 0 '); day= (str[8]-' 0 ') *10+ (str[9]-' 0 '); if (month==1 | | month==3 | | month==5 | | month==7 | | month==8 | | month==10 | | month==12 {if (Day >=1 && day <=31) {Day + = 2;if (Day >) {month + = 1;day = 1;} if (month >) {yeAr + = 1;month = 1;}} else{flag=0;cout<< "The date you entered does not exist" &LT;&LT;ENDL&LT;&LT;ENDL;}} else if (month==4 | | month==6 | | month==9 | | month==11) {if (Day >=1 && day <=30) {Day + = 2;if (Day >) {mont H + = 1;day = 1;} if (Month >) {year + = 1;month = 1;}} else{flag=0;cout<< "The date you entered does not exist" &LT;&LT;ENDL&LT;&LT;ENDL;}} else if (month = = 2) {if (year%4==0 && year%100!=0) | | (year%400==0)) {//If the leap year if (Day >=1 && day <=29) {Day + = 2;if (Day >) {month + = 1;day = 1;} if (Month >) {year + = 1;month = 1;}}   else{flag=0;    cout<< "The date you entered does not exist" <<endl<<endl; }}else{if (Day >=1 && day <=28) {day + 2;if (Day >) {month + = 1;day = 1;} if (Month >) {year + = 1;month = 1;}}   else{flag=0;    cout<< "The date you entered does not exist" <<endl<<endl;  }}}else{cout<< "The month you entered does not exist" &LT;&LT;ENDL&LT;&LT;ENDL;} if (flag==1) {cout<< "date acquired:" <<year<< "year" <<month<< "month" <<day<< "Day" <  <endl<<endl; }}retUrn 0;} 

Import Java.util.scanner;public class Calculatethedayaftertomorrow {public static Boolean checknum (char c) {// Determines whether the current character is a numeric if (c >= ' 0 ' && C <= ' 9 ') return True;return false;} public static Boolean checkstring (String str) {//Determines whether the string is of the specified date format: Yyyy-mm-ddif (Checknum (Str.charat (0)) && Checknum (Str.charat (1)) && Checknum (Str.charat (2)) && Checknum (Str.charat (3)) && (Str.charat (4) = = '-') && Checknum (Str.charat (5)) && Checknum (Str.charat (6)) && (Str.charat (7) = = '-') & & Checknum (Str.charat (8)) && Checknum (Str.charat (9))) return True;return false; public static void Main (string args[]) {string Str;int year = 0;int month = 0;int Day = 0; Scanner Scanner = new Scanner (system.in), while ((str = scanner.nextline ()) = null) {int flag = 1;IF (!checkstring (str)) { Flag = 0; System.out.println ("The date format you entered does not meet the requirements" + "\ n");} else {//calculated date Year = (Str.charat (0)-' 0 ') * + (Str.charat (1)-' 0 ') * + (Str.charat (2)-' 0 ') * + + (Str.charat (3)-' 0 '); month = (Str.charat (5)-' 0 ') * + (Str.charat (6)-' 0 ');d ay = (Str.charat (8)-' 0 ') * + (str).  CharAt (9)-' 0 ');//The number of days per month is different, category discussion if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7| | month = = 8 | | month = =) {if (Day >= 1 && days <=) {Day + = 2;if (Day > 31) {//has been month + = 1;day-= 31;} if (Month > 12) {//After two days spanned year + = 1;month = 1;}} else {flag = 0; System.out.println ("The date you entered does not exist" + "\ n");}}  else if (month = = 4 | | month = = 6 | | month = = 9| | month = =) {if (Day >= 1 && day <=) {Day + = 2;if (day >) {month + = 1;day-= 30;} if (Month >) {year + = 1;month = 1;}} else {flag = 0; System.out.println ("The date you entered does not exist" + "\ n");}} else if (month = = 2) {if (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) {//If the leap year if (Day >= 1 && day <=) {Day + = 2;if (Day >) {month + 1;day-= 29;} if (Month >) {year + = 1;month = 1;}} else {flag = 0; System.out.println ("The date you entered does not existIn "+" \ n ");}} else {//Common year if (day >= 1 && day <=) {Day + = 2;if (Day >) {month + = 1;day-= 28;} if (Month >) {year + = 1;month = 1;}} else {flag = 0; System.out.println ("The date you entered does not exist" + "\ n");}}} else {flag = 0; System.out.println ("The month you entered does not exist" + "\ n");}} if (flag = = 1) {//the date entered through the flag flag is legal System.out.println ("The Day After Tomorrow is:" + year + "years" + month + "month" + day+ "Day" + "\ n");}}}

Reprint Please specify source: http://blog.csdn.net/lindonglian/article/details/46043637

Calculate the day after tomorrow in C + + and Java, respectively, code

Related Article

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.