⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrproj_prob.cpp

📁 aiParts is a set of C++ classes that can be used to develop artificial intelligence for multi-decisi
💻 CPP
📖 第 1 页 / 共 4 页
字号:

  if (!ofp || !pde || !is_valid()) return 0;

  rppProjWeekEmpDay *pwed = new rppProjWeekEmpDay;
  if (!pwed) { log("new rppProjWeekEmpDay error"); return 0; }

  long wnum, wpos;
  week_dt (pde->yyyymmdd(), &wnum, &wpos);

  pwed->set ( pde->proj_id(), wnum, pde->emp_id(),
              wpos, pde->yyyymmdd(), pde->pos_id() );

  if (m_proj_week_emp_days) m_proj_week_emp_days->add(pwed);

  return 1;

}

//----------------------------------------------------------------------
//  post_proc - called after solution-detail data is handled

int rppProbWriteWeek1::post_proc (FILE *ofp) {

  if (!ofp || !is_valid()) return 0;

  int have_written_week_hdr = 0;
  int have_written_emp_hdr  = 0;

  if ( !write_blank_line(ofp) ) return 0;

  rppProjWeekEmpDayItr pwed_itr = projweekempday_iterator();
  rppProjWeekEmpDay *pwed;
  for (pwed=pwed_itr.first(); pwed; pwed=pwed_itr.next()) {

    if (m_any_week_data) {
      if (pwed->proj_id()  != m_proj_week->proj_id() ||
        pwed->week_num() != m_proj_week->week_num()) {
        m_proj_week->reset();
        m_any_week_data = 0;
      }
    }

    if (m_any_emp_data) {
      if (pwed->proj_id()  != m_proj_week_emp->proj_id()  ||
          pwed->week_num() != m_proj_week_emp->week_num() ||
          pwed->emp_id()   != m_proj_week_emp->emp_id()) {
        if (!have_written_emp_hdr) {
          if ( !m_proj_week_emp->write_hdr(ofp) ) return 0;
          have_written_emp_hdr = 1;
        }
        if ( !m_proj_week_emp->write(ofp) ) return 0;
        m_proj_week_emp->reset();
        m_any_emp_data = 0;
      }
    }

    if (!m_any_week_data) {
      if (!have_written_week_hdr) {
        if ( !m_proj_week->write_hdr(ofp) ) return 0;
        have_written_week_hdr = 1;
      }
      if ( !set_proj_week(pwed) ) return 0;  // set whole week
      if ( !m_proj_week->write(ofp) ) return 0;
      if ( !m_any_week_data ) m_any_week_data = 1;
    }

    if ( !add_to_proj_week_emp(pwed) ) return 0;  // set one day
    if (!m_any_emp_data) m_any_emp_data = 1;

  }   // end of loop through proj-week-emp-days

  if (m_any_emp_data) {
    if (!have_written_emp_hdr) {
      if ( !m_proj_week_emp->write_hdr(ofp) ) return 0;
      // have_written_emp_hdr = 1;   not used
    }
    if ( !m_proj_week_emp->write(ofp) ) return 0;
    m_proj_week->reset();
    m_proj_week_emp->reset();
    m_any_emp_data = 0;
  }

  return 1;

}

//----------------------------------------------------------------------
//  set m_proj_week for the week containing pwed

int rppProbWriteWeek1::set_proj_week (rppProjWeekEmpDay *pwed) {

  m_proj_week->set_pw (pwed->proj_id(), pwed->week_num());

  aipTime x_dt(pwed->yyyymmdd());
  long diff = 1 - pwed->week_pos();   // zero or negative
  if (diff) x_dt.add_days(diff);
  long x_yyyymmdd;

  for (int iday=1; iday<=7; iday++) {
    x_yyyymmdd = x_dt.yyyymmdd();
    m_proj_week->set_day (iday, x_yyyymmdd);
    x_dt.add_days(1);
  }

  return 1;

}

//----------------------------------------------------------------------
//  set m_proj_week_emp for the day of pwed
//  set key data members if required

int rppProbWriteWeek1::add_to_proj_week_emp (rppProjWeekEmpDay *pwed) {

  if (!m_any_emp_data) {
    m_proj_week_emp->set_pwe (pwed->proj_id(), 
                              pwed->week_num(), pwed->emp_id());
  }

  m_proj_week_emp->set_day(pwed->week_pos(), pwed->pos_id());

  return 1;

}

//----------------------------------------------------------------------
//  Get an iterator for Proj-Week-Emp-Day in a Prob-Write-Week1

rppProjWeekEmpDayItr 
            rppProbWriteWeek1::projweekempday_iterator () const {

  rppProjWeekEmpDayItr i(m_proj_week_emp_days);

  return i;

}

//======================================================================
//  rppProjWeek
//
//----------------------------------------------------------------------
//  reset this master-data

void rppProjWeek::reset () {

  m_proj_id = m_week_num = -1;

  for (int i=0; i<7; i++) m_yyyymmdd[i] = -1;

}

//----------------------------------------------------------------------
//  Write the header for the data in this object to the string; 
//  return 0 on failure

int rppProjWeek::write_hdr (char *x) const {

  int n = sprintf (x, "#rppProjWeek|proj|week|...");

  return (n ? 1 : 0);

}

//----------------------------------------------------------------------
//  Write the header for the data in this object to a file; 
//  return 0 on failure

int rppProjWeek::write_hdr (FILE *ofp) const {

  char rec[201];

  if ( !write_hdr(rec) ) {
    log("Error writing proj_week to header string");
    return 0;
  }

  if ( !fprintf (ofp, "%s\n", rec) ) {
    log("Error writing proj_week to header file");
    return 0;
  }

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to the string; return 0 on failure

int rppProjWeek::write_str (char *x) const {

  long  l_dom;                 // dom = day-of-month
  char  s_mon[4], s_dow[4];    // dow = day-of-week

  int nn = sprintf (x, "rppProjWeek|%ld|%ld", m_proj_id, m_week_num);
  if (!nn) return 0;
  int n = nn;         // total chars written = chars just written

  for (int i=0; i<7; i++) {
    if (m_yyyymmdd[i] <= 0) return 0;
    aipTime dt(m_yyyymmdd[i]);
    if (!dt.is_valid()) return 0;
    dt.get (0, 0, &l_dom, 0, 0, 0, s_mon, s_dow);
    if (i != 0 && l_dom != 1) strcpy (s_mon, " ");
    nn = sprintf (x+n, "|%s|%ld|%s", s_mon, l_dom, s_dow);
    if (!nn) return 0;
    n += nn;
  }   // end of loop through day in week

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to a file; return 0 on failure

int rppProjWeek::write (FILE *ofp) const {

  char rec[201];

  if ( !write_str(rec) ) {
    log("Error writing proj_week to output string");
    return 0;
  }

  if ( !fprintf (ofp, "%s\n", rec) ) {
    log("Error writing proj_week to output file");
    return 0;
  }

  return 1;

}

//======================================================================
//  rppProjWeekEmp
//
//----------------------------------------------------------------------
//  Reset this set of detail data

void rppProjWeekEmp::reset () {

  m_proj_id = m_week_num = m_emp_id = -1;

  for (int i=0; i<7; i++) m_pos_id[i] = -1;

}

//----------------------------------------------------------------------
//  Write the header for the data in this object to the string; 
//  return 0 on failure

int rppProjWeekEmp::write_hdr (char *x) const {

  char fmt[201];
  strcpy (fmt, "#rppProjWeekEmp|proj|week|emp");
  strcat (fmt, "|day1pos_id|day2pos_id|...");
  int n = sprintf (x, fmt);

  return (n ? 1 : 0);

}

//----------------------------------------------------------------------
//  Write the header for the data in this object to a file; 
//  return 0 on failure

int rppProjWeekEmp::write_hdr (FILE *ofp) const {

  char rec[201];

  if ( !write_hdr(rec) ) {
    log("Error writing proj_week_emp to header string");
    return 0;
  }

  if ( !fprintf (ofp, "%s\n", rec) ) {
    log("Error writing proj_week_emp to header file");
    return 0;
  }

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to the string; return 0 on failure

int rppProjWeekEmp::write_str (char *x) const {

  int nn = sprintf (x, "rppProjWeekEmp|%ld|%ld|%ld",
                       m_proj_id, m_week_num, m_emp_id);
  if (!nn) return 0;
  int n = nn;         // total chars written = chars just written

  for (int i=0; i<7; i++) {
    if (m_pos_id[i] >= 0) {
      nn = sprintf (x+n, "|%ld", m_pos_id[i]);
    } else {
      nn = sprintf (x+n, "| ");
    }
    if (!nn) return 0;
    n += nn;
  }   // end of loop through day in week

  return 1;

}

//----------------------------------------------------------------------
//  Write the data in this object to a file; return 0 on failure

int rppProjWeekEmp::write (FILE *ofp) const {

  char rec[201];

  if ( !write_str(rec) ) {
    log("Error writing proj_week_emp to output string");
    return 0;
  }

  if ( !fprintf (ofp, "%s\n", rec) ) {
    log("Error writing proj_week_emp to output file");
    return 0;
  }

  return 1;

}

//======================================================================
//  Iterators
//
//  All function bodies are in the header file 
//
//======================================================================
//  rppFileDate
//
//----------------------------------------------------------------------
//  Constructor  -  from a string "yyyy-mm-dd"

rppFileDate::rppFileDate (const char *a_yyyy_mm_dd) {

  char *p = m_yyyy_mm_dd;

  aip_strncpy (p, a_yyyy_mm_dd, File_Date_Len);

  if ( strlen(p) != File_Date_Len) { reset(); return; }

  p[4] = p[7] = '\0';

  long yr = atol(p);
  long mo = atol(p+5);
  long dy = atol(p+8);

  if ( yr < 1800 || yr > 2300 ||
       mo <    1 || mo >   12 ||
       dy <    1 || dy > aip_days_in_month(yr,mo) ) { reset(); return; }

  m_yyyymmdd =  yr*10000 + mo*100 + dy;

}

//----------------------------------------------------------------------
//  Constructor  -  from a long yyyymmdd

rppFileDate::rppFileDate (long a_yyyymmdd) {

  long yr = a_yyyymmdd / 10000;
  a_yyyymmdd -= (yr * 10000);
  long mo = a_yyyymmdd / 100;
  a_yyyymmdd -= (mo * 100);
  long dy = a_yyyymmdd;

  if ( yr < 1800 || yr > 2300 ||
       mo <    1 || mo >   12 ||
       dy <    1 || dy > aip_days_in_month(yr,mo) ) { reset(); return; }

  sprintf (m_yyyy_mm_dd, "%ld-%02ld-%02ld", yr, mo, dy);

}

//----------------------------------------------------------------------
//  Destructor

rppFileDate::~rppFileDate () {}

//----------------------------------------------------------------------
//  Return true if the date is valid

int rppFileDate::is_valid () { 

  return ( m_yyyymmdd ? 1 : 0 );

}

//======================================================================
//  rppFileTime
//
//----------------------------------------------------------------------
//  Constructor  -  from a string "hh:mm"

rppFileTime::rppFileTime (const char *a_hh_mm) {

  char *p = m_hh_mm;

  aip_strncpy (p, a_hh_mm, File_Time_Len);

  if ( strlen(p) != File_Time_Len) { reset(); return; }

  if (p[1] == ':') {
    p[1] = '\0';
  } else {
    p[2] = '\0';
  }

  long hr = atol(p);
  long mn = atol(p+3);

  if ( hr <  0 || hr > 23 || 
       mn <  0 || mn > 59 ) { reset(); return; }

  m_hhmm =  hr*100 + mn;

}

//----------------------------------------------------------------------
//  Constructor  -  from a long hhmm

rppFileTime::rppFileTime (long a_hhmm) {

  long hr = a_hhmm / 100;
  a_hhmm -= (hr * 100);
  long mn = a_hhmm;

  if ( hr <  0 || hr > 23 || 
       mn <  0 || mn > 59 ) { reset(); return; }

  sprintf (m_hh_mm, "%02ld:%02ld", hr, mn);

}

//----------------------------------------------------------------------
//  Destructor

rppFileTime::~rppFileTime () {}

//----------------------------------------------------------------------
//  Return true if the time is valid

int rppFileTime::is_valid () { 

  return ( m_hhmm<2400 ? 1 : 0 );

}

//======================================================================
//                           License
//
//   Permission is hereby granted, free of charge, to any
//   person obtaining a copy of this software and associated
//   documentation files (the "Software"), to deal in the Software
//   without restriction, including without limitation the rights
//   to use, copy, modify, merge, publish, distribute, sublicense,
//   and/or sell copies of the Software, and to permit persons to
//   whom the Software is furnished to do so, subject to the
//   following conditions:
//
//   The copyright notice and this license shall be included in all
//   copies or substantial portions of the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
//
//======================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -