C++一年中的天数

new9mtju  于 2023-04-08  发布在  其他
关注(0)|答案(3)|浏览(116)

你好,我一直在做一些程序,我的程序是得到一年中过去的天数。现在,当我试图运行和输出给我“4438232”。
例如,如果用户输入(mm-dd-yy)3-18-2013,则该年经过的扩髓天数为77。
这段代码对获取一年中过去的铰孔天数有意义吗?

void dateType::Num_DayPassed()
{
     int sum;
     int yy = 365;
     if (month ==1)
        {
            cout<<"Number of days Passed in the Year: "<<sum<<endl;
            day=31;
            sum=day-yy;
        }
  ........
 continued until month 12..

Full Code
Output

brvekthn

brvekthn1#

C++程序从上到下运行。在设置sum之前,您将输出sum

kh212irz

kh212irz2#

You can also use this logic:
#include<iostream>
using namespace std;
class dateType
{
private:
int dMonth; 
int dDay; 
int dYear; 
void setDate(int month, int day, int year);
void set_Date();
void setNew_Date();
void add_days(int D);
int return_day(int M);
bool check_leap(int y);
int pass_days();
int remaining_days();
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate() const;
void set_month(int m);
void set_day(int d);
void set_year(int y);
public:
dateType(int month = 1, int day = 1, int year = 1900);
void choice();
};
void dateType::set_Date()
{
    cout<<"Enter Date in (dd/mm/yy) formate:"<<endl;
    int d,m,y;
    cin>>d>>m>>y;
    setDate(m,d,y);
}
dateType::dateType(int month , int day , int year)
{
    setDate(month,day,year);
}
void dateType::setDate(int month, int day, int year)
{
    set_day(day);
    set_month(month);
    set_year(year);
   
}
void dateType::printDate() const
{
   cout<<dDay<<"\\"<<dMonth<<"\\"<<dYear<<endl;
}
int dateType::return_day(int M)
{
    for(int i=1;i<13;i++)
    {
        if(i==M && M==2)
        {
            return 28;
        }
        else if(i==M && (M==1||M==3||M==5||M==7||M==8||M==10||M==12))
        {
            return 31;
        }
        else
        return 30;
    }
}
bool dateType::check_leap(int y)
{
     if((y%400 == 0 || y % 100 !=0 )&& y%4==0)
       {
        return true;
       }
     else
      return false;
}
void dateType::setNew_Date()
{
    set_Date();
}
void dateType::add_days(int D)
{
       if(dMonth==2)
       {
          dDay=dDay+D;
          if(dDay>28)
            {
                dMonth++;
                dDay=dDay-28;
            }
       }
       else if(dMonth==1||dMonth==3||dMonth==5||dMonth==7||dMonth==8||dMonth==10||dMonth==12)
       {
          dDay=dDay+D;
          if(dDay>31)
          {
            dMonth++;
            dDay=dDay-31;
          }
          else if(dDay>31&&dMonth==12)
          {
            dYear++;
            dMonth=1;
            dDay=dDay-31;
          }
       }
       else
       {
        dDay=dDay+D;
        if(dDay>30)
        {
            dMonth++;
            dDay=dDay-30;
        }
       }
}
int dateType::pass_days()
{   
    int count=0;
      for (int  i = 1; i <dMonth; i++)
      {
           if(i==2)
          {
              for(int i=1;i<=28;i++)
              {
                count++;
                 if(check_leap(2))
                 {
                    count++;
                 }
              }
          }
       else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
       {
         for(int i=1;i<=31;i++)
              {
                count++;
              }
        }
       
       else
       {
        for(int i=1;i<=30;i++)
              {
                count++;
              }
      
       }  
      }
      return count+dDay;
}
int dateType::remaining_days()
{

    if(dMonth!=2)
    return 365-pass_days();
    else
    return 366-pass_days();
}
void dateType:: set_day(int d)
{
   if(d<31)
    {
        dDay=d;
    }
    else
    dDay=1;
}
void dateType::set_month(int m)
{
    if(m<=12)
    {
        dMonth=m;
    }
    else
    dMonth=1;
}
void dateType::set_year(int y)
{
    dYear=y;
}
void dateType:: choice()
{
    set_Date();
    while(1)
    {
        cout<<"1.Update Date"<<endl;
        cout<<"2.Check leap year"<<endl;
        cout<<"3.Find number of days remaining:"<<endl;
        cout<<"4.Find number of days passed:"<<endl;
        cout<<"5.Find number of days of a month"<<endl;
        cout<<"6.Add Days to Date"<<endl;
        cout<<"7.Exit"<<endl;
        cout<<"Enter your choice: ";
        int choice;
        cin>>choice;
        if(choice==1)
        {
            cout<<"1.Update Month"<<endl;
            cout<<"2.Update Year"<<endl;
            cout<<"3.Update Day"<<endl;
            cout<<"4.Exit"<<endl;
            cout<<"Enter your choice: ";
            int choice1;
            cin>>choice1;
            if(choice1==1)
            {
                cout<<"Enter the month: ";
                int month;
                cin>>month;
                set_month(month);

            }
            else if(choice1==2)
            {
                cout<<"Enter the year: ";
                int year;
                cin>>year;
                set_year(year);
            }
            else if(choice1==3)
            {
                cout<<"Enter the day: ";
                int day;
                cin>>day;
                set_day(day);

            }
            else
            break;
           cout<<"Updated Dated:  ";
            printDate();
        }
        else if(choice==2)
        {
          if(check_leap(dYear))
          {
            cout<<"It is a leap year"<<endl;
          }
          else
          {
            cout<<"It is not a leap year"<<endl;
          }
        }
        else if(choice==3)
        {
            int rem=remaining_days();
            cout<<"Remaining days: "<<rem<<endl;
        }
        else if(choice==4)
        {
            int pass=pass_days();
            cout<<"Passed days: "<<pass<<endl;
        }
        else if(choice==5)
        {
            int days=return_day(dMonth);
            cout<<"Days in month  "<<dMonth<<" : "<<days<<endl;
        }
        else if(choice==6)
        {
            cout<<"Enter Number of Days you want to add  to the Date"<<endl;
            int days;
            cin>>days;
            add_days(days);
            cout<<"Updated Dated:  ";
            printDate();
        }
        else
        break;
    }
}

int main()
{
    dateType D;
     D.choice();
}
s8vozzvw

s8vozzvw3#

密码:

void dateType::Num_DayPassed()
        {
            int sum;
            int yy = 365;
 
             if (month ==1)
            {
                cout<<"Number of days Passed in the Year: "<<sum<<endl;
                day=31;
                sum=day-yy;
            }
            else if (month==2)
            {
                cout<<"Number of days Passed in the Year: "<<sum<<endl;
                day=60;
                sum=day-yy;
            ...

不是在一个函数中硬编码12个case语句(每个月一个),而是使用一个查找表来查找月份中的天数,并简单地循环计数。
这里有一个非常简化的版本,我将让你决定如何将它与你的dateType类集成。

bool isLeapYear(int year)
{
   if (year % 4 != 0) return false;
   if (year % 400 == 0) return true;
   if (year % 100 == 0) return false;
   return true;
}

// returns the number of days that have passed before month/day/year
int getDaysPassedInYear(int month, int day, int year)
{
    int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    days_in_month[2] = isLeapYear(year) ? 29 : 28;
    
    // sanity check params
    if ((month > 12) || (month < 1) || (day < 1) || (day > days_in_month[month]) || (year < 1))
    {
        return -1; // error - bad parameter
    }

    // now count the days
    int days_passed = 0; // initialize to "1" if you want to include the target date into the number of days passed

    // start on january 1
    int m = 1;
    int d = 1;
    
    // now count the number of days that need to elapse
    // until the intended month/day date
    while (m != month && d != day)
    {
        days_passed++;
        d++;
        if (d > days_in_month[m])
        {
            m++;
            d = 1;
        }
    }
    return days_passed;
}

相关问题