// Encoding: UTF-8 #include "calendar.h" #include #include #include /*以下为"calendar.h规定之量*/ double day = 86400; double delta = 1e-11; /*计算需要用的类的实例化*/ Julian julian; parameter p; void radToDMS(const char *message, int lowerBound, int upperBound, double radians) { // Convert radians to degrees double degrees = radians * 180.0 / M_PI; while (degrees < lowerBound) degrees += (upperBound - lowerBound); while (degrees >= upperBound) degrees -= (upperBound - lowerBound); // Calculate degrees, minutes, and seconds int deg = static_cast(degrees); double min_sec = (degrees - deg) * 60.0; int min = static_cast(min_sec); double sec = (min_sec - min) * 60.0; // Output the result printf("%s: %d°%d'%06.3f''\n", message, deg, min, sec); } int main(int argc, char *argv[]) { Date date; if (argc != 2) { printf("Input the time you want to calculate in " "format: "); scanf("%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon, &date.tm_mday, &date.tm_hour, &date.tm_min, &date.tm_sec); } else { sscanf(argv[1], "%d-%d-%d,%d:%d:%d", &date.tm_year, &date.tm_mon, &date.tm_mday, &date.tm_hour, &date.tm_min, &date.tm_sec); } date.tm_year -= 1900; date.tm_mon -= 1; date.tm_isdst = -1; time_t time = mktime(&date); double t = julian.getJulianKiloYear(time); pair sun = p.sun_longitude(t); radToDMS("Sun longitude: ", 0, 360, sun.first); radToDMS("Sun latitude: ", -90, 90, sun.second); double alpha, delta; // 太阳赤经赤纬 double lambda = sun.first, beta = sun.second; // 太阳黄经黄纬 // 黄赤交角epsilon,23°26'21.448''转换为弧度 double epsilon = (23.0 + 26.0 / 60.0 + 21.448 / 3600.0) * M_PI / 180.0; delta = asin(sin(epsilon) * sin(lambda) * cos(beta) + cos(epsilon) * sin(beta)); alpha = atan2( (cos(epsilon) * sin(lambda) * cos(beta) - sin(epsilon) * sin(beta)), (cos(epsilon) * cos(beta))); radToDMS("Sun right ascension: ", 0, 360, alpha); radToDMS("Sun declination: ", -90, 90, delta); // 时角 double H = p.getHourAngle(t, LONGITUDE, LATITUDE, alpha); double A, h; //方位角和高度角 h = asin(sin(LATITUDE) * sin(delta) + cos(LATITUDE) * cos(delta) * cos(H)); A = atan2(-cos(delta) * sin(H), cos(LATITUDE) * sin(delta) - sin(LATITUDE) * cos(delta) * cos(H)); A += M_PI; // 转换为角度制输出 radToDMS("Hour angle: ", 0, 360, H); radToDMS("Azimuth: ", 0, 360, A); radToDMS("Elevation: ", 0, 90, h); return 0; }