您的位置:首页 > 编程语言 > C语言/C++

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

2015-05-27 10:51 519 查看
#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<<"您输入的日期格式不符合要求"<<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 > 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 && 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 && year%100!=0) || (year%400==0)){
//如果为闰年
if(day >=1 && 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 && 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' && c <= '9')
return true;
return false;
}

public static boolean checkString(String str) {
// 判断字符串是否为指定的日期格式:yyyy-mm-dd
if (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("您输入的日期格式不符合要求" + "\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 && 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 && 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 && year % 100 != 0) || (year % 400 == 0)) {
// 如果为闰年
if (day >= 1 && 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 && 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");
}
}
}
}




转载请注明出处:http://blog.csdn.net/lindonglian/article/details/46043637
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: