C - передать структурную переменную из одной функции в другую. - PullRequest
1 голос
/ 26 сентября 2019

Новый кодер, посещающий класс на C. Я пытаюсь написать программу, которая вычисляет число для данной даты, а затем использует число, чтобы определить, в какой день недели выпала дата.Программа не завершена, потому что я не могу передать пользовательский ввод, хранящийся в структуре (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*/

1 Ответ

1 голос
/ 26 сентября 2019

Вот рабочая версия вашей программы с дополнительными комментариями для объяснения.

#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*/
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...