【问题标题】:Issue with functions in program for C [closed]C程序中的函数问题[关闭]
【发布时间】:2020-02-16 06:58:33
【问题描述】:

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

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

我的输出是 欢迎参加 Date to Day-of-Week 计划。

程序将给出从 1900 年 1 月 1 日起的任何日期的星期几

输入日期(月/日/年):09/24/1994 无效年份。请重新输入日期。年份必须大于 1900。 00/1961062381/6356776 是星期一

感谢您使用 Date to Day-of-Week 程序。

【问题讨论】:

  • 什么“问题”?你有构建错误吗?然后将完整和完整的构建输出复制粘贴到问题中,并在代码中出现错误的行中添加注释。或者,如果您得到意想不到的结果,请显示实际和预期的输出(对于某些指定的输入)。也请阅读或刷新How to Askthis question checklist
  • 您的函数被声明为返回整数,但它们不包含return 语句。回到你的书/讲义,重新访问关于函数的部分。
  • do { … }while(valid=0); 循环不会重复,是吗?分配与比较!使用void main(int c) 是错误的——int main(void) 在这里是合适的。您需要在代码中定义c,然后将函数的结果分配给它。最后的/* Call to main to receive numeric day */ main(c); 很奇怪!这不是你的做法。你用return c;!
  • 您需要编辑您的问题并告诉我们:1)程序应该做什么,2)您有什么问题。
  • 总结一下:您似乎主要是在猜测事物以及 C 的工作原理。这不是学习任何语言、编程或书面/口语的好方法。投资一些初学者书籍,在线阅读一些教程,参加课程或一些在线付费课程。学习编程或语言没有捷径可走,除了努力学习。

标签: c function parameter-passing


【解决方案1】:

有一些问题

  • 函数不返回任何内容

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

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

  • printf("%.2i/%.2i/%.2i falls on a %s\n\n",a.month, a.day, a.day, days[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 */

【讨论】:

  • 我已根据您的修订更新了我的代码。我确实对其进行了调整,因为我想要一个普通整数而不是字符串数组的长整数。我的问题是验证和结果无法正常工作。最初,验证工作正常。
  • 你能举一个输入和输出不能正常工作的例子吗?
  • 我的帖子中有一个例子。所以我输入 09/24/1994,它说年份不正确。然后当它应该输出输入的日期时,它打印了 00/1961062381/6356776 和错误的日期。
  • 我的教授回复了我。问题是我的 scanf 和输入。我不能将 0 放在带有 %i 的数字之前,所以我将它们更改为 %d 并且它现在可以工作了。非常感谢您的帮助和宽容!
  • @ubutom 很高兴如果它有帮助。
猜你喜欢
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多