fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. class Date {
  5. public:
  6. int year, month, day;
  7.  
  8. Date() : year(0), month(0), day(0) {}
  9.  
  10. Date(const char* dateStr) {
  11. sscanf(dateStr, "%d-%d-%d", &year, &month, &day);
  12. }
  13.  
  14. // 将日期转换为yyyymmdd格式的字符串
  15. char* toYYYYMMDD() {
  16. static char dateStr[9];
  17. sprintf(dateStr, "%04d%02d%02d", year, month, day);
  18. return dateStr;
  19. }
  20. };
  21.  
  22. class People {
  23. private:
  24. char name[11];
  25. char number[7];
  26. char sex[7];
  27. Date birthday;
  28. char id[20];
  29.  
  30. public:
  31. People() {
  32. memset(name, 0, sizeof(name));
  33. memset(number, 0, sizeof(number));
  34. memset(sex, 0, sizeof(sex));
  35. memset(id, 0, sizeof(id));
  36. }
  37.  
  38. void input() {
  39. std::cin.getline(name, 11);
  40. std::cin.getline(number, 7);
  41. std::cin.getline(sex, 7);
  42. char birthdayStr[11];
  43. std::cin.getline(birthdayStr, 11);
  44. birthday = Date(birthdayStr);
  45. std::cin.getline(id, 20);
  46. }
  47.  
  48. void display() {
  49. if (strncmp(id + 6, birthday.toYYYYMMDD(), 8) == 0) {
  50. std::cout << "Name: " << name << std::endl;
  51. std::cout << "Number: " << number << std::endl;
  52. std::cout << "Sex: " << sex << std::endl;
  53. std::cout << "Birthday: " << birthday.year << "-" << birthday.month << "-" << birthday.day << std::endl;
  54. std::cout << "ID: " << id << std::endl;
  55. } else {
  56. std::cout << "birthday mismatch!" << std::endl;
  57. std::cout << "Name: " << name << std::endl;
  58. std::cout << "Number: " << number << std::endl;
  59. std::cout << "Sex: " << sex << std::endl;
  60. std::cout << "Birthday: " << birthday.year << "-" << birthday.month << "-" << birthday.day << std::endl;
  61. std::cout << "ID: " << id << std::endl;
  62. }
  63. }
  64. };
  65.  
  66. int main() {
  67. People person;
  68. person.input();
  69. person.display();
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0.01s 5284KB
stdin
rose
001
female
1992-12-02
11010819921216472
stdout
birthday mismatch!
Name: rose
Number: 001
Sex: female
Birthday: 1992-12-2
ID: 11010819921216472