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

标签 c variables structure

正在上 C 语言类(class)的新编码员。我正在尝试编写一个程序来计算给定日期的数字,然后使用该数字来确定该日期是星期几。程序未完成,因为我无法将存储在结构(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*/

最佳答案

这是你的程序的工作版本,带有额外的注释来解释。

#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*/

关于C - 将结构变量从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58120763/

相关文章:

c - 关于结构中变量的内存分配的问题(在 C 中)

c++ - 如何从 C++ 中 vector 的最后一个元素中获取字段?

c - 试图理解这段c代码但就是无法理解

c - 带有 SEEK_END 和正偏移量的 fseek 的行为?

c - 哪个 Linux 命令确定是否正在使用消息队列或共享内存?

javascript - 如何从 Tumblr 主题选项的输出创建全局变量?

javascript - angular2 - 使变量更具可变性

c - gtk_label_set_text() 段错误

c - C 中的按位运算 - AnyOddBit

arrays - Swift 中的数组返回错误