catalogue
1, C language calculates the day of a year
2, Compiling C language program under Linux
1, C language calculates the day of a year
The general idea of this program is: day = (requested date month - 1) * 31 + date of requested date - redundant days (because the days of each month are different, the previous * 31 will make the result a few more days, so it needs to be subtracted)
There are three cases
Month = 1, 2, month < = 7, month > 7
Month = 1, 2 hours
In both normal and leap years, January and February do not need to subtract "extra days"
Month < = 7:00
"Extra days" = quotient of month month divided by 2 (leap year)
"Extra days" = month - (quotient of month divided by 2 + 1) (normal year)
Month > 7:00
"Extra days" = quotient of month divided by 2 (leap year)
"Extra days" = quotient of month divided by 2 + 1 (normal year)
#include <stdio.h> void main() { int mm0, mm1, mm2, mm3, mm4, mm5,mm6, mm7,mm8,mm9,day; printf("Enter date(8 Digits (example: 20210922):"); scanf("%d", &mm0); mm1 = mm0 / 10000;//particular year mm2 = mm0 / 100 % 100;//month mm3 = mm0 % 100;//day mm4 = mm1 % 4;//Judge whether the leap year is divided by 4 mm5 = mm2 - 1;//Month minus 1 mm6 = mm2 / 2;//Quotient of month divided by 2 mm7 = mm2 - mm6; mm8 = mm7 + 1; mm9 = mm2 / 2 + 1; if (mm4==0)//A leap year is divided by four { if (mm2 == 2) { day = mm5 * 31 + mm3; printf("%d The second day of the year%d day", mm1, day); } else if (mm2 <= 7) { if (mm2 == 1) { day = mm5 * 31 + mm3; printf("%d The second day of the year%d day", mm1, day); } else { day = mm5 * 31 + mm3 - mm7; printf("%d The second day of the year%d day", mm1, day); } } else { day = mm5 * 31 + mm3 - mm6; printf("%d The second day of the year%d day", mm1, day); } } else//Ordinary year { if (mm2 == 2) { day = mm5 * 31 + mm3; printf("%d The second day of the year%d day", mm1, day); } else if (mm2 <= 7) { if (mm2 == 1) { day = mm5 * 31 + mm3; printf("%d The second day of the year%d day", mm1, day); } else { day = mm5 * 31 + mm3 - mm8; printf("%d The second day of the year%d day", mm1, day); } } else { day = mm5 * 31 + mm3 - mm9; printf("%d The second day of the year%d day", mm1, day); } } }
In some scientific calculations, it is necessary to calculate the day of a year. For example, in the process of calculating MJD for astronomical observation, the following time format needs to be converted.
2018-11-26T21:39:19
Convert it to the following format for the next operation
2018-330-21:39:19
#include <stdio.h> void main() { int mm1, mm2, mm3, mm4, mm5, mm6, mm7, mm8, mm9, day, nm1, nm2, nm3;//nm1,nm2,nm3 are time printf("Enter date:"); scanf("%d-%d-%dT%d:%d:%d", &mm1, &mm2, &mm3, &nm1, &nm2, &nm3); mm4 = mm1 % 4;//Judge whether the leap year is divided by 4 mm5 = mm2 - 1;//Month minus one mm6 = mm2 / 2; mm7 = mm2 - mm6; mm8 = mm7 + 1; mm9 = mm2 / 2 + 1; if (mm4 == 0)//A leap year is divided by four { if (mm2 == 2) { day = mm5 * 31 + mm3; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } else if (mm2 <= 7) { if (mm2 == 1) { day = mm5 * 31 + mm3; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } else { day = mm5 * 31 + mm3 - mm7; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } } else { day = mm5 * 31 + mm3 - mm6; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } } else { if (mm2 == 2) { day = mm5 * 31 + mm3; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } else if (mm2 <= 7) { if (mm2 == 1) { day = mm5 * 31 + mm3; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } else; { day = mm5 * 31 + mm3 - mm8; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } } else { day = mm5 * 31 + mm3 - mm9; printf("%d-%d-%d:%d:%d", mm1, day, nm1, nm2, nm3); } } }
2, Compiling under Linux
Many astronomical data processing tools need to run under Linux, so it is necessary to compile C language programs in Linux. (gcc command is required for this procedure)
1. Create a file named. c (take vim as an example)
vim test.c
2. Paste the code of C language program into test.c file and save it
3. Compilation
gcc day.c -o day
Finally, a file will be generated and try to run it
./day
This concludes the tutorial
Write at the end
This program comes from students who have just learned C language (only one class, so they can only use simple sentences if, / laugh and cry) and is the first blog post. I will constantly learn to update the program from time to time to make it the simplest. If you have any questions, you can comment or send a private letter. I hope you can give suggestions and hope to communicate with you~