quiettttt 2022-08-25 16:51 采纳率: 97.9%
浏览 95
已结题

这条代码能改成多文件形式吗?


#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.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}
};

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; //输入月份有误后的终止条件;
}

这条代码能改成多文件形式吗?就是把分函数放在别的文件中。改的话怎么改?最好有演示

  • 写回答

3条回答 默认 最新

  • 快乐鹦鹉 2022-08-25 17:09
    关注

    8-33行放在一个头文件
    62-110行放到一个c文件,需要#include头文件
    main函数放到一个c文件,需要#include头文件

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月2日
  • 已采纳回答 8月25日
  • 创建了问题 8月25日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度