三天打渔两天晒网问题
刚学习c++不久,自己写了一个简单的小程序。由于不太会用c++的文件操作,里面涉及到文件的操作使用了c的语法。程序使用vs2015编译。
有不足之处希望能够指出,我会努力改进的。
//主要功能:渔夫从2010年1月1日起三天打鱼两天晒网,计算之后的任意一天他是打渔还是晒网
#include "stdafx.h"
#include "iostream"
using namespace std;
int year; //存入年
int month; //存入月
int day; //存入天
int days; //存总天数
int yearflag; //标记,1为闰年,0为平年
int monthflag; //标记,1为大月,0为小月,2为二月
char s[30]; //用来存结果
//此函数用来向文件中写入日期
void writefile1()
{
FILE *fp;
fp = fopen("in.txt", "w");
if (!fp)
{
printf("\n打开文件in.txt失败!");
}
fprintf(fp,"%d %d %d", year,month,day);
printf("\n");
fclose(fp);
}
//此函数用来向文件中写入结果
void writefile2()
{
FILE *fp;
fp = fopen("out.txt", "w");
if (!fp)
{
printf("\n打开文件out.txt失败!");
}
fprintf(fp, "%s",s);
printf("\n");
fclose(fp);
}
//输入年的函数
void inputyear()
{
cout << "请输入年(起始年为2010年):";
cin >> year;
if (year < 2010)//年份不能小于起始年份
{
cout << "年份有误,请重新输入"<<endl;
inputyear();
}
}
//输入月的函数
void inputmonth()
{
cout << "请输入月:";
cin >> month;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)//大月
monthflag = 1;
if (month == 4 || month == 6 || month == 9 || month == 11)//小月
monthflag = 0;
if (month == 2)//二月
monthflag = 2;
if (month < 1 || month>12)
{
cout << "月份有误,请重新输入" << endl;
inputmonth();
}
}
//判断闰年还是平年
int judge(int x)
{
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
return 1;//闰年返回1
else
return 0;//平年返回0
}
//输入日的函数
void inputday()
{
cout << "请输入日:";
cin >> day;
if ((judge(year) == 1 && monthflag == 2 && day > 29) || (judge(year) == 0 && monthflag == 2 && day > 28) || (monthflag == 1 && day > 31) || (monthflag == 0 && day > 30))
{
cout << "天数有误,请重新输入" << endl;
inputday();
}
}
//计算总天数的函数
void calculate()
{
int m1[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31};//平年每个月的天数
int m2[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年每个月的天数
for (int a = 2010; a <year; a++)
{
//计算整年的天数
yearflag=judge(a);
if (yearflag ==1)//如果为闰年
days = days + 366;//一年366天
if(yearflag ==0)//平年
days = days + 365;//365天
}
//计算正月的天数
if (yearflag == 1)
for (int i = 0; i < month; i++)
days = days + m2[i];
if(yearflag == 0)
for (int i = 0; i < month; i++)
days = days + m1[i];
days = days + day;//加上最后的日得到总天数
}
//输出结果的函数
void result()
{
cout << "结果为:" << endl;
int n;
n = days % 5;
if (n == 0 || n == 1 || n == 2)
{
cout << "他这天打渔";
strcpy(s, "他这天打渔");
}
if (n == 3 || n == 4)
{
cout << "他今天晒网";
strcpy(s, "他今天晒网");
}
}
//菜单
void menu()
{
int choose;
cout << " *********************************************************" << endl;
cout << " # #"<< endl;
cout << " # 欢迎来到三天打鱼两天晒网问题 #"<< endl;
cout << " # #"<< endl;
cout << " # 版本 : v1.0 #"<< endl;
cout << " # #"<< endl;
cout << " *********************************************************"<< endl;
cout << " # #"<< endl;
cout << " # 1.输入日期 2.退出程序 #"<< endl;
cout << " # #"<< endl;
cout << " *********************************************************"<< endl;
while (1)
{
cout << endl<<"请输入操作编号:"<<endl;
cin >> choose;
switch (choose)
{
case 1:
inputyear();
inputmonth();
inputday();
writefile1();
calculate();
result();
writefile2();
break;
case 2:
return;
default:
cout << "请在1和2之间选择" << endl;
}
}
}
int main()
{
menu();
return 0;
}