鱼C论坛

 找回密码
 立即注册
查看: 1517|回复: 0

[技术交流] 使用string转数值读取日期的一个类和几个函数(来自c++ primer 习题集)

[复制链接]
发表于 2017-4-24 11:20:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <string>
  3. #include "date.h"
  4. using namespace std;
  5. int main()
  6. {
  7.         string dates[]{"Jan 1,2014", "February 1 2014", "3/1/2014"};
  8.         try
  9.         {
  10.                 for (auto ds : dates)
  11.                 {
  12.                         date d1(ds);
  13.                         cout << d1;
  14.                 }
  15.         }
  16.         catch (invalid_argument e)
  17.         {
  18.                 cout << e.what() << endl;
  19.         }
  20.         system("pause");
  21.         return 0;
  22. }
复制代码



这是main,以下是头文件

  1. #ifndef DATE_H_
  2. #define DATE_H_
  3. #include <iostream>
  4. #include <string>
  5. #include <stdexcept>
  6. using namespace std;
  7. class date
  8. {
  9. public:
  10.         friend ostream &operator<<(ostream & os, const date & d);
  11.         date() = default;
  12.         date(string &ds);
  13.         unsigned y()const { return year; }
  14.         unsigned m()const { return month; }
  15.         unsigned d()const { return day; }
  16. private:
  17.         unsigned year, month, day;
  18. };
  19. const string month_name[]{"January", "February", "March", "april", "May", "June", "July", "August", "September", "October", "November", "December"};
  20. const string month_abbr[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
  21. const int days[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  22. int get_month(string &ds, int &end_of_month)
  23. {
  24.         int i, j;
  25.         for (i = 0; i < 12; ++i)
  26.         {
  27.                 for (j = 0; unsigned(j) < month_abbr[i].size();++j)
  28.                         if (ds[j]!=month_abbr[i][j])
  29.                                 break;
  30.                 if (j == month_abbr[i].size())
  31.                         break;
  32.         }
  33.         if (i == 12)
  34.                 throw invalid_argument("不是合法的月份名");
  35.         if(ds[j]==' ')
  36.         {
  37.                 end_of_month = j + 1;
  38.                 return i + 1;
  39.         }
  40.         for (; unsigned(j) < month_name[i].size(); ++j)
  41.                 if (ds[j] != month_name[i][j])
  42.                         break;
  43.         if (j == month_name[i].size() && ds[j] == ' ')
  44.         {
  45.                 end_of_month = j + 1;
  46.                 return i + 1;
  47.         }
  48.         throw invalid_argument("不是合法月份名");
  49. }
  50. int get_day(string &ds, int month, int &p)
  51. {
  52.         size_t q;
  53.         int day = stoi(ds.substr(p), &q);
  54.         if (day<1 || day>days[month])
  55.                 throw invalid_argument("不是合法的月份值");
  56.         p += q;
  57.         return day;
  58. }
  59. int get_year(string &ds, int &p)
  60. {
  61.         size_t q;
  62.         int year = stoi(ds.substr(p), &q);
  63.         if (p + q < ds.size())
  64.                 throw invalid_argument("非法结尾内容");
  65.         return year;
  66. }
  67. date::date(string &ds)
  68. {
  69.         int p;
  70.         size_t q = 0;
  71.         if ((p = ds.find_first_of("0123456789")) == string::npos)
  72.                 throw invalid_argument("没有数字非法日期");
  73.         if (p > 0)
  74.         {
  75.                 month = get_month(ds, p);
  76.                 day = get_day(ds, month, p);
  77.                 if (ds[p] != ' '&&ds[p] != ',')
  78.                         throw invalid_argument("非法间隔符");
  79.                 ++p;
  80.                 year = get_year(ds, p);
  81.                 p = q;
  82.         }
  83.         else
  84.         {
  85.                 month = stoi(ds, &q);
  86.                 p = q;
  87.                 if (month < 1 || month>12)
  88.                         throw invalid_argument("不是合法月份值");
  89.                 if (ds[p++] != '/')
  90.                         throw invalid_argument("非法间隔符");
  91.                 if (ds[p++] != '/')
  92.                         throw invalid_argument("非法间隔符");
  93.                 year = get_year(ds, p);
  94.         }
  95. }
  96. ostream &operator<<(ostream & os, const date & d)
  97. {
  98.         os << d.y() << "年" << d.m() << "月" << d.d() << "日" << endl;
  99.         return os;
  100. }
  101. #endif // !DATE_H_
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 03:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表