C Primer Plus第六版第十四章 编程练习14-2

调了很久,记录一下。

虽然很稀烂。


 

//protect.h
#pragma warning(disable:4996);

//numbersOfDays.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "protect.h"

struct month {
	char name[10];
	char abbrev[4];
	int days;
	int monumb;
};
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 ff = 0;
union monthnames
{
	int monthnum;
	char monthname[10];
};

char* s_gets(char* st, int n);
void getinfo(int* yy, union monthnames* mm, int *dd);
void sum_days(int yy, union monthnames mm, int dd);
void transMonthname(char* x);
void monthget(union monthnames* mm);

int main(void)
{
	int year, day;
	union monthnames month;

	getinfo(&year, &month, &day);
	sum_days(year, month, day);

	return 0;
}


char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');   // look for newline
		if (find)                  // if the address is not NULL,
			*find = '\0';          // place a null character there
		else
			while (getchar() != '\n')
				continue;          // dispose of rest of line
	}
	return ret_val;
}

void getinfo(int* yy, union monthnames* mm, int *dd)
{
	puts("Please enter the year:");
	scanf_s("%d", yy);
	puts("Please enter the month:");
	monthget(mm);
	puts("Please enter the day:");
	scanf_s("%d", dd);
}

void transMonthname(char* x)
{
	for (int i = 0; i < strlen(x); i++)
	{
		if (i == 0)
			x[i] = toupper(x[i]);
		else
			x[i] = tolower(x[i]);
	}
}

void sum_days(int yy, union monthnames mm, int dd)
{
	int sum = 0;
	int i;
	int flag = 0;


	if (!ff)
	{
		for (i = 0; i < mm.monthnum-1; i++)
			sum += months[i].days;
		flag = 1;
	}
	else if (ff)
	{
		if (strlen(mm.monthname) == 3)
		{
			for (i = 0; i < 3; i++)
				mm.monthname[i] = tolower(mm.monthname[i]);
			for (i = 0; i < 12; i++)
			{
				if (strcmp(mm.monthname, months[i].abbrev) == 0)
				{
					flag = 1;
					break;
				}
			}
			if (flag)
			{
				for (int j = 0; j < i; j++)
					sum += months[j].days;
			}
		}
		else
		{
			transMonthname(mm.monthname);
			for (i = 0; i < 12; i++)
			{
				if (strcmp(mm.monthname, months[i].name) == 0)
				{
					flag = 1;
					break;
				}
			}
			if (flag)
			{
				for (int j = 0; j < i; j++)
					sum += months[j].days;
			}
		}

	}
	

	if (flag)
	{
		if (yy % 4 == 0)
			sum += dd + 1;
		else
			sum += dd;
		printf("There are %d days before %s.\n", sum, months[i].name);
	}
	else
		puts("Error!");
}

void monthget(union monthnames* mm)
{
	if (scanf("%d",& mm->monthnum) == 1)
		;
	else
	{
		ff = 1;
		s_gets(mm->monthname, 10);
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值