c++编程作业

youngtree003 2008-03-22 09:45:50
请高手用c++编一下啊:输入年月日,问这是一年的第几天?要考虑闰年,判断月日的合法性

如输入2008,3,18 输出为78
...全文
183 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
herman~~ 2008-03-24
  • 打赏
  • 举报
回复
这个题有空玩玩不错
clhposs 2008-03-24
  • 打赏
  • 举报
回复
利用循环加数组更加简便一些
RookieAC 2008-03-24
  • 打赏
  • 举报
回复
to lz:

这种基础的俄作业题一定要自己编写
否则到大4毕业你就会后悔了!!!

当然,家里有关系或者不想搞计算机的话,就当我没说
happyhpy 2008-03-24
  • 打赏
  • 举报
回复
没事干就帮你编了个 水平有限~

用if()语句的方法:

#include<iostream>
using namespace std;
int main()
{
cout<<"请输入年、月、日:";
int y,m,d;
cin>>y>>m>>d;
int er,n=0;
if((y%4==0&&y%100!=0)||(y%400==0))
er=29;
else
er=28;
if(y<=0||m<=0||m>12||d<=0||(((m<=7&&m%2==1)||(m>=8&&m%2==0))&&d>31)||(((m<=7&&m%2==0)||(m>=8&&m%2==1))&&d>30)||(m==2&&d>er))
{
cout<<"error!"<<endl;return -1;
}
else
if(m>=3&&m<=7)n=31*(m/2)+30*((m-3)/2)+d+er;
else
if(m>7)n=31*4+30*2+31*((m-7)/2)+30*((m-8)/2)+er+d;
else
n=31*(m-1)+d;
cout<<"这是这年的第"<<n<<"天。"<<endl;
return 0;
}


用switch()语句的方法:

#include<iostream>
using namespace std;
int main()
{
int er,year,month,day,sum=0;
cout<<"请输入年月日:";
cin>>year>>month>>day;
if((year%4==0&&year%100!=0)||(year%400==0))
er=29;
else
er=28;
if(year<=0||month<=0||month>12||day<=0||(((month<=7&&month%2==1)||(month>=8&&month%2==0))&&day>31)||(((month<=7&&month%2==0)||(month>=8&&month%2==1))&&day>30)||(month==2&&day>er))
{
cout<<"error!"<<endl;return -1;
}
for(int num=1;num<month;num++)
{
switch(num)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:sum+=31;break;
case 4:
case 6:
case 9:
case 11:sum+=30;break;
case 2:sum+=er;break;
}
}
sum=sum+day;
cout<<"这天是这年的第"<<sum<<"天。"<<endl;
return 0;
}
sunnywyg 2008-03-24
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
int year,mon,day,num=0;
cin>>year>>mon>>day;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int temp=0;
if(year%400==0) temp=1;
else if(year%100==0) temp=0;
else if(year%4==0) temp=1;
else temp=0;
month[2]+=temp;
for(int i=0;i<mon;i++) num+=month[i];
num+=day;
cout<<num<<endl;
system("pause");
}


DEV下通过
sunnywyg 2008-03-24
  • 打赏
  • 举报
回复
其实7楼的才是正解
qmm161 2008-03-24
  • 打赏
  • 举报
回复
每月天数用数组实现比较好!
quchan 2008-03-22
  • 打赏
  • 举报
回复
int DayofYear(int iYear , int iMonth , int iDay)
{
int iResult = 0 ;
int DayofMonth[12] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31};
if ( iYear%400 == 0 || iYear%100 != 0 && iYear%4 == 0 )
DayofMonth[1] = 29;
if ( iMonth > 12 || iMonth < 1 )
return 0;
while ( iMonth-- > 1 )
{
iResult += DayofMonth[iMonth - 1];
}
iResult += iDay;


return iResult;
}
太乙 2008-03-22
  • 打赏
  • 举报
回复

以前写个个java代码,你看着改改~


//import java.util.GregorianCalendar;
import java.io.*;
//import java.util.Calendar;
import java.util.*;
public class Data {

/**
* @param args
*/
private int month;
private int year;
public Data(int year,int month)
{
this.year=year;
this.month=month;
}
private int GetDayNum()
{
int[] days={31,28,31,30,31,30,31,31,30,31,30,31};
GregorianCalendar cal=new GregorianCalendar(year,month,1);
if(cal.isLeapYear(year)&&month==2)
return days[month-1]+1;
else return days[month-1];
}
private int GetFirstDay()
{
Calendar cal=Calendar.getInstance();
cal.set(year, month-1,1);
return cal.get(Calendar.DAY_OF_WEEK);

}
public void ExportData()
{
System.out.println(year+"--"+month+":\n");
System.out.print("周日 周一 周二 周三 周四 周五 周六\n");
int spaces=GetFirstDay()-1;
for(int i=1;i<=spaces;i++)
{
System.out.print(" ");
}
/**
* print days
*/
int days=GetDayNum();
for(int d=1;d<=days;d++)
{
System.out.print(d+" ");
if((d+spaces)%7==0)
{
System.out.print("\n");
}
}

}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
Date now = new Date(System.currentTimeMillis());//
System.out.println("Begin: ( now, time is "+now+" )");
System.out.println("Please input the year you want to lookup:");
BufferedReader stin = new BufferedReader(new InputStreamReader(System.in));
int year = Integer.parseInt(stin.readLine());
System.out.println("Please input the month you want to lookup:");
//BufferedReader stin = new BufferedReader(new InputStreamReader(System.in));
int month = Integer.parseInt(stin.readLine());
Data dt=new Data(year,month);
dt.ExportData();
}

}




aini10011 2008-03-22
  • 打赏
  • 举报
回复
编程要先自己多动手做,这个问题满基础的,
LZ应该能自己搞定的!
zhb200692279 2008-03-22
  • 打赏
  • 举报
回复
闰年用来知道2月是28天还是29天,
其他月份的天数是固定的。
可以用switch
case 结构
zhb200692279 2008-03-22
  • 打赏
  • 举报
回复
判断闰年
1.能被4整除,但不能被100整除
2.能被400整除
hcon_1755 2008-03-22
  • 打赏
  • 举报
回复
用几个if 语句就出了.....
多几个循环
ttlyfast 2008-03-22
  • 打赏
  • 举报
回复

64,849

社区成员

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

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