写了一个程序,可以编译,但无法运行,求指教,谢谢,急
一个程序,从年月日得出一年第几天,在用别的方程换算回年月日,输出到txt#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMS 100
int calDayofyear (int month, int year, int day,int *daysofmonthArray);
bool isLeapyear (int year);
int getData (FILE *spIn,int *daysofmonthArray);
int writeData (FILE *spOut, int *daysArray,int *yearsArray, int *dayofmonthArray,int numElems);
void caclCalendar(int *daysArray, int *yearsArray, int *dayofmonthArray,int k);
int main (void)
{
int daysofmonthArray[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int numElems;
int daysArray[MAX_ELEMS];
int yearsArray[MAX_ELEMS];
FILE *spOut;
FILE *spIn;
getData (spIn, daysofmonthArray);
writeData ( spOut, daysArray, yearsArray, daysofmonthArray, numElems);
system("pause");
return 0;
}
int getData (FILE *spIn,int *daysofmonthArray)
{
int ioResult,month,day,year,numElems;
int daysArray[MAX_ELEMS];
int yearsArray[MAX_ELEMS];
if(!(spIn = fopen("Lab1Input.txt","r"))){
printf("\aError opening input file\n");
return 0;
}
for (int i=0; i<MAX_ELEMS; ++i)
{
ioResult=fscanf(spIn, "%d%d%d", month, day, year);
if (ioResult==EOF)
return 0;
else
{if((month<=12 || month>=1 )&& (year<=9999 || year>=1000))
yearsArray[i]=year;
calDayofyear ( month, year, day, daysofmonthArray);
daysArray[i]=day;
}
return numElems=i++;
if(fclose(spIn)==EOF){
printf("\aError closing input file\n");
exit (101);
}
}}
int calDayofyear (int month, int year, int day,int *daysofmonthArray)
{
int daysArray=0;
if((month<=12 || month>=1 )&& (year<=9999 || year>=1000))
{
if(daysofmonthArray[month]<day||day<=0)
return 0;
else {
for(int i=1;i<month;i++)
{
daysArray=daysArray+daysofmonthArray[i];
}
daysArray=daysArray+day;
if (isLeapyear(year)==true)
return daysArray++;
else
return daysArray;
}}
else
return -1;
}
bool isLeapyear (int year)
{
if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
return true;
else
return false;
}
int writeData (FILE *spOut, int *daysArray,int *yearsArray, int *dayofmonthArray, int numElems)
{
int *pday, *pmonth;
if(!(spOut = fopen("Lab1Output.txt","r"))){
printf("\aError opening output file\n");
return 0;
}
fprintf(spOut, "Day of Year Calendar Date");
for (int i=0; i<numElems; ++i){
fprintf(spOut," %d", daysArray[i]);
caclCalendar(daysArray,yearsArray,dayofmonthArray,i);
fprintf(spOut," %d/%d/%d", *pday, *pmonth, yearsArray[i]);
}
if(fclose(spOut)==EOF){
printf("\aError closing input file\n");
exit (201);
}
}
void caclCalendar(int *daysArray, int *yearsArray, int *dayofmonthArray,int k)
{
int i=0;
int *pday, *pmonth;
if (isLeapyear (yearsArray[k])==true)
dayofmonthArray[2]=29;
do{
*pday = daysArray[k]-dayofmonthArray[i];
i++;}
while(daysArray[k]-dayofmonthArray[i]>0);
*pmonth=i;
return;
}