June 15th Monday (六月 十五日 月曜日)

  I wrote a string class.  Maybe I can used it in the future.

//GString.h
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

class GString {
private:
    char *_buf;
    int _len;
public:
    GString() : _buf(NULL), _len(0) {}
   
    GString(char *s) : _buf(NULL), _len(0) {
        if (s){
            _len = strlen(s);
            if (_len > 0) {
                _buf = new char[_len + 1];
                strncpy(_buf, s, _len);
                _buf[_len] = 0;
            }
            else {
                _len = 0;
                _buf = NULL;
            }
        }
    }
   
    GString(GString& s) : _buf(NULL), _len(0) {
        _len = s.getLength();
        if (_len > 0) {
            delete [] _buf;
           
            _buf = new char[_len + 1];
            memset(_buf, 0, (_len + 1));
            strncpy(_buf, s.to_cstr(), _len);
        }
        else {
            _len = 0;
            _buf = NULL;
        }
    }
   
    ~GString() {
        if (_buf) {
            delete [] _buf;
            _len = 0;
        }
    }
   
    GString& operator= (const GString& s) {
        if (this == &s)
            return *this;
       
        _len = s.getLength();
        delete [] _buf;
       
        _buf = new char[_len + 1];
        memset(_buf, 0, (_len + 1));
        strncpy(_buf, s.to_cstr(), _len);
        return *this;
    }
   
    char operator[] (const int index) const {
        char r = '/0';
       
        if ((index >= 0) && (index <= _len))
            r = _buf[index];
        return r;
    }
   
    inline int getLength() const {
        return _len;
    }
   
    const char *to_cstr() const {
        return _buf;
    }
   
    GString& operator+(const GString& s) {
        char *p = new char[_len + s.getLength() + 1];
        if (p) {
            strncpy(p, _buf, _len);
            strncpy(&p[_len], s.to_cstr(), s.getLength());
            _len += s.getLength();
            p[_len] = '/0';
            delete [] _buf;
            _buf = p;
        }
       
        return *this;
    }
   
    bool operator==(const GString& s) const {
        const char *p = s.to_cstr();
        if (&s == this) return true;
        return (strcmp(_buf, p) == 0) ? true : false;
    }
   
    bool operator!=(const GString& s) const {
        const char *p = s.to_cstr();
        if (&s != this) return true;
        return (strcmp(_buf, p) != 0) ? true : false;
    }
   
    GString substring(int offset, int len) {
       
        char buf[1024];
       
        memset(buf, 0, sizeof(buf));
        if ((offset >= 0) && (offset <= _len)) {
            len = (len <= 0) ? 1 : len;
            len = min(len, _len - offset);
            strncpy(buf, &_buf[offset], len);
            GString r(buf);
            cout<<"> "<<r.to_cstr()<<endl;
            return r;
        }
        GString s;
        return s;
    }
};

int main() {
    GString s("abc");
   
    for (int i = 0; i <= s.getLength(); i++)
        cout<<s[i];
    cout<<endl;
   
    GString cs(s);
    cout<<cs.to_cstr()<<endl;
   
    GString ms("luming ");
    cout<<ms.to_cstr()<<endl;
    ms + s;
    cout<<ms.to_cstr()<<endl;
    ms = s;
    cout<<ms.to_cstr()<<endl;
   
    if (ms==s)
        cout<<"true"<<endl;
    else cout<<"false"<<endl;
   
    if (ms!=s)
        cout<<"true"<<endl;
    else cout<<"false"<<endl;
   
    GString sub;
    sub = ms.substring(0, 1);
    cout<<sub.to_cstr()<<endl;
   
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取每个月最后一个非节假日的工作日可以分为三步: 1. 获取当月最后一天 2. 如果最后一天是周六或周日,则向前推到最后一个工作日 3. 如果最后一天是节假日,则不断向前推到最后一个非节假日的工作日 以下是一个示例代码: ```java import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class LastWorkdayOfMonthWithoutHolidayExample { public static void main(String[] args) { int year = 2021; Month[] months = {Month.JANUARY, Month.FEBRUARY, Month.MARCH, Month.APRIL, Month.MAY, Month.JUNE, Month.JULY, Month.AUGUST, Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER, Month.DECEMBER}; for (Month month : months) { // 获取当月最后一天 LocalDate lastDayOfMonth = LocalDate.of(year, month, 1) .with(TemporalAdjusters.lastDayOfMonth()); // 如果最后一天是周六或周日,则向前推到最后一个工作日 DayOfWeek lastDayOfWeek = lastDayOfMonth.getDayOfWeek(); if (lastDayOfWeek == DayOfWeek.SATURDAY || lastDayOfWeek == DayOfWeek.SUNDAY) { lastDayOfMonth = lastDayOfMonth.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)); } // 如果最后一天是节假日,则向前推到最后一个非节假日的工作日 while (isHoliday(lastDayOfMonth)) { lastDayOfMonth = lastDayOfMonth.minusDays(1); } System.out.println(month + "月最后一个非节假日的工作日:" + lastDayOfMonth); } } // 判断一个日期是否是节假日 private static boolean isHoliday(LocalDate date) { // 这里假设元旦、春节、清明节、劳动节、端午节、中秋节和国庆节都是节假日 return date.getMonth() == Month.JANUARY && date.getDayOfMonth() == 1 || isSpringFestival(date) || date.getMonth() == Month.APRIL && date.getDayOfMonth() == 4 || date.getMonth() == Month.MAY && date.getDayOfMonth() == 1 || isDragonBoatFestival(date) || isMidAutumnFestival(date) || date.getMonth() == Month.OCTOBER && date.getDayOfMonth() == 1 || date.getMonth() == Month.OCTOBER && date.getDayOfMonth() == 2 || date.getMonth() == Month.OCTOBER && date.getDayOfMonth() == 3; } // 判断一个日期是否是春节 private static boolean isSpringFestival(LocalDate date) { int year = date.getYear(); LocalDate springFestival = LocalDate.of(year, Month.JANUARY, 1) .with(TemporalAdjusters.firstDayOfNextMonth()) .with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); if (springFestival.getMonth() == Month.FEBRUARY) { springFestival = springFestival.minusDays(7); } return date.equals(springFestival) || date.equals(springFestival.plusDays(1)) || date.equals(springFestival.plusDays(2)) || date.equals(springFestival.plusDays(3)) || date.equals(springFestival.plusDays(4)) || date.equals(springFestival.plusDays(5)) || date.equals(springFestival.plusDays(6)); } // 判断一个日期是否是端午节 private static boolean isDragonBoatFestival(LocalDate date) { int year = date.getYear(); LocalDate dragonBoatFestival = LocalDate.of(year, Month.JUNE, 1) .with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)) .plusWeeks(2); return date.equals(dragonBoatFestival); } // 判断一个日期是否是中秋节 private static boolean isMidAutumnFestival(LocalDate date) { int year = date.getYear(); LocalDate midAutumnFestival = LocalDate.of(year, Month.SEPTEMBER, 1) .with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)) .plusWeeks(2); return date.equals(midAutumnFestival); } } ``` 在上面的代码中,我们使用Java 8提供的日期和时间API来获取当月最后一天,并使用TemporalAdjusters工具类来实现日期调整。如果最后一天是周六或周日,则向前推到最后一个工作日。如果最后一天是节假日,则不断向前推到最后一个非节假日的工作日。最后输出每个月最后一个非节假日的工作日的结果。为了简化示例代码,这里假设元旦、春节、清明节、劳动节、端午节、中秋节和国庆节都是节假日,实际情况可能会有所不同,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值