10_83.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<iostream.h>
  2. int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
  3. {31,29,31,30,31,30,31,31,30,31,30,31}};
  4. class student
  5. {
  6. int year,month,day;
  7. int leap(int);
  8. int dton(student &);
  9. student ntod(int);
  10. public:
  11. student(){}
  12. student(int y,int m,int d)
  13. {
  14. year=y;month=m;day=d;
  15. }
  16. int operator-(student &);
  17. void disp()
  18. {
  19. cout<<year<<"/"<<month<<"/"<<day<<endl;
  20. }
  21. };
  22. int student::leap(int year)
  23. {
  24. if(year%4==0 && year%100!=0 ||year%400==0)
  25. return 1;
  26. else
  27. return 0;
  28. }
  29. int student::dton(student &d)
  30. {
  31. int y,m,days=0;
  32. for(y=1;y<=d.year;y++)
  33. if(leap(y))
  34. days+=366;
  35. else
  36. days+=365;
  37. for(m=0;m<d.month-1;m++)
  38. if(leap(d.year))
  39. days+=day_tab[1][m];
  40. else
  41. days+=day_tab[0][m];
  42. days+=d.day;
  43. return days;
  44. }
  45. student student::ntod(int n)
  46. {
  47. int y=1,m=1,d,rest=n,lp;
  48. while(1)
  49. {
  50. if(leap(y))
  51. {
  52. if(rest<=366)
  53. break;
  54. else
  55. rest-=366;
  56. }
  57. else
  58. {
  59. if(rest<=365)
  60. break;
  61. else
  62. rest-=365;
  63. }
  64. y++;
  65. }
  66. y--;
  67. lp=leap(y);
  68. while(1)
  69. {
  70. if(lp)
  71. {
  72. if(rest>day_tab[1][m-1])
  73. rest-=day_tab[1][m-1];
  74. else
  75. break;
  76. }
  77. else
  78. {
  79. if(rest>day_tab[0][m-1])
  80. rest-=day_tab[0][m-1];
  81. else
  82. break;
  83. }
  84. m++;
  85. }
  86. d=rest;
  87. return student(y,m,d);
  88. }
  89. int student::operator-(student & b)
  90. {
  91. int days=dton(*this)-dton(b);
  92. return days;
  93. }
  94. void main()
  95. {
  96. student Henry(1980,11,12),Tom(1976,3,25);
  97. cout<<"Henry's birthday is ";Henry.disp(); 
  98. cout<<"Tom's birthday is ";Tom.disp();
  99. if((Henry-Tom)>0)
  100.     cout<<"Tom is older than Henry "<<Henry-Tom<<" days."<<endl;
  101. else
  102. cout<<"Henry is older than Tom "<<Tom-Henry<<" days."<<endl;
  103. }