c练习题——日、月(英文、缩写、数字)、年统计输入日期的总天数

#include <stdio.h>
#include <ctype.h> // strchr()在字符串中查找特定字符; strcmp()比较两组字符串是否相同
#include <string.h> // toupper()转成大写字母;tolower()转成小写字母
void func_UserEnter(char c_array[15],int* i_days);
char* s_gets(char* p_array, int i_MaxCount);
unsigned int func_Tonumber(char* c_array);
unsigned int func_countdays(unsigned int u_month_num, int* i_days);
unsigned int func_strTOint(char* p_array);

struct month
{

	char c_month[10];
	char c_mouth_abbreviation[4];
	unsigned int u_month_days;
	unsigned int u_month_num;
};

// 初始化月份结构数组
struct month months[12] =
{
	{"January","Jan",31,1},
	{"February","Feb",28,2},
	{"March","Mar",31,3},
	{"April","Apr",30,4},
	{"May","May",31,5},
	{"June","Jun",30,6},
	{"July","Jul",31,7},
	{"August","Aug",31,8},
	{"September","Sep",30,9},
	{"October","Oct",31,10},
	{"November","Nov",30,11},
	{"December","Dec",31,12}
};

int main(void) {
	unsigned int u_month_num, u_totaldays;
	int i_days;
	char c_array[15];

	func_UserEnter(c_array,&i_days);
	u_month_num = func_Tonumber(c_array);
	while (u_month_num < 1 || u_month_num > 12 || i_days <= 0 || i_days >31)
	{
		printf("请重新输入正确的日期\n");
		reEnter:
		func_UserEnter(c_array, &i_days);
		u_month_num = func_Tonumber(c_array);
	}
	
	for (size_t i = 0; i < 12; i++)
	{
		if (u_month_num == months[i].u_month_num)
		{
			if (i_days > months[i].u_month_days)
			{
				printf("%u月没有%d天,请重新输入正确的日期!\n", u_month_num, i_days);
				goto reEnter;
			}
		}
	}
	u_totaldays = func_countdays(u_month_num, &i_days);
	printf("All have %u days", u_totaldays);
	return 0;
}

// 用户输入
void func_UserEnter(char c_array[15], int * i_days) {
	int i_years;
	printf("Please Enter number of days: ");
	scanf_s("%d", i_days);
	while (getchar() != '\n')
		continue;
	printf("Please Enter number or name of month: ");
	scanf_s("%s", c_array, 15 * sizeof(char));
	while (getchar() != '\n')
		continue;
	printf("Please Enter number of years: ");
	scanf_s("%d", &i_years);
	while (getchar() != '\n')
		continue;
	// 处理月份


}

// 处理月份
unsigned int func_Tonumber(char* c_array) {
	c_array[0] = toupper(c_array[0]);
	for (size_t i = 1; i < strlen(c_array); i++)
	{
		c_array[i] = tolower(c_array[i]);
	}
	for (size_t i = 0; i < 12; i++)
	{
		if (strcmp(c_array, months[i].c_month)==0 || strcmp(c_array, months[i].c_mouth_abbreviation) == 0 || func_strTOint(c_array) == months[i].u_month_num)
		{
			return months[i].u_month_num;
		}
		
	}
	return 0;
	
}

// 计算天数
unsigned int func_countdays(unsigned int u_month_num, int * i_days) {
	unsigned int u_totaldays = 0;
	for (unsigned int i = 0; i < u_month_num-1; i++)
	{
		u_totaldays += (unsigned int)months[i].u_month_days;
	}
	return u_totaldays + (unsigned int)*i_days;
}

//数字字符串类型数组转整数类型数字
unsigned int func_strTOint(char* p_array) {
	unsigned int i_result;
	for (i_result = 0; *p_array; p_array++)
	{
		i_result = i_result * 10 + *p_array - '0';
	}
	return i_result;
}

// 处理字符串输入
char* s_gets(char* p_array, int i_MaxCount) {
	char* p_find;
	char* p_str;
	p_str = fgets(p_array, i_MaxCount, stdin);
	if (p_str)
	{
		p_find = strchr(p_array, '\n');
		if (p_find)
		{
			*p_find = '\0';
		}
		else
		{
			while (getchar() != '\n')
			{
				continue;
			}
		}
	}
	return p_str;
}

过程展示

Please Enter number of days: 32
Please Enter number or name of month: 1
Please Enter number of years: 2100
请重新输入正确的日期
Please Enter number of days: 30
Please Enter number or name of month: 2
Please Enter number of years: 2100
2月没有30天,请重新输入正确的日期!
Please Enter number of days: 4
Please Enter number or name of month: 13
Please Enter number of years: 2100
请重新输入正确的日期
Please Enter number of days: 31
Please Enter number or name of month: 4
Please Enter number of years: 2100
4月没有31天,请重新输入正确的日期!
Please Enter number of days: 30
Please Enter number or name of month: july
Please Enter number of years: 2021
All have 211 days
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值