#include
#include
#include
#include
#define LEN 12
struct month
{
char name[10];
char abbrev[4];
int days;
int monumb;
};
struct month months[LEN] =
{
{"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}
};
void is_leap_year(int year);
int days_result(char *month, int days);
int main(void)
{
int val;
int day, year;
char month[LEN];
printf("Please enter day, month and year (q to quit): ");
while (scanf("%d %11s %d", &day, month, &year) == 3)
{
is_leap_year(year);
val = days_result(month, day);
if (val > 0)
{
printf("There are %d days from the beginning of %d to %s %d\n", val, year, month, day);
}
else
{
printf("You entered invalid datas!\n");
}
months[1].days = 28; //2月份天数还原为非闰年时的天数;
printf("You can enter day, month and year again (q to quit): ");
}
printf("Done.\n");
return 0;
}
void is_leap_year(int year) //判断是否是闰年;
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
months[1].days = 29; //是闰年则2月份天数改为29天;
}
return;
}
int days_result(char *month, int days)
{
int i;
int total = 0;
int temp = atoi(month);
if (days < 1 || days > 31)
{
return -1; //输入的天数有误则终止本函数;
}
if (0 == temp) //用户输入的月份不是数字时要进行转换;
{
month[0] = toupper(month[0]);
for (i = 1; month[i] != '\0'; i++)
{
month[i] = tolower(month[i]);
}
}
for (i = 0; i < LEN; i++)
{
if ((temp == months[i].monumb) ||
(strcmp(month, months[i].name) == 0) ||
(strcmp(month, months[i].abbrev) == 0))
{
if (days > months[i].days)
{
return -1; //输入的天数不符合月份的规则;
}
else
{
return total + days; //累加到此月后加上本月天数;
}
}
else
{
total += months[i].days;
}
}
return -1; //输入月份有误后的终止条件;
}
这条代码能改成多文件形式吗?就是把分函数放在别的文件中。改的话怎么改?最好有演示
8-33行放在一个头文件
62-110行放到一个c文件,需要#include头文件
main函数放到一个c文件,需要#include头文件
//a.c
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define LEN 12
#include "testit1.h"
int main(void)
{
int val;
int day, year;
char month[LEN];
printf("Please enter day, month and year (q to quit): ");
while (scanf("%d %11s %d", &day, month, &year) == 3)
{
is_leap_year(year);
val = days_result(month, day);
if (val > 0)
{
printf("There are %d days from the beginning of %d to %s %d\n", val, year, month, day);
}
else
{
printf("You entered invalid datas!\n");
}
months[1].days = 28; //2月份天数还原为非闰年时的天数;
printf("You can enter day, month and year again (q to quit): ");
}
printf("Done.\n");
return 0;
}
//testit1.h
#ifndef MAIN_C_TESTIT1_H
#define MAIN_C_TESTIT1_H
void is_leap_year(int year);
int days_result(char *month, int days);
struct month
{
char name[10];
char abbrev[4];
int days;
int monumb;
};
struct month months[LEN] =
{
{"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}
};
#endif //MAIN_C_TESTIT1_H
//testit1.c
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define LEN 12
#include "testit1.h"
void is_leap_year(int year) //判断是否是闰年;
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
months[1].days = 29; //是闰年则2月份天数改为29天;
}
return;
}
int days_result(char *month, int days)
{
int i;
int total = 0;
int temp = atoi(month); //s -> d
if (days < 1 || days > 31)
{
return -1; //输入的天数有误则终止本函数;
}
if (0 == temp) //用户输入的月份不是数字时要进行转换;
{
month[0] = toupper(month[0]);
for (i = 1; month[i] != '\0'; i++)
{
month[i] = tolower(month[i]);
}
}//转成标准格式
for (i = 0; i < LEN; i++)
{
if ((temp == months[i].monumb) ||
(strcmp(month, months[i].name) == 0) ||
(strcmp(month, months[i].abbrev) == 0))
//超级识别,精准判断哪一个是输入的月份
{
if (days > months[i].days)
{
return -1; //输入的天数不符合月份的规则;
}
else
{
return total + days; //累加到此月后加上本月天数;
}
}
else
{
total += months[i].days;
}
}
return -1; //输入月份有误后的终止条件;
}
我想这样改但是报错
/*头文件 my_head.h */
#ifndef MY_HEAD_H
#define MY_HEAD_H
#define LEN 12
struct month
{
char name[10];
char abbrev[4];
int days;
int monumb;
};
struct month months[LEN] =
{
{"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}
};
extern void is_leap_year(int year);
extern int days_result(char *month, int days);
#endif
//实现函数文件 my_head.c
void is_leap_year(int year) //判断是否是闰年;
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
months[1].days = 29; //是闰年则2月份天数改为29天;
}
return;
}
int days_result(char *month, int days)
{
int i;
int total = 0;
int temp = atoi(month);
if (days < 1 || days > 31)
{
return -1; //输入的天数有误则终止本函数;
}
if (0 == temp) //用户输入的月份不是数字时要进行转换;
{
month[0] = toupper(month[0]);
for (i = 1; month[i] != '\0'; i++)
{
month[i] = tolower(month[i]);
}
}
for (i = 0; i < LEN; i++)
{
if ((temp == months[i].monumb) ||
(strcmp(month, months[i].name) == 0) ||
(strcmp(month, months[i].abbrev) == 0))
{
if (days > months[i].days)
{
return -1; //输入的天数不符合月份的规则;
}
else
{
return total + days; //累加到此月后加上本月天数;
}
}
else
{
total += months[i].days;
}
}
return -1; //输入月份有误后的终止条件;
}
//调用函数文件名字随便 如main.c
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include"my_head.h"
#include"my_head.c"
int main(void)
{
int val;
int day, year;
char month[LEN];
printf("Please enter day, month and year (q to quit): ");
while (scanf("%d %11s %d", &day, month, &year) == 3)
{
is_leap_year(year);
val = days_result(month, day);
if (val > 0)
{
printf("There are %d days from the beginning of %d to %s %d\n", val, year, month, day);
}
else
{
printf("You entered invalid datas!\n");
}
months[1].days = 28; //2月份天数还原为非闰年时的天数;
printf("You can enter day, month and year again (q to quit): ");
}
printf("Done.\n");
return 0;
}