2024年10月getmonthdays(C++万年历)

 更新时间:2024-10-12

  ⑴getmonthdays(C++万年历

  ⑵//LZ你好...花了一整个上午的时间,总算是按你的要求把程序都写好了...//原来的程序有点小错误,我都修正了,还有把代码精简了一下,程序在下面//但我在这里要说明一下,按你老师给的算星期的公式,输出的结果是和你照片上的一样,但却和实际不符!//实际的年月号是星期二,而不是星期日,你可以打开自己的系统时间看一下//我这里的程序输出结果是和你照片上一样的,如果你想改正成和现实一样可以自己改一下date::week(),或者联系我修改也可以/////////////date.h//////////////////////#ifndef_date_h#define_date_h#include《iostream》usingnamespacestd;classdate{private:intyear;intmonth;intday;public:date(inty=,intm=,intd=):year(y),month(m),day(d){}voidsetyear(inty){year=y;}voidsetmonth(intm){month=m;}voidsetday(intd){day=d;}intgetyear()const{returnyear;}intgetmonth()const{returnmonth;}intgetday()const{returnday;}dateoperator+(intdays);dateoperator-(intdays);staticboolisleapyear(inty){return(y%==&&y%!=||y%==);}staticintgetyeardays(inty){returnisleapyear(y)?:;}staticintgetmonthdays(constdate&d);staticintdatetonum(constdate&d);staticdatenumtodate(intn);staticintweek(constdate&d){return(datetonum(d)+-)%;}};ostream&operator《《(ostream&out,constdate&d);istream&operator》》(istream∈,date&d);#endif/////////////date.cpp//////////////////////#include“date.h“datedate::operator+(intdays){intn=datetonum(*this)+days;returnnumtodate(n);}datedate::operator-(intdays){intn=datetonum(*this)-days;returnnumtodate(n);}intdate::getmonthdays(constdate&d){switch(d.getmonth()){case:case:case:case:case:case:case:return;case:case:case:case:return;case:returnisleapyear(d.getyear())?:;}}intdate::datetonum(constdate&d){inty,n=;for(y=;y《d.getyear();y++)n+=getyeardays(y);for(intm=;m《d.getmonth();m++)n+=getmonthdays(date(y,m,));n+=d.getday();returnn;}datedate::numtodate(intn){inty,m,d,rest=n;for(y=,rest=n;rest》getyeardays(y);y++)rest-=getyeardays(y);for(m=;rest》getmonthdays(date(y,m,));m++)rest-=getmonthdays(date(y,m,));d=rest;returndate(y,m,d);}ostream&operator《《(ostream&out,constdate&d){out《《d.getyear()《《“年“《《d.getmonth()《《“月“《《d.getday()《《“日“;returnout;}istream&operator》》(istream∈,date&d){intyear,month,day;cin》》year》》month》》day;d=date(year,month,day);returnin;}/////////////main.cpp//////////////////////#include《iomanip》#include“date.h“inlinevoidprintyear(inty){cout《《setw()《《y《《“年“《《endl;}inlinevoidprintline(){cout《《“----------------------------------------------------“《《endl;}voidprintmonth(intm){charmonth={““,“一“,“二“,“三“,“四“,“五“,“六“,“七“,“八“,“九“,“十“,“十一“,“十二“};cout《《setw()《《month《《“月“《《endl;}inlinevoidprintweek(){cout《《“星期日星期一星期二星期三星期四星期五星期六“《《endl;}voidprintday(intmd,intweek){intline=;for(intblank=week;blank;--blank,++line)cout《《setw()《《““;for(intd=;d《=md;++d,++line){cout《《setw()《《d;if(line%==)cout《《endl;}cout《《endl;}intmain(){inty;cout《《“输入年份:“;cin》》y;datelz(y);printyear(lz.getyear());for(intm=;m《=;++m){lz.setmonth(m);printline();printmonth(m);printweek();printday(lz.getmonthdays(lz),lz.week(lz));printline();}return;}

  ⑶您好,关于Java程序的问题,万年历插入背景图片

  ⑷要为万年历插入背景图片,每个月份都采用不同的背景图片,需要在JPanel的paintponent函数内,用g.drawImage方法,把背景图片画在JPanel上。

  ⑸另外,你的程序有问题年六月没有天。

  ⑹下面我给你一个例子。(注意:需要把图片文件放在项目名文件夹下)

  ⑺import?java.awt.*;import?java.awt.event.*;import?java.util.Calendar;import?javax.swing.*;public?class?K?extends?JFrame?implements?ActionListener{?JButton?jb=new?JButton(“《《“);?JButton?jb=new?JButton(“《“);?JButton?jb=new?JButton(“》“);?JButton?jb=new?JButton(“》》“);?JPanel?jp=new?JPanel();?JPanel?jp=new?JPanel();?JImagePanel?jp=new?JImagePanel();?JPanel?jp=new?JPanel();?JLabel?jl=new?JLabel();?JLabel?jl=new?JLabel();?JLabel;?String?week={“Sun“,“Mon“,“Tue“,“Wed“,“Thu“,“Fri“,“Sat“};?String?bgImageFiles={“spring.jpg“,“spring.jpg“,“spring.jpg“,“summer.jpg“,“summer.jpg“,“summer.jpg“???,“autumn.jpg“,“autumn.jpg“,“autumn.jpg“,“winter.jpg“,“winter.jpg“,“winter.jpg“};?Calendar?c=Calendar.getInstance();?int?year,month,day;?int?nowyear,nowmonth,nowday;?K(){??super(“简单日历“);??nowyear=c.get(Calendar.YEAR);??nowmonth=c.get(Calendar.MONTH)+;??nowday=c.get(Calendar.DAY_OF_MONTH);??year=nowyear;??month=nowmonth;??day=nowday;??jp.setImage(bgImageFiles);??String?s=year+“年“+month+“月“;??jl.setForeground(Color.RED);??jl.setText(s);??jb.addActionListener(this);??jb.addActionListener(this);??jb.addActionListener(this);??jb.addActionListener(this);??jp.add(jb);jp.add(jb);jp.add(jl);jp.add(jb);jp.add(jb);??jp.setLayout(null);??createMonthPanel();??jp.add(jp);??jl.setText(“今天是“+nowyear+“年“+nowmonth+“月“+nowday+“日“);??jp.add(jl);??add(jp,BorderLayout.NORTH);??add(jp,BorderLayout.CENTER);??add(jp,BorderLayout.SOUTH);??setSize(,);??setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??setLocationRelativeTo(null);??setVisible(true);?}?Override?public?void?actionPerformed(ActionEvent?ae)?{??if(ae.getSource()==jb){???year=year-;???String?s=year+“年“+month+“月“;???jl.setText(s);???jp.removeAll();???createMonthPanel();???jp.setImage(bgImageFiles);???jp.validate();???jp.repaint();??}??if(ae.getSource()==jb){???if(month==){????year=year-;????month=;???}else{????month=month-;???}???String?s=year+“年“+month+“月“;???jl.setText(s);???jp.removeAll();???createMonthPanel();???jp.setImage(bgImageFiles);???jp.validate();???jp.repaint();??}??if(ae.getSource()==jb){???if(month==){????year=year+;????month=;???}else{????month=month+;???}???String?s=year+“年“+month+“月“;???jl.setText(s);???jp.removeAll();???createMonthPanel();???jp.setImage(bgImageFiles);???jp.validate();???jp.repaint();??}??if(ae.getSource()==jb){???year=year+;???String?s=year+“年“+month+“月“;???jl.setText(s);???jp.removeAll();???createMonthPanel();???jp.setImage(bgImageFiles);???jp.validate();???jp.repaint();??}?}?public?static?void?main(String?args)?{??new?K();?}?public?int?getMonthDays(int?year,?int?month)?{???switch?(month)?{???case?:????case?:????case?:????case?:???case?:????case?:????case?:????return?;????case?:?????if?((year%==&&year%!=)||year%==)?{??????return?;?????}?else?{??????return?;?????}????default:?????return?;???}??}??public?void?createMonthPanel(){??c.set(year,?month-,?getMonthDays(year,month));??int?weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);??if(weekOfMonth==){???jp.setLayout(new?GridLayout(,));???jp.setBounds(,?,?,?);??}else{???jp.setLayout(new?GridLayout(,));???jp.setBounds(,?,?,?);??}??jp.setBorder(BorderFactory.createEtchedBorder());??for(int?i=;i《;i++){???jl,JLabel.CENTER);???jp.add(jl);??}??c.set(year,?month-,?);??int?emptyFirst=c.get(Calendar.DAY_OF_WEEK)-;??int?daysOfMonth=getMonthDays(year,month);??for(int?i=+emptyFirst;i》=;i--){???int?intyear=year;???int?intmonth=month;???if(intmonth==){????intyear=intyear-;????intmonth=;???}else{????intmonth=intmonth-;???}???int?intdays=getMonthDays(intyear,intmonth);???jl=new?JLabel((intdays+-i)+““,JLabel.CENTER);???jl.setForeground(Color.GRAY);???jp.add(jl);??}??for(int?i=+emptyFirst;i《daysOfMonth++emptyFirst;i++){???jl=new?JLabel((i--emptyFirst+)+““,JLabel.CENTER);???if((i+)%==?||?(i+)%==?||?(i--emptyFirst+)==nowday&&month==nowmonth&&year==nowyear)????jl.setForeground(Color.RED);???else????jl.setForeground(Color.BLACK);???jp.add(jl);??}??if(weekOfMonth==)???for(int?i=;i》=daysOfMonth+emptyFirst+;i--){????jl=new?JLabel((-i)+““,JLabel.CENTER);????jl.setForeground(Color.GRAY);????jp.add(jl);???}??else???for(int?i=;i》=daysOfMonth+emptyFirst+;i--){????jl=new?JLabel((-i)+““,JLabel.CENTER);????jl.setForeground(Color.GRAY);????jp.add(jl);???}?}?class?JImagePanel?extends?JPanel{??ImageIcon?imageIcon;??JImagePanel(){???setOpaque(true);??}??public?void?setImage(String?imageFile){???imageIcon=new?ImageIcon(imageFile);??}??public?void?paintponent(Graphics?g){???super.paintponent(g);???Image?image=imageIcon.getImage();???c.set(year,?month-,?getMonthDays(year,month));???int?weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);???if(weekOfMonth==){????g.drawImage(image,?,?,,,?this);???}else{????g.drawImage(image,?,?,,,?this);???}??}?}}

  ⑻JS获取指定月份的天数两种实现方法

  ⑼最近看到一个有意思的试题,正好在需求中也碰到类似的问题,即计算某个月的天数问题。碰到类似问题也许大部分会想是不是还要判断闰年、平年,如果这样想的话就复杂了,下面给出具体的计算方法。获取月份天数方法一://传入年份和月份获取该年对应月份的天数functiongetMonthDays(year,month){varthisDate=newDate(year,month,);//当天数为js自动处理为上一月的最后一天returnthisDate.getDate();}上面应该时获取月份天数最简单的方法之一了,当Date中参数超出范围时js会自动处理,如果传值小于等于,则日期将会向前推,否则日期将会向后推。下面时MDN原文,也提到了此现象:Note:当Date作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为或者分钟数为,相邻的数值会被调整。比如newDate(,,)等于newDate(,,),它们都表示日期--(注意月份是从开始的。其他数值也是类似,newDate(,,,,)等于newDate(,,,,),都表示时间--T::。下面给出几个例子供参考:.参数传入负数.传入大于规定范围的值以上可看出,js本身对日期处理还是比较好的,同时开发人员也可以利用这一点获取想要的结果。获取月份天数方法一:下面就是比较正常的获取方式了,代码://根据两个月份同一天的差值获取,同样是传入需要获取的对应年份和月份functiongetMonthDays(year,month){varstratDate=newDate(year,month-,),endData=newDate(year,month,);vardays=(endData-stratDate)/(***);returndays;}第二种方式则是根据所求月份和下一个月份某一天的差值获取对应月份的天数。总结以上所述是小编给大家介绍的JS获取指定月份的天数两种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!您可能感兴趣的文章:JS获取月份最后天数、最大天数与某日周数的方法JS获取月的最后一天与JS得到一个月份最大天数的实例代码

  ⑽java万年历总是日期不对,求教!!

  ⑾importjava.util.Scanner;publilassbxm{/***paramargs*/SuppressWarnings(“resource“)publicstaticvoidmain(Stringargs){//TODOAuto-generatedmethodstubinti;intyear;intmonth;intdw;intmonthday=;intww=;inttotalday=;System.out.println(“*********欢迎使用万年历××××××××ד);System.out.println(“

  ⑿请输入年份“);Scannerinput=newScanner(System.in);year=input.nextInt();System.out.println(“

  ⒀请输入月份“);Scannerinput=newScanner(System.in);month=input.nextInt();switch(month){case:case:case:case:case:case:case:monthday=;break;case:case:case:case:monthday=;break;case:if(year%==&&year%!=||year%==)//是!=monthday=;elsemonthday=;break;default:break;}for(i=;i《year;i++){totalday+=;}for(i=;i《year;i++){if(i%==&&i%!=||i%==)//是!=,同时使用的是i不是yeartotalday++;}for(i=;i《month;i++)//《=去掉={switch(i){case:case:case:case:case:case:case:totalday+=;break;case:case:case:case:totalday+=;break;case:if(year%==&&year%!=||year%==)////是!=totalday+=;elsetotalday+=;break;}}dw=(ww+totalday)%;System.out.println(“打印这个月月历如下“);System.out.println(“日一二三四五六“);for(i=;i《=dw;i++){System.out.print(““);}for(i=;i《=monthday;i++){System.out.print(““+i);if((i+dw)%==)System.out.println(““);}}}//好像就改了这些,再有不同你自己找吧

  ⒁java编写某年某月有多少天

  ⒂importjava.util.Scanner;publilassDayOfMonth{publicstaticvoidmain(Stringargs){Scanners=newScanner(System.in);System.out.println(“请输入年“);intyear=s.nextInt();System.out.println(“请输入月“);intmonth=s.nextInt();switch(month){case:case:case:case:case:case:case:System.out.println(year+“年“+month+“月有天“);break;case:case:case:case:System.out.println(year+“年“+month+“月有天“);break;case:if((year%==&&year%!=)||year%==)System.out.println(year+“年月有天“);elseSystem.out.println(year+“年月有天“);break;}}}这样可以了么?

  ⒃在java中如何编写‘’输出两个月后的今天,是星期几‘’

  ⒄Scannersc=newScanner(System.in);intday=,month=;do{day=sc.nextInt();//输入星期几(到}while(day《=&&day》);do{month=sc.nextInt();//输入当前几月(到}while(month《=&&month》);sumday=getmonthdays((month+-(month+/)+getmonthdays((month+)-(month+)/);然后算出总天数%加上星期几(如果超过减去intgetday=sumday%+day;getday=getday》?getday-:getday;publicintgetmonthdays(intmonth){......}//这个方法自己写,用switchcase判断一个月多少天(二月的话还需要输入年份判断是否闰年,闰年则,否则望采纳

  ⒅用C++怎么算任意两个时间段的天数如年月日到年月日期间有几天

  ⒆先做个tm结构,然后用mktime变成time_t,两个time_t的差就是之间的秒数,换算天数的话就除*就好了。代码大致上这样,需要time.hstructtmtm={},tm={};tm.tm_year=;tm.tm_mon=;//注意月份是从开始的tm.tm_mday=;//月份日期是从开始的tm.tm_year=;tm.tm_mon=;tm.tm_mday=;intdiff=(int)(mktime(tm)-mktime(tm));cout《《diff/(*)《《endl;

  ⒇#?-*-?coding:?cp?-*-#设置星期天的初始值为mondays=def?getmonthdays(year):????isleapyear=year%==?or?(year%==?and?(not?year%==))????if?isleapyear:????????return?????return?#计算..(这天是星期天..之间的天数pastdays=??#..过一天是..monthdays=getmonthdays()for?month?in?range?(,):????pastdays+=monthdays#计算..到..星期天的数字for?year?in?range(,):????monthdays=getmonthdays(year)????for?month?in?range(,):????????if?pastdays%==:????????????mondays+=????????pastdays+=monthdaysprint?“年月月至年月日共有%d个星期天落在每月第一天“%mondays

  ⒈c++用outtextxy(输出日历应如何实现:每行个如何使日期连续.

  ⒉//这是C的代码//这个代码在VC.可以编译运行的dev没试过...你看看合适不....//输入的时候如果是年月,你就要输入-输入格式你要改的话改那个scanf语句//这题才给分这么小气......你多给点吧....#include《stdio.h》intGetWeekdayDay(int,int,int);//用于计算输入月份的一号是星期几intGetMonthDays(int,int);//用于计算所输入日期所属月份天数voidPrintMonthCalender(int,int);//用于打印该月日历intCheckLeap(int);//闰年的判断main(){intyear,month;//存储输入的年月intstartDay;//存储当月一号是星期几intdays;//存储当月的天数intday=;printf(“inputtheyearandmonth《YYYY-MM》:“);scanf(“%d-%d“,&year,&month);startDay=GetWeekdayDay(year,month,day);//由函数GetWeekdayDay得到当月一号是星期几days=GetMonthDays(year,month);//由函数GetMonthDays得到当月的天数printf(“ %d-%d

  ⒊“,year,month);PrintMonthCalender(startDay,days);//由函数PrintMonthCalender打印日历system(“pause“);}intCheckLeap(intyear)//定义函数CheckLeap{if((year%!=)||(year%==&&year%!=))return;//如果不是闰年,返回值为elsereturn;//如果是闰年,返回值为}intGetWeekdayDay(intyear,intmonth,intday)//定义函数GetWeekdayDay{inttotalDays;//表示当月第一天是公元后的天数if(CheckLeap(year)==)//如果是闰年{switch(month){case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;}}else//如果不是闰年{switch(month){case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;case:totalDays=(year-)*+(year-)/+(year-)/-(year-)/+;break;}}returntotalDays%;//返回值为当月第一天是星期几,为周日}intGetMonthDays(intyear,intmonth)//定义函数GetMonthDays{if(CheckLeap(year)==)//如果是闰年{if(month==||month==||month==||month==)return;if(month==)return;if(month==||month==||month==||month==||month==||month==||month==)return;}else//如果不是闰年{if(month==||month==||month==||month==)return;if(month==)return;if(month==||month==||month==||month==||month==||month==||month==)return;}}voidPrintMonthCalender(intstartDay,intdays)//定义函数PrintMonthCalender{inti;//用于循环计数intday;//循环中用于表示当天多少号printf(“Sun. Mon. Tue. Wed. Thu. Fri. Sat.

  ⒋“);for(i=;i《=startDay;i++){printf(“ “);}for(day=;day《=days;day++){printf(“%d “,day);if(startDay!=)//当月一号不是星期天时,若如下表达式成立则此天为周六,则后加换行{if((day%+startDay-)==){printf(“

  ⒌“);}}else//当月一号是是星期天时,当如下表达式成立则此天为周六,则后加换行if(day!=&&(day%+startDay)==){printf(“

  ⒍“);}}printf(“

  ⒎JAVA如何得到给定日期所在月份天数的集合

  ⒏importjava.util.Calendar;publilassDu{publicstaticvoidmain(Stringargs){Stringdate=“--“;intdaysInMonth=getMonthDays(date);for(intvalue:daysInMonth){System.out.print(value+““);}}privatestaticintgetMonthDays(Stringdate){Calendarcalendar=Calendar.getInstance();calendar.set(Calendar.YEAR,Integer.parseInt(date.substring(,)));calendar.set(Calendar.MONTH,Integer.parseInt(date.substring(,))-);intmaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);int;for(inti=;i《=maxDay;i++){days=i;}returndays;}}-----------------

您可能感兴趣的文章:

相关文章