當前位置:編程學習大全網 - 源碼下載 - C++日期類編程題

C++日期類編程題

#include <iostream>

#include <stdio.h>

#include <time.h>

#include "12.h"

using namespace std;

int main(){

myfun myclas;

struct tm *local;

time_t t = time(NULL);

local=gmtime(&t);

cout<<"當前系統時間:"<<1900+local->tm_year<<"年"<<1+local->tm_mon<<"月"<<local->tm_mday<<"日"<<endl;

int year=1900+local->tm_year;

int month=1+local->tm_mon;

int day=local->tm_mday;

int mon[]={31,28,31,30,31,30,31,31,30,31,30,31};

int a=year-myclas.year;

int i=0;

int j=0;

int mon1=0;

int mon2=0;

for (i=0;i<myclas.month;i++)

{

int mon1=mon1+mon[i];

}

for (i=0;i<month;i++)

{

int mon2=mon2+mon[i];

}

int monday=mon1-mon2;

int c=myclas.day-day;

if (month-myclas.month<0)

a=a-1;

if ((year/100)||(year/4)&&(year/400))

{

cout<<"是閏年";

cout<<"輸入的年份與本地時間的間隔是:"<<a*366+c<<"天"<<endl;

}

else

{

cout<<"不是閏年";

cout<<"輸入年份與本地年份間隔是:"<<a*365+c<<"天"<<endl;

}

return 0;

}

這是頭文件:12.h

#ifndef time_h

#define time_h

class myfun

{

public:

int day,month,year;

myfun(){

std::cout<<"year:";

std::cin>>year;

std::cout<<std::endl;

std::cout<<"month:";

std::cin>>month;

std::cout<<std::endl;

std::cout<<"day:";

std::cin>>day;

std::cout<<std::endl;

}

};

#endif

慢慢看,我寫的,寫完放在電腦找了很久,找到了,這個程序考慮了幾種情況,日期是可以手動輸入的,判斷輸入的是不是閏年,然後計算輸入的日期跟系統本身的日期相差多少天。至於妳要的時分秒,妳仔細看time的頭文件的源代碼,我這裏是給妳參考的,還有其他細節,妳應該可以解決,妳不是想我給做好的妳吧,那妳什麽都學不到的也。。輸出的方面可以按妳的喜好改。算是給妳提示了。

  • 上一篇:無線路由器怎麽設置?美國網件輕松概括多範圍
  • 下一篇:如何在Maven中配置Spring Security依賴
  • copyright 2024編程學習大全網