注册 登录
编程论坛 C++教室

[求助] 一个C++程序

yuziguang 发布于 2007-05-16 11:04, 1212 次点击
在线的C++编程高手我想求助的问题是:
任意输入一个年,月,日,然后输出这个日期是这一年的第几天.
10 回复
#2
aipb20072007-05-16 11:12
首先要判断日期的合法性了(注意2月,和大月小月),这个不难。

再,记录下每月的第一天是该年的第几天(都一样,不过,闰年有1天的差别)

输入一个日期,根据年判断是否多一天,少一天;根据月份选择记录点,再加上日就是这一年的第几天。



程序嘛,自己动手了
#3
yuziguang2007-05-16 13:02

这位大哥,首先谢谢你的及时回复.
你讲的那些我们的老师也跟我们讲过,但我是C++的初学者,以前从未接触过任何计算机语言.真的惭愧,程序我是实在编写不出,还望指教.
不慎感激!

#4
aipb20072007-05-16 13:27

好久没练练基础了,写了个,你自己看看吧!

[CODE]#include<iostream>
using namespace std;
struct date{
int year;
int month;
int day;
};
bool isLeapYear(const date &d){
if (d.year % 100 == 0){
if (d.year % 400 != 0)
return false;
}
else{
if (d.year % 4 != 0)
return false;
}
return true;
}

bool isValid(const date &d){
if (d.year < 1 || d.month < 1 || d.month > 12 || d.day < 1)
return false;
switch (d.month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (d.day > 31)
return false;
break;
case 4: case 6: case 9 : case 11:
if (d.day > 30)
return false;
break;
case 2:
if (isLeapYear(d)){
if (d.day > 29)
return false;
}
else{
if (d.day > 28)
return false;
}
break;
}
return true;
}
int countDay(const date &d){
int record[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
if (d.month <= 2)
return record[d.month-1]+d.day;
else{
if (isLeapYear(d))
return record[d.month-1]+1+d.day;
else
return record[d.month-1]+d.day;
}
}


int main(){
int year,month,day;
cin >> year >> month >> day;
date d = {year,month,day};
if (!isValid(d)){
cout << "invalid" << endl;
system("pause");
return 0;
}
cout << countDay(d) << endl;
system("pause");
}[/CODE]


#5
leeco2007-05-16 13:53


#include <iostream>
#include <numeric>
using namespace std;

const int arr[2][12+1]={
    {31,28,31,30,31,30,31,31,30,31,30,31},
    {31,29,31,30,31,30,31,31,30,31,30,31}
};

inline int isleapyear(int y)
{
    return (y%4==0 && y%100!=0 || y%400==0);
}

int main()
{
    int y,m,d;
    scanf(\"%d %d %d\",&y,&m,&d);
    printf(\"%d\n\",accumulate(arr[isleapyear(y)],arr[isleapyear(y)]+m-1,d));
}

#6
kisscjy2007-05-16 15:17

比较简单~~~
代码:

#include<iostream>
using namespace std;

void main()
{
int year,month,day;
int days=0,day1;
int x=0,i;

cout<<"请输入一个日期"<<endl;

cin>>year>>month>>day;

for(i=1;i<=month-1;i++)
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day1=31;break;
case 4:
case 6:
case 9:
case 11:day1=30;break;
case 2:if((year%4==0&&year%100!=0)||(year%400==0)) day1=29; //判断二月为大月或小月
else day1=28;
}
days=days+day1;

}
days=days+day;

cout<<"这是一年中的第"<<days<<"天!\n";
}

#7
yuziguang2007-05-16 18:30
真的非常感谢各位高手的回复  谢谢!
#8
neverDie2007-05-17 10:45
恩,恩,

后面两个怎么没判断有效性呢?
#9
kisscjy2007-05-17 17:43
这里只是给出大概思路而已~~~

而至于判断输入有效性的话就自己去想吧(也不会太难)~~

还不懂的话发帖问一下就好啦!

#10
baggio19852007-05-17 20:44
QUOTE:
以下是引用aipb2007在2007-5-16 13:27:11的发言:

好久没练练基础了,写了个,你自己看看吧!

[CODE]#include<iostream>
using namespace std;
struct date{
    int year;
    int month;
    int day;
};
bool isLeapYear(const date &d){
    if (d.year % 100 == 0){
        if (d.year % 400 != 0)
            return false;
    }
    else{
        if (d.year % 4 != 0)
            return false;
    }
    return true;
}
    
bool isValid(const date &d){
    if (d.year < 1 || d.month < 1 || d.month > 12 || d.day < 1)
        return false;
    switch (d.month){
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            if (d.day > 31)
                return false;
            break;
        case 4: case 6: case 9 : case 11:
            if (d.day > 30)
                return false;
            break;
        case 2:
            if (isLeapYear(d)){
                if (d.day > 29)
                    return false;
            }
            else{
                if (d.day > 28)
                    return false;
            }
            break;
    }
    return true;
}
int countDay(const date &d){
    int record[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    if (d.month <= 2)
        return record[d.month-1]+d.day;
    else{
        if (isLeapYear(d))
            return record[d.month-1]+1+d.day;
        else
            return record[d.month-1]+d.day;
    }
}
    
            
int main(){
    int year,month,day;
    cin >> year >> month >> day;
    date d = {year,month,day};
    if (!isValid(d)){
        cout << "invalid" << endl;
        system("pause");
        return 0;
    }
    cout << countDay(d) << endl;
    system("pause");
}[/CODE]

创建了struct  这种程序还没用过 都是直接上数组

#11
天天tyler2007-05-17 22:53
回复:(yuziguang)[求助] 一个C++程序

初学者代码可以简单点`我写了个比较简单容易看的``仅供参考``

#include <iostream>
using namespace std;

bool Isleap(int y)
{
return y%4==0 && y%100!=0 || y%400==0;
}

int Dom(int y , int m )
{
int d ;
if(m==2)
{
if(Isleap(y))
d = 29;
else
d = 28;
}
else if(m%2==1 && m<=7 || m%2==0 && m>=8)
d = 31;
else
d = 30 ;
return d;
}


int coutday(int y , int m , int d)
{
int day=0,i;
for(i=1;i<m;i++)
day = day + Dom(y , i);
day = day + d;
return day;
}

int main()
{
int year , month , day;
cout<<"please input year , month , day:"<<endl;
cin>>year>>month>>day;
cout<<"the reason is"<<coutday(year,month,day)<<endl;
return 0;
}

1