返回顶部

收藏

分别用C++和Java写的计算后天的日期代码

更多
    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    using namespace std;  
    bool checkNum(char c){  
        if(c >='0' &amp;&amp; c <='9')  
            return true;  
        return false;  
    }  
    bool checkString(string str){  
        if(checkNum(str[0])&amp;&amp;checkNum(str[1])&amp;&amp;checkNum(str[2])  
           &amp;&amp;checkNum(str[3])&amp;&amp;(str[4]=='-')&amp;&amp;checkNum(str[5])  
           &amp;&amp;checkNum(str[6])&amp;&amp;(str[7]=='-')&amp;&amp;checkNum(str[8])&amp;&amp;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<<"您输入的日期格式不符合要求"<<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 &amp;&amp; day <=31){  
                    day += 2;  
                    if(day > 31){  
                        month += 1;  
                        day = 1;  
                    }  
                    if(month > 12){  
                        year += 1;  
                        month = 1;  
                    }  
                }else{  
                    flag=0;  
                    cout<<"您输入的日期不存在"<<endl<<endl;  
                }  
            }  
            else if(month==4 || month==6 || month==9 || month==11){  
                if(day >=1 &amp;&amp; day <=30){  
                    day += 2;  
                    if(day > 30){  
                        month += 1;  
                        day = 1;  
                    }  
                    if(month > 12){  
                        year += 1;  
                        month = 1;  
                    }  
                }else{  
                    flag=0;  
                    cout<<"您输入的日期不存在"<<endl<<endl;  
                }  
            }  
            else if(month == 2){  
                if((year%4==0 &amp;&amp; year%100!=0) || (year%400==0)){    
                //如果为闰年   
                    if(day >=1 &amp;&amp; day <=29){  
                        day += 2;  
                        if(day > 29){  
                            month += 1;  
                            day = 1;  
                        }  
                        if(month > 12){  
                            year += 1;  
                            month = 1;  
                        }  
                    }else{  
                       flag=0;  
                       cout<<"您输入的日期不存在"<<endl<<endl;  
                    }  
                }  
                else{  
                    if(day >=1 &amp;&amp; day <=28){  
                        day += 2;  
                        if(day > 28){  
                            month += 1;  
                            day = 1;  
                        }  
                        if(month > 12){  
                            year += 1;  
                            month = 1;  
                        }  
                    }else{  
                       flag=0;  
                       cout<<"您输入的日期不存在"<<endl<<endl;  
                    }  
                }  
            }  
            else{  
                cout<<"您输入的月份不存在"<<endl<<endl;  
            }  
          }  
          if(flag==1){  
            cout<<"后天的日期为: "<<year<<"年"<<month<<"月"<<day<<"日"<<endl<<endl;  
          }  
        }  
        return 0;  
    }  
    import java.util.Scanner;  

    public class CalculateTheDayAfterTomorrow {  

        public static boolean checkNum(char c) {  
            // 判断当前字符是否为数字  
            if (c >= '0' &amp;&amp; c <= '9')  
                return true;  
            return false;  
        }  

        public static boolean checkString(String str) {  
            // 判断字符串是否为指定的日期格式:yyyy-mm-dd  
            if (checkNum(str.charAt(0)) &amp;&amp; checkNum(str.charAt(1))  
                    &amp;&amp; checkNum(str.charAt(2)) &amp;&amp; checkNum(str.charAt(3))  
                    &amp;&amp; (str.charAt(4) == '-') &amp;&amp; checkNum(str.charAt(5))  
                    &amp;&amp; checkNum(str.charAt(6)) &amp;&amp; (str.charAt(7) == '-')  
                    &amp;&amp; checkNum(str.charAt(8)) &amp;&amp; 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("您输入的日期格式不符合要求" + "\n");  
                } else {  
                    // 计算年月日  
                    year = (str.charAt(0) - '0') * 1000 + (str.charAt(1) - '0')  
                            * 100 + (str.charAt(2) - '0') * 10  
                            + (str.charAt(3) - '0');  
                    month = (str.charAt(5) - '0') * 10 + (str.charAt(6) - '0');  
                    day = (str.charAt(8) - '0') * 10 + (str.charAt(9) - '0');  
                    // 每个月的天数不一样,分类讨论  
                    if (month == 1 || month == 3 || month == 5 || month == 7  
                            || month == 8 || month == 10 || month == 12) {  
                        if (day >= 1 &amp;&amp; day <= 31) {  
                            day += 2;  
                            if (day > 31) { // 已经跨月了  
                                month += 1;  
                                day -= 31;  
                            }  
                            if (month > 12) { // 后两天跨年了  
                                year += 1;  
                                month = 1;  
                            }  
                        } else {  
                            flag = 0;  
                            System.out.println("您输入的日期不存在" + "\n");  
                        }  
                    } else if (month == 4 || month == 6 || month == 9  
                            || month == 11) {  
                        if (day >= 1 &amp;&amp; day <= 30) {  
                            day += 2;  
                            if (day > 30) {  
                                month += 1;  
                                day -= 30;  
                            }  
                            if (month > 12) {  
                                year += 1;  
                                month = 1;  
                            }  
                        } else {  
                            flag = 0;  
                            System.out.println("您输入的日期不存在" + "\n");  
                        }  
                    } else if (month == 2) {  
                        if ((year % 4 == 0 &amp;&amp; year % 100 != 0) || (year % 400 == 0)) {  
                            // 如果为闰年  
                            if (day >= 1 &amp;&amp; day <= 29) {  
                                day += 2;  
                                if (day > 29) {  
                                    month += 1;  
                                    day -= 29;  
                                }  
                                if (month > 12) {  
                                    year += 1;  
                                    month = 1;  
                                }  
                            } else {  
                                flag = 0;  
                                System.out.println("您输入的日期不存在" + "\n");  
                            }  
                        } else {// 平年  
                            if (day >= 1 &amp;&amp; day <= 28) {  
                                day += 2;  
                                if (day > 28) {  
                                    month += 1;  
                                    day -= 28;  
                                }  
                                if (month > 12) {  
                                    year += 1;  
                                    month = 1;  
                                }  
                            } else {  
                                flag = 0;  
                                System.out.println("您输入的日期不存在" + "\n");  
                            }  
                        }  
                    } else {  
                        flag = 0;  
                        System.out.println("您输入的月份不存在" + "\n");  
                    }  
                }  
                if (flag == 1) { // 通过flag标志输入的日期是否合法  
                    System.out.println("后天的日期为: " + year + "年" + month + "月" + day  
                            + "日" + "\n");  
                }  
            }  
        }  
    }  

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论