Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-将结构变量从一个函数传递到另一个函数_C_Variables_Structure - Fatal编程技术网

C-将结构变量从一个函数传递到另一个函数

C-将结构变量从一个函数传递到另一个函数,c,variables,structure,C,Variables,Structure,我正在尝试编写一个程序,为给定的日期计算一个数字,然后用这个数字来确定该日期是星期几。程序未完成,因为我无法将存储在结构(struct date udate)中的用户输入从函数get_date传递到函数calc_date_number。任何帮助都将不胜感激 #include <stdio.h> /*Define structure ------------------*/ struct date { int month; int day; i

我正在尝试编写一个程序,为给定的日期计算一个数字,然后用这个数字来确定该日期是星期几。程序未完成,因为我无法将存储在结构(struct date udate)中的用户输入从函数get_date传递到函数calc_date_number。任何帮助都将不胜感激

#include <stdio.h>

    /*Define structure
    ------------------*/
struct date
{
    int month;
    int day;
    int year;
};


    /*Declare function prototypes
    -----------------------------*/
struct date get_date (struct date);
void calc_date_number (struct date);


void main (void)
{
    struct date   udate, calc_date;
    printf("Welcome to the Date to Day-of-the-Week program.\n\nThe program will give the day of the  
           for any date from 1/1/1900.\n\n");

    get_date (udate);
    calc_date_number (calc_date);
}



    /*Define functions get_date
    ----------------------------*/
struct date get_date (struct date udate)
{
    do
    {
        printf ("Enter the date (mm/dd/yyyy): ");
        scanf ("%d/%d/%d", &udate.month, &udate.day, &udate.year);
        if (udate.month < 1 || udate.month > 12)
            printf ("Invalid month. Please re-enter date.\n\n");
        else if (udate.day <1 || udate.day > 31)
            printf ("Invalid day. Please re-enter date.\n\n");
        else if (udate.year < 1900)
            printf ("Invalid year. Please re-enter date.\n\n");
        else if (udate.month ==2 && udate.day == 29 && (udate.year !=0 && (udate.year == 0 || 
                 udate.year % 400 != 0)))
            printf ("Invalid date. Not a leap year. Please re-enter date.\n\n");

    }while (udate.month < 1 || udate.month > 12 || udate.day < 1 || udate.day > 31 || udate.year < 
            1900);

    return udate;

} /*End get_date*/

    /*Define function calc_date_number
    ----------------------------------*/
void calc_date_number (struct date calc_date)
{

    printf("calc_date is %i   %i   %i\n\n", calc_date.month, calc_date.day, calc_date.year);
    long int n;

    if (calc_date.month <= 2)
    {
        calc_date.year = calc_date.year - 1;
        calc_date.month = calc_date.month + 13;
    }
    else
    {
        calc_date.month = calc_date.month + 1;
    }

    n = 1461 * calc_date.year / 4 + 153 * calc_date.month / 5 + calc_date.day;
}/*End function calc_date_number*/
#包括
/*定义结构
------------------*/
结构日期
{
整月;
国际日;
国际年;
};
/*声明函数原型
-----------------------------*/
结构日期获取_日期(结构日期);
作废计算日期编号(结构日期);
真空总管(真空)
{
结构日期udate、计算日期;
printf(“欢迎使用Date to Day of the Week”计划。\n\n该计划将给出日期
1900年1月1日起的任何日期。\n\n“;
获取日期(udate);
计算日期编号(计算日期);
}
/*定义函数获取日期
----------------------------*/
结构日期获取日期(结构日期udate)
{
做
{
printf(“输入日期(mm/dd/yyyy):”;
scanf(“%d/%d/%d”,&udate.month,&udate.day,&udate.year);
如果(udate.month<1 | | udate.month>12)
printf(“无效的月份。请重新输入日期。\n\n”);
否则,如果(截止日期31日)
printf(“无效的日期。请重新输入日期。\n\n”);
否则如果(udate.year<1900)
printf(“无效年份。请重新输入日期。\n\n”);
如果(udate.month==2&&udate.day==29&&udate.year!=0&&udate.year==0 |
udate.year%400!=0)))
printf(“无效日期。不是闰年。请重新输入日期。\n\n”);
}而(udate.month<1|udate.month>12|udate.day<1|udate.day>31|udate.year<
1900);
返回udate;
}/*结束日期*/
/*定义函数计算日期编号
----------------------------------*/
作废计算日期编号(结构日期计算日期)
{
printf(“计算日期为%i%i%i\n\n”,计算日期.month,计算日期.day,计算日期.year);
长整数n;

如果(calc_date.month,这里是您的程序的工作版本,需要解释额外的注释

#include <stdio.h>

/*Define structure
 *     ------------------*/
struct date
{
    int month;
    int day;
    int year;
};


/*Declare function prototypes
 *     -----------------------------*/
struct date get_date (struct date);
long int calc_date_number (struct date); /* now return the number */ 

/* use int instead of void */
int main (void)
{
    struct date   udate, calc_date;
    printf("Welcome to the Date to Day-of-the-Week program.\n\nThe program will give the day of the"
    "for any date from 1/1/1900.\n\n");

     calc_date = get_date (udate); /* store the result in calc_date */
     long int n = calc_date_number (calc_date); /* store the result in n */
     printf("calculated date number : %ld\n", n); /* display the value just calculated */
     return 0; /* return code of the program */:
}



/*Define functions get_date
 *     ----------------------------*/
struct date get_date (struct date udate)
{
    do
    {
        printf ("Enter the date (mm/dd/yyyy): ");
        scanf ("%d/%d/%d", &udate.month, &udate.day, &udate.year);
        if (udate.month < 1 || udate.month > 12)
            printf ("Invalid month. Please re-enter date.\n\n");
        else if (udate.day <1 || udate.day > 31)
            printf ("Invalid day. Please re-enter date.\n\n");
        else if (udate.year < 1900)
            printf ("Invalid year. Please re-enter date.\n\n");
        else if (udate.month ==2 && udate.day == 29 && (udate.year !=0 && (udate.year == 0 || 
            udate.year % 400 != 0)))
            printf ("Invalid date. Not a leap year. Please re-enter date.\n\n");

    }while (udate.month < 1 || udate.month > 12 || udate.day < 1 || udate.day > 31 || udate.year < 
    1900);

    return udate;

} /*End get_date*/

/*Define function calc_date_number
 *     ----------------------------------*/
long int calc_date_number (struct date calc_date)
{

    printf("calc_date is %i   %i   %i\n\n", calc_date.month, calc_date.day, calc_date.year);
    long int n;

    if (calc_date.month <= 2)
    {
        calc_date.year = calc_date.year - 1;
        calc_date.month = calc_date.month + 13;
    }
    else
    {
        calc_date.month = calc_date.month + 1;
    }

    n = 1461 * calc_date.year / 4 + 153 * calc_date.month / 5 + calc_date.day;
    return n; 
}/*End function calc_date_number*/
#包括
/*定义结构
*     ------------------*/
结构日期
{
整月;
国际日;
国际年;
};
/*声明函数原型
*     -----------------------------*/
结构日期获取_日期(结构日期);
long int calc_date_number(结构日期);/*现在返回数字*/
/*使用int而不是void*/
内部主(空)
{
结构日期udate、计算日期;
printf(“欢迎使用Date to Day of the Week计划。\n\n该计划将给出星期几”
“1900年1月1日起的任何日期。\n\n”);
计算日期=获取日期(udate);/*将结果存储在计算日期中*/
long int n=calc_date_number(calc_date);/*将结果存储在n中*/
printf(“计算日期编号:%ld\n”,n);/*显示刚刚计算的值*/
返回0;/*程序的返回代码*/:
}
/*定义函数获取日期
*     ----------------------------*/
结构日期获取日期(结构日期udate)
{
做
{
printf(“输入日期(mm/dd/yyyy):”;
scanf(“%d/%d/%d”,&udate.month,&udate.day,&udate.year);
如果(udate.month<1 | | udate.month>12)
printf(“无效的月份。请重新输入日期。\n\n”);
否则,如果(截止日期31日)
printf(“无效的日期。请重新输入日期。\n\n”);
否则如果(udate.year<1900)
printf(“无效年份。请重新输入日期。\n\n”);
如果(udate.month==2&&udate.day==29&&udate.year!=0&&udate.year==0 |
udate.year%400!=0)))
printf(“无效日期。不是闰年。请重新输入日期。\n\n”);
}而(udate.month<1|udate.month>12|udate.day<1|udate.day>31|udate.year<
1900);
返回udate;
}/*结束日期*/
/*定义函数计算日期编号
*     ----------------------------------*/
长整数计算日期编号(结构日期计算日期)
{
printf(“计算日期为%i%i%i\n\n”,计算日期.month,计算日期.day,计算日期.year);
长整数n;

如果(calc_date.month在calc_date_number中,您计算n,但不进行任何处理。函数没有返回正确的结果,因此我停止编写程序,直到我可以修复它。感谢您花时间提供帮助。因此,main中添加的行对于在函数之间传递信息是必要的,您已更正了一个原型以重新执行现在我可以编写完成程序所需的最后几个函数了。