我如何包含来自不同文件的功能? C

时间:2017-02-26 23:12:48

标签: c

我有问题。我有一个练习......有原型和函数,一切正常。但是当我想把原型和函数放在不同的源文件中时,一切都改变了......

为了更好地理解,这是我必须要做的细节...

创建一个文件mydates.h来保存函数的原型,以及一个源文件mydates.c(包括头文件mydates.h)来保存函数定义。然后从练习8.3中更改程序,以使用在mydates.h中声明并在mydates.c中定义的函数。

正如我前面所说。 8.3工作(函数,原型和main(void)在一个文件中)但是当我划分它们时,它们不再工作了...这里是我的文件..

9.3mydates.h

#ifndef _SET_H_
#define _SET_H_


/*PROTOTYPES*/
int promptYear(void); /* asks for a year and returns the year number read frokm the standard input */
int leapYear(int); /* return true (1) if the year passed in as a parameter is a leap year */
void computus(int); /* procedure for calculating the date of Easter in a given year */
int findWeekday(int,int,int); /* return the day of the week for a given date as a number between 0 (Sunday) and 6 (Saturday) */

#endif

9.3mydates.c

#include<stdio.h>
#include"9.3mydates.h"

int promptYear(void)
{
    printf("Give me a year bitch: ");
    scanf("%d", &year);
    fflush(stdin);  
    return year;    
}   



int leapYear(int year)
{
    int x=1; /* x=1 -> leap year */

        if(year%4==0)
        {
            if(year<1582)
                x=1;
            else
            {
                if(year%100 == 0)
                {
                    if(year%400==0)
                        x=1;

                    else
                        x=0;
                }
                else x=1;
            }       
        }
        else x=0;
        return x;
}


void computus(year)
{
        int a = year%19;
        int b = year/100;
        int c = year%100;
        int d = b/4;
        int e = b%4;
        int f = (b+8)/25;
        int g = (b-f+1)/3;
        int h = (19*a +b-d-g+15)%30;
        int i = c/4;
        int k = c%4;
        int L = (32+2*e+2*i-h-k)%7;
        int m = (a=11*h+22L)/451;

        month = (h+L-7*m+114)/31;
        day = ((h+L-7*m+114)%31)+1;

        printf("The date for Easter in %4d is %02d/%02d/%04d.\n",year,day,month,year);
}

int findWeekday(int day,int month, int year)
{
   int weekday=0; /* variable for storing the weekday */
   year=year+month;
   year=year+day;
   weekday=year%7;
   return weekday;
}

9.3.c

#include<stdio.h>
#include"9.3mydates.c"
#include"9.3mydates.h"

int cDay=0,cMonth=0; /* global variable to hold the result for the Easter day calculation */
int month,day;
int main(void)
{
   int year=0;

   year=promptYear();

   computus(year);

   printf("Easter day %04d, the %02d/%02d/%02d is a ",year,cDay,cMonth,year);
   switch(findWeekday(cDay,cMonth,year))
   {
     case 0: printf("Sunday");
             break;
     case 1: printf("Monday");
             break;
     case 2: printf("Tuesday");
             break;
     case 3: printf("Wednesday");
             break;
     case 4: printf("Thursday");
             break;
     case 5: printf("Friday");
             break;
     case 6: printf("Saturday");
             break;
  }
  printf(".\n");

  return 0;
}

2 个答案:

答案 0 :(得分:2)

你不想包含93mydates.c,你想把它编译为一个模块,所以摆脱这一行:

#include"9.3mydates.c"

然后将9.3mydates.c添加到编译器命令行或项目中。

答案 1 :(得分:2)

找不到

year,因为它未定义。 : - )

int promptYear(void)
{
    int year; /* you have to define the variable! */

    printf("Give me a year buddy: ");
    scanf("%d", &year);
    fflush(stdin);  
    return year;    
}   

顺便说一下:绝不能出现在生产代码中!这是家庭作业吗?