“三天打渔,两天晒网”,从2010年1月1日开始,计算后来的任意一天是打鱼还是晒网

来源:互联网 发布:怕错过了也不会知 编辑:程序博客网 时间:2024/06/10 05:57


//编译环境:windos 8 

//编译工具:visual studio 2015
#include"stdafx.h"


#include<fstream>
#include<string>
#include<iostream>
#include<Windows.h>


using namespace std;
#define LENGTH 8     //输入日期长度
#define FISH "出海打渔"
#define NET "在家晒网"
class Date       //日期类
{
public:
int year;//年
int month;//月
int day;//日
int daysum;//总天数
void format(string str)                 //从字符变量中提取日期数据
{
year = atoi((str.substr(0, 4)).c_str());
month= atoi((str.substr(4, 2)).c_str());
day= atoi((str.substr(6, 2)).c_str());
daysum = day;
}
bool yearType(int y)//判断是否为闰年
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
int monthType(int m)//根据对应的月份得到这个月的天数
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
return 31;
else if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
else
{
if (yearType(year))//判断这一年是否为闰年,则2月对应着不同天数
return 29;
else
return 28;
}
}
bool match(string str)//判断数据是否合法
{
const char* ch = str.c_str();     //将string类型数据转为char数组便于查看每一个字符是否合法
if (str.length() == LENGTH)    
{
for (int i = 0;i < 8;i++)//遍历每一个字符
if (ch[i] >= '0'&&ch[i] <= '9')
continue;
else
return false;
}
format(str);//格式化字符串,将数据中包含的信息进行分析
if (year < 2010 ||month == 0 || month > 12)//判断年月日是否合法
return false;
else
{
if (day > 0 && day <= monthType(month))
return true;
else
return false;
}
}
void daySum() //求总天数
{
int y = 0, m = 1, x = 0;
while (x < year - 2010)      //与2010年相差每一年的天数相加
{
y = 2010 + x;
if (yearType(y))
daysum += 366;
else
daysum += 365;
x++;
}
while (m < month)        //每月天数相加
{
daysum += monthType(m);
m++;
}
}
string work() //得到这一天的工作内容
{
int index = daysum % 5;
if (index > 0 && index < 4)
return  FISH;
else
return  NET;
}
};
HANDLE OpenNewFile(LPSTR newfile)         //获得进程句柄


{
STARTUPINFOA si = { sizeof(si) };
SECURITY_ATTRIBUTES saProdess, saThread;
PROCESS_INFORMATION piProcess;
saProdess.nLength = sizeof(saProdess);
saProdess.lpSecurityDescriptor = NULL;
saProdess.bInheritHandle = TRUE;
saThread.nLength = sizeof(saThread);
saThread.lpSecurityDescriptor = NULL;
saThread.bInheritHandle = FALSE;
if (CreateProcessA(NULL, newfile, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &piProcess))
return(piProcess.hProcess);
else
return 0;
}
void input0()//在本程序中输入数据,并查看输入数据的工作信息
{
Date date;
fstream infile;
fstream writefile;
writefile.open("C:\\Users\\sywong\\Desktop\\out.txt", ios::out | ios::app);
infile.open("C:\\Users\\sywong\\Desktop\\in.txt", ios::out | ios::beg);
fstream readfile;
string s;//判定输入是否结束
string str;//将文件中的每行数据读到字符串中
cout << "请输入合法数据(如20100101):(输入xxxxxxxx结束)" << endl;
cin >> s;
while (s != "xxxxxxxx")
{
infile << s << endl;
cin >> s;
}
infile.close();
cout << "输入完毕" << endl;
readfile.open("C:\\Users\\sywong\\Desktop\\in.txt",ios::in|ios::beg);
while (getline(readfile, str))//对得到每一行数据进行处理
{
if (date.match(str))//该行数据是否合法
{
date.daySum();
char str1[40] = "";
sprintf_s(str1, 40, "%d年%d月%d日", date.year, date.month, date.day);//将日期格式化存储在str1
writefile << str1 << date.work() << endl;//将str1和该日期的工作写入文件out.txt
cout << str1 << date.work() << endl;
continue;
}
else
{
writefile << str << "此行日期数据错误" << endl;
cout << "数据格式错误" << endl;
}
}
readfile.close();
writefile.close();
}
void input1()//打开记事本输入数据,并查看输入数据的工作信息
{
Date date;
fstream writefile;
fstream readfile;
writefile.open("C:\\Users\\sywong\\Desktop\\out.txt", ios::out | ios::app);
readfile.open("C:\\Users\\sywong\\Desktop\\in.txt", ios::out | ios::beg);//清除之前输入数据
readfile.close();
readfile.open("C:\\Users\\sywong\\Desktop\\in.txt", ios::in|ios::beg);//重新关联文件
string str;
HANDLE hand = OpenNewFile("notepad.exe C:\\Users\\sywong\\Desktop\\in.txt");//暂停主进程,创建一个新的子进程,打开记事本打开此文件,并获得进程句柄
WaitForSingleObject(hand, INFINITE);//当该进程结束时继续运行主进程


while (getline(readfile, str))//得到每一行数据
{
if (date.match(str))//该行数据是否合法
{
date.daySum();
char str1[40] = "";
sprintf_s(str1, 40, "%d年%d月%d日", date.year, date.month, date.day);//将日期格式化存储在str1
writefile << str1 << date.work() << endl;//将str1和该日期的工作写入文件out.txt
cout << str1 << date.work() << endl;
continue;
}
else
{
writefile << str << "此行日期数据错误" << endl;
cout << "数据格式错误" << endl;
}
}
readfile.close();
writefile.close();
}
int function()
{
int choose;
cout << "请选择功能:";
cin >> choose;
cout << endl;
switch (choose)
{
case 0://在本程序中输入数据,并查看输入数据的工作信息
input0();break;
case 1://打开记事本输入数据,并查看输入数据的工作信息
input1();break;
case 2://查看所有输入日期的工作信息
WinExec("notepad.exe C:\\Users\\sywong\\Desktop\\out.txt", SW_SHOW);
break;
case 3://退出程序
return 0;
default:
cout << "功能输入错误" << endl;
}
return 1;
}
void maininterface()//主界面
{
cout << "************************************" << endl;
cout << "*****   0、间接输入日期数据    *****" << endl;
cout << "*****   1、直接输入日期数据    *****" << endl;
cout << "*****   2、查看所有已查询日期  *****" << endl;
cout << "*****   3、退出程序            *****" << endl;
cout << "************************************" << endl;
}
void main()
{
int i;
do
{
maininterface();
i=function();
} while (i == 1);
cout << "退出程序"<<endl;

}
0 0
原创粉丝点击