动物检疫员排班表查询程序

来源:互联网 发布:游戏工作室网络组建 编辑:程序博客网 时间:2024/04/29 23:32

我现在的本职工作是动物检疫员,业余时间学习IT技术。我的上班时间比较复杂,有时需要连续七天凌晨早起,有时又休息三四天。其中有规律,而且长期固定。为了便于自己查询排班情况,我编写了这个程序。

Debug经验:结构体不能放在头文件中然后包含在main文件,只能放在main文件中。如何把结构体放在main函数以外然后加以引用?#include语句和extern标识符都已被证明不起作用。留待以后研究。

#include <iostream>#include <cstdlib>#include <cstring>using namespace std;struct Time_t{int year, month, day;};int main(){void Scheduling();int GetInteger();cout << "**********************************************" << endl;cout << "*提示:本程序只对2017年8月21日以后的日期有效!*" << endl;cout << "**********************************************" << endl;cout << endl;Scheduling();cout << "++++++++++++++++++++++++++++++++++++++++++++++" << endl;while(1){cout << endl << "是否继续输入?请选择数字:(1.是;2.否)" << endl;int input = GetInteger();switch(input){case 1:cout << endl;Scheduling();cout << "++++++++++++++++++++++++++++++++++++++++++++++" << endl;break;case 2:system("pause");return 0;}}system("pause");return 0;}int GetInteger()  {      char buf[100] = {0};      while(strlen(buf) == 0)  //用户直接输入回车           cin.getline(buf, 100);      return atoi(buf);  //atoi函数是cstring头文件自带的   }  void Scheduling(){int TimeDifference(Time_t s, Time_t t);bool LeapYear(int n);Time_t t[2];char tmp;t[0].year = 2017;t[0].month = 8;t[0].day = 21;cout << "请输入需要排班的日期:(格式“年/月/日”)" << endl;cin >> t[1].year >> tmp >> t[1].month >> tmp >> t[1].day;int days = TimeDifference(t[0], t[1]);if( days / 7 % 2 == 0 ) cout << "这一天上早班!早睡早起!保重身体!" << endl;else{if( days / 7 % 4 == 1 ){if( days % 7 < 4 && days % 7 >= 0)//注意:小于4,而不是小于等于4 cout << "这一天上白班!这周休五六七!" << endl;else cout << "这一天休息!这周休五六七!" << endl;}if( days / 7 % 4 == 3){if(days % 7 <= 6 && days % 7 >= 4)cout << "这一天上白班!这周休一二三四!" << endl;else cout << "这一天休息!这周休一二三四!" << endl;}}}int TimeDifference(Time_t s, Time_t t)   //核心技巧:要把闰年考虑进去 {      bool LeapYear(int n);int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day_count = 0;    if(t.year - s.year < 1)      //若首年末年是同一年     {day_count += t.day - s.day;      for(int i = s.month; i < t.month; i++)          day_count += days[i - 1];  //减1的目的是数组下标与实际的月份数相一致 if(LeapYear(s.year) == true && s.month <= 2 && t.month >= 3)    day_count++;               //若是闰年且包含2月29日,总天数加1      } if(t.year - s.year == 1)     //若首年末年刚好是相邻的两年     {day_count += 31 - s.day;      for(int i = s.month; i < 12; i++)          day_count += days[i - 1];    //计算首年的起始日到最后一天的天数        day_count += t.day - 0;for(int i = 1; i < t.month; i++)day_count += days[i - 1];     //计算末年的第一天到终止日的天数并累加 if(LeapYear(s.year) == true && s.month <= 2)    day_count++;               //若首年是闰年且包含2月29日,总天数加1if(LeapYear(t.year) == true && t.month >= 3)day_count++;               //若末年是闰年且包含2月29日,总天数加1 }    if(t.year - s.year > 1)     //若首年末年不相邻,即至少夹了一年的 {day_count += 31 - s.day;      for(int i = s.month; i < 12; i++)          day_count += days[i - 1];    //计算首年的起始日到最后一天的天数        day_count += t.day - 0;for(int i = 1; i < t.month; i++)day_count += days[i - 1];     //计算末年的第一天到终止日的天数并累加for(int i = s.year + 1; i < t.year; i++)day_count += 365;     //计算中间所夹的年的天数并累加,暂时默认为平年 if(LeapYear(s.year) == true && s.month <= 2)    day_count++;               //若首年是闰年且包含2月29日,总天数加1if(LeapYear(t.year) == true && t.month >= 3)day_count++;               //若末年是闰年且包含2月29日,总天数加1 for(int i = s.year + 1; i < t.year; i++)if(LeapYear(i) == true)day_count++;       //对于所夹的每年,判断是否闰年,若是则总天数加1     } int result = day_count;      return result;  }  bool LeapYear(int n){if(n % 400 == 0)return true;elseif(n % 4 == 0 && n % 100 != 0)return true;elsereturn false;}