【发布时间】: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 Ask和this 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