C语言作业,急急急

englishqaaa 2014-04-23 05:40:00
输入任意一个日期的年、月、日的值,求出从公元1年1月1日到该日期前一年的年末总共有多少天,到该日期前一个月的月末总共有多少天,到这一天总共有多少天,并求出这一天是星期几。

...全文
216 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Symfund 2014-05-09
  • 打赏
  • 举报
回复
这个作业出得好,引申一下,顺便把万年历也给做了!
  • 打赏
  • 举报
回复
#include <iostream> using namespace std; int main() { int y,m,d;//定义变量y,m,d分别表示所求的那天的年月日 int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};//定义数组记录平年十二个月每一月的天数 int n,N;//定义n记录在要求的那一年以前的闰年的个数,定义N记录从纪年开始到所求的那天经过的天数 cout<<"请按顺序输入所求的那天的年月日:"<<endl;//输出"请按顺序输入所求的那天的年月日:" cin>>y>>m>>d;//输入所求的那天的年月日 if(y%400==0||(y%4==0&&y%100!=0))mon[1]=29;//若所求的那天所在年份是闰年,将数组中二月对应的天数数值改为29 n=(y-1)/4-((y-1)/100-(y-1)/400);//计算在要求的那天所在的年份之前有多少个闰年并赋给n N=365*(y-1)+n;//计算在要求的那天所在的年份之前过了多少天并赋给N for(int i=0;i<m-1;i++) N+=mon[i];//将这一年中,要求的那天所在的月份之前的天数加给N N+=d;//N再加上这个月度过的天数即得从纪年开始到所求的那经过的总天数 switch (N%7){//N对7 求余,由公元一年一月一日是星期一有以下结果 case 0: cout<<"这一天是星期日"<<endl;break;//若余数为0,则输出"这一天是星期日" case 1: cout<<"这一天是星期一"<<endl;break;//若余数为1,则输出"这一天是星期一" case 2: cout<<"这一天是星期二"<<endl;break;//若余数为2,则输出"这一天是星期二" case 3: cout<<"这一天是星期三"<<endl;break;//若余数为3,则输出"这一天是星期三" case 4: cout<<"这一天是星期四"<<endl;break;//若余数为4,则输出"这一天是星期四" case 5: cout<<"这一天是星期五"<<endl;break;//若余数为5,则输出"这一天是星期五" default: cout<<"这一天是星期六"<<endl;//若余数为6,则输出"这一天是星期六" } system("pause"); return 0;//结束程序 }
  • 打赏
  • 举报
回复
引用 楼主 englishqaaa 的回复:
输入任意一个日期的年、月、日的值,求出从公元1年1月1日到该日期前一年的年末总共有多少天,到该日期前一个月的月末总共有多少天,到这一天总共有多少天,并求出这一天是星期几。
这个算法是有的你可以在百度搜一下"输入日期算星期几的算法"就包含了你所需的所有问题。
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
COleDateTime::GetDayOfYear int GetDayOfYear( ) const; Return Value The day of the year represented by the value of this COleDateTime object. Remarks Call this member function to get the day of the year represented by this date/time value. Valid return values range between 1 and 366, where January 1 = 1. If the status of this COleDateTime object is not valid, the return value is AFX_OLE_DATETIME_ERROR. For information on other member functions that query the value of this COleDateTime object, see the following member functions: GetDay GetMonth GetYear GetHour GetMinute GetSecond GetDayOfWeek COleDateTime Overview | Class Members | Hierarchy Chart See Also COleDateTime::COleDateTime, COleDateTime::SetDateTime, COleDateTime::operator =, COleDateTime::GetStatus
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
COleDateTime::GetDayOfWeek int GetDayOfWeek( ) const; Return Value The day of the week represented by the value of this COleDateTime object. Remarks Call this member function to get the day of the month represented by this date/time value. Valid return values range between 1 and 7, where 1=Sunday, 2=Monday, and so on. If the status of this COleDateTime object is not valid, the return value is AFX_OLE_DATETIME_ERROR. For information on other member functions that query the value of this COleDateTime object, see the following member functions: GetDay GetMonth GetYear GetHour GetMinute GetSecond GetDayOfYear COleDateTime Overview | Class Members | Hierarchy Chart See Also COleDateTime::COleDateTime, COleDateTime::SetDateTime, COleDateTime::operator =, COleDateTime::GetStatus
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
仅供参考
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
    COleDateTime t;
    COleDateTimeSpan ts;
    CString s,fmt;
    int nYear;
    int nMonth;
    int nDay;
    int nHour;
    int nMin;
    int nSec;
    int lDays;
    int nHours;
    int nMins;
    int nSecs;
    int i,N;
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
        printf("Fatal Error: MFC initialization failed\n");
        return 1;
    }
    if (argc<13) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 2;
    }
    if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
    else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
    else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
    else {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 3;
    }
    nYear =atoi(argv[ 1]);
    nMonth=atoi(argv[ 2]);
    nDay  =atoi(argv[ 3]);
    nHour =atoi(argv[ 4]);
    nMin  =atoi(argv[ 5]);
    nSec  =atoi(argv[ 6]);
    lDays =atoi(argv[ 7]);
    nHours=atoi(argv[ 8]);
    nMins =atoi(argv[ 9]);
    nSecs =atoi(argv[10]);
    N     =atoi(argv[11]);
    if (N<=0) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 4;
    }
    t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
    ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
    for (i=1;i<=N;i++) {
        s=t.Format(fmt);
        printf("%08d %s\n",i,s);
        t=t+ts;
    }
    return 0;
}
king1076 2014-05-08
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
[quote=引用 1 楼 king1076 的回复:] 考虑平年和闰年。想想逻辑
知识面太窄了,怎么当程序员? http://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86 [/quote] 多多练习,当初我刚学习的时候 期末的课程设计都不会写,后来慢慢的 多多练习 ,就会了
mujiok2003 2014-04-24
  • 打赏
  • 举报
回复
那去吧: http://blog.csdn.net/mujiok2003/article/details/8818662
zzz814390564 2014-04-24
  • 打赏
  • 举报
回复
星期的日期计算错了,你自己改吧。我写的很不正规,但是能运行,凑合吧 #include "stdio.h" void main() { int year,month,day,i,sum=0,j; int run[12]={31,29,31,30,31,30,31,31,30,31,30,31}; int pin[12]={31,28,31,30,31,30,31,31,30,31,30,31}; printf("请输入年份:"); scanf("%d",&year); printf("请输入月份:"); scanf("%d",&month); printf("请输入日期:"); scanf("%d",&day); if(year<2) { printf("*************\n"); printf("***错误 !!***\n"); printf("*************\n\n\n"); } for(i=2;i<year;i++) { if(i%4==0 && i%100!=0) sum=sum+366; else if(i%100==0 && i%400==0) sum=sum+366; else sum=sum+365; } if((year-1)%4==0 && (year-1)%100!=0 ) { for(i=0;i<month-1;i++) sum=sum+run[i]; } else if((year-1)%100==0 && (year-1)%400==0) { for(i=0;i<month-1;i++) sum=sum+run[i]; } else { for(i=0;i<month-1;i++) sum=sum+pin[i]; } printf("前一年的今天距离公元1年1月1日 %d 天\n\n",sum+day-1); if(year%4==0 && year%100!=0) sum=sum+366; else if(year%100==0 && year%400==0 ) sum=sum+366; else sum=sum+365; printf("今天距离公元1年1月1日 %d 天\n\n",sum); j=(sum%7); printf("今天是星期%d\n",j); }
threenewbee 2014-04-23
  • 打赏
  • 举报
回复
引用 1 楼 king1076 的回复:
考虑平年和闰年。想想逻辑
知识面太窄了,怎么当程序员? http://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86
king1076 2014-04-23
  • 打赏
  • 举报
回复
考虑平年和闰年。想想逻辑

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧