三天打鱼两天晒网 判断从2010开始后的每一天是在打鱼还是晒网

用结构体重写了一个

// written by kingdeguo
// All rights reserved
// 2021.3.4
#include <stdio.h>
#include <stdbool.h>

typedef struct Date{
	int year;
	int month;
	int day;
}Date;

void getDate(Date* date);
bool judeDate(Date* date);
bool isLeapYear(int year);
int getDays(Date* date);
bool fishing_or_not(int days);

int main(){
	printf("============================================================\n");
	printf("This is program is mean to judge the date, fishing or resting\n");
	printf("Remeber: The date should be after 2010.1.1\n");
	printf("============================================================\n");
	// initialize the date
	Date mydate;
	Date* p_mydate;
	p_mydate = &mydate;
	// get the data
	printf("Please input the date: ");
	getDate(p_mydate);
	// judge the data
	while(judeDate(p_mydate)){
		// Calculation
		int sum = getDays(p_mydate);
		printf("%d days in total\n", sum);
		if(fishing_or_not(sum)){
			printf("Fishing\n\n\n");
		}else{
			printf("Resting\n\n\n");
		}
		// do the loop
		printf("Please input the date: ");
		getDate(p_mydate);
	}
	return 0;
}

void getDate(Date* date){
	scanf("%d %d %d",&date->year, &date->month, &date->day);
	printf("Your Input is: %d %d %d\n",date->year, date->month, date->day);
}

bool judeDate(Date* date){
	// judge year
	if(date->year < 2011){
		printf("The year should be after 2011\n");
		return false;
	}
	// judge month
	if(date->month <0 || date->month > 12){
		printf("The month should be between 0 - 12\n");
		return false;
	}
	// judge February
	if(isLeapYear(date->year) && date->month==2){
		if(date->day <0 || date->day>30){
			printf("The wrong day\n");
			return false;
		}
	}else{
		if(date->day <0 || date->day>30){
			printf("The wrong day\n");
			return false;
		}
	}
	// judge other months
	if((date->month <8 && date->month%2==1) || (date->month>7 && date->month%2==0)){
		if(date->day <0 || date->day>31){
			printf("The wrong day\n");
			return false;
		}
	}else{
		if(date->day <0 || date->day>30){
			printf("The wrong day\n");
			return false;
		}
	}
}

bool isLeapYear(int year){
	if(((year%4==0) && (year%100!=0)) || year%400==0){  
        return true;  
    }else{  
       return false;  
    }  
}

int getDays(Date* date){
	// two kinds of months
	int days_leap[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
	int days_not_leap[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	// initialize
	int days = 0;
	int sum = 0;
	int i = 0;
	// get the date of the last years
	if(isLeapYear(date->year)){
		for(i=0; i<date->month-1; ++i){
			days += days_leap[i];
		}
	}else{
		for(i=0; i<date->month-1; ++i){
			days += days_not_leap[i];
		}
	}
	// get the dates before last year
	int years = 0;
	for(i=2011; i<date->year; ++i){
		if(isLeapYear(i)){
			years += 366;
		}else{
			years += 365;
		}
	}
	// get all the dates
	sum = years + days + date->day;
	return sum;
}

bool fishing_or_not(int days){
	if(days % 5 <=3){
		return true;
	}else{
		return false;
	}
}

分界线(下面的是之前写的)


三天打鱼两天晒网
判断从2010开始后的每一天是在打鱼还是晒网
这个程序增加了一些错误时间判断与提示等

//判断从2010开始后的每一天是在打鱼还是晒网
#include <stdio.h>
int main()
 {
	int year, month, day;
//	int year; int month; int day;
	printf("Please enter the date:( order as year month day )"); 
	scanf ("%d%d%d",&year,&month,&day);
	int a, b, c,sum, t, l;
//	int a; int b; int k; int sum; int t; int l;
	int p = 0,q = 0, i = 2010;

	//判断数字是否合法
	//判断年是否合法
	if ( year < 2010 ) {
		printf("您的输入有误\n");
		return 0;
	}
	//判断月 与 日 是否合法
	if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {	//判断天数为31的月份
		if (day < 1 || day > 31) {
			printf ("您的输入有误\n");
			return 0;
		}
	}

	if ( month == 4 || month == 6 || month == 9 || month == 11) { //判断天数为30的月份
		if (day < 1 || day > 30) {
			printf ("您的输入有误\n");
			return 0;
		}
	}

	if ((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0) { //判断天数为28,29的月份
		if (month == 2 && day > 29 ) {
			printf("您的输入有误\n");
			return 0;
		}
	} else if (month == 2 && day >= 28 ) {
		printf ("您的输入有误\n");
		return 0;
	}

	//月份b 值的初始化
	if (((year - 1) % 4 == 0 && (year - 1) % 100 != 0 ) || (year -1) % 400 == 0) {
		l = 29;
	} else {
		l = 28;
	}

	b = month - 1;

	switch(b) {
		case 0:
			c = 0;
			break;
		case 1:
			c = 31;
			break;
		case 2:
			c = 31 + l;
			break;
		case 3:
			c = 31 + l + 31;
			break;
		case 4:
			c = 31 + l + 31 + 30;
			break;
		case 5:
			c = 31 + l + 31 + 30 + 31;
			break;
		case 6:
			c = 31 + l + 31 + 30 + 31 +30;
			break;
		case 7:
			c = 31 + l + 31 + 30 + 31 +30 + 31;
			break;
		case 8:
			c = 31 + l + 31 + 30 + 31 +30 + 31 + 31;
			break;
		case 9:
			c = 31 + l + 31 + 30 + 31 +30 + 31 + 31 + 30;
			break;
		case 10:
			c = 31 + l + 31 + 30 + 31 +30 + 31 + 31 + 30 + 31;
			break;
		case 11:
			c = 31 + l + 31 + 30 + 31 +30 + 31 + 31 + 30 + 31 + 30;
			break;
	}

	if (year > 2010) {
		for (i=2010; i < year; ++i) {
			if (((year - 1) % 4 == 0 && (year - 1) % 100 != 0 ) || (year -1) % 400 == 0) {
				p = p + 1;
			} else {
				q = q + 1;
			}
			a = p * 366 + q * 365;
		}
	}

	sum = a + c + day;

	t = t % 5;

	if (t == 1 || t == 2 || t == 3)
		printf ("打鱼\n");
	else
		printf ("晒网\n");

//	printf("%d\n",sum);
//	printf ("\n");
//	printf ("%d\n",a);
//	printf ("%d\n",b);
//	printf ("%d\n",c);
//	printf ("%d\n",day);

	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值