fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. class Date {
  7. public:
  8. int year;
  9. int month;
  10. int day;
  11.  
  12. Date() {}
  13. Date (int y, int m, int d) {
  14. Set(y, m, d);
  15. }
  16. int operator-(Date d) {
  17. return TotalDays(this) - TotalDays(&d);
  18. }
  19. void Set(int y, int m, int d) {
  20. if (isValidDate(y, m, d)) {
  21. year = y;
  22. month = m;
  23. day = d;
  24. } else {
  25. cout << "Error: Date is invalid.";
  26. }
  27. }
  28. void Print() {
  29. cout << year << "/" << month << "/" << day << endl;
  30. }
  31. int DayOfMonth() {
  32. return DayOfMonth(year, month);
  33. }
  34. bool isLeapYear() {
  35. return isLeapYear(year);
  36. }
  37.  
  38. private:
  39. int TotalDays(Date *d) {
  40. int md[] = {0, 0, 31, 59, 90, 120, 151, 181,
  41. 212, 243, 273, 304, 334, 365};
  42. int sumday = (d->year * 365) + (d->year / 4)
  43. - (d->year / 100) + (d->year / 400)
  44. - ((d->month < 3) && (isLeapYear(d->year)))
  45. + md[d->month] + d->day - 693959;
  46. sumday -= 693959; // compatible with Excel
  47. return sumday;
  48. }
  49. int DayOfMonth(int y, int m) {
  50. int dayOfMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  51. if ((m >= 1) && (m <= 12)) {
  52. if ((m == 2) && isLeapYear(y)) {
  53. return dayOfMonth[m] + 1;
  54. } else {
  55. return dayOfMonth[m];
  56. }
  57. } else {
  58. return 0;
  59. }
  60. }
  61. bool isValidDate(int y, int m, int d) {
  62. if ((y < 1800) || (y > 2200)) return false;
  63. if ((m < 1) || (m > 12)) return false;
  64. if ((d < 1) || (d > DayOfMonth(y, m))) return false;
  65. return true;
  66. }
  67. bool isLeapYear(int y) {
  68. return (((y % 4 == 0) && (y % 100))
  69. || (y % 400 == 0));
  70. }
  71. };
  72.  
  73. class Biorhythm {
  74. public:
  75. Date birthDate;
  76. Date checkDate;
  77. Biorhythm(Date birth, Date check) {
  78. birthDate = birth;
  79. checkDate = check;
  80. Print();
  81. }
  82. private:
  83. void Print() {
  84. Date dispDate = checkDate;
  85. dispDate.day = 1;
  86.  
  87. int sumDay = dispDate - birthDate;
  88. int dayOfMonth = dispDate.DayOfMonth();
  89. cout << "Birthday : ";
  90. birthDate.Print();
  91. cout << "Date : ";
  92. checkDate.Print();
  93. cout << "P:physical S:sensitivity I:inttelectual" << endl;
  94. cout << " day: - 0 + " << endl;
  95. cout << "------------------------------------------------" << endl;
  96. for(int day = 1; day <= dayOfMonth; day++) {
  97. if (day == checkDate.day) {
  98. cout << "*" << setw(3) << day << ": ";
  99. } else {
  100. cout << " " << setw(3) << day << ": ";
  101. }
  102. int physical = sin((double)(sumDay % 23) / 23 * 2 * M_PI) * 20;
  103. int sensitivity = sin((double)(sumDay % 28) / 28 * 2 * M_PI) * 20;
  104. int intellectual = sin((double)(sumDay % 33) / 33 * 2 * M_PI) * 20;
  105. char graph[42];
  106. for (int i = 0; i < sizeof(graph); i++) {
  107. graph[i] = ' ';
  108. }
  109. graph[sizeof(graph)-1] = '\0';
  110. graph[20] = '|';
  111. graph[physical + 20] = 'P';
  112. graph[sensitivity + 20] = 'S';
  113. graph[intellectual + 20] = 'I';
  114. cout << graph;
  115. cout << endl;
  116. sumDay++;
  117. }
  118. }
  119. };
  120.  
  121. int main() {
  122. // your code goes here
  123. Date d1(1980,4,1); // birthday
  124. Date d2(2018,6,23); // check date
  125.  
  126. Biorhythm bio(d1, d2);
  127. return 0;
  128. }
Success #stdin #stdout 0s 4464KB
stdin
Standard input is empty
stdout
Birthday   : 1980/4/1
Date       : 2018/6/23
P:physical  S:sensitivity  I:inttelectual
 day: -                   0                   + 
------------------------------------------------
   1:      S              |        IP          
   2:         S           |    I        P      
   3:             S       |I               P   
   4:                 S  I|                  P 
   5:                I    S                  P 
   6:            I        |   S             P  
   7:         I           |       S       P    
   8:      I              |           S        
   9:    I                |      P       S     
  10:   I                 | P               S  
  11:  I                P |                  S 
  12:  I           P      |                   S
  13:  I      P           |                  S 
  14:   I P               |                 S  
  15:   P I               |              S     
  16:  P     I            |           S        
  17:  P        I         |       S            
  18:    P         I      |   S                
  19:       P          I  S                    
  20:           P     S   I                    
  21:             S  P    |  I                 
  22:         S           P      I             
* 23:      S              |    P    I          
  24:   S                 |         P  I       
  25:  S                  |             P I    
  26: S                   |                PI  
  27:  S                  |                  I 
  28:   S                 |                  I 
  29:      S              |                 PI 
  30:         S           |               P I