在我开始之前,我想说我是新来的,而且我还有3周的C编程,所以我没有经验。
这是使用世界末日法确定星期几的代码。请注意,标题和主要功能是模板,应该是它。主函数下面的所有其他内容以及顶部的相应声明都是我的代码。
/*
* doomsday.c
* A program to test a function dayOfWeek which determines which
* day of the week a particular date falls on.
* (only for dates after the start of the Gregorian calendar).
*
* Program stub created by Richard Buckland on 17/03/14
* This program by YOUR-NAME-HERE
* Freely licensed under Creative Commons CC-BY-3.0
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define THURSDAY 0
#define FRIDAY 1
#define SATURDAY 2
#define SUNDAY 3
#define MONDAY 4
#define TUESDAY 5
#define WEDNESDAY 6
#define TRUE 1
#define FALSE 0
#define DAYS_PER_WEEK 7
int dayOfWeek (int doomsday, int leapYear, int month, int day);
int doomsDate (int month, int leapYear, int doomsday);
int numDaysLeft (int numDays, int doomsdayDate, int day);
int doomsdayDate;
int numDays;
int main (int argc, char *argv[]) {
assert (dayOfWeek (THURSDAY, FALSE, 4, 4) == THURSDAY);
assert (dayOfWeek (FRIDAY, FALSE, 6, 6) == FRIDAY);
assert (dayOfWeek (MONDAY, FALSE, 8, 8) == MONDAY);
assert (dayOfWeek (WEDNESDAY, FALSE, 10, 10) == WEDNESDAY);
assert (dayOfWeek (FRIDAY, FALSE, 12, 12) == FRIDAY);
assert (dayOfWeek (THURSDAY, FALSE, 9, 5) == THURSDAY);
assert (dayOfWeek (FRIDAY, FALSE, 5, 9) == FRIDAY);
assert (dayOfWeek (SUNDAY, FALSE, 7, 11) == SUNDAY);
assert (dayOfWeek (TUESDAY, FALSE, 11, 7) == TUESDAY);
assert (dayOfWeek (WEDNESDAY, FALSE, 3, 7) == WEDNESDAY);
assert (dayOfWeek (THURSDAY, FALSE, 4, 5) == FRIDAY);
assert (dayOfWeek (SATURDAY, FALSE, 6, 5) == FRIDAY);
assert (dayOfWeek (MONDAY, FALSE, 8, 9) == TUESDAY);
assert (dayOfWeek (WEDNESDAY, FALSE, 10, 9) == TUESDAY);
assert (dayOfWeek (FRIDAY, FALSE, 12, 20) == SATURDAY);
assert (dayOfWeek (THURSDAY, FALSE, 9, 9) == MONDAY);
assert (dayOfWeek (FRIDAY, FALSE, 5, 5) == MONDAY);
assert (dayOfWeek (SUNDAY, FALSE, 7, 7) == WEDNESDAY);
assert (dayOfWeek (TUESDAY, FALSE, 11, 11) == SATURDAY);
assert (dayOfWeek (THURSDAY, FALSE, 3, 30) == SATURDAY);
assert (dayOfWeek (TUESDAY, FALSE, 2, 28) == TUESDAY);
assert (dayOfWeek (TUESDAY, FALSE, 2, 27) == MONDAY);
assert (dayOfWeek (THURSDAY, FALSE, 1, 3) == THURSDAY);
printf ("all tests passed - You are awesome!\n");
return EXIT_SUCCESS;
}
// given the doomsday for a year, and whether or not it is a
// leap year, this function return the day of the week for any
// given month and day in the year.
int dayOfWeek (int doomsday, int leapYear, int month, int day) {
int dayOfWeek;
// Call function
doomsDate (month, leapYear, doomsdayDate);
// Making numDays always positive
if (day < doomsdayDate) {
doomsdayDate = -(doomsdayDate);
day = -(day);
}
// Calculate how many days between given day and closest doomsday
numDays = (day - doomsdayDate);
// Conditions needed to be satisfied
if ((numDays = 0)) {
numDays = 0;
} else if (numDays >= DAYS_PER_WEEK) {
numDays = numDays % DAYS_PER_WEEK;
} else if (numDays > 0 && numDays < DAYS_PER_WEEK) {
numDays = numDays;
}
// Final calculation for the day of the week
dayOfWeek = (numDays + doomsday);
return (dayOfWeek);
}
//Function to calculate nearest doomsday date
int doomsDate (int month, int leapYear, int doomsdayDate) {
if (month == 1) {
if (leapYear == TRUE){
doomsdayDate = 11;
} else if (leapYear == FALSE){
doomsdayDate = 10;
}
} else if (month == 2) {
if (leapYear == TRUE) {
doomsdayDate = 29;
} else if (leapYear == FALSE) {
doomsdayDate = 28;
}
} else if (month == 3){
doomsdayDate = 0;
} else if (month == 4) {
doomsdayDate = 4;
} else if (month == 6) {
doomsdayDate = 6;
} else if (month == 8) {
doomsdayDate = 8;
} else if (month == 10) {
doomsdayDate = 10;
} else if (month == 12) {
doomsdayDate = 12;
} else if (month == 7) {
doomsdayDate = 11;
} else if (month == 11) {
doomsdayDate = 7;
} else if (month == 9) {
doomsdayDate = 5;
} else if (month == 5) {
doomsdayDate = 9;
}
return doomsdayDate;
}
我的代码中有一个神秘的错误,我无法指出,但我相信它可能存在于我编写的变量或if语句中。所有计算似乎都是正确的,但是,只要世界末日变量(在主函数中)不等于相应的答案,代码就会失败。我已经玩弄了变量,在我看来,他们的价值观并没有像他们应该的那样被改变。有什么提示吗?
编辑:更新了代码。在main函数中断言的第2部分仍然失败。 gcc
接受代码,调试器不返回任何内容。
答案 0 :(得分:2)
至少这部分会造成麻烦;
if (month == 1) {
if (leapYear = TRUE){
doomsdayDate = 11;
} else if (leapYear = FALSE){
doomsdayDate = 10;
}
} else if (month == 2) {
if (leapYear = TRUE) {
doomsdayDate = 29;
} else if (leapYear = FALSE) {
doomsdayDate = 28;
}
...
例如,几个比较实际上是分配;
if (leapYear = TRUE) {
应该是
if (leapYear == TRUE) {
编辑:您似乎也遇到了一些逻辑问题,在您的方法dayOfWeek
中,您使用全局变量doomsdayDate
而没有设置它。我假设您在某个地方错过了对doomsDate
的调用。此外,如果变量是本地的,编译器会告诉你它从未初始化。
答案 1 :(得分:-1)
if ((numDays = 0))
应该是:
if ((numDays == 0))
因为它正在请求条件,而不是将变量设置为0。