c++编程输入某年某月某日,判断这一天是这一年的第几天.45
发布网友
发布时间:2023-10-14 01:23
我来回答
共4个回答
热心网友
时间:2024-12-04 03:11
#include <iostream>
using namespace std;
int main()
{
int day=0; // 日期
int month=0; // 月份
int year=0; // 年份
int sum=0; // 一年中的第几天
cin>>year>>month>>day;
//write your own codes
int m=0;
if(year%4==0&&year%100!=0||year%400==0)m=29;//闰年二月份29天
else m=28;
switch (month)
{
case 12:sum+=30;
case 11:sum+=31;
case 10:sum+=30;
case 9:sum+=31; //如果是某个月,则加上前一个月份的所有天数;
case 8:sum+=31;
case 7:sum+=30;
case 6:sum+=31;
case 5:sum+=30;
case 4:sum+=31;
case 3:sum+=m;
case 2:sum+=31;
case 1:sum+=day; //最后加上这个月的天数;
}
cout<<sum;
return 0;
}
//也可以在每条case语句里直接算出天数,不过要记得加上break语句
热心网友
时间:2024-12-04 03:12
关键字case后面留个空格哈:
#include<iostream>
using namespace std;
void main()
{
int a, b, c, d;
cout << "请输入年,月,日" << endl;
cin >> a >> b >> c;
d = 0;
switch (b)
{
case 12:d = d + c + 30;
case 11:d = d + c + 31;
case 10:d = d + c + 30;
case 9:d = d + c + 31;
case 8:d = d + c + 31;
case 7:d = d + c + 30;
case 6:d = d + c + 31;
case 5:d = d + c + 30;
case 4:d = d + c + 30;
case 3:
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
d = d + c + 29;
else
d = d + c + 28;
case 2:d = d + c + 31;
case 1:d = d + c;
}
cout << "为这一年的第" << d << "天" << endl;
}
热心网友
时间:2024-12-04 03:12
你没敲错我这个就没毛病哦 我运行过的
运行环境 gcc vc6.0
#include <iostream>
using namespace std;
main()
{
cout<<"请输入年、月、日"<<endl;
int year,month,day,sum=0;//定义年月日及天数
cin>>year;
cin>>month;
cin>>day;//输入
switch (month)
{
case 1:
sum=day;break;
case 2:
sum=day+31;
break;
case 3:
sum=day+59;
break;
case 4:
sum=day+90;
break;
case 5:
sum=day+120;
break;
case 6:
sum=day+151;
break;
case 7:
sum=day+181;
break;
case 8:
sum=day+212;
break;
case 9:
sum=day+243;
break;
case 10:
sum=day+273;
break;
case 11:
sum=day+304;
break;
case 12:
sum=day+334;
break;
}
if(((year%4==0&&year%100!=0)||year%400==0)&&month>=3)
{
sum=sum+1;
}//如果是闰年,那么三月及以后月份计算时+1,(非闰年2月28天,闰年29天)
cout<<sum<<endl;
}
热心网友
时间:2024-12-04 03:13
#include<stdio.h>
#include<stdlib.h>
int main() {
int year, month, day, look = 0, day1 = 0;
printf("请输入日期:");
scanf_s("%d,%d,%d",&year, &month,&day);
if ((year % 400 == 0) || (year % 100 == 0) && (year % 4 != 0))
look = 1;
switch(month)
{
case 2:day1 = 31; break;
case 3:day1 = 59; break;
case 4:day1 = 90; break;
case 5:day1 = 120; break;
case 6:day1 = 151; break;
case 7:day1 = 181; break;
case 8:day1 = 212; break;
case 9:day1 = 243; break;
case 10:day1 = 273; break;
case 11:day1 = 303; break;
case 12:day1 = 334; break;
}
if (look)
day1 += day + 1;
else
day1 += day;
printf("%d年%d月%d日是这一年的第%d天\n", year, month, day, day1);
system("pause");
return 0;
}