C语言程序中函数的问题

C语言程序中函数的问题,c,function,parameter-passing,C,Function,Parameter Passing,我的程序功能有一个大问题。我知道这与我如何制作函数有关,但我不确定问题出在哪里。请记住,这是学校的作业,显然我缺乏一些理解 #include <stdio.h> /* Define structure */ struct date { int day, month, year; }; /* Function prototype */ int calc_date_number(struct date); /* Begin Main */ int main() {

我的程序功能有一个大问题。我知道这与我如何制作函数有关,但我不确定问题出在哪里。请记住,这是学校的作业,显然我缺乏一些理解

#include <stdio.h>

/* Define structure */

struct date 
{
    int day, month, year;
};

/* Function prototype */

int calc_date_number(struct date);

/* Begin Main */

 int main()
{
    /* Declare variable */

    struct date a;

    int valid=0, c;

    char *days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    /* Issue greeting */

    printf("Welcome to the Date to Day-of-Week program. \n\n");
    printf("The program will give the day of the week for any date from 1/1/1900\n\n");

    /*Prompt user for date */

    do 
    {
        printf("Enter date (mm/dd/yyyy): ");
        scanf("%i/%i/%i", &a.month, &a.day, &a.year);

        /* Validate inputted date and re-prompt user for valid date */

        if(a.year>=1900 && a.year<=9999)
        {
            if(a.month>=1 && a.month<=12)
            {
                if((a.day>=1 && a.day<=31) && (a.month==1 || a.month==3 || a.month==5 || a.month==7 || a.month==8 || a.month==10 || a.month==12))
                valid = 1;
                else if((a.day>=1 && a.day<=30) && (a.month==4 || a.month==6 || a.month==9 || a.month==11))
                valid = 1;
                else if((a.day>=1 && a.day<=28) && (a.month==2))
                valid = 1;
                else if(a.day==29 && a.month==2 && (a.year%400==0 ||(a.year%4==0 && a.year%100!=0)))
                valid = 1;
                else
                printf("Invalid day. Please re-enter date.\n");
            }
            else
            {
                printf("Invalid month. Please re-enter date.\n");
            }
         }
        else
        {
            printf("Invalid year. Please re-enter date. Year must be greater than 1900.\n");
        }
    }while(valid=0);

    /* Call function to calculate the number to represent the date */

    c = calc_date_number(a);

    /* Output date and day of the week */

    printf("%.2i/%.2i/%.2i falls on a %s\n\n", a.month, a.day, a.year, days[c]);
    printf("Thank you for using the Date to Day-of-Week program.");

    return 0;
}/* end of main */

/* Begin function calc_date_number */

  int calc_date_number(struct date d)
{
    int f, g, n;
    long long n1, n2;

    /* Establish the calculation for N1 1/1/1900 */

    n1 = (1461 * 1899) / 4 + (153 * 15) / 5 + 1;

    /* Establish the calculation for f and g for N2 calculation */

    if (d.month <= 2)
    {
        f = d.year- 1;
    }
    else
    {
        f = d.year;
    }

    if(d.month <= 2)
    {
        g = d.month + 13;
    }       
    else 
    {
        g = d.month + 1;
    }

    /* Calculate N2 for inputted date */

    n2 = (1461 * f) / 4 + (153 * g) / 5 + d.day;

    /* Calculate numeric day of the week */

    n =(n2 - 621049) % 7;

    return n;

} /* end of function */
#包括
/*定义结构*/
结构日期
{
整数日、月、年;
};
/*功能原型*/
内部计算日期编号(结构日期);
/*开始干管*/
int main()
{
/*声明变量*/
结构日期a;
int valid=0,c;
字符*天[7]={“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};
/*问题问候语*/
printf(“欢迎参加星期几计划。\n\n”);
printf(“程序将给出1900年1月1日起的任何日期的星期日\n\n”);
/*提示用户输入日期*/
做
{
printf(“输入日期(mm/dd/yyyy):”;
scanf(“%i/%i/%i”、&a.month、&a.day、&a.year);
/*验证输入的日期并重新提示用户输入有效日期*/

如果(a.year>=1900&&a.year=1&&a.month=1&&a.day=1&&a.day=1&&a.day,则会出现以下问题:

  • 函数不返回任何内容

  • 使用void作为主函数的返回值(可能,但不是 (建议)

  • 调用主函数(可能,但不建议)

  • printf中,“%.2i/%.2i/%.2i落在%s上\n\n”,a.月,a.日,a.日,
    天[c]);
    您将
    a.day
    而不是
    a.year
    作为第四个参数

虽然我没有验证你对日期的计算,但这个方法似乎有效

#include <stdio.h>

/* Define structure */

struct date 
{
    int day, month, year;
};

/* Function prototype */

int calc_date_number(struct date);

int calc_day_number(long long);


/* Begin Main */
int main()
{
    /* Declare variable */
    struct date a;
    long long c;
    int valid=0;

    char *days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    /* Issue greeting */
    printf("Welcome to the Date to Day-of-Week program. \n\n");
    printf("The program will give the day of the week for any date from 1/1/1900\n\n");

    /*Prompt user for date */
    do 
    {
        printf("Enter date (mm/dd/yyyy): ");
        scanf("%i/%i/%i", &a.month, &a.day, &a.year);

        /* Validate inputted date and re-prompt user for valid date */

        if(a.year>=1900 && a.year<=9999)
            if(a.month>=1 && a.month<=12)
                if((a.day>=1 && a.day<=31) && (a.month==1 || a.month==3 || a.month==5 || a.month==7 || a.month==8 || a.month==10 || a.month==12))
                    valid = 1;
                else if((a.day>=1 && a.day<=30) && (a.month==4 || a.month==6 || a.month==9 || a.month==11))
                    valid = 1;
                else if((a.day>=1 && a.day<=28) && (a.month==2))
                    valid = 1;
                else if(a.day==29 && a.month==2 && (a.year%400==0 ||(a.year%4==0 && a.year%100!=0)))
                    valid = 1;
                else
                    printf("Invalid day. Please re-enter date.\n");
            else
                printf("Invalid month. Please re-enter date.\n");
        else
            printf("Invalid year. Please re-enter date. Year must be greater than 1900.\n");
    } while(valid=0);

    /* Call function to calculate the number to represent the date */
    c = calc_date_number(a);

    /* Output date and day of the week */
    printf("%.2i/%.2i/%.2i falls on a %s\n\n", a.month, a.day, a.year, days[c]);
    printf("Thank you for using the Date to Day-of-Week program.");

    return 0;
}/* end of main */

/* Begin function calc_date_number */

int calc_date_number(struct date d)
{
    int f, g;
    long long n1, n2;

    /* Establish the calculation for N1 1/1/1900 */
    n1 = (1461 * 1899) / 4 + (153 * 15) / 5 + 1;

    /* Establish the calculation for f and g for N2 calculation */
    if (d.month <= 2)
        f = d.year- 1;
    else
        f = d.year;

    if(d.month <= 2)
        g = d.month + 13;
    else 
        g = d.month + 1;

    /* Calculate N2 for inputted date */
    n2 = (1461 * f) / 4 + (153 * g) / 5 + d.day;

    /* Call function to calculate the day number */
    return calc_day_number(n2); // you could just do return (n2 - 621049) % 7 and get rid of calc_day_number function

} /* end of function */

/* Begin function calc_day_number */
/* Calculate numeric day of the week */
int calc_day_number(long long n2)
{
    return (n2 - 621049) % 7;
} /* end of function */
#包括
/*定义结构*/
结构日期
{
整数日、月、年;
};
/*功能原型*/
内部计算日期编号(结构日期);
整数计算日数(长);
/*开始干管*/
int main()
{
/*声明变量*/
结构日期a;
长c;
int valid=0;
字符*天[7]={“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};
/*问题问候语*/
printf(“欢迎参加星期几计划。\n\n”);
printf(“程序将给出1900年1月1日起的任何日期的星期日\n\n”);
/*提示用户输入日期*/
做
{
printf(“输入日期(mm/dd/yyyy):”;
scanf(“%i/%i/%i”、&a.month、&a.day、&a.year);
/*验证输入的日期并重新提示用户输入有效日期*/

如果(a.year>=1900&&a.year=1&&a.month=1&&a.day=1&&a.day=1&&a.day,则会出现以下问题:

  • 函数不返回任何内容

  • 使用void作为主函数的返回值(可能,但不是 (建议)

  • 调用主函数(可能,但不建议)

  • printf中,“%.2i/%.2i/%.2i落在%s上\n\n”,a.月,a.日,a.日,
    天[c]);
    您将
    a.day
    而不是
    a.year
    作为第四个参数

虽然我没有验证你对日期的计算,但这个方法似乎有效

#include <stdio.h>

/* Define structure */

struct date 
{
    int day, month, year;
};

/* Function prototype */

int calc_date_number(struct date);

int calc_day_number(long long);


/* Begin Main */
int main()
{
    /* Declare variable */
    struct date a;
    long long c;
    int valid=0;

    char *days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    /* Issue greeting */
    printf("Welcome to the Date to Day-of-Week program. \n\n");
    printf("The program will give the day of the week for any date from 1/1/1900\n\n");

    /*Prompt user for date */
    do 
    {
        printf("Enter date (mm/dd/yyyy): ");
        scanf("%i/%i/%i", &a.month, &a.day, &a.year);

        /* Validate inputted date and re-prompt user for valid date */

        if(a.year>=1900 && a.year<=9999)
            if(a.month>=1 && a.month<=12)
                if((a.day>=1 && a.day<=31) && (a.month==1 || a.month==3 || a.month==5 || a.month==7 || a.month==8 || a.month==10 || a.month==12))
                    valid = 1;
                else if((a.day>=1 && a.day<=30) && (a.month==4 || a.month==6 || a.month==9 || a.month==11))
                    valid = 1;
                else if((a.day>=1 && a.day<=28) && (a.month==2))
                    valid = 1;
                else if(a.day==29 && a.month==2 && (a.year%400==0 ||(a.year%4==0 && a.year%100!=0)))
                    valid = 1;
                else
                    printf("Invalid day. Please re-enter date.\n");
            else
                printf("Invalid month. Please re-enter date.\n");
        else
            printf("Invalid year. Please re-enter date. Year must be greater than 1900.\n");
    } while(valid=0);

    /* Call function to calculate the number to represent the date */
    c = calc_date_number(a);

    /* Output date and day of the week */
    printf("%.2i/%.2i/%.2i falls on a %s\n\n", a.month, a.day, a.year, days[c]);
    printf("Thank you for using the Date to Day-of-Week program.");

    return 0;
}/* end of main */

/* Begin function calc_date_number */

int calc_date_number(struct date d)
{
    int f, g;
    long long n1, n2;

    /* Establish the calculation for N1 1/1/1900 */
    n1 = (1461 * 1899) / 4 + (153 * 15) / 5 + 1;

    /* Establish the calculation for f and g for N2 calculation */
    if (d.month <= 2)
        f = d.year- 1;
    else
        f = d.year;

    if(d.month <= 2)
        g = d.month + 13;
    else 
        g = d.month + 1;

    /* Calculate N2 for inputted date */
    n2 = (1461 * f) / 4 + (153 * g) / 5 + d.day;

    /* Call function to calculate the day number */
    return calc_day_number(n2); // you could just do return (n2 - 621049) % 7 and get rid of calc_day_number function

} /* end of function */

/* Begin function calc_day_number */
/* Calculate numeric day of the week */
int calc_day_number(long long n2)
{
    return (n2 - 621049) % 7;
} /* end of function */
#包括
/*定义结构*/
结构日期
{
整数日、月、年;
};
/*功能原型*/
内部计算日期编号(结构日期);
整数计算日数(长);
/*开始干管*/
int main()
{
/*声明变量*/
结构日期a;
长c;
int valid=0;
字符*天[7]={“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”};
/*问题问候语*/
printf(“欢迎参加星期几计划。\n\n”);
printf(“程序将给出1900年1月1日起的任何日期的星期日\n\n”);
/*提示用户输入日期*/
做
{
printf(“输入日期(mm/dd/yyyy):”;
scanf(“%i/%i/%i”、&a.month、&a.day、&a.year);
/*验证输入的日期并重新提示用户输入有效日期*/

如果(a.year>=1900&&a.year=1&&a.month=1&&a.day=1&&a.day=1&&a.day=1&&a.day什么是“问题”?是否出现生成错误?然后复制粘贴完整的生成输出到问题中,并在代码中出现错误的行上添加注释。或者如果出现意外结果,则显示实际和预期的输出(对于某些指定的输入)。也请阅读或刷新。您的函数声明为返回整数,但它们不包含
return
语句。请返回您的书/课堂讲稿,重新访问有关函数的部分。
do{…}while(valid=0);
循环不会重复,是吗?赋值还是比较!使用
void main(int c)
是错误的-
int main(void)
在这里是合适的。您需要在代码中定义
c
,然后将函数的结果分配给它。并且调用main以接收数字日*/main(c)
结尾很奇怪!你不是这样做的。你使用
返回c;
!你需要编辑你的问题并告诉我们:1)程序应该做什么;2)你有什么问题。总结一下:你似乎主要是在猜测C语言的工作原理。这不是学习任何语言、编程或书面/口语的好方法。投资几本入门书,在线阅读一些教程,参加一门课程,或者在线上几门付费课程。学习编程没有捷径或者一种语言,除了努力工作和学习。什么是“问题”?你有生成错误吗?然后复制粘贴完整的生成输出到问题中,并在代码中出现错误的行上添加注释。或者如果你得到意外的结果,则显示实际和预期的输出(对于某些指定的输入)。也请阅读或刷新。您的函数声明为返回整数,但它们不包含
return
语句。请返回您的书/课堂讲稿,重新访问有关函数的部分。
do{…}while(valid=0);
循环不会重复,是吗?赋值还是比较!使用
void main(int c)
完全错误-
int