| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1406 人关注过本帖
标题:谁能帮帮我编个程序啊,输日期知道天数的哪种
只看楼主 加入收藏
chy4393963
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-10-11
收藏
 问题点数:0 回复次数:9 
谁能帮帮我编个程序啊,输日期知道天数的哪种
大家能用C++帮我编个程序吗,谢谢了。题目如下:

使用分支语句设计一个程序,要求输入年月日以后,算出这天是这一年的第几天。运行时输入输出情况下如下:
请输入日期(eg.2005 2 28): 2005 2 28
2005年2月28日是2005年的第59天。
提示:注意闰年,4年一闰,百年不闰,4百年又一闰。使用switch语句的时候要注意其break特性,尽量设计出较简洁的代码。



谢谢了,我实在做不出来了,大家帮帮忙发个来参考下也好
搜索更多相关主题的帖子: 天数 
2008-10-11 13:43
xieguoyong888
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-10-11
收藏
得分:0 
我也的想个半月吧,嘿嘿,不怎么会自学嘿嘿
2008-10-11 13:48
happycplusplus
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-10-10
收藏
得分:0 
好象你这个题目比较麻烦 要算法的
2008-10-11 22:07
沼泽
Rank: 4
等 级:业余侠客
威 望:8
帖 子:291
专家分:228
注 册:2008-9-15
收藏
得分:0 
#include<iostream.h>
#include<stdlib.h>
void main()
{
    int year,month,day;
    int month_2th;
    int n;
    cin>>year>>month>>day;
    if(year%4==0)            //判断是平年还是闰年
        if(year%100!=0) month_2th=-1;

        else if(year%400!=0) month_2th=-2;
            else month_2th=-1;
   
    else   month_2th=-2;

    if(month_2th==-2&&month==2&&day>29) //闰年二月等于29天
        exit(0);
    if(month_2th==-2&&month==2&&day>28) //平年二月等于28天
        exit(0);

    if(month<=2)
    {
        n=(month-1)*31+day-(month-1)/2;
        cout<<n<<endl;
    }

    else if(month<=12) //月份小于13
    {
        n=(month-1)*31+day-(month-1)/2+month_2th;
        cout<<n<<endl;
    }

    
}
2008-10-11 23:31
chy1047255
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-9-8
收藏
得分:0 
先感谢楼上的答案,但你用的是if else语句,我想知道怎么用switch语句来做。
2008-10-11 23:56
沼泽
Rank: 4
等 级:业余侠客
威 望:8
帖 子:291
专家分:228
注 册:2008-9-15
收藏
得分:0 
那楼主你自己再改点呗,又不是很难的,练习一下也不错的
2008-10-12 09:20
ytb745892798
Rank: 1
来 自:漳平。永福
等 级:新手上路
帖 子:63
专家分:0
注 册:2008-9-29
收藏
得分:0 
本人也来,不过觉得switch不好,以下是我的代码:
#include<iostream.h>
int leapyear(int year);
void main()
{int year,mon,day;
 static int day1[]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年
 static int day2[]={31,28,31,30,31,30.31,31,30,31,30,31};//非闰年
 cout<<"年:";
 cin>>year;
 cout<<"月:";
 cin>>mon;
 cout<<"日:";
 cin>>day;
int today=day;
for(int m=0;m<mon-1;m++)
 if(leapyear(year))today+=day1[m];
 else today+=day2[m];
cout<<year<<"年"<<mon<<"月"<<day<<"日是该年的第"<<today<<"天"<<endl;
}
int leapyear(int year)
{
 int leap;
 if(year%400==0)
 leap=1;
 else if(year%4==0 && year%100!=0)
 leap=1;
 else leap=0;
 return leap;
}

我的梦想是成为一名出色的程序员!
2008-10-12 11:40
ytb745892798
Rank: 1
来 自:漳平。永福
等 级:新手上路
帖 子:63
专家分:0
注 册:2008-9-29
收藏
得分:0 
楼主喜欢switch
那你可以从上面进行修改!

我的梦想是成为一名出色的程序员!
2008-10-12 11:42
happycplusplus
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-10-10
收藏
得分:0 
下面是我做的最简单的办法了  但是还有很多异常和条件都没有加  你看看符合不符合你要求
应该是很简单能看懂的switch方法  如果有高手还有别的方法请发上来 谢谢!~
#include <iostream.h>
#include <fstream.h>
void main()
{
    int n,y,days;char flag;
    do
    {
        cout<<"请输入年份:";cin>>n;
        cout<<"请输入月份:";cin>>y;
        cout<<"请输入日期:";cin>>days;
        cout<<n<<"年"<<y<<"月"<<days<<"日";
        int nextdays;
        nextdays=days;
        switch(y)
        {
            case 1:
                if(nextdays=days)
                    days=nextdays;
                else
                    days=31;
                break;

            case 2:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=nextdays+31;
                    else
                        days=29+31;
                }
                else
                {
                    if(nextdays=days)
                        days=nextdays+31;
                    else
                        days=28+31;
                }
                break;

            case 3:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+nextdays;
                    else
                        days=31+29+31;
                }
                else
                        days=31+28+31;
                break;

            case 4:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+nextdays;
                    else
                        days=31+29+31+30;
                }
                else
                        days=31+28+31+30;
                    break;

            case 5:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+nextdays;
                    else
                        days=31+29+31+30+31;
                }
                else
                        days=31+28+31+30+31;
                    break;

            case 6:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+nextdays;
                    else
                        days=31+29+31+30+31+30;
                }
                else
                        days=31+28+31+30+31+30;
                    break;
            

            case 7:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+nextdays;
                    else
                        days=31+29+31+30+31+30+31;
                }
                else
                        days=31+28+31+30+31+30+31;
                    break;
            

            case 8:    
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+31+nextdays;
                    else
                        days=31+29+31+30+31+30+31+31;
                }
                else
                        days=31+28+31+30+31+30+31+31;
                    break;

            case 9:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+31+31+nextdays;
                    else
                        days=31+29+31+30+31+30+31+31+30;
                }
                else
                        days=31+28+31+30+31+30+31+31+30;
                    break;
            

            case 10:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+31+31+30+nextdays;
                    else
                        days=31+29+31+30+31+30+31+31+30+31;
                }
                else
                        days=31+28+31+30+31+30+31+31+30+31;
                    break;

            case 11:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+31+31+30+31+nextdays;
                    else
                        days=31+29+31+30+31+30+31+31+30+31+30;
                }
                else
                        days=31+28+31+30+31+30+31+31+30+31+30;
                    break;
            
            case 12:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(nextdays=days)
                        days=31+29+31+30+31+30+31+31+30+31+30+nextdays;
                    else
                        days=31+29+31+30+31+30+31+31+30+31+30+31;
                }
                else
                        days=31+28+31+30+31+30+31+31+30+31+30+31;
                    break;

            default:
                days=0;
                break;
        }
        if(days==0)
            cout<<"月份输入错误!"<<endl;
        else
        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
    cout<<"是否继续<y/n>:";cin>>flag;
    }while(flag!='n');
}
2008-10-12 16:13
happycplusplus
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-10-10
收藏
得分:0 
帮你把月份,日期输入过范围的条件都给加入了  看看适合吗
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    int n,y,days;
    do
    {
        fstream f;
        f.open("date.txt",ios::out|ios::app);
            cout<<"请输入年份:";cin>>n;
            cout<<"请输入月份:";cin>>y;
            cout<<"请输入日期:";cin>>days;
        f<<n<<" "<<y<<" "<<days<<endl;
        f.close();

        int nextdays;
        nextdays=days;
        switch(y)
        {
            case 1:
                if(days>31)
                {
                    cout<<y<<"月不可能超过31天"<<endl;
                }else
                if(nextdays=days)
                   cout<<n<<"年1月"<<days<<"日"<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                break;

            case 2:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>29)
                    {
                        cout<<"闰年的"<<y<<"月不可能超过29天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年2月"<<days<<"日";
                        days=nextdays+31;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                {
                    if(days>28)
                    {
                        cout<<y<<"月不可能超过28天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年2月"<<days<<"日";
                        days=nextdays+31;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                break;

            case 3:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年3月"<<days;
                        days=31+29+nextdays;
                        cout<<"日"<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年3月"<<days<<"日";
                        days=31+28+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                break;

            case 4:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                {    
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                    cout<<n<<"年"<<y<<"月"<<days<<"日";
                    days=31+28+31+nextdays;
                    cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                    break;

            case 5:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                }
                    break;

            case 6:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                    break;
            

            case 7:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+30+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+30+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                    break;
            
            case 8:   
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+30+31+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>31)
                    {    
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+30+31+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                    break;

            case 9:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+30+31+31+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>30)
                    {    
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+30+31+31+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                    break;
            

            case 10:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+30+31+31+30+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>31)
                    {    
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+30+31+31+30+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                        break;

            case 11:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>30)
                    {
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+29+31+30+31+30+31+31+30+31+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>30)
                    {    
                        cout<<y<<"月不可能超过30天"<<endl;
                    }else
                        if(nextdays=days)
                        {
                            cout<<n<<"年"<<y<<"月"<<days<<"日";
                            days=31+28+31+30+31+30+31+31+30+31+nextdays;
                            cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                        }
                    break;
            
            case 12:
                if((n%4==0)&&(n%100!=0)||(n%400==0))
                {
                    if(days>31)
                    {
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";                    
                        days=31+29+31+30+31+30+31+31+30+31+30+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                }
                else
                    if(days>31)
                    {    
                        cout<<y<<"月不可能超过31天"<<endl;
                    }else
                    if(nextdays=days)
                    {
                        cout<<n<<"年"<<y<<"月"<<days<<"日";
                        days=31+28+31+30+31+30+31+31+30+31+30+nextdays;
                        cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
                    }
                    break;

            default:
                days=0;
                break;
        }
        if(days==0)
            cout<<"月份输入错误!"<<endl;
    system("pause");
    }while(1);
    
    fstream f;
    f.open("date.txt",ios::in);
    while(f>>n)
    {
        f>>y;
        f>>days;
    }
    cout<<n<<" "<<y<<" "<<days<<endl;
    f.close();
}
2008-10-12 20:06
快速回复:谁能帮帮我编个程序啊,输日期知道天数的哪种
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018532 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved