C++ Primer Exercise 9.51 date construct

本文介绍了一个用于解析多种格式日期字符串的C++类设计,包括全称月份、缩写月份、带逗号和斜杠分隔的日期格式。通过枚举类型和辅助函数,该类能够准确地将日期字符串转换为年、月、日的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Demo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class dateObj {
public:
   dateObj(const string &s);
   ~dateObj();

private:
   unsigned year = 0;
   unsigned month = 0;
   unsigned day = 0;

   enum type { dateyear, datemonth, dateday };

   vector<string> ml = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November","December" };
   vector<string> aml = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
   unsigned _trans(const string &s);
   void _match(const string &s, dateObj::type);
   bool _hasSlash(const string &s);
};

// January 1, 1900,  1/1/1900, Jan 1, 1900
dateObj::dateObj(const string &s) {
   _match(s, datemonth);
   _match(s, dateyear);
   _match(s, dateday);
   cout << month << " " << day << " " << year << endl;
}

unsigned dateObj::_trans(const string &s) {
   for (int i = 0; i < 12; ++i) {
      if (s.compare(ml[i]) == 0) { return ++i; }
      if (s.compare(aml[i]) == 0) { return ++i; }
   }
   return 255;
}

void dateObj::_match(const string &s, dateObj::type t) {
   if (!_hasSlash(s)) {
      if (t == dateObj::type::datemonth) {
         auto pos = s.find_first_of(' ');
         if (pos == string::npos) {
            cout << "Not a validate string, cannot found the month" << endl;
            return;
         }
         else {
            string tmp(s, 0, pos - 0);
            month = _trans(tmp);
         }
      }
      else if (t == dateObj::type::dateday) {
         auto posBeg = s.find_first_of(' ');
         auto posEnd = s.find_first_of(",");
         if (posBeg == string::npos || posEnd == string::npos) {
            cout << "Not a validate string, cannot found the day" << endl;
            return;
         }
         string tmp(s, posBeg, posEnd - posBeg);
         day = stoi(tmp);
      }
      else if (t == dateObj::type::dateyear) {
         auto posBeg = s.find_first_of(",");
         size_t posEnd = s.size();
         if (posBeg == string::npos) {
            cout << "Not a validate string, cannot found the year" << endl;
            return;
         }
         string tmp(s, posBeg, posEnd - posBeg);
         year = stoi(tmp.substr(tmp.find_first_of("0123456789")));
      }
      else {
         cout << "Invalide date type" << endl;
      }
   }
   else {
      string tmp = s;
      if (t == dateObj::type::datemonth) {
         auto posEnd = s.find_first_of('/');
         if (posEnd == string::npos) {
            cout << "Not a validate string, cannot found the month" << endl;
            return;
         }
         string monthStr(s, 0, posEnd);
         month = _trans(monthStr);
      }
      else if (t == dateObj::type::dateday) {
         auto posBeg = s.find_first_of('/');
         auto posEnd = s.find_first_of('/', posBeg + 1);
         if (posBeg == string::npos || posEnd == string::npos) {
            cout << "Not a validate string, cannot found the day" << endl;
            return;
         }
         string dayStr(s, posBeg, posEnd - posBeg);
         day = stoi(dayStr.substr(dayStr.find_first_of("0123456789")));
      }
      else if (t == dateObj::type::dateyear) {
         auto posTmp = s.find_first_of('/');
         auto posBeg = s.find_first_of('/', posTmp + 1);
         auto posEnd = s.size();
         if (posBeg == string::npos || posTmp == string::npos) {
            cout << "Not a validate string, cannot found the year" << endl;
            return;
         }
         string yearStr(s, posBeg, posEnd);
         year = stoi(yearStr.substr(yearStr.find_first_of("0123456789")));
      }
      else {
         cout << "Invalide date type" << endl;
      }
   }

}

bool dateObj::_hasSlash(const string &s) {
   return (s.find_first_of('/') != string::npos);
}

dateObj::~dateObj() {
   cout << "destruct" << endl;
}

int main()
{
   ifstream f;
   f.open("test.txt");
   string date;
   if (f) {
      getline(f, date);
      dateObj d = dateObj(date);
      f.close()
   }
   else { 
      cout << "Cannot open file" << endl;
      f.close();
   }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值