C++练习之 判断某一天是该年的第几天

这段C++代码用于接收用户输入的年、月、日,并通过判断闰年和平年以及各月天数,计算出输入的日期是当年的第几天。程序中包含了对输入合法性进行检查的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
using namespace std;
int main()
{
 bool judge(int year,int month,int day);
 int year,month,day,i,j,sum=0;
 int a[7]={1,3,5,7,8,10,12};//此数组包含31天的月份
 int b[4]={4,6,9,11};//此数组包含30天的月份
 cout<<"please input year:";
 cin>>year;
 cout<<"please input month:";
 cin>>month;
 cout<<"please input day:";
 cin>>day;
 while(judge(year,month,day)==0)//此参数为判断输入的年月日是否合法,若不合法则重新输入,直到合法为止。
 {
  cout<<"error!"<<endl<<"please input again"<<endl;
  cout<<"please input year:";
  cin>>year;
  cout<<"please input month:";
  cin>>month;
  cout<<"please input day:";
  cin>>day;
 }
 for(i=0;a[i]<=month;i++)//此语句求出1月到所输入月份的天数之和。
  sum+=31;
 for(j=0;b[j]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值