简体   繁体   English

我的 C 语法有问题吗?

[英]Is there something wrong with my C syntax?

Just before I start, I'd like to say that I'm new here, and I'm also 3 weeks into C programming, so I'm not as experienced.在开始之前,我想说我是新来的,而且我也有 3 周的 C 编程经验,所以我没有那么有经验。

This is a code to determine the day of the week using the doomsday method.这是使用世界末日方法确定星期几的代码。 Please note that the header and the main function is the template and is as it should be.请注意,标题和主要功能是模板,它应该是。 Everything else underneath the main function and the corresponding declarations at the top is my code. main 函数下面的所有其他内容以及顶部的相应声明都是我的代码。

    /*
*  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;    
}

There's a mysterious error in my code which I cannot pin-point, but I believe it may lie within the variables that I have written or with the if-statements.我的代码中有一个我无法指出的神秘错误,但我相信它可能存在于我编写的变量或 if 语句中。 All calculations seem to be correct, however, the code fails whenever the doomsday variable (in main function) is not equal to the corresponding answer.所有计算似乎都是正确的,但是,只要世界末日变量(在主函数中)不等于相应的答案,代码就会失败。 I've played around with my variables and it seems to me that their values aren't being changed as they should.我玩弄了我的变量,在我看来,它们的值没有像他们应该的那样改变。 Any tips?有小费吗?

EDIT: Updated code.编辑:更新的代码。 Still fails at section 2 of asserts in main function.在 main 函数中断言的第 2 部分仍然失败。 gcc accepts the code, debuggers return nothing. gcc接受代码,调试器不返回任何内容。

At least this section will cause trouble;至少这部分会引起麻烦;

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;
  } 
...

Several comparisons are actually assignments, for example;例如,几个比较实际上是赋值;

if (leapYear = TRUE) {

should be应该

if (leapYear == TRUE) {

EDIT: You also seem to have some logic problems, in your method dayOfWeek you're using the global variable doomsdayDate without ever setting it.编辑:您似乎也有一些逻辑问题,在您的方法dayOfWeek您使用了全局变量doomsdayDate而没有设置它。 I assume you're missing a call to doomsDate somewhere.我假设您在某处错过了对doomsDate的调用。 Also, had the variable been local, the compiler would have told you it was never initialised.此外,如果变量是本地的,编译器会告诉你它从未被初始化。

if ((numDays = 0))

should be:应该:

if ((numDays == 0))

as it is requesting a condition, not setting the variable to 0.因为它正在请求一个条件,而不是将变量设置为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM