├── mylib ├── test │ ├── test_str.cpp │ ├── test_tools.cpp │ ├── lib_test.cpp │ ├── test_static_array.cpp │ └── test_itoa.cpp ├── lat_lon_data.h ├── math_patch.h ├── mystl │ ├── algorithm.h │ ├── exceptdef.h │ ├── type_traits.h │ ├── construct.h │ ├── static_array.h │ ├── allocator.h │ ├── stack.h │ ├── numeric.h │ ├── memory.h │ ├── my_string.h │ ├── set_algo.h │ ├── functional.h │ ├── heap_algo.h │ ├── uninitialized.h │ ├── util.h │ ├── queue.h │ ├── itoa.h │ └── iterator.h ├── tool.h ├── conio.h └── tool.cpp ├── autobuild.sh ├── rebuild.sh ├── TODO.md ├── .github └── workflows │ └── c-cpp.yml ├── lunar ├── lunar.h ├── lunar_ssq.h ├── lunar_ob.h └── lunar.cpp ├── .gitignore ├── README.md ├── eph ├── eph_yspl.h ├── eph_show.h ├── eph_szj.h ├── .c4droid ├── eph_msc.h ├── eph.h ├── eph_rspl.h ├── eph0.h ├── eph_rsgs.h ├── eph_yspl.cpp ├── eph_szj.cpp ├── eph_msc.cpp ├── eph_rspl.cpp └── eph_show.cpp ├── test ├── test.cpp ├── .c4droid └── test1.cpp ├── Makefile ├── CMakeLists.txt ├── LICENSE └── .c4droid /mylib/test/test_str.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /autobuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | build_dir=build 4 | 5 | if [ ! -d "$build_dir" ]; then 6 | mkdir -p $build_dir 7 | fi 8 | cd build && cmake .. && make -j4 9 | -------------------------------------------------------------------------------- /rebuild.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | build_dir=build 3 | 4 | if [ ! -d "$build_dir" ]; then 5 | mkdir -p $build_dir 6 | else 7 | rm -rf ./$build_dir/* 8 | fi 9 | cd build && cmake .. && make -j4 10 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | 寿星天文历(V5.10) 2 | 3 | 核心功能 4 | 主要 5 | 月历✓,年历✓,日月食✓,地方食,星历✓,日食概略✓ 6 | 次要 7 | 天象✓ 恒星 气朔✓ 升降✓ 八字✓ 8 | 无关紧要 9 | 工具 常数 帮助 10 | 11 | API重构 12 | 核心模块,日历模块 13 | 14 | 展示 15 | 命令行: 16 | Demo展示✓ 交互式 17 | 图形化: 18 | SDL2 IMGUI QT 19 | -------------------------------------------------------------------------------- /mylib/lat_lon_data.h: -------------------------------------------------------------------------------- 1 | #ifndef LAT_LON_DATA_H 2 | #define LAT_LON_DATA_H 3 | 4 | struct JINGWEI 5 | { 6 | double J;//经度 7 | double W;//纬度 8 | char s[48];//省市 9 | char x[48];//区县 10 | }; 11 | 12 | extern JINGWEI jw; 13 | JINGWEI GeographicalPosition(void); 14 | #endif -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: make 17 | run: make 18 | -------------------------------------------------------------------------------- /mylib/math_patch.h: -------------------------------------------------------------------------------- 1 | // FIXED M_PI UNDEFINE 2 | 3 | #ifndef MATH_PATCH__H 4 | #define MATH_PATCH__H 5 | 6 | #ifndef _USE_MATH_DEFINES 7 | #define _USE_MATH_DEFINES 8 | #endif //_USE_MATH_DEFINES 9 | 10 | #include 11 | 12 | #ifndef M_PI 13 | #define M_PI 3.14159265358979323846 14 | #endif //M_PI 15 | 16 | #endif // MATH_PATCH__H -------------------------------------------------------------------------------- /mylib/mystl/algorithm.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_ALGORITHM_H_ 2 | #define MYTINYSTL_ALGORITHM_H_ 3 | 4 | // 这个头文件包含了 mystl 的所有算法,包括基本算法,数值算法,heap 算法,set 算法和其他算法 5 | 6 | #include "algobase.h" 7 | #include "algo.h" 8 | #include "set_algo.h" 9 | #include "heap_algo.h" 10 | #include "numeric.h" 11 | 12 | namespace mystl 13 | { 14 | 15 | } // namespace mystl 16 | 17 | #endif // !MYTINYSTL_ALGORITHM_H_ 18 | 19 | -------------------------------------------------------------------------------- /lunar/lunar.h: -------------------------------------------------------------------------------- 1 | #ifndef LUNAR_H 2 | #define LUNAR_H 3 | 4 | #include "../mylib/mystl/my_string.h" 5 | #include "../mylib/mystl/vector.h" 6 | 7 | #include "lunar_ob.h" 8 | 9 | struct OB_LUN 10 | { 11 | int w0; // 本月第一天的星期 12 | int y; // 公历年份 13 | int m; // 公历月分 14 | int d0; // 月首的J2000.0起算的儒略日数 15 | int dn; // 本月的天数 16 | mystl::string nianhao; // 年号纪年信息 17 | OB_DAY day[31]; 18 | }; 19 | 20 | void init_ob(); 21 | OB_LUN yueLiCalc(int By, int Bm); 22 | mystl::string nianLiSTR(int y); 23 | 24 | #endif -------------------------------------------------------------------------------- /mylib/test/test_tools.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../mystl/dtoa.h" 3 | #include "../tool.cpp" 4 | #include 5 | 6 | int main(){ 7 | Date d = {-5621,11,12,10,30,50.6987}; 8 | // std::cout<"< 5 | 6 | #include 7 | 8 | namespace mystl 9 | { 10 | 11 | #define MYSTL_DEBUG(expr) \ 12 | assert(expr) 13 | 14 | #define THROW_LENGTH_ERROR_IF(expr, what) \ 15 | if ((expr)) throw std::length_error(what) 16 | 17 | #define THROW_OUT_OF_RANGE_IF(expr, what) \ 18 | if ((expr)) throw std::out_of_range(what) 19 | 20 | #define THROW_RUNTIME_ERROR_IF(expr, what) \ 21 | if ((expr)) throw std::runtime_error(what) 22 | 23 | } // namepsace mystl 24 | 25 | #endif // !MYTINYSTL_EXCEPTDEF_H_ 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sxwnl-cpp 2 | 寿星天文历(万年历)c++版 3 | Sxwnl for cpp version 4 | # Build 5 | ## 1.Use cmake 6 | ```bash 7 | # build or update bin 8 | ./autobuild.sh 9 | 10 | # clean obj files and build 11 | ./rebuild.sh 12 | 13 | ``` 14 | ## 2.Use make 15 | ```bash 16 | # clean obj files 17 | make clean 18 | # for windows 19 | make cleanw 20 | 21 | ### Build test0 22 | make test0 23 | ### Build test1 24 | make test1 25 | ### build all 26 | make 27 | 28 | ``` 29 | 30 | # Run test 31 | ```bash 32 | # cmake build 33 | ./build/test0 34 | ./build/test1 35 | # make build 36 | ./test0 37 | ./test1 38 | ``` 39 | 40 | # Bugs 41 | TODO 42 | -------------------------------------------------------------------------------- /eph/eph_yspl.h: -------------------------------------------------------------------------------- 1 | #ifndef EPH_YSPL_H 2 | #define EPH_YSPL_H 3 | 4 | #include "../mylib/mystl/my_string.h" 5 | #include "../mylib/mystl/static_array.h" 6 | 7 | struct RE0 8 | { 9 | double e_mRad; 10 | double eShadow; 11 | double eShadow2; 12 | double x; 13 | double y; 14 | double mr; 15 | double er; 16 | double Er; 17 | double t; 18 | }; 19 | 20 | class YS_PL 21 | { //月食快速计算器 22 | public: 23 | static mystl::array7 lT; 24 | static mystl::string LX; 25 | static double sf; 26 | static void lecXY(double jd, RE0 &re); 27 | static double lineT(RE0 G,double v,double u, double r, bool n); 28 | static void lecMax(double jd); 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /test/test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 寿星天文历v5.05js版 C++翻译工程 3 | API测试 4 | 2018-8-28 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "../mylib/tool.h" 12 | #include "../lunar/lunar.h" 13 | #include "../eph/eph_show.h" 14 | #include "../eph/eph0.h" 15 | #include "../eph/eph.h" 16 | int main() 17 | { 18 | #if defined(__WIN32__) || defined(_MSC_VER) 19 | system("@chcp 65001"); 20 | #endif 21 | 22 | Date date = { 2008, 1, 1, 0, 0, 0}; 23 | double jd1 = toJD(date) - J2000; 24 | mystl::string ss = xingX(1, jd1, A2R(116.383333), A2R(39.900000)); 25 | std::cout< ym;//各月名称 22 | static int ZQ[25];//中气表 23 | static int HS[15];//合朔表 24 | static int dx[14];//各月大小 25 | static int pe[2]; //补算二气 26 | static void init(); 27 | static void calcY(double jd); 28 | }; 29 | 30 | #endif -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6.0) 2 | project(sxwnl-cpp) 3 | set(CMAKE_VERBOSE_MAKEFILE on) 4 | # 编译参数 5 | 6 | if (WIN32) 7 | set(CMAKE_CXX_STANDARD 11) 8 | add_compile_options("$<$:/utf-8>") 9 | add_compile_options("$<$:/utf-8>") 10 | add_compile_options(-DWIN32) 11 | else() 12 | # set(CMAKE_BUILD_TYPE "Release") 13 | set(ENV{CXXFLAGS} "$ENV{CXXFLAGS} -std=c++11 -w -s -fexceptions") 14 | set(CMAKE_CXX_FLAGS $ENV{CXXFLAGS}) 15 | message("CMAKE_CXX_FLAGS:" ${CMAKE_CXX_FLAGS}) 16 | endif() 17 | 18 | 19 | include_directories( 20 | ${PROJECT_SOURCE_DIR}/eph 21 | ${PROJECT_SOURCE_DIR}/lunar 22 | ${PROJECT_SOURCE_DIR}/mylib 23 | ) 24 | file(GLOB SRCS 25 | "${PROJECT_SOURCE_DIR}/eph/*" 26 | "${PROJECT_SOURCE_DIR}/lunar/*" 27 | "${PROJECT_SOURCE_DIR}/mylib/*" 28 | ) 29 | 30 | # 测试 31 | # add_executable(test0 test/test.cpp ${SRCS}) 32 | 33 | add_executable(test1 test/test1.cpp ${SRCS}) -------------------------------------------------------------------------------- /mylib/test/lib_test.cpp: -------------------------------------------------------------------------------- 1 | // #include "../mystl/my_string.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char *base91(int x, char *res) 9 | { 10 | static char ss[92] = "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~"; 11 | char res0[16] = {}; 12 | int k = 91; 13 | int i = 0; 14 | while (x >= k) 15 | { 16 | res0[i++] = ss[x % k]; 17 | x /= k; 18 | } 19 | res0[i] = ss[x]; 20 | int size = strlen(res0); 21 | for (size_t i = 0; i < size; i++) 22 | { 23 | res[i] = res0[size - i - 1]; 24 | } 25 | return res; 26 | } 27 | 28 | 29 | int main() 30 | { 31 | for (size_t i = 61000000; i < 61001000; i++) 32 | { 33 | char s[16] = {}; 34 | // std::cout<< 35 | base91(i, s); 36 | // <<"\n"; 37 | puts(s); 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /mylib/tool.h: -------------------------------------------------------------------------------- 1 | #ifndef TOOL_H 2 | #define TOOL_H 3 | 4 | #include 5 | #include "../mylib/mystl/my_string.h" 6 | #include "../mylib/math_patch.h" 7 | 8 | #define A2R(x) ((x) / 180 * M_PI) 9 | 10 | inline int int2(double v) {return (int)floor(v);}; 11 | 12 | 13 | struct Date 14 | {//儒略历结构,包含: 年 月 日 时 分 秒 (浮点型) 15 | int Y, M, D, h, m; 16 | double s; 17 | }; 18 | 19 | //mystl::vector split(const mystl::string& src, const mystl::string& separator); 20 | void string_replace( mystl::string &strBig, const mystl::string &strsrc, const mystl::string &strdst); 21 | mystl::string timeStr(double jd); 22 | mystl::string rad2strE(double d, bool tim, int ext); 23 | mystl::string rad2str(double d, bool tim); 24 | mystl::string rad2str2(double d); 25 | mystl::string m2fm(double v, int fx, int fs); 26 | double toJD(Date date); 27 | Date setFromJD(double jd); 28 | mystl::string DD2str(Date r); 29 | mystl::string JD2str(double jd); 30 | mystl::string fill_str(mystl::string s, int n, mystl::string c); 31 | 32 | #endif -------------------------------------------------------------------------------- /mylib/mystl/type_traits.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_TYPE_TRAITS_H_ 2 | #define MYTINYSTL_TYPE_TRAITS_H_ 3 | 4 | // 这个头文件用于提取类型信息 5 | 6 | // use standard header for type_traits 7 | #include 8 | 9 | namespace mystl 10 | { 11 | 12 | // helper struct 13 | 14 | template 15 | struct m_integral_constant 16 | { 17 | static constexpr T value = v; 18 | }; 19 | 20 | template 21 | using m_bool_constant = m_integral_constant; 22 | 23 | typedef m_bool_constant m_true_type; 24 | typedef m_bool_constant m_false_type; 25 | 26 | /*****************************************************************************************/ 27 | // type traits 28 | 29 | // is_pair 30 | 31 | // --- forward declaration begin 32 | template 33 | struct pair; 34 | // --- forward declaration end 35 | 36 | template 37 | struct is_pair : mystl::m_false_type {}; 38 | 39 | template 40 | struct is_pair> : mystl::m_true_type {}; 41 | 42 | } // namespace mystl 43 | 44 | #endif // !MYTINYSTL_TYPE_TRAITS_H_ 45 | 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 qianbaiducode 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /mylib/test/test_static_array.cpp: -------------------------------------------------------------------------------- 1 | #include "../mystl/static_array.h" 2 | #include "../mystl/my_string.h" 3 | 4 | 5 | using namespace mystl; 6 | array3 get(){ 7 | return {1.6,65.9,6.8}; 8 | } 9 | 10 | #include 11 | int main(){ 12 | array3 arr={1.1},arr2{1.9}; 13 | double a=9; 14 | arr2.fill(a); 15 | a=10; 16 | arr2[0]=66; 17 | for (auto i:arr2){ 18 | printf("arr2 -> %lf\n", i); 19 | } 20 | 21 | arr2.fill(999.999); 22 | arr2[0]=66; 23 | for (auto i:arr2){ 24 | printf("arr2 -> %lf\n", i); 25 | } 26 | arr2=get(); 27 | arr=arr2; 28 | arr[0]=1.23; 29 | for (auto i:arr){ 30 | printf("arr -> %lf\n", i); 31 | } 32 | 33 | for (auto i:arr2){ 34 | printf("arr2 -> %lf\n", i); 35 | } 36 | printf("arr -> %lf\n", arr.get(0)); 37 | 38 | static_array mc = 39 | {"食中心点", "本影北界", "本影南界", "半影北界", "半影南界"}; 40 | 41 | array4 aaa={13.664, -1.64331, 64.64615181, 1141.9}; 42 | for (auto i:aaa){ 43 | printf("mc -> %s|%s|\n", to_str_fmt(i, 2, 8, true).c_str(), to_str_fmt(i, 2, 8).c_str()); 44 | } 45 | for (auto i:aaa){ 46 | printf("mc -> %s|%s|\n", to_str(i, 2, 8, true).c_str(), to_str_fmt(i, 2, 8).c_str()); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /eph/eph_szj.h: -------------------------------------------------------------------------------- 1 | #include "../mylib/mystl/my_string.h" 2 | #include "../mylib/mystl/vector.h" 3 | 4 | struct SJ 5 | { 6 | double z; 7 | double x; 8 | double s; 9 | double j; 10 | double c; 11 | double h; 12 | double c2; 13 | double h2; 14 | double c3; 15 | double h3; 16 | double H0; 17 | double H; 18 | double H1; 19 | double H2; 20 | double H3; 21 | double H4; 22 | mystl::string sm; 23 | }; 24 | 25 | struct SJ_S 26 | { 27 | mystl::string s; 28 | mystl::string z; 29 | mystl::string j; 30 | mystl::string c; 31 | mystl::string h; 32 | mystl::string ch; 33 | mystl::string sj; 34 | mystl::string Ms; 35 | mystl::string Mz; 36 | mystl::string Mj; 37 | }; 38 | 39 | 40 | class SZJ 41 | { //日月的升中天降,不考虑气温和气压的影响 42 | public: 43 | static mystl::vector rts; //多天的升中降 44 | 45 | static double getH(double h, double w); 46 | static void Mcoord(double jd, double H0, SJ & r); 47 | static void Scoord(double jd, int xm, SJ & r); 48 | static SJ Mt(double jd); 49 | static SJ Qt(double jd); 50 | static SJ St(double jd); 51 | static void calcRTS(double jd, int n, double Jdl, double Wdl, double sq); 52 | 53 | static double L; //站点地理经度,向东测量为正 54 | static double fa; //站点地理纬度 55 | 56 | private: 57 | static double E; //黄赤交角 58 | static double dt; //TD-UT 59 | }; -------------------------------------------------------------------------------- /test/.c4droid: -------------------------------------------------------------------------------- 1 | #Thu Dec 07 16:41:22 GMT+08:00 2023 2 | binary_fname= 3 | run_mode=0 4 | use_cmake=false 5 | sm_names=test.cpp test1.cpp 6 | qmake_cmd=export PATH\=(c4droid\:GCCROOT)qt/bin/\:$PATH\nqmake -spec android-g++ 7 | cmake_cmd=cmake . 8 | prepare_cmds=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:DATADIR)/bintools\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CC\="(c4droid\:PREFIX)-gcc (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport CXX\="(c4droid\:PREFIX)-g++ (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport SHELL\="(c4droid\:DATADIR)sh"\ncd (c4droid\:CURSRCDIR) 9 | comp_mode=3 10 | make_cmd=make CC\="$CC" CXX\="$CXX" SHELL\="$SHELL" 11 | conf_patch=true 12 | conf_cmd=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:DATADIR)/bintools\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CFLAGS\="-Os -s (c4droid\:PIE)"\nexport CXXFLAGS\="-Os -s (c4droid\:PIE)"\nexport SHELL\="(c4droid\:DATADIR)sh"\nexport CONFIG_SHELL\="sh"\nexport PKG_CONFIG_PATH\=(c4droid\:GCCROOT)(c4droid\:PREFIX)/lib/pkgconfig\ncd (c4droid\:CURSRCDIR)\nfind . -exec touch {} \\;\ncd (c4droid\:BUILDDIR)\n(c4droid\:CURSRCDIR)/configure --host\=(c4droid\:PREFIX) --prefix\=(c4droid\:GCCROOT)(c4droid\:PREFIX) CFLAGS\="$CFLAGS" CXXFLAGS\="$CXXFLAGS" --build\=i686-linux --disable-shared --enable-static\nmake SHELL\="$SHELL"\nmake install SHELL\="$SHELL" 13 | conf_internal=false 14 | -------------------------------------------------------------------------------- /eph/.c4droid: -------------------------------------------------------------------------------- 1 | #Sun Aug 14 18:47:10 GMT+08:00 2022 2 | binary_fname= 3 | run_mode=0 4 | use_cmake=false 5 | sm_names=eph.cpp eph0.cpp eph_msc.cpp eph_rsgs.cpp eph_rspl.cpp eph_show.cpp eph_szj.cpp eph_yspl.cpp 6 | qmake_cmd=export PATH\=(c4droid\:GCCROOT)qt/bin/\:$PATH\nqmake -spec android-g++ 7 | cmake_cmd=cmake . 8 | prepare_cmds=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:DATADIR)/bintools\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CC\="(c4droid\:PREFIX)-gcc (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport CXX\="(c4droid\:PREFIX)-g++ (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport SHELL\="(c4droid\:DATADIR)sh"\ncd (c4droid\:CURSRCDIR) 9 | comp_mode=3 10 | make_cmd=make CC\="$CC" CXX\="$CXX" SHELL\="$SHELL" 11 | conf_patch=true 12 | conf_cmd=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:DATADIR)/bintools\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CFLAGS\="-Os -s (c4droid\:PIE)"\nexport CXXFLAGS\="-Os -s (c4droid\:PIE)"\nexport SHELL\="(c4droid\:DATADIR)sh"\nexport CONFIG_SHELL\="sh"\nexport PKG_CONFIG_PATH\=(c4droid\:GCCROOT)(c4droid\:PREFIX)/lib/pkgconfig\ncd (c4droid\:CURSRCDIR)\nfind . -exec touch {} \\;\ncd (c4droid\:BUILDDIR)\n(c4droid\:CURSRCDIR)/configure --host\=(c4droid\:PREFIX) --prefix\=(c4droid\:GCCROOT)(c4droid\:PREFIX) CFLAGS\="$CFLAGS" CXXFLAGS\="$CXXFLAGS" --build\=i686-linux --disable-shared --enable-static\nmake SHELL\="$SHELL"\nmake install SHELL\="$SHELL" 13 | conf_internal=false 14 | -------------------------------------------------------------------------------- /.c4droid: -------------------------------------------------------------------------------- 1 | #Thu Dec 07 15:47:24 GMT+08:00 2023 2 | binary_fname=./test1 3 | run_mode=0 4 | use_cmake=false 5 | sm_names=lat_lon_data.cpp eph_msc.cpp eph_show.cpp eph.cpp lunar_ob.cpp lunar.cpp eph_rsgs.cpp eph_szj.cpp eph0.cpp eph_rspl.cpp eph_yspl.cpp lunar_ssq.cpp tool.cpp 6 | qmake_cmd=export PATH\=(c4droid\:GCCROOT)qt/bin/\:$PATH\nqmake -spec android-g++ 7 | cmake_cmd=build_dir\=build\nif [ \! -d "$build_dir" ]; then\n mkdir -p $build_dir\nfi\ncd build && cmake .. 8 | prepare_cmds=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CC\="(c4droid\:PREFIX)-gcc (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport CXX\="(c4droid\:PREFIX)-g++ -Os (c4droid\:PIE) (c4droid\:MAKEMODEARGS)"\nexport SHELL\="(c4droid\:DATADIR)sh"\ncd (c4droid\:CURSRCDIR) 9 | comp_mode=1 10 | make_cmd=make -j8 CC\="$CC" CXX\="$CXX" SHELL\="$SHELL" 11 | conf_patch=true 12 | conf_cmd=export PATH\=/busybox-virtual\:(c4droid\:DATADIR)\:(c4droid\:GCCROOT)bin\:(c4droid\:GCCROOT)(c4droid\:PREFIX)/bin/\:$PATH\nexport CFLAGS\="-Os -s (c4droid\:PIE)"\nexport CXXFLAGS\="-Os -s (c4droid\:PIE)"\nexport SHELL\="(c4droid\:DATADIR)sh"\nexport CONFIG_SHELL\="sh"\nexport PKG_CONFIG_PATH\=(c4droid\:GCCROOT)(c4droid\:PREFIX)/lib/pkgconfig\ncd (c4droid\:CURSRCDIR)\nfind . -exec touch {} \\;\ncd (c4droid\:BUILDDIR)\n(c4droid\:CURSRCDIR)/configure --host\=(c4droid\:PREFIX) --prefix\=(c4droid\:GCCROOT)(c4droid\:PREFIX) CFLAGS\="$CFLAGS" CXXFLAGS\="$CXXFLAGS" --build\=i686-linux --disable-shared --enable-static\nmake SHELL\="$SHELL"\nmake install SHELL\="$SHELL" 13 | conf_internal=false 14 | -------------------------------------------------------------------------------- /eph/eph_msc.h: -------------------------------------------------------------------------------- 1 | #ifndef EPH_MSC_H 2 | #define EPH_MSC_H 3 | 4 | #include "../mylib/mystl/my_string.h" 5 | 6 | //========太阳月亮计算类============= 7 | class MSC 8 | { 9 | public: 10 | static double T; //TD力学时 11 | static double L; //地理纬度 12 | static double fa; //地理经度 13 | static double dt; //力学-世界时时差 14 | static double jd; //UT世界时 15 | static double dL; //黄经章动 16 | static double dE; //黄纬章动 17 | static double E; //交角章动 18 | static double gst; //真恒星时 19 | 20 | static double mHJ; //月球视黄经 21 | static double mHW; //月球视黄纬 22 | static double mR; //地月质心距 23 | static double mCJ; //月球视赤经 24 | static double mCW; //月球视赤纬 25 | static double mShiJ; //月球时角 26 | 27 | static double mCJ2; //时差修正后的赤道坐标 28 | static double mCW2; 29 | static double mR2; 30 | static double mDJ; //高度角 31 | static double mDW; //方位角 32 | static double mPJ; //大气折射修正后的高度角 33 | static double mPW; //大气折射修正后的方位角 34 | 35 | static double sHJ; //太阳视黄经 36 | static double sHW; //太阳视黄纬 37 | static double sCJ; //太阳视赤经 38 | static double sCW; //太阳视赤纬 39 | static double sCJ2; //时差修正后的赤道坐标 40 | static double sCW2; 41 | static double sR2; 42 | static double sShiJ; //太阳时角 43 | 44 | static double sDJ; //高度角 45 | static double sDW; //方位角 46 | static double sR; 47 | static double sPJ; //方位角 48 | static double sPW; //高度角 49 | static double sc; //时差 50 | 51 | static double pty; //平恒星时 52 | static double zty; //真恒星时 53 | static double mRad; //月亮视半径 54 | static double sRad; //太阳视半径 55 | static double e_mRad; //月亮地心视半径 56 | static double eShadow; //地本影在月球向径处的半径(角秒) 57 | static double eShadow2; //地半影在月球向径处的半径(角秒) 58 | static double mIll; //月面被照亮比例 59 | static double zx_J; //中心食坐标 60 | static double zx_W; 61 | 62 | static void calc(double T,double L,double fa,double high); 63 | static mystl::string toStr(bool fs); 64 | }; 65 | #endif -------------------------------------------------------------------------------- /eph/eph.h: -------------------------------------------------------------------------------- 1 | #ifndef EPH_H 2 | #define EPH_H 3 | 4 | #include "../mylib/mystl/my_string.h" 5 | #include "../mylib/mystl/static_array.h" 6 | 7 | struct COORDP 8 | { 9 | double x; 10 | double y; 11 | double z; 12 | double R1; 13 | double R2; 14 | double D; 15 | double X; 16 | double J; 17 | double W; 18 | }; 19 | 20 | struct NODE 21 | { 22 | mystl::array3 A; 23 | mystl::array3 B; 24 | double R1; 25 | double R2; 26 | int n; 27 | }; 28 | 29 | struct NODEX 30 | {//天象位置(近日,远日,升交点) 31 | double r, t; 32 | }; 33 | 34 | //==================================== 35 | /***** 36 | ecFast()函数返回参数说明 37 | r.jdSuo 朔时刻 38 | r.lx 日食类型 39 | *****/ 40 | typedef struct 41 | { 42 | double jd; 43 | double jdSuo; 44 | double ac; 45 | mystl::string lx; 46 | }_ECFAST; 47 | 48 | //========行星天象及星历============= 49 | double xingJJ(int xt, double t, int jing); 50 | mystl::array2 daJu(int xt, double t, bool dx); 51 | mystl::array3 xingLiu0(int xt, double t, int n, double gxs); 52 | double xingLiu(int xt, double t, bool sn); 53 | 54 | mystl::array4 xingMP(int xt, double t, int n, double E, mystl::array4 g); 55 | mystl::array4 xingHY(int xt, double t); 56 | mystl::array4 xingSP(int xt, double t, int n, double w0, double ts, double tp); 57 | mystl::array2 xingHR(int xt, double t, bool f); 58 | 59 | mystl::string xingX(int xt,double jd,double L,double fa); 60 | COORDP lineEll(double x1,double y1,double z1, double x2,double y2,double z2, double e,double r); 61 | COORDP lineEar2(double x1,double y1,double z1, double x2,double y2,double z2, double e,double r, mystl::array3 I); 62 | COORDP lineEar(mystl::array3 P,mystl::array3 Q,double gst); 63 | 64 | NODE cirOvl(double R,double ba,double R2,double x0,double y0); 65 | NODE lineOvl(double x1,double y1,double dx,double dy,double r,double ba); 66 | _ECFAST ecFast(double jd); 67 | 68 | #endif -------------------------------------------------------------------------------- /mylib/mystl/construct.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_CONSTRUCT_H_ 2 | #define MYTINYSTL_CONSTRUCT_H_ 3 | 4 | // 这个头文件包含两个函数 construct,destroy 5 | // construct : 负责对象的构造 6 | // destroy : 负责对象的析构 7 | 8 | #include 9 | 10 | #include "type_traits.h" 11 | #include "iterator.h" 12 | 13 | #ifdef _MSC_VER 14 | #pragma warning(push) 15 | #pragma warning(disable : 4100) // unused parameter 16 | #endif // _MSC_VER 17 | 18 | namespace mystl 19 | { 20 | 21 | // construct 构造对象 22 | 23 | template 24 | void construct(Ty* ptr) 25 | { 26 | ::new ((void*)ptr) Ty(); 27 | } 28 | 29 | template 30 | void construct(Ty1* ptr, const Ty2& value) 31 | { 32 | ::new ((void*)ptr) Ty1(value); 33 | } 34 | 35 | template 36 | void construct(Ty* ptr, Args&&... args) 37 | { 38 | ::new ((void*)ptr) Ty(mystl::forward(args)...); 39 | } 40 | 41 | // destroy 将对象析构 42 | 43 | template 44 | void destroy_one(Ty*, std::true_type) {} 45 | 46 | template 47 | void destroy_one(Ty* pointer, std::false_type) 48 | { 49 | if (pointer != nullptr) 50 | { 51 | pointer->~Ty(); 52 | } 53 | } 54 | 55 | template 56 | void destroy(Ty* pointer) 57 | { 58 | destroy_one(pointer, std::is_trivially_destructible{}); 59 | } 60 | 61 | template 62 | void destroy_cat(ForwardIter , ForwardIter , std::true_type) {} 63 | 64 | template 65 | void destroy_cat(ForwardIter first, ForwardIter last, std::false_type) 66 | { 67 | for (; first != last; ++first) 68 | destroy(&*first); 69 | } 70 | 71 | template 72 | void destroy(ForwardIter first, ForwardIter last) 73 | { 74 | destroy_cat(first, last, std::is_trivially_destructible< 75 | typename iterator_traits::value_type>{}); 76 | } 77 | 78 | } // namespace mystl 79 | 80 | #ifdef _MSC_VER 81 | #pragma warning(pop) 82 | #endif // _MSC_VER 83 | 84 | #endif // !MYTINYSTL_CONSTRUCT_H_ 85 | 86 | -------------------------------------------------------------------------------- /eph/eph_rspl.h: -------------------------------------------------------------------------------- 1 | //日食批量快速计算器 2 | 3 | #ifndef EPH_RSPL_H 4 | #define EPH_RSPL_H 5 | 6 | #include "../mylib/mystl/my_string.h" 7 | #include "../mylib/mystl/static_array.h" 8 | 9 | struct _SECXY 10 | { 11 | double mCJ; 12 | double mCW; 13 | double mR; 14 | double mCJ2; 15 | double mCW2; 16 | double mR2; 17 | 18 | double sCJ; 19 | double sCW; 20 | double sR; 21 | double sCJ2; 22 | double sCW2; 23 | double sR2; 24 | 25 | double mr; 26 | double sr; 27 | double x; 28 | double y; 29 | double t; 30 | }; 31 | 32 | struct _ZB 33 | { 34 | mystl::array3 S; 35 | mystl::array3 M; 36 | double sr; 37 | double mr; 38 | double x; 39 | double y; 40 | double g; 41 | }; 42 | 43 | struct _GJW 44 | { 45 | double J; 46 | double W; 47 | mystl::string c; 48 | }; 49 | 50 | 51 | class RS_PL 52 | { 53 | public: 54 | static bool nasa_r;//为1表示采用NASA的视径比 55 | static mystl::array5 sT;//地方日食时间表 56 | static mystl::string LX; 57 | static double sf; 58 | static double sf2; //食分(日出食分) 59 | static double sf3; //食分(日没食分) 60 | static mystl::string sflx; //食分类型 61 | static double b1; 62 | static double dur; 63 | static double sun_s; 64 | static double sun_j; 65 | static double P1; 66 | static double V1; 67 | static double P2; 68 | static double V2; 69 | static void secMax(double jd,double L,double fa,double high); 70 | static void nbj(double jd); 71 | //以下涉及南北界计算 72 | static mystl::array3 A; 73 | static mystl::array3 B; //本半影锥顶点坐标 74 | static _ZB P;//t1时刻的日月坐标,g为恒星时 75 | static _ZB Q;//t2时刻的日月坐标 76 | static mystl::array10 V;//食界表 77 | static mystl::string Vc; 78 | static mystl::string Vb; //食中心类型,本影南北距离 79 | 80 | static double lineT(_SECXY G, double v,double u, double r, bool n); 81 | static void zbXY(_ZB &p,double L,double fa); 82 | static void zb0(double jd); 83 | static void p2p(double L,double fa,_GJW &re,bool fAB,int f); 84 | static void pp0(_GJW &re); 85 | static void secXY(double jd,double L,double fa,double high,_SECXY &re); 86 | 87 | }; 88 | 89 | #endif -------------------------------------------------------------------------------- /mylib/conio.h: -------------------------------------------------------------------------------- 1 | /** 2 | * conio.h 3 | * Author: Jakash3 QAIU 4 | */ 5 | 6 | // Update.2021-11-01 兼容Cygwin 修改getch实现方式 7 | // Update.2021-11-03 添加windows下MinGW-GCC编译支持 8 | // Update.2023-03-15 修改getch和kbhit的实现 9 | // Update.2025-07-28 添加MACOS下__llvm__宏定义 10 | 11 | #ifndef CONIO_H 12 | #define CONIO_H 13 | 14 | #if defined (__unix__) || defined(__llvm__) 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | //读取单字符 https://my.oschina.net/yougui/blog/111345 21 | static char getch() 22 | { 23 | struct termios oldt, newt; 24 | tcgetattr(STDIN_FILENO, &oldt); 25 | memcpy(&newt, &oldt, sizeof(newt)); 26 | newt.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOKE | ICRNL); 27 | tcsetattr(STDIN_FILENO, TCSANOW, &newt); 28 | char c=getchar(); 29 | tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 30 | return c; 31 | } 32 | 33 | //判断输入 https://www.cnblogs.com/xiayong123/archive/2011/07/19/3717262.html 34 | static int kbhit(void) 35 | { 36 | struct termios oldt, newt; 37 | tcgetattr(STDIN_FILENO, &oldt); 38 | newt = oldt; 39 | newt.c_lflag &= ~(ICANON | ECHO); 40 | tcsetattr(STDIN_FILENO, TCSANOW, &newt); 41 | int oldf = fcntl(STDIN_FILENO, F_GETFL, 0); 42 | fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); 43 | int ch = getchar(); 44 | tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 45 | fcntl(STDIN_FILENO, F_SETFL, oldf); 46 | return (ch != EOF)? ungetc(ch, stdin),1:0; 47 | } 48 | 49 | static void gotoxy(int x, int y) { printf("\033[%d;%df", y, x); } 50 | 51 | static void _gotoxy(int x,int y) {gotoxy(x,y);} 52 | 53 | static void clrscr() { printf("\033[2J\033[0;0f"); } 54 | 55 | static void _clrscr() {clrscr();} 56 | 57 | static int _getch() {return getch();} 58 | 59 | static int _kbhit() {return kbhit();} 60 | 61 | #elif defined (__WIN32__) || defined (__GNUC__) || defined (_MSC_VER) // __unix__ 62 | #include 63 | #include 64 | #include 65 | 66 | static void clrscr() {system("cls");} 67 | 68 | static void _clrscr() {clrscr();} 69 | 70 | static void gotoxy(int x, int y) { 71 | COORD c; 72 | c.X = x; 73 | c.Y = y; 74 | SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c); 75 | } 76 | static void _gotoxy(int x,int y) {gotoxy(x,y);} 77 | #endif // __WIN32__ 78 | 79 | #endif // CONIO_H 80 | -------------------------------------------------------------------------------- /mylib/mystl/static_array.h: -------------------------------------------------------------------------------- 1 | #ifndef STATIC_ARRAY_H 2 | #define STATIC_ARRAY_H 3 | #include 4 | #include 5 | 6 | namespace mystl { 7 | // 实现一个非常简易的静态固定容量数组 8 | template 9 | struct static_array { 10 | using t_list = const std::initializer_list&; 11 | using t_list_i = const std::initializer_list&; 12 | using t_list_d = const std::initializer_list&; 13 | 14 | // 定义迭代器类型 15 | using iterator = T*; 16 | 17 | static_array() {} 18 | 19 | static_array& operator=(const static_array& right) { 20 | for (int i = 0; i < N; ++i) { 21 | data[i] = right.data[i]; 22 | } 23 | return *this; 24 | } 25 | 26 | T& operator[](size_t i) { 27 | return data[i]; 28 | } 29 | 30 | T get(size_t i) { 31 | return data[i]; 32 | } 33 | 34 | 35 | void fill(const T& val) { 36 | for (size_t i = 0; i < N; ++i) { 37 | data[i] = val; 38 | } 39 | } 40 | 41 | // 通过列表数组初始化 42 | static_array(t_list r) { 43 | auto it = r.begin(); 44 | for (size_t i = 0; i < r.size(); ++i) { 45 | data[i] = *it++; 46 | } 47 | } 48 | 49 | iterator begin() { 50 | return _begin; 51 | } 52 | iterator end() { 53 | return _end; 54 | } 55 | 56 | private: 57 | // 数据实体 58 | T data[N]={}; 59 | iterator _begin = data; 60 | iterator _end = data + N; 61 | }; 62 | 63 | // 容量为2的double数组 64 | struct array2 : static_array { 65 | array2() {}; 66 | array2(t_list_d r):static_array(r){} 67 | }; 68 | 69 | // 容量为3的double数组 70 | struct array3 : static_array { 71 | array3() {}; 72 | array3(t_list_d r):static_array(r){} 73 | 74 | }; 75 | 76 | // 容量为4的double数组 77 | struct array4 : static_array { 78 | array4() {}; 79 | array4(t_list_d r):static_array(r){} 80 | }; 81 | 82 | // 容量为5的double数组 83 | struct array5 : static_array { 84 | array5() {}; 85 | array5(t_list_d r):static_array(r){} 86 | }; 87 | 88 | // 容量为7的double数组 89 | struct array7 : static_array { 90 | array7() {}; 91 | array7(t_list_d r):static_array(r){} 92 | }; 93 | 94 | // 容量为10的double数组 95 | struct array10 : static_array { 96 | array10() {}; 97 | array10(t_list_d r):static_array(r){} 98 | }; 99 | 100 | } // namespace mystl 101 | 102 | #endif // STATIC_ARRAY_H -------------------------------------------------------------------------------- /mylib/mystl/allocator.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_ALLOCATOR_H_ 2 | #define MYTINYSTL_ALLOCATOR_H_ 3 | 4 | // 这个头文件包含一个模板类 allocator,用于管理内存的分配、释放,对象的构造、析构 5 | 6 | #include "construct.h" 7 | #include "util.h" 8 | 9 | namespace mystl 10 | { 11 | 12 | // 模板类:allocator 13 | // 模板函数代表数据类型 14 | template 15 | class allocator 16 | { 17 | public: 18 | typedef T value_type; 19 | typedef T* pointer; 20 | typedef const T* const_pointer; 21 | typedef T& reference; 22 | typedef const T& const_reference; 23 | typedef size_t size_type; 24 | typedef ptrdiff_t difference_type; 25 | 26 | public: 27 | static T* allocate(); 28 | static T* allocate(size_type n); 29 | 30 | static void deallocate(T* ptr); 31 | static void deallocate(T* ptr, size_type n); 32 | 33 | static void construct(T* ptr); 34 | static void construct(T* ptr, const T& value); 35 | static void construct(T* ptr, T&& value); 36 | 37 | template 38 | static void construct(T* ptr, Args&& ...args); 39 | 40 | static void destroy(T* ptr); 41 | static void destroy(T* first, T* last); 42 | }; 43 | 44 | template 45 | T* allocator::allocate() 46 | { 47 | return static_cast(::operator new(sizeof(T))); 48 | } 49 | 50 | template 51 | T* allocator::allocate(size_type n) 52 | { 53 | if (n == 0) 54 | return nullptr; 55 | return static_cast(::operator new(n * sizeof(T))); 56 | } 57 | 58 | template 59 | void allocator::deallocate(T* ptr) 60 | { 61 | if (ptr == nullptr) 62 | return; 63 | ::operator delete(ptr); 64 | } 65 | 66 | template 67 | void allocator::deallocate(T* ptr, size_type /*size*/) 68 | { 69 | if (ptr == nullptr) 70 | return; 71 | ::operator delete(ptr); 72 | } 73 | 74 | template 75 | void allocator::construct(T* ptr) 76 | { 77 | mystl::construct(ptr); 78 | } 79 | 80 | template 81 | void allocator::construct(T* ptr, const T& value) 82 | { 83 | mystl::construct(ptr, value); 84 | } 85 | 86 | template 87 | void allocator::construct(T* ptr, T&& value) 88 | { 89 | mystl::construct(ptr, mystl::move(value)); 90 | } 91 | 92 | template 93 | template 94 | void allocator::construct(T* ptr, Args&& ...args) 95 | { 96 | mystl::construct(ptr, mystl::forward(args)...); 97 | } 98 | 99 | template 100 | void allocator::destroy(T* ptr) 101 | { 102 | mystl::destroy(ptr); 103 | } 104 | 105 | template 106 | void allocator::destroy(T* first, T* last) 107 | { 108 | mystl::destroy(first, last); 109 | } 110 | 111 | } // namespace mystl 112 | #endif // !MYTINYSTL_ALLOCATOR_H_ 113 | 114 | -------------------------------------------------------------------------------- /mylib/test/test_itoa.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License 3 | 4 | Copyright (c) 2017 James Edward Anhalt III - https://github.com/jeaiii/itoa 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "../mystl/itoa.h" 33 | 34 | template struct is_float {const static bool value = false;}; 35 | template <> struct is_float {const static bool value = true;}; 36 | template <> struct is_float {const static bool value = true;}; 37 | template <> struct is_float {const static bool value = true;}; 38 | 39 | template 40 | void show(T n) 41 | { 42 | std::cout << "is_float == " << std::boolalpha << is_float::value << std::endl; 43 | char text[32] = {}; 44 | jeaiii::to_text_from_integer(text, n); 45 | std::cout << text << "\n"; 46 | } 47 | 48 | template 49 | void test(T n) 50 | { 51 | show(n); 52 | } 53 | 54 | int main() 55 | { 56 | test(-1.2); 57 | test(-1); 58 | test(99963555669888.9687); 59 | test(0x7fffffff); 60 | test(-0x7fffffff - 1); 61 | 62 | test(1000000000900000000ULL); 63 | test(1000000000800000001ULL); 64 | test(1000000000700000002ULL); 65 | test(1000000000600000003ULL); 66 | test(1000000000500000004ULL); 67 | test(1000000000400000005ULL); 68 | test(1000000000300000006ULL); 69 | test(1000000000200000007ULL); 70 | test(1000000000100000008ULL); 71 | test(1000000000000000009ULL); 72 | 73 | test(999010000000ULL); 74 | test(999010000001ULL); 75 | test(999010000009ULL); 76 | 77 | test(99909000000ULL); 78 | test(99909000001ULL); 79 | test(99909000009ULL); 80 | 81 | test(99909000009LL); 82 | test(-99909000009LL); 83 | 84 | test(17999999999999999999ULL); 85 | 86 | test(5999999999999999999LL); 87 | test(-5999999999999999999LL); 88 | 89 | return 0; 90 | } -------------------------------------------------------------------------------- /lunar/lunar_ob.h: -------------------------------------------------------------------------------- 1 | #ifndef LUNAR_OB_H 2 | #define LUNAR_OB_H 3 | 4 | #include "../mylib/mystl/my_string.h" 5 | #include "../mylib/mystl/vector.h" 6 | 7 | struct OB_DAY 8 | { 9 | /* 日的公历信息 */ 10 | int d0; // 2000.0起算儒略日,北京时12:00 11 | int di; // 所在公历月内日序数 12 | int y; 13 | int m; 14 | int d; // 日名称(公历) 15 | int dn; 16 | int week0; 17 | int week; // 星期 18 | int weeki; // 在本月中的周序号 19 | int weekN; // 本月的总周数 20 | /* 日的农历信息 */ 21 | int Ldi; // 日距农历月首偏移 22 | mystl::string Ldc; // 日名称(农历),即'初一,初二等' 23 | 24 | int cur_dz; //冬至的天数 25 | int cur_xz; //夏至的天数 26 | int cur_lq; //立秋的天数 27 | int cur_mz; //芒种的天数 28 | int cur_xs; //小暑的天数 29 | mystl::string Lmc; 30 | mystl::string Lmc2; // 月名称 31 | int Ldn; // 月大小 32 | mystl::string Lleap; // 闰状况 33 | /* 日的农历纪年、月、日、时及星座 */ 34 | int Lyear; // 农历纪年(10进制,1984年起算,分界点可以是立春也可以是春节,在程序中选择一个) 35 | int Lyear0; 36 | mystl::string Lyear2; // 干支纪年 37 | mystl::string Lyear3; // 干支纪年(春节) 38 | int Lyear4; // 干支纪年(黄帝纪元) 39 | int Lmonth; // 纪月处理,1998年12月7日(大雪)开始连续进行节气计数,0为甲子 40 | mystl::string Lmonth2; // 干支纪月 41 | mystl::string Lday2; // 纪日 42 | mystl::string Ltime2; // 纪时 43 | mystl::string Ljq; // 节气 44 | mystl::string XiZ; // 星座 45 | /* 日的回历信息 */ 46 | int Hyear; // 年(回历) 47 | int Hmonth; // 月(回历) 48 | int Hday; // 日(回历) 49 | /* 日的其它信息 */ 50 | mystl::string yxmc; // 月相名称 51 | double yxjd; // 月相时刻(儒略日) 52 | mystl::string yxsj; // 月相时间串 53 | mystl::string jqmc; // 节气名称 54 | double jqjd; // 节气时刻(儒略日) 55 | mystl::string jqsj; // 节气时间串 56 | 57 | bool Fjia; 58 | mystl::string A; 59 | mystl::string B; 60 | mystl::string C; 61 | }; 62 | 63 | struct MLBZ 64 | { 65 | mystl::string bz_jn; 66 | mystl::string bz_jy; 67 | mystl::string bz_jr; 68 | mystl::string bz_js; 69 | mystl::string bz_JS; 70 | mystl::string bz_zty; 71 | }; 72 | 73 | class OBA 74 | { 75 | public: 76 | static void init(); 77 | static void getDayName(OB_DAY &r); 78 | static void getHuiLi(double d0,OB_DAY &r); 79 | 80 | private: 81 | static mystl::vector> sFtv; //假日表,由init初始化 82 | static mystl::vector wFtv; 83 | }; 84 | 85 | class OBB//农历对象,气朔计算等 86 | { 87 | public: 88 | static void init(); 89 | static mystl::string getNH(int y); 90 | static void getDayName2(OB_DAY &r); 91 | static void mingLiBaZi(double jd, double J, MLBZ &ob); 92 | static double qi_accurate(double W); 93 | static double so_accurate(double W); 94 | static double qi_accurate2(double jd); 95 | static double so_accurate2(double jd); 96 | 97 | private: 98 | static mystl::vector JNB; 99 | }; 100 | 101 | extern const char *str_num[]; 102 | extern const char *str_ymc[]; 103 | extern const char *str_yxmc[]; 104 | extern const char *str_jqmc[]; 105 | extern const char *str_gan[]; 106 | extern const char *str_zhi[]; 107 | extern const char *str_sxmc[]; 108 | extern const char *str_nywx[]; 109 | extern const char *str_xqmc[]; 110 | extern const char *str_rmc[]; 111 | extern const char *str_rmc0[]; 112 | extern const char *str_xz[]; 113 | extern const char *str_dx[]; 114 | extern const char *str_ago[]; 115 | extern const char *str_fw[]; 116 | extern const char *str_sjd[]; 117 | extern const char *str_ry[]; 118 | extern const char *str_ry2[]; 119 | extern const char *str_yx[]; 120 | 121 | #endif -------------------------------------------------------------------------------- /eph/eph0.h: -------------------------------------------------------------------------------- 1 | #ifndef EPH_0_H 2 | #define EPH_0_H 3 | 4 | #include "../mylib/mystl/static_array.h" 5 | 6 | #define _pi 3.14159265358979323846 7 | #define cs_rEar 6378.1366 //地球赤道半径(千米) 8 | #define cs_rEarA (0.99834*cs_rEar) //平均半径 9 | #define cs_ba 0.99664719 //地球极赤半径比 10 | #define cs_ba2 (cs_ba*cs_ba) //地球极赤半径比的平方 11 | #define cs_AU 1.49597870691e8 //天文单位长度(千米) 12 | #define cs_sinP (cs_rEar/cs_AU) //sin(太阳视差) 13 | #define cs_PI asin(cs_sinP) //太阳视差 14 | #define cs_GS 299792.458 //光速(行米/秒) 15 | #define cs_Agx (cs_AU/cs_GS/86400.0/36525) //每天文单位的光行时间(儒略世纪) 16 | #define rad (180*3600/_pi) //每弧度的角秒数 17 | #define radd (180/_pi) //每弧度的度数 18 | 19 | #define pi2 (_pi*2) 20 | #define pi_2 (_pi/2) 21 | #define J2000 2451545 22 | 23 | #define cs_k 0.2725076 //月亮与地球的半径比(用于半影计算) 24 | #define cs_k2 0.2722810 //月亮与地球的半径比(用于本影计算) 25 | #define cs_k0 109.1222 //太阳与地球的半径比(对应959.64) 26 | #define cs_sMoon (cs_k*cs_rEar*1.0000036*rad) //用于月亮视半径计算 27 | #define cs_sMoon2 (cs_k2*cs_rEar*1.0000036*rad) //用于月亮视半径计算 28 | #define cs_sSun 959.64 //用于太阳视半径计算 29 | #define cs_xxHH_DATA 116,584,780,399,378,370,367,367 30 | 31 | extern double cs_xxHH[]; 32 | 33 | double rad2mrad(double v); 34 | double rad2rrad(double v); 35 | 36 | mystl::array3 llr2xyz(mystl::array3 JW); 37 | mystl::array3 xyz2llr(mystl::array3 xyz); 38 | mystl::array3 llrConv(mystl::array3 JW, double E); 39 | mystl::array3 CD2DP(mystl::array3 z, double L, double fa, double gst); 40 | double j1_j2(double J1, double W1, double J2, double W2); 41 | mystl::array3 h2g(mystl::array3 z, mystl::array3 a); 42 | double shiChaJ(double gst,double L,double fa,double J,double W); 43 | double hcjj(double t); 44 | double dt_calc(double y); 45 | double dt_T(double t); 46 | mystl::array3 CDllr_J2D(double t, mystl::array3 llr, const char *mx); 47 | mystl::array3 CDllr_D2J(double t, mystl::array3 llr, const char *mx); 48 | mystl::array3 HDllr_J2D(double t, mystl::array3 llr, const char *mx); 49 | mystl::array3 HDllr_D2J(double t, mystl::array3 llr, const char *mx); 50 | double MQC(double h); 51 | double MQC2(double ho); 52 | mystl::array3 parallax(mystl::array3 z, double H, double fa, double high); 53 | mystl::array2 nutation2(double t); 54 | mystl::array2 nutation(double t, int zq); 55 | double nutationLon2(double t); 56 | double XL0_calc(int xt, int zn, double t, int n); 57 | mystl::array3 pluto_coord(double t); 58 | mystl::array3 p_coord(int xt, double t, int n1, int n2, int n3); 59 | mystl::array3 e_coord(double t, int n1, int n2, int n3); 60 | double XL1_calc(int zn, double t, int n); 61 | mystl::array3 m_coord(double t, int n1, int n2, int n3); 62 | double gxc_sunLon(double t); 63 | double gxc_sunLat(double t); 64 | double gxc_moonLon(double t); 65 | double gxc_moonLat(double t); 66 | double E_Lon(double t, int n); 67 | double M_Lon(double t, int n); 68 | double pGST(double T, double dt); 69 | double pGST2(double jd); 70 | double E_v(double t); 71 | double M_v(double t); 72 | double MS_aLon(double t, int Mn, int Sn); 73 | double S_aLon(double t, int n); 74 | double MS_aLon_t(double W); 75 | double S_aLon_t(double W); 76 | double MS_aLon_t2(double W); 77 | double S_aLon_t2(double W); 78 | 79 | mystl::array2 moonMinR(double t, bool min); 80 | mystl::array2 moonNode(double t, double asc); 81 | mystl::array2 earthMinR(double t, bool min); 82 | 83 | int suoN(double jd); 84 | double sunShengJ(double jd, double L, double fa, int sj); 85 | double pty_zty(double t); 86 | double pty_zty2(double t); 87 | double moonIll(double t); 88 | double moonRad(double r, double h); 89 | 90 | #endif -------------------------------------------------------------------------------- /eph/eph_rsgs.h: -------------------------------------------------------------------------------- 1 | #ifndef EPH_RSGS_H 2 | #define EPH_RSGS_H 3 | 4 | #include "../mylib/mystl/vector.h" 5 | #include "../mylib/mystl/my_string.h" 6 | #include "../mylib/mystl/static_array.h" 7 | 8 | struct _VXY 9 | { 10 | double vx; 11 | double vy; 12 | double Vx; 13 | double Vy; 14 | double V; 15 | }; 16 | 17 | struct _RSM 18 | { 19 | double r1; 20 | double r2; 21 | double ar2; 22 | double sf; 23 | }; 24 | 25 | struct _FEATURE 26 | { 27 | double jdSuo; 28 | double dT; 29 | double ds; 30 | double vx; 31 | double vy; 32 | double ax; 33 | double ay; 34 | double v; 35 | double k; 36 | 37 | double t0; 38 | double jd; 39 | double xc; 40 | double yc; 41 | double zc; 42 | double D; 43 | double d; 44 | mystl::array3 I; 45 | mystl::array3 gk1; 46 | mystl::array3 gk2; 47 | mystl::array3 gk3; 48 | mystl::array3 gk4; 49 | mystl::array3 gk5; 50 | mystl::string lx; 51 | 52 | double zxJ; 53 | double zxW; 54 | double dw; 55 | double sf; 56 | double tt; 57 | mystl::array3 Sdp; 58 | 59 | mystl::vector p1; 60 | mystl::vector p2; 61 | mystl::vector p3; 62 | mystl::vector p4; 63 | mystl::vector q1; 64 | mystl::vector q2; 65 | mystl::vector q3; 66 | mystl::vector q4; 67 | mystl::vector L0; 68 | mystl::vector L1; 69 | mystl::vector L2; 70 | mystl::vector L3; 71 | mystl::vector L4; 72 | mystl::vector L5; 73 | mystl::vector L6; 74 | }; 75 | 76 | struct _JIEX2 77 | { 78 | mystl::vector p1; 79 | mystl::vector p2; 80 | mystl::vector p3; 81 | }; 82 | 83 | struct _FLAG 84 | { 85 | int f; 86 | int f2; 87 | }; 88 | 89 | class RS_GS 90 | { 91 | public: 92 | 93 | static double Zjd; 94 | static void init(double jd,int n); 95 | static _FEATURE feature(double jd); 96 | //static _FEATURE __rsGS::jieX(double jd); 97 | //static _JIEX2 __rsGS::jieX2(double jd); 98 | static mystl::string jieX3(double jd); 99 | static inline mystl::array3 sun (double jd){ return chazhi(jd,0); } //传回值可能超过360度 100 | static inline mystl::array3 moon(double jd){ return chazhi(jd,1); } 101 | static inline mystl::array3 bse (double jd){ return chazhi(jd,2); } 102 | 103 | private: 104 | static mystl::vector Zs; 105 | static double Zdt; 106 | static double dT; 107 | static double tanf1; 108 | static double tanf2; 109 | static double srad; 110 | static double bba; 111 | static double bhc; 112 | static double dyj; 113 | 114 | static mystl::array3 chazhi(double jd,int xt); 115 | static mystl::array3 cd2bse(mystl::array3 z,mystl::array3 I); 116 | static mystl::array3 bse2cd(mystl::array3 z,mystl::array3 I ); 117 | static mystl::array3 bse2db(mystl::array3 z,mystl::array3 I ,bool f); 118 | static mystl::array3 bseXY2db(double x,double y,mystl::array3 I,bool f); 119 | static mystl::array3 bseM(double jd); 120 | static _VXY Vxy(double x,double y,double s, double vx,double vy); 121 | static _RSM rSM(double mR); 122 | static mystl::array3 qrd(double jd,double dx,double dy,bool fs); 123 | static void push(mystl::array3 z,mystl::vector &p); 124 | static mystl::array4 nanbei(mystl::array3 M,double vx0,double vy0, double h,double r,mystl::array3 I); 125 | static bool mDian(mystl::array3 M,double vx0,double vy0,bool AB, double r,mystl::array3 I,mystl::vector &A); 126 | //static void __rsGS::elmCpy(mystl::vector &a,int n,mystl::vector b,int m); 127 | //static void __rsGS::mQie(mystl::array3 M,double vx0,double vy0,double h, double r,mystl::array3 I, mystl::vector &A,_FLAG &FLAG); 128 | 129 | }; 130 | 131 | // extern std::map lxb; 132 | #endif -------------------------------------------------------------------------------- /eph/eph_yspl.cpp: -------------------------------------------------------------------------------- 1 | #include "eph0.h" 2 | #include "eph_yspl.h" 3 | #include "../mylib/math_patch.h" 4 | 5 | mystl::array7 YS_PL::lT; 6 | mystl::string YS_PL::LX; 7 | double YS_PL::sf; 8 | 9 | double YS_PL::lineT(RE0 G,double v,double u, double r, bool n) 10 | {//已知t1时刻星体位置、速度,求x*x+y*y=r*r时,t的值 11 | double b=G.y*v-G.x*u, A=u*u+v*v, B=u*b, C=b*b-r*r*v*v, D=B*B-A*C; 12 | if(D<0) return 0; 13 | D=sqrt(D); if(!n) D=-D; 14 | return G.t+((-B+D)/A-G.x)/v; 15 | } 16 | void YS_PL::lecXY(double jd, RE0 &re) 17 | {//日月黄经纬差转为日面中心直角坐标(用于月食) 18 | double T=jd/36525; 19 | mystl::array3 zm={}, zs={}; 20 | 21 | //=======太阳月亮黄道坐标======== 22 | zs = e_coord(T,-1,-1,-1); //地球坐标 23 | zs[0] = rad2mrad(zs[0]+_pi+gxc_sunLon(T)); zs[1] =-zs[1] + gxc_sunLat(T); //补上太阳光行差 24 | zm = m_coord(T,-1,-1,-1); //月球坐标 25 | zm[0] = rad2mrad( zm[0]+gxc_moonLon(T) ); zm[1] += gxc_moonLat(T); //补上月球光行差就可以了 26 | 27 | //=======视半径======= 28 | re.e_mRad = cs_sMoon/zm[2]; //月亮地心视半径(角秒) 29 | re.eShadow = (cs_rEarA/zm[2]*rad-(959.63-8.794)/zs[2] )*51/50; //地本影在月球向径处的半径(角秒),式中51/50是大气厚度补偿 30 | re.eShadow2= (cs_rEarA/zm[2]*rad+(959.63+8.794)/zs[2] )*51/50; //地半影在月球向径处的半径(角秒),式中51/50是大气厚度补偿 31 | 32 | re.x = rad2rrad(zm[0]+_pi-zs[0]) * cos((zm[1]-zs[1])/2); 33 | re.y = zm[1]+zs[1]; 34 | 35 | re.mr= re.e_mRad/rad, 36 | re.er=re.eShadow/rad, 37 | re.Er=re.eShadow2/rad; 38 | 39 | re.t = jd; 40 | } 41 | 42 | void YS_PL::lecMax(double jd) 43 | { //月食的食甚计算(jd为近朔的力学时,误差几天不要紧) 44 | //YS_PL::lT={}; 45 | for(int i=0;i<7;i++) YS_PL::lT[i]=0; //分别是:食甚,初亏,复圆,半影食始,半影食终,食既,生光 46 | YS_PL::sf=0; 47 | YS_PL::LX=""; 48 | 49 | jd = MS_aLon_t2( floor((jd-4)/29.5306)*_pi*2 +_pi)*36525; //低精度的朔(误差10分钟),与食甚相差10分钟左右 50 | 51 | RE0 g={}, G={}; 52 | double u,v; 53 | 54 | //求极值(平均误差数秒) 55 | u = -18461 * sin(0.057109+0.23089571958*jd)*0.23090/rad; //月日黄纬速度差 56 | v = (M_v(jd/36525)-E_v(jd/36525))/36525; //月日黄经速度差 57 | YS_PL::lecXY(jd,G); 58 | jd -= (G.y*u+G.x*v)/(u*u+v*v); //极值时间 59 | 60 | //精密求极值 61 | double dt=60/86400.0; 62 | YS_PL::lecXY(jd,G); YS_PL::lecXY(jd+dt,g); //精密差分得速度,再求食甚 63 | u = (g.y-G.y)/dt; 64 | v = (g.x-G.x)/dt; 65 | dt= -(G.y*u+G.x*v)/(u*u+v*v); jd += dt; //极值时间 66 | 67 | //求直线到影子中心的最小值 68 | double x=G.x+dt*v, y=G.y+dt*u, rmin=sqrt(x*x+y*y); 69 | //注意,以上计算得到了极值及最小距rmin,但没有再次计算极值时刻的半径,对以下的判断造成一定的风险,必要的话可以再算一次。不过必要性不很大,因为第一次极值计算已经很准确了,误差只有几秒 70 | //求月球与影子的位置关系 71 | if(rmin<=G.mr+G.er){ //食计算 72 | YS_PL::lT[1] = jd; //食甚 73 | YS_PL::LX = "偏"; 74 | YS_PL::sf=(G.mr+G.er-rmin)/G.mr/2; //食分 75 | 76 | YS_PL::lT[0] = YS_PL::lineT(G,v,u, G.mr+G.er, 0); //初亏 77 | YS_PL::lecXY(YS_PL::lT[0],g); 78 | YS_PL::lT[0] = YS_PL::lineT(g,v,u, g.mr+g.er, 0); //初亏再算一次 79 | // std::cout<> 15 | class stack 16 | { 17 | public: 18 | typedef Container container_type; 19 | // 使用底层容器的型别 20 | typedef typename Container::value_type value_type; 21 | typedef typename Container::size_type size_type; 22 | typedef typename Container::reference reference; 23 | typedef typename Container::const_reference const_reference; 24 | 25 | static_assert(std::is_same::value, 26 | "the value_type of Container should be same with T"); 27 | private: 28 | container_type c_; // 用底层容器表现 stack 29 | 30 | public: 31 | // 构造、复制、移动函数 32 | stack() = default; 33 | 34 | explicit stack(size_type n) 35 | :c_(n) 36 | { 37 | } 38 | stack(size_type n, const value_type& value) 39 | :c_(n, value) 40 | { 41 | } 42 | 43 | template 44 | stack(IIter first, IIter last) 45 | : c_(first, last) 46 | { 47 | } 48 | 49 | stack(std::initializer_list ilist) 50 | :c_(ilist.begin(), ilist.end()) 51 | { 52 | } 53 | 54 | stack(const Container& c) 55 | :c_(c) 56 | { 57 | } 58 | stack(Container&& c) noexcept(std::is_nothrow_move_constructible::value) 59 | :c_(mystl::move(c)) 60 | { 61 | } 62 | 63 | stack(const stack& rhs) 64 | :c_(rhs.c_) 65 | { 66 | } 67 | stack(stack&& rhs) noexcept(std::is_nothrow_move_constructible::value) 68 | :c_(mystl::move(rhs.c_)) 69 | { 70 | } 71 | 72 | stack& operator=(const stack& rhs) 73 | { 74 | c_ = rhs.c_; 75 | return *this; 76 | } 77 | stack& operator=(stack&& rhs) noexcept(std::is_nothrow_move_assignable::value) 78 | { 79 | c_ = mystl::move(rhs.c_); 80 | return *this; 81 | } 82 | 83 | stack& operator=(std::initializer_list ilist) 84 | { 85 | c_ = ilist; 86 | return *this; 87 | } 88 | 89 | ~stack() = default; 90 | 91 | // 访问元素相关操作 92 | reference top() { return c_.back(); } 93 | const_reference top() const { return c_.back(); } 94 | 95 | // 容量相关操作 96 | bool empty() const noexcept { return c_.empty(); } 97 | size_type size() const noexcept { return c_.size(); } 98 | 99 | // 修改容器相关操作 100 | 101 | template 102 | void emplace(Args&& ...args) 103 | { c_.emplace_back(mystl::forward(args)...); } 104 | 105 | void push(const value_type& value) 106 | { c_.push_back(value); } 107 | void push(value_type&& value) 108 | { c_.push_back(mystl::move(value)); } 109 | 110 | void pop() 111 | { c_.pop_back(); } 112 | 113 | void clear() 114 | { 115 | while (!empty()) 116 | pop(); 117 | } 118 | 119 | void swap(stack& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_))) 120 | { mystl::swap(c_, rhs.c_); } 121 | 122 | public: 123 | friend bool operator==(const stack& lhs, const stack& rhs) { return lhs.c_ == rhs.c_; } 124 | friend bool operator< (const stack& lhs, const stack& rhs) { return lhs.c_ < rhs.c_; } 125 | }; 126 | 127 | // 重载比较操作符 128 | template 129 | bool operator==(const stack& lhs, const stack& rhs) 130 | { 131 | return lhs == rhs; 132 | } 133 | 134 | template 135 | bool operator<(const stack& lhs, const stack& rhs) 136 | { 137 | return lhs < rhs; 138 | } 139 | 140 | template 141 | bool operator!=(const stack& lhs, const stack& rhs) 142 | { 143 | return !(lhs == rhs); 144 | } 145 | 146 | template 147 | bool operator>(const stack& lhs, const stack& rhs) 148 | { 149 | return rhs < lhs; 150 | } 151 | 152 | template 153 | bool operator<=(const stack& lhs, const stack& rhs) 154 | { 155 | return !(rhs < lhs); 156 | } 157 | 158 | template 159 | bool operator>=(const stack& lhs, const stack& rhs) 160 | { 161 | return !(lhs < rhs); 162 | } 163 | 164 | // 重载 mystl 的 swap 165 | template 166 | void swap(stack& lhs, stack& rhs) noexcept(noexcept(lhs.swap(rhs))) 167 | { 168 | lhs.swap(rhs); 169 | } 170 | 171 | } // namespace mystl 172 | #endif // !MYTINYSTL_STACK_H_ 173 | 174 | -------------------------------------------------------------------------------- /mylib/mystl/numeric.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_NUMERIC_H_ 2 | #define MYTINYSTL_NUMERIC_H_ 3 | 4 | // 这个头文件包含了 mystl 的数值算法 5 | 6 | #include "iterator.h" 7 | 8 | namespace mystl 9 | { 10 | 11 | /*****************************************************************************************/ 12 | // accumulate 13 | // 版本1:以初值 init 对每个元素进行累加 14 | // 版本2:以初值 init 对每个元素进行二元操作 15 | /*****************************************************************************************/ 16 | // 版本1 17 | template 18 | T accumulate(InputIter first, InputIter last, T init) 19 | { 20 | for (; first != last; ++first) 21 | { 22 | init += *first; 23 | } 24 | return init; 25 | } 26 | 27 | // 版本2 28 | template 29 | T accumulate(InputIter first, InputIter last, T init, BinaryOp binary_op) 30 | { 31 | for (; first != last; ++first) 32 | { 33 | init = binary_op(init, *first); 34 | } 35 | return init; 36 | } 37 | 38 | /*****************************************************************************************/ 39 | // adjacent_difference 40 | // 版本1:计算相邻元素的差值,结果保存到以 result 为起始的区间上 41 | // 版本2:自定义相邻元素的二元操作 42 | /*****************************************************************************************/ 43 | // 版本1 44 | template 45 | OutputIter adjacent_difference(InputIter first, InputIter last, OutputIter result) 46 | { 47 | if (first == last) return result; 48 | *result = *first; // 记录第一个元素 49 | auto value = *first; 50 | while (++first != last) 51 | { 52 | auto tmp = *first; 53 | *++result = tmp - value; 54 | value = tmp; 55 | } 56 | return ++result; 57 | } 58 | 59 | // 版本2 60 | template 61 | OutputIter adjacent_difference(InputIter first, InputIter last, OutputIter result, 62 | BinaryOp binary_op) 63 | { 64 | if (first == last) return result; 65 | *result = *first; // 记录第一个元素 66 | auto value = *first; 67 | while (++first != last) 68 | { 69 | auto tmp = *first; 70 | *++result = binary_op(tmp, value); 71 | value = tmp; 72 | } 73 | return ++result; 74 | } 75 | 76 | /*****************************************************************************************/ 77 | // inner_product 78 | // 版本1:以 init 为初值,计算两个区间的内积 79 | // 版本2:自定义 operator+ 和 operator* 80 | /*****************************************************************************************/ 81 | // 版本1 82 | template 83 | T inner_product(InputIter1 first1, InputIter1 last1, InputIter2 first2, T init) 84 | { 85 | for (; first1 != last1; ++first1, ++first2) 86 | { 87 | init = init + (*first1 * *first2); 88 | } 89 | return init; 90 | } 91 | 92 | // 版本2 93 | template 94 | T inner_product(InputIter1 first1, InputIter1 last1, InputIter2 first2, T init, 95 | BinaryOp1 binary_op1, BinaryOp2 binary_op2) 96 | { 97 | for (; first1 != last1; ++first1, ++first2) 98 | { 99 | init = binary_op1(init, binary_op2(*first1, *first2)); 100 | } 101 | return init; 102 | } 103 | 104 | /*****************************************************************************************/ 105 | // iota 106 | // 填充[first, last),以 value 为初值开始递增 107 | /*****************************************************************************************/ 108 | template 109 | void iota(ForwardIter first, ForwardIter last, T value) 110 | { 111 | while (first != last) 112 | { 113 | *first++ = value; 114 | ++value; 115 | } 116 | } 117 | 118 | /*****************************************************************************************/ 119 | // partial_sum 120 | // 版本1:计算局部累计求和,结果保存到以 result 为起始的区间上 121 | // 版本2:进行局部进行自定义二元操作 122 | /*****************************************************************************************/ 123 | template 124 | OutputIter partial_sum(InputIter first, InputIter last, OutputIter result) 125 | { 126 | if (first == last) return result; 127 | *result = *first; // 记录第一个元素 128 | auto value = *first; 129 | while (++first != last) 130 | { 131 | value = value + *first; 132 | *++result = value; 133 | } 134 | return ++result; 135 | } 136 | 137 | // 版本2 138 | template 139 | OutputIter partial_sum(InputIter first, InputIter last, OutputIter result, 140 | BinaryOp binary_op) 141 | { 142 | if (first == last) return result; 143 | *result = *first; //记录第一个元素 144 | auto value = *first; 145 | while (++first != last) 146 | { 147 | value = binary_op(value, *first); 148 | *++result = value; 149 | } 150 | return ++result; 151 | } 152 | 153 | } // namespace mystl 154 | #endif // !MYTINYSTL_NUMERIC_H_ 155 | 156 | -------------------------------------------------------------------------------- /mylib/mystl/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_MEMORY_H_ 2 | #define MYTINYSTL_MEMORY_H_ 3 | 4 | // 这个头文件负责更高级的动态内存管理 5 | // 包含一些基本函数、空间配置器、未初始化的储存空间管理,以及一个模板类 auto_ptr 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include "algobase.h" 12 | #include "allocator.h" 13 | #include "construct.h" 14 | #include "uninitialized.h" 15 | 16 | namespace mystl 17 | { 18 | 19 | // 获取对象地址 20 | template 21 | constexpr Tp* address_of(Tp& value) noexcept 22 | { 23 | return &value; 24 | } 25 | 26 | // 获取 / 释放 临时缓冲区 27 | 28 | template 29 | pair get_buffer_helper(ptrdiff_t len, T*) 30 | { 31 | if (len > static_cast(INT_MAX / sizeof(T))) 32 | len = INT_MAX / sizeof(T); 33 | while (len > 0) 34 | { 35 | T* tmp = static_cast(malloc(static_cast(len) * sizeof(T))); 36 | if (tmp) 37 | return pair(tmp, len); 38 | len /= 2; // 申请失败时减少 len 的大小 39 | } 40 | return pair(nullptr, 0); 41 | } 42 | 43 | template 44 | pair get_temporary_buffer(ptrdiff_t len) 45 | { 46 | return get_buffer_helper(len, static_cast(0)); 47 | } 48 | 49 | template 50 | pair get_temporary_buffer(ptrdiff_t len, T*) 51 | { 52 | return get_buffer_helper(len, static_cast(0)); 53 | } 54 | 55 | template 56 | void release_temporary_buffer(T* ptr) 57 | { 58 | free(ptr); 59 | } 60 | 61 | // -------------------------------------------------------------------------------------- 62 | // 类模板 : temporary_buffer 63 | // 进行临时缓冲区的申请与释放 64 | template 65 | class temporary_buffer 66 | { 67 | private: 68 | ptrdiff_t original_len; // 缓冲区申请的大小 69 | ptrdiff_t len; // 缓冲区实际的大小 70 | T* buffer; // 指向缓冲区的指针 71 | 72 | public: 73 | // 构造、析构函数 74 | temporary_buffer(ForwardIterator first, ForwardIterator last); 75 | 76 | ~temporary_buffer() 77 | { 78 | mystl::destroy(buffer, buffer + len); 79 | free(buffer); 80 | } 81 | 82 | public: 83 | 84 | ptrdiff_t size() const noexcept { return len; } 85 | ptrdiff_t requested_size() const noexcept { return original_len; } 86 | T* begin() noexcept { return buffer; } 87 | T* end() noexcept { return buffer + len; } 88 | 89 | private: 90 | void allocate_buffer(); 91 | void initialize_buffer(const T&, std::true_type) {} 92 | void initialize_buffer(const T& value, std::false_type) 93 | { mystl::uninitialized_fill_n(buffer, len, value); } 94 | 95 | private: 96 | temporary_buffer(const temporary_buffer&); 97 | void operator=(const temporary_buffer&); 98 | }; 99 | 100 | // 构造函数 101 | template 102 | temporary_buffer:: 103 | temporary_buffer(ForwardIterator first, ForwardIterator last) 104 | { 105 | try 106 | { 107 | len = mystl::distance(first, last); 108 | allocate_buffer(); 109 | if (len > 0) 110 | { 111 | initialize_buffer(*first, std::is_trivially_default_constructible()); 112 | } 113 | } 114 | catch (...) 115 | { 116 | free(buffer); 117 | buffer = nullptr; 118 | len = 0; 119 | } 120 | } 121 | 122 | // allocate_buffer 函数 123 | template 124 | void temporary_buffer::allocate_buffer() 125 | { 126 | original_len = len; 127 | if (len > static_cast(INT_MAX / sizeof(T))) 128 | len = INT_MAX / sizeof(T); 129 | while (len > 0) 130 | { 131 | buffer = static_cast(malloc(len * sizeof(T))); 132 | if (buffer) 133 | break; 134 | len /= 2; // 申请失败时减少申请空间大小 135 | } 136 | } 137 | 138 | // -------------------------------------------------------------------------------------- 139 | // 模板类: auto_ptr 140 | // 一个具有严格对象所有权的小型智能指针 141 | template 142 | class auto_ptr 143 | { 144 | public: 145 | typedef T elem_type; 146 | 147 | private: 148 | T* m_ptr; // 实际指针 149 | 150 | public: 151 | // 构造、复制、析构函数 152 | explicit auto_ptr(T* p = nullptr) :m_ptr(p) {} 153 | auto_ptr(auto_ptr& rhs) :m_ptr(rhs.release()) {} 154 | template 155 | auto_ptr(auto_ptr& rhs) : m_ptr(rhs.release()) {} 156 | 157 | auto_ptr& operator=(auto_ptr& rhs) 158 | { 159 | if (this != &rhs) 160 | { 161 | delete m_ptr; 162 | m_ptr = rhs.release(); 163 | } 164 | return *this; 165 | } 166 | template 167 | auto_ptr& operator=(auto_ptr& rhs) 168 | { 169 | if (this->get() != rhs.get()) 170 | { 171 | delete m_ptr; 172 | m_ptr = rhs.release(); 173 | } 174 | return *this; 175 | } 176 | 177 | ~auto_ptr() { delete m_ptr; } 178 | 179 | public: 180 | // 重载 operator* 和 operator-> 181 | T& operator*() const { return *m_ptr; } 182 | T* operator->() const { return m_ptr; } 183 | 184 | // 获得指针 185 | T* get() const { return m_ptr; } 186 | 187 | // 释放指针 188 | T* release() 189 | { 190 | T* tmp = m_ptr; 191 | m_ptr = nullptr; 192 | return tmp; 193 | } 194 | 195 | // 重置指针 196 | void reset(T* p = nullptr) 197 | { 198 | if (m_ptr != p) 199 | { 200 | delete m_ptr; 201 | m_ptr = p; 202 | } 203 | } 204 | }; 205 | 206 | } // namespace mystl 207 | #endif // !MYTINYSTL_MEMORY_H_ 208 | 209 | -------------------------------------------------------------------------------- /eph/eph_szj.cpp: -------------------------------------------------------------------------------- 1 | #include "eph_szj.h" 2 | #include "eph0.h" 3 | #include "../mylib/tool.h" 4 | #include "../mylib/math_patch.h" 5 | #include "../mylib/mystl/static_array.h" 6 | 7 | double SZJ::L = 0; 8 | double SZJ::fa = 0; 9 | double SZJ::dt = 0; 10 | double SZJ::E = 0.409092614; 11 | 12 | mystl::vector SZJ::rts; //多天的升中降 13 | 14 | static inline double mod2(double a,double b) 15 | { //临界余数(a与最近的整倍数b相差的距离) 16 | 17 | double c=a/b; 18 | c -= floor(c); 19 | if(c>0.5) c-=1; 20 | return c*b; 21 | 22 | } 23 | 24 | double SZJ::getH(double h, double w) 25 | { //h地平纬度,w赤纬,返回时角 26 | double c = (sin(h) - sin(SZJ::fa) * sin(w)) / cos(SZJ::fa) / cos(w); 27 | if (fabs(c) > 1) 28 | return M_PI; 29 | return acos(c); 30 | }; 31 | 32 | void SZJ::Mcoord(double jd, double H0, SJ &r) 33 | { //章动同时影响恒星时和天体坐标,所以不计算章动。返回时角及赤经纬 34 | mystl::array3 z = m_coord((jd + SZJ::dt) / 36525, 40, 30, 8); //低精度月亮赤经纬 35 | z = llrConv(z, SZJ::E); //转为赤道坐标 36 | r.H = rad2rrad(pGST(jd, SZJ::dt) + SZJ::L - z[0]); //得到此刻天体时角 37 | if (H0) 38 | r.H0 = SZJ::getH(0.7275 * cs_rEar / z[2] - 34 * 60 / rad, z[1]); //升起对应的时角 39 | } 40 | 41 | SJ SZJ::Mt(double jd) 42 | { //月亮到中升降时刻计算,传入jd含义与St()函数相同 43 | SZJ::dt = dt_T(jd); 44 | SZJ::E = hcjj(jd / 36525); 45 | jd -= mod2(0.1726222 + 0.966136808032357 * jd - 0.0366 * SZJ::dt + SZJ::L / pi2, 1); //查找最靠近当日中午的月上中天,mod2的第1参数为本地时角近似值 46 | 47 | SJ r = {}; 48 | double sv = pi2 * 0.966; 49 | r.z = r.x = r.s = r.j = r.c = r.h = jd; 50 | SZJ::Mcoord(jd, 1, r); //月亮坐标 51 | r.s += (-r.H0 - r.H) / sv; 52 | r.j += (r.H0 - r.H) / sv; 53 | r.z += (0 - r.H) / sv; 54 | r.x += (M_PI - r.H) / sv; 55 | SZJ::Mcoord(r.s, 1, r); 56 | r.s += rad2rrad(-r.H0 - r.H) / sv; 57 | SZJ::Mcoord(r.j, 1, r); 58 | r.j += rad2rrad(+r.H0 - r.H) / sv; 59 | SZJ::Mcoord(r.z, 0, r); 60 | r.z += rad2rrad(0 - r.H) / sv; 61 | SZJ::Mcoord(r.x, 0, r); 62 | r.x += rad2rrad(M_PI - r.H) / sv; 63 | return r; 64 | } 65 | 66 | void SZJ::Scoord(double jd, int xm, SJ &r) 67 | { //章动同时影响恒星时和天体坐标,所以不计算章动。返回时角及赤经纬 68 | mystl::array3 z = {E_Lon((jd + SZJ::dt) / 36525, 5) + M_PI - 20.5 / rad, 0, 1}; //太阳坐标(修正了光行差) 69 | z = llrConv(z, SZJ::E); //转为赤道坐标 70 | r.H = rad2rrad(pGST(jd, SZJ::dt) + SZJ::L - z[0]); //得到此刻天体时角 71 | 72 | if (xm == 10 || xm == 1) 73 | r.H1 = SZJ::getH(-50 * 60 / rad, z[1]); //地平以下50分 74 | if (xm == 10 || xm == 2) 75 | r.H2 = SZJ::getH(-6 * 3600 / rad, z[1]); //地平以下6度 76 | if (xm == 10 || xm == 3) 77 | r.H3 = SZJ::getH(-12 * 3600 / rad, z[1]); //地平以下12度 78 | if (xm == 10 || xm == 4) 79 | r.H4 = SZJ::getH(-18 * 3600 / rad, z[1]); //地平以下18度 80 | } 81 | 82 | SJ SZJ::St(double jd) 83 | { //太阳到中升降时刻计算,传入jd是当地中午12点时间对应的2000年首起算的格林尼治时间UT 84 | SZJ::dt = dt_T(jd); 85 | SZJ::E = hcjj(jd / 36525); 86 | jd -= mod2(jd + SZJ::L / pi2, 1); //查找最靠近当日中午的日上中天,mod2的第1参数为本地时角近似值 87 | 88 | SJ r = {}; 89 | double sv = pi2; 90 | r.z = r.x = r.s = r.j = r.c = r.h = r.c2 = r.h2 = r.c3 = r.h3 = jd; 91 | r.sm = ""; 92 | SZJ::Scoord(jd, 10, r); //太阳坐标 93 | r.s += (-r.H1 - r.H) / sv; //升起 94 | r.j += (r.H1 - r.H) / sv; //降落 95 | 96 | r.c += (-r.H2 - r.H) / sv; //民用晨 97 | r.h += (r.H2 - r.H) / sv; //民用昏 98 | r.c2 += (-r.H3 - r.H) / sv; //航海晨 99 | r.h2 += (r.H3 - r.H) / sv; //航海昏 100 | r.c3 += (-r.H4 - r.H) / sv; //天文晨 101 | r.h3 += (r.H4 - r.H) / sv; //天文昏 102 | 103 | r.z += (0 - r.H) / sv; //中天 104 | r.x += (M_PI - r.H) / sv; //下中天 105 | SZJ::Scoord(r.s, 1, r); 106 | r.s += rad2rrad(-r.H1 - r.H) / sv; 107 | if (r.H1 == M_PI) 108 | r.sm += "无升起."; 109 | SZJ::Scoord(r.j, 1, r); 110 | r.j += rad2rrad(+r.H1 - r.H) / sv; 111 | if (r.H1 == M_PI) 112 | r.sm += "无降落."; 113 | 114 | SZJ::Scoord(r.c, 2, r); 115 | r.c += rad2rrad(-r.H2 - r.H) / sv; 116 | if (r.H2 == M_PI) 117 | r.sm += "无民用晨."; 118 | SZJ::Scoord(r.h, 2, r); 119 | r.h += rad2rrad(+r.H2 - r.H) / sv; 120 | if (r.H2 == M_PI) 121 | r.sm += "无民用昏."; 122 | SZJ::Scoord(r.c2, 3, r); 123 | r.c2 += rad2rrad(-r.H3 - r.H) / sv; 124 | if (r.H3 == M_PI) 125 | r.sm += "无航海晨."; 126 | SZJ::Scoord(r.h2, 3, r); 127 | r.h2 += rad2rrad(+r.H3 - r.H) / sv; 128 | if (r.H3 == M_PI) 129 | r.sm += "无航海昏."; 130 | SZJ::Scoord(r.c3, 4, r); 131 | r.c3 += rad2rrad(-r.H4 - r.H) / sv; 132 | if (r.H4 == M_PI) 133 | r.sm += "无天文晨."; 134 | SZJ::Scoord(r.h3, 4, r); 135 | r.h3 += rad2rrad(+r.H4 - r.H) / sv; 136 | if (r.H4 == M_PI) 137 | r.sm += "无天文昏."; 138 | 139 | SZJ::Scoord(r.z, 0, r); 140 | r.z += (0 - r.H) / sv; 141 | SZJ::Scoord(r.x, 0, r); 142 | r.x += rad2rrad(M_PI - r.H) / sv; 143 | return r; 144 | } 145 | 146 | void SZJ::calcRTS(double jd, int n, double Jdl, double Wdl, double sq) 147 | { //多天升中降计算,jd是当地起始略日(中午时刻),sq是时区 148 | int i, c; 149 | SJ_S rr; 150 | SJ r; 151 | if (!SZJ::rts.size()) 152 | { 153 | for (i = 0; i < 31; i++) 154 | SZJ::rts.push_back({}); 155 | } 156 | SZJ::L = Jdl, SZJ::fa = Wdl, sq /= 24; //设置站点参数 157 | for (i = 0; i < n; i++) 158 | { 159 | rr = SZJ::rts[i]; 160 | rr.Ms = rr.Mz = rr.Mj = "--:--:--"; 161 | } 162 | for (i = -1; i <= n; i++) 163 | { 164 | if (i >= 0 && i < n) 165 | { //太阳 166 | r = SZJ::St(jd + i + sq); 167 | SZJ::rts[i].s = timeStr(r.s - sq); //升 168 | SZJ::rts[i].z = timeStr(r.z - sq); //中 169 | SZJ::rts[i].j = timeStr(r.j - sq); //降 170 | SZJ::rts[i].c = timeStr(r.c - sq); //晨 171 | SZJ::rts[i].h = timeStr(r.h - sq); //昏 172 | SZJ::rts[i].ch = timeStr(r.h - r.c - 0.5); //光照时间,timeStr()内部+0.5,所以这里补上-0.5 173 | SZJ::rts[i].sj = timeStr(r.j - r.s - 0.5); //昼长 174 | } 175 | r = SZJ::Mt(jd + i + sq); //月亮 176 | c = int2(r.s - sq + 0.5) - jd; 177 | if (c >= 0 && c < n) 178 | SZJ::rts[c].Ms = timeStr(r.s - sq); 179 | c = int2(r.z - sq + 0.5) - jd; 180 | if (c >= 0 && c < n) 181 | SZJ::rts[c].Mz = timeStr(r.z - sq); 182 | c = int2(r.j - sq + 0.5) - jd; 183 | if (c >= 0 && c < n) 184 | SZJ::rts[c].Mj = timeStr(r.j - sq); 185 | } 186 | } -------------------------------------------------------------------------------- /mylib/tool.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mystl/my_string.h" 5 | #include "tool.h" 6 | #include "math_patch.h" 7 | 8 | //实现字符替换 9 | void string_replace( mystl::string &strBig, const mystl::string &strsrc, const mystl::string &strdst) 10 | { 11 | mystl::string::size_type pos = 0; 12 | mystl::string::size_type srclen = strsrc.size(); 13 | mystl::string::size_type dstlen = strdst.size(); 14 | 15 | while( (pos=strBig.find(strsrc, pos)) != mystl::string::npos ) 16 | { 17 | strBig.replace( pos, srclen, strdst ); 18 | pos += dstlen; 19 | } 20 | } 21 | 22 | //提取jd中的时间(去除日期) 23 | mystl::string timeStr(double jd) 24 | { 25 | int h, m, s; 26 | jd += 0.5; 27 | jd = (jd - int2(jd)); 28 | s = int2(jd * 86400 + 0.5); 29 | h = int2(s / 3600.0); 30 | s -= h * 3600; 31 | m = int2(s / 60.0); 32 | s -= m * 60; 33 | mystl::string H, M, S; 34 | H = "0" + to_str(h); 35 | M = "0" + to_str(m); 36 | S = "0" + to_str(s); 37 | return H.substr(H.length() - 2, 2) + ":" + M.substr(M.length() - 2, 2) + ":" + S.substr(S.length() - 2, 2); 38 | } 39 | 40 | //===============角度格式化================== 41 | mystl::string rad2strE(double d, bool flag, int ext) 42 | { 43 | //将弧度转为字串,ext为小数保留位数 44 | //flag=0输出格式示例: -23°59" 48.23" 45 | //flag=1输出格式示例: 18h 29m 44.52s 46 | mystl::string s = " ", w1 = "°", w2 = "\'", w3 = "\""; 47 | if (d < 0) 48 | d = -d, s = "-"; 49 | if (flag) 50 | { 51 | d *= 12 / M_PI; 52 | w1 = "h", w2 = "m", w3 = "s"; 53 | } 54 | else 55 | d *= 180 / M_PI; 56 | int a = floor(d); 57 | d = (d - a) * 60; 58 | int b = floor(d); 59 | d = (d - b) * 60; 60 | int c = floor(d); 61 | 62 | double Q = pow(10, ext); 63 | 64 | d = floor((d - c) * Q + 0.5); 65 | if (d >= Q) 66 | d -= Q, c++; 67 | if (c >= 60) 68 | c -= 60, b++; 69 | if (b >= 60) 70 | b -= 60, a++; 71 | 72 | mystl::string A, B, C, D; 73 | A = " " + to_str(a); 74 | B = "0" + to_str(b); 75 | C = "0" + to_str(c); 76 | D = "00000" + to_str((int)d); 77 | s += A.substr(A.length() - 3, 3) + w1; 78 | s += B.substr(B.length() - 2, 2) + w2; 79 | s += C.substr(C.length() - 2, 2); 80 | if (ext) 81 | s += "." + D.substr(D.length() - ext, ext) + w3; 82 | return s; 83 | } 84 | 85 | //将弧度转为字串,保留2位 86 | mystl::string rad2str(double d, bool tim) 87 | { 88 | return rad2strE(d, tim, 2); 89 | } 90 | 91 | //将弧度转为字串,精确到分 92 | mystl::string rad2str2(double d) 93 | { 94 | //输出格式示例: -23°59" 95 | mystl::string s = "+", w1 = "°", w2 = "\'", w3 = "\""; 96 | if (d < 0) 97 | d = -d, s = "-"; 98 | d *= 180 / M_PI; 99 | int a = floor(d); 100 | int b = floor((d - a) * 60 + 0.5); 101 | if (b >= 60) 102 | b -= 60, a++; 103 | mystl::string A = " " + to_str(a), B = "0" + to_str(b); 104 | s += A.substr(A.length() - 3, 3) + w1; 105 | s += B.substr(B.length() - 2, 2) + w2; 106 | return s; 107 | } 108 | 109 | //秒转为分秒,fx为小数点位数,fs为1转为"分秒"格式否则转为"角度分秒"格式 110 | mystl::string m2fm(double v, int fx, int fs) 111 | { 112 | mystl::string gn = ""; 113 | if (v < 0) 114 | v = -v, gn = "-"; 115 | int f = floor(v / 60); 116 | double m = v - f * 60; 117 | if (!fs) 118 | return gn + to_str(f) + "\'" + to_str(m, fx) + "\""; 119 | if (fs == 1) 120 | return gn + to_str(f) + "分" + to_str(m, fx) + "秒"; 121 | if (fs == 2) 122 | return gn + to_str(f) + "m" + to_str(m, fx) + "s"; 123 | else 124 | return "error"; 125 | } 126 | 127 | //公历转儒略日 128 | double toJD(Date date) 129 | { 130 | double y = date.Y, m = date.M, n = 0; //取出年月 131 | if (m <= 2) 132 | m += 12, y--; 133 | if (date.Y * 372 + date.M * 31 + date.D >= 588829) 134 | //判断是否为格里高利历日1582*372+10*31+15 135 | n = (int) (y / 100), n = 2 - n + (int) (n / 4); //加百年闰 136 | n += (int) (365.25 * (y + 4716) + 0.01); //加上年引起的偏移日数 137 | n += (int) (30.6 * (m + 1)) + date.D; //加上月引起的偏移日数及日偏移数 138 | n += ((date.s / 60.0 + date.m) / 60.0 + date.h) / 24.0 - 1524.5; 139 | return n; 140 | } 141 | 142 | //儒略日数转公历 143 | Date setFromJD(double jd) 144 | { 145 | Date r = { 0 }; 146 | int D = int2(jd + 0.5), c; 147 | double F = jd + 0.5 - D; //取得日数的整数部份A及小数部分F 148 | if (D >= 2299161) 149 | c = int2((D - 1867216.25) / 36524.25), D += 1 + c - int2(c / 4.0); 150 | D += 1524; 151 | r.Y = int2((D - 122.1) / 365.25); //年数 152 | D -= int2(365.25 * r.Y); 153 | r.M = int2(D / 30.601); //月数 154 | D -= int2(30.601 * r.M); 155 | r.D = D; //日数 156 | if (r.M > 13) 157 | r.M -= 13, r.Y -= 4715; 158 | else 159 | r.M -= 1, r.Y -= 4716; 160 | 161 | //日的小数转为时分秒 162 | F *= 24.0; 163 | r.h = int2(F); 164 | F -= r.h; 165 | F *= 60.0; 166 | r.m = int2(F); 167 | F -= r.m; 168 | F *= 60.0; 169 | r.s = F; 170 | return r; 171 | } 172 | 173 | // 日期对象转为字符串 174 | mystl::string DD2str(Date r) 175 | { 176 | mystl::string 177 | Y = " " + to_str(r.Y), 178 | M = "0" + to_str(r.M), 179 | D = "0" + to_str(r.D); 180 | 181 | int h = r.h, m = r.m, s = int2(r.s + .5); 182 | if (s >= 60) 183 | s -= 60, m++; 184 | if (m >= 60) 185 | m -= 60, h++; 186 | 187 | mystl::string _h, _m, _s; 188 | _h = "0" + to_str(h); 189 | _m = "0" + to_str(m); 190 | _s = "0" + to_str(s); 191 | Y = Y.substr(Y.length() - 5, 5); 192 | M = M.substr(M.length() - 2, 2); 193 | D = D.substr(D.length() - 2, 2); 194 | _h = _h.substr(_h.length() - 2, 2); 195 | _m = _m.substr(_m.length() - 2, 2); 196 | _s = _s.substr(_s.length() - 2, 2); 197 | 198 | return Y + "-" + M + "-" + D + " " + _h + ":" + _m + ":" + _s; 199 | } 200 | 201 | // JD转为字符串 202 | mystl::string JD2str(double jd) 203 | { 204 | Date r=setFromJD(jd); 205 | return DD2str(r); 206 | } 207 | 208 | mystl::string fill_str(mystl::string s, int n, mystl::string c) { 209 | int len=s.length(); 210 | for(int i=0;i"< 4 | #include 5 | #include 6 | #include 7 | #include "itoa.h" 8 | #include "dtoa.h" 9 | 10 | namespace mystl 11 | { 12 | using string = std::string; 13 | } // namespace mystl 14 | 15 | 16 | // 萃取float类型 17 | template struct is_float {const static bool value = false;}; 18 | template <> struct is_float {const static bool value = true;}; 19 | template <> struct is_float {const static bool value = true;}; 20 | template <> struct is_float {const static bool value = true;}; 21 | 22 | inline int my_stoi(const mystl::string &str, typename mystl::string::size_type *pos = nullptr, int base = 10) 23 | { 24 | typename mystl::string::value_type *end; 25 | int answer = ::strtol(str.data(), &end, base); 26 | if (end == str.data()) 27 | { 28 | throw std::invalid_argument("invalid stof argument"); 29 | } 30 | 31 | if (errno == ERANGE) 32 | { 33 | throw std::out_of_range("stof argument out of range"); 34 | } 35 | 36 | if (pos) 37 | { 38 | *pos = end - str.data(); 39 | } 40 | 41 | return answer; 42 | } 43 | 44 | inline long stol(const mystl::string &str, typename mystl::string::size_type *pos = nullptr, int base = 10) 45 | { 46 | typename mystl::string::value_type *end; 47 | long answer = ::strtol(str.data(), &end, base); 48 | if (end == str.data()) 49 | { 50 | throw std::invalid_argument("invalid stof argument"); 51 | } 52 | 53 | if (errno == ERANGE) 54 | { 55 | throw std::out_of_range("stof argument out of range"); 56 | } 57 | 58 | if (pos) 59 | { 60 | *pos = end - str.data(); 61 | } 62 | 63 | return answer; 64 | } 65 | 66 | inline long long stoll(const mystl::string &str, typename mystl::string::size_type *pos = nullptr, int base = 10) 67 | { 68 | typename mystl::string::value_type *end; 69 | long long answer = ::strtoll(str.data(), &end, base); 70 | if (end == str.data()) 71 | { 72 | throw std::invalid_argument("invalid stof argument"); 73 | } 74 | 75 | if (errno == ERANGE) 76 | { 77 | throw std::out_of_range("stof argument out of range"); 78 | } 79 | 80 | if (pos) 81 | { 82 | *pos = end - str.data(); 83 | } 84 | 85 | return answer; 86 | } 87 | 88 | inline unsigned long stoul(const mystl::string &str, typename mystl::string::size_type *pos = nullptr, int base = 10) 89 | { 90 | typename mystl::string::value_type *end; 91 | unsigned long answer = ::strtoul(str.data(), &end, base); 92 | if (end == str.data()) 93 | { 94 | throw std::invalid_argument("invalid stof argument"); 95 | } 96 | 97 | if (errno == ERANGE) 98 | { 99 | throw std::out_of_range("stof argument out of range"); 100 | } 101 | 102 | if (pos) 103 | { 104 | *pos = end - str.data(); 105 | } 106 | 107 | return answer; 108 | } 109 | 110 | inline unsigned long long stoull(const mystl::string &str, typename mystl::string::size_type *pos = nullptr, int base = 10) 111 | { 112 | typename mystl::string::value_type *end; 113 | unsigned long long answer = ::strtoull(str.data(), &end, base); 114 | if (end == str.data()) 115 | { 116 | throw std::invalid_argument("invalid stof argument"); 117 | } 118 | 119 | if (errno == ERANGE) 120 | { 121 | throw std::out_of_range("stof argument out of range"); 122 | } 123 | 124 | if (pos) 125 | { 126 | *pos = end - str.data(); 127 | } 128 | 129 | return answer; 130 | } 131 | 132 | inline float stof(const mystl::string &str, typename mystl::string::size_type *pos = nullptr) 133 | { 134 | typename mystl::string::value_type *end; 135 | float answer = ::strtof(str.data(), &end); 136 | 137 | if (end == str.data()) 138 | { 139 | throw std::invalid_argument("invalid stof argument"); 140 | } 141 | 142 | if (errno == ERANGE) 143 | { 144 | throw std::out_of_range("stof argument out of range"); 145 | } 146 | 147 | if (pos) 148 | { 149 | *pos = end - str.data(); 150 | } 151 | 152 | return answer; 153 | } 154 | 155 | inline double stod(const mystl::string &str, typename mystl::string::size_type *pos = nullptr) 156 | { 157 | typename mystl::string::value_type *end; 158 | double answer = ::strtod(str.data(), &end); 159 | if (end == str.data()) 160 | { 161 | throw std::invalid_argument("invalid stof argument"); 162 | } 163 | 164 | if (errno == ERANGE) 165 | { 166 | throw std::out_of_range("stof argument out of range"); 167 | } 168 | 169 | if (pos) 170 | { 171 | *pos = end - str.data(); 172 | } 173 | 174 | return answer; 175 | } 176 | 177 | inline long double stold(const mystl::string &str, typename mystl::string::size_type *pos = nullptr) 178 | { 179 | typename mystl::string::value_type *end; 180 | long double answer = ::strtold(str.data(), &end); 181 | if (end == str.data()) 182 | { 183 | throw std::invalid_argument("invalid stof argument"); 184 | } 185 | 186 | if (errno == ERANGE) 187 | { 188 | throw std::out_of_range("stof argument out of range"); 189 | } 190 | 191 | if (pos) 192 | { 193 | *pos = end - str.data(); 194 | } 195 | 196 | return answer; 197 | } 198 | 199 | // length 总长度,是否右对齐 200 | template 201 | inline mystl::string to_str(T value, int precision = 4, int length = 0, bool right_align = false) 202 | { 203 | // digits10 returns floor value, so add 1 for remainder, 1 for - and 1 for null terminator 204 | char str[32] = {}; 205 | 206 | if ( is_float::value) { // 浮点数 207 | dtoa_milo2(value, str, precision, true); 208 | } else { // 整数 209 | jeaiii::to_text_from_integer(str, value); 210 | } 211 | 212 | char fill[64] = ""; 213 | size_t s_len = strlen(str); 214 | if (length > s_len) { 215 | size_t len = length - s_len; 216 | for (size_t i = 0; i < len; i++) { 217 | fill[i] = ' '; 218 | } 219 | } 220 | return right_align ? 221 | (mystl::string(fill) + mystl::string(str)) : (mystl::string(str) + mystl::string(fill)); 222 | } 223 | 224 | #endif // !MY_STRING_H -------------------------------------------------------------------------------- /lunar/lunar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lunar.h" 3 | #include "lunar_ssq.h" 4 | #include "../eph/eph0.h" 5 | #include "../mylib/tool.h" 6 | #include "../mylib/math_patch.h" 7 | 8 | void init_ob() 9 | { 10 | OBA::init(); 11 | OBB::init(); 12 | SSQ::init(); 13 | } 14 | 15 | /*返回公历某一个月的'公农回'三合历*/ 16 | OB_LUN yueLiCalc(int By, int Bm) 17 | { 18 | int i, j, k, D, Bd0, Bdn, xn; 19 | //日历物件初始化 20 | Date JD = { By, Bm, 1, 12, 0, 0.1 }; 21 | Bd0 = int2(toJD(JD)) - J2000; //公历月首,中午 22 | JD.M++; 23 | if (JD.M > 12) 24 | JD.Y++, JD.M = 1; 25 | Bdn = int2(toJD(JD)) - J2000 - Bd0; //本月天数(公历) 26 | 27 | OB_LUN lun; 28 | lun.w0 = (Bd0 + J2000 + 1 + 7000000) % 7; //本月第一天的星期 29 | lun.y = By; //公历年份 30 | lun.m = Bm; //公历月分 31 | lun.d0 = Bd0; 32 | lun.dn = Bdn; 33 | 34 | lun.nianhao = OBB::getNH(By); 35 | 36 | double w; 37 | OB_DAY *ob,*ob2; 38 | //提取各日信息 39 | for (i = 0, j = 0; i < Bdn; i++) 40 | { 41 | ob = &lun.day[i]; 42 | ob->d0 = Bd0 + i; //儒略日,北京时12:00 43 | ob->di = i; //公历月内日序数 44 | ob->y = By; //公历年 45 | ob->m = Bm; //公历月 46 | ob->dn = Bdn; //公历月天数 47 | ob->week0 = lun.w0; //月首的星期 48 | ob->week = (lun.w0 + i) % 7; //当前日的星期 49 | ob->weeki = int2((lun.w0 + i) / 7); //本日所在的周序号 50 | ob->weekN = int2((lun.w0 + Bdn - 1) / 7) + 1; //本月的总周数 51 | JD = setFromJD(ob->d0 + J2000); 52 | ob->d = JD.D; //公历日名称 53 | 54 | //农历月历 55 | if (!SSQ::ZQ[0] || ob->d0 < SSQ::ZQ[0] || ob->d0 >= SSQ::ZQ[24]) //如果d0已在计算农历范围内则不再计算 56 | SSQ::calcY(ob->d0); 57 | int mk = int2((ob->d0 - SSQ::HS[0]) / 30.0); 58 | if (mk < 13 && SSQ::HS[mk + 1] <= ob->d0) 59 | mk++; //农历所在月的序数 60 | 61 | ob->Ldi = ob->d0 - SSQ::HS[mk]; //距农历月首的编移量,0对应初一 62 | ob->Ldc=str_rmc[ob->Ldi]; //农历日名称 63 | 64 | ob->cur_dz = ob->d0 - SSQ::ZQ[0]; // 距冬至的天数 65 | ob->cur_xz = ob->d0 - SSQ::ZQ[12]; // 距夏至的天数 66 | ob->cur_lq = ob->d0 - SSQ::ZQ[15]; // 距立秋的天数 67 | ob->cur_mz = ob->d0 - SSQ::ZQ[11]; // 距芒种的天数 68 | ob->cur_xs = ob->d0 - SSQ::ZQ[13]; // 距小暑的天数 69 | 70 | if (ob->d0 == SSQ::HS[mk] || ob->d0 == Bd0) 71 | { //月的信息 72 | ob->Lmc = SSQ::ym[mk]; //月名称 73 | ob->Ldn = SSQ::dx[mk]; //月大小 74 | ob->Lleap = (SSQ::leap && SSQ::leap == mk) ? "闰" : ""; //闰状况 75 | ob->Lmc2= mk<13?SSQ::ym[mk+1]:"未知"; 76 | } 77 | else 78 | { 79 | ob2 = &lun.day[i - 1]; 80 | ob->Lmc = ob2->Lmc; 81 | ob->Ldn = ob2->Ldn; 82 | ob->Lleap = ob2->Lleap; 83 | ob->Lmc2=ob2->Lmc2; 84 | } 85 | int qk = int2((ob->d0 - SSQ::ZQ[0] - 7) / 15.2184); 86 | if (qk < 23 && ob->d0 >= SSQ::ZQ[qk + 1]) 87 | qk++; //节气的取值范围是0-23 88 | if (ob->d0 == SSQ::ZQ[qk]) 89 | ob->Ljq =str_jqmc[qk]; 90 | else 91 | ob->Ljq = ""; 92 | 93 | ob->yxmc = ""; //月相名称,月相时刻(儒略日),月相时间串 94 | ob->jqmc = ""; //定气名称,节气时刻(儒略日),节气时间串 95 | 96 | //干支纪年处理 97 | //以立春为界定年首 98 | D = SSQ::ZQ[3] + (ob->d0 < SSQ::ZQ[3] ? -365 : 0) + 365.25 * 16 - 35; //以立春为界定纪年 99 | ob->Lyear = floor(D / 365.2422 + 0.5); //农历纪年(10进制,1984年起算) 100 | //以下几行以正月初一定年首 101 | D = SSQ::HS[2]; //一般第3个月为春节 102 | for (j = 0; j < 14; j++) 103 | { //找春节 104 | if (SSQ::ym[j] != "正" || SSQ::leap == j && j) 105 | continue; 106 | D = SSQ::HS[j]; 107 | if (ob->d0 < D) 108 | { 109 | D -= 365; 110 | break; 111 | }//无需再找下一个正月 112 | } 113 | D = D + 5810;//计算该年春节与1984年平均春节(立春附近)相差天数估计 114 | ob->Lyear0 = floor(D / 365.2422 + 0.5); 115 | D = ob->Lyear + 12000; 116 | ob->Lyear2 = str_gan[D % 10]; 117 | ob->Lyear2 += str_zhi[D % 12];//干支纪年(立春) 118 | D = ob->Lyear0 + 12000; 119 | ob->Lyear3 = str_gan[D % 10]; 120 | ob->Lyear3 += str_zhi[D % 12];//干支纪年(正月) 121 | ob->Lyear4 = ob->Lyear0 + 1984 + 2698;//黄帝纪年 122 | 123 | //纪月处理,1998年12月7(大雪)开始连续进行节气计数,0为甲子 124 | mk = int2((ob->d0 - SSQ::ZQ[0]) / 30.43685); 125 | if (mk < 12 && ob->d0 >= SSQ::ZQ[2 * mk + 1]) 126 | mk++;//相对大雪的月数计算,mk的取值范围0-12 127 | 128 | D = mk + int2((SSQ::ZQ[12] + 390) / 365.2422) * 12 + 900000; //相对于1998年12月7(大雪)的月数,900000为正数基数 129 | ob->Lmonth = D % 12; 130 | ob->Lmonth2 = str_gan[D % 10]; 131 | ob->Lmonth2 += str_zhi[D % 12]; 132 | 133 | //纪日,2000年1月7日起算 134 | D = ob->d0 - 6 + 9000000; 135 | ob->Lday2 = str_gan[D % 10]; 136 | ob->Lday2 += str_zhi[D % 12]; 137 | 138 | //星座 139 | mk = int2((ob->d0 - SSQ::ZQ[0] - 15) / 30.43685); 140 | if (mk < 11 && ob->d0 >= SSQ::ZQ[2 * mk + 2]) 141 | mk++; //星座所在月的序数,(如果j=13,ob->d0不会超过第14号中气) 142 | ob->XiZ = str_xz[(mk + 12) % 12]; 143 | 144 | 145 | //回历 146 | OBA::getHuiLi(ob->d0,*ob); 147 | //节日 148 | OBA::getDayName(*ob); //公历 149 | OBB::getDayName2(*ob); //农历 150 | } 151 | 152 | //以下是月相与节气的处理 153 | double d, jd2 = Bd0 + dt_T(Bd0) - 8 / 24.0; 154 | //月相查找 155 | w = MS_aLon(jd2 / 36525, 10, 3); 156 | w = int2((w - 0.78) / M_PI * 2) * M_PI / 2; 157 | do 158 | { 159 | d = OBB::so_accurate(w); 160 | D = int2(d + 0.5); 161 | xn = int2(w / pi2 * 4 + 4000000.01) % 4; 162 | w += pi2 / 4; 163 | if (D >= Bd0 + Bdn) 164 | break; 165 | if (D < Bd0) 166 | continue; 167 | ob = &lun.day[D - Bd0]; 168 | ob->yxmc = str_yxmc[xn]; //取得月相名称 169 | ob->yxjd = d; 170 | ob->yxsj = timeStr(d); 171 | } 172 | while (D + 5 < Bd0 + Bdn); 173 | 174 | //节气查找 175 | w = S_aLon(jd2 / 36525, 3); 176 | w = int2((w - 0.13) / pi2 * 24) * pi2 / 24; 177 | do 178 | { 179 | d = OBB::qi_accurate(w); 180 | D = int2(d + 0.5); 181 | xn = int2(w / pi2 * 24 + 24000006.01) % 24; 182 | w += pi2 / 24.0; 183 | if (D >= Bd0 + Bdn) 184 | break; 185 | if (D < Bd0) 186 | continue; 187 | ob = &lun.day[D - Bd0]; 188 | ob->jqmc = str_jqmc[xn]; //取得节气名称 189 | ob->jqjd = d; 190 | ob->jqsj = timeStr(d); 191 | } 192 | while (D + 12 < Bd0 + Bdn); 193 | return lun; 194 | } 195 | 196 | mystl::string nianLiSTR(int y) 197 | { //字符串年历生成 198 | int i,j; 199 | mystl::string s="", s1,s2; 200 | double v,qi; 201 | SSQ::calcY( int2((y-2000.0)*365.2422+180) ); 202 | for(i=0;i<14;i++) 203 | { 204 | if(SSQ::HS[i+1]>SSQ::ZQ[24]) break; //已包含下一年的冬至 205 | if(SSQ::leap && i==SSQ::leap) s1="闰"; else s1=" "; 206 | s1 += SSQ::ym[i]; 207 | if(s1.length()<6 || (s1.length()<9&&(SSQ::leap && i==SSQ::leap))) 208 | s1 += "月"; 209 | s1 += SSQ::dx[i]>29?"大":"小"; 210 | s1 += " "+JD2str(SSQ::HS[i]+J2000).substr(6,5); 211 | 212 | v = OBB::so_accurate2(SSQ::HS[i]); 213 | s2 = "("+ JD2str(v+J2000).substr(9,11)+")"; 214 | if(int2(v+0.5)!=SSQ::HS[i]) s2 = "\033[31m"+s2+"\033[0m"; 215 | // s2+="\n"; 216 | //v=(v+0.5+J2000)%1; if(v>0.5) v=1-v; if(v<8/1440) s2 = ""+s2+""; //对靠近0点的加注 217 | s1 += s2; 218 | 219 | for(j=-2;j<24;j++) 220 | { 221 | if(j>=0) qi=SSQ::ZQ[j]; 222 | if(j==-1) qi=SSQ::pe[0]; 223 | if(j==-2) qi=SSQ::pe[1]; 224 | 225 | if(qi=SSQ::HS[i+1]) continue; 226 | s1 += " "; 227 | s1 += str_jqmc[(j+24)%24]+JD2str(qi+J2000).substr(6,5); 228 | 229 | v = OBB::qi_accurate2(qi); 230 | s2 = "("+ JD2str(v+J2000).substr(9,11)+")"; 231 | if(int2(v+0.5)!=qi) s2 = "\033[31m"+s2+"\033[0m"; 232 | //v=(v+0.5+J2000)%1; if(v>0.5) v=1-v; if(v<8/1440) s2 = ""+s2+""; //对靠近0点的加注 233 | s1 += s2; 234 | } 235 | s += s1 + "\n"; 236 | } 237 | return s; 238 | } 239 | 240 | /* 241 | main() 242 | { 243 | init_ob(); 244 | // OB_LUN lun=yueLiCalc(2008,1); 245 | std::cout< _pi) 90 | MSC::mShiJ -= pi2; 91 | 92 | //修正了视差的赤道坐标 93 | z = parallax(z, MSC::mShiJ, fa, high); //视差修正 94 | MSC::mCJ2 = z[0], MSC::mCW2 = z[1], MSC::mR2 = z[2]; 95 | 96 | //月亮时角坐标 97 | z[0] += _pi / 2 - MSC::gst - L; //转到相对于地平赤道分点的赤道坐标(时角坐标) 98 | 99 | //月亮地平坐标 100 | z = llrConv(z, _pi / 2 - fa); //转到地平坐标(只改经纬度) 101 | z[0] = rad2mrad(-_pi / 2 - z[0]); 102 | MSC::mDJ = z[0]; 103 | MSC::mDW = z[1]; //方位角,高度角 104 | if (z[1] > 0) 105 | z[1] += MQC(z[1]); //大气折射修正 106 | MSC::mPJ = z[0]; 107 | MSC::mPW = z[1]; //方位角,高度角 108 | 109 | //=======太阳======== 110 | //太阳黄道坐标 111 | z = e_coord(T, -1, -1, -1); //地球坐标 112 | z[0] = rad2mrad(z[0] + _pi + gxc_sunLon(T) + MSC::dL); //补上太阳光行差及章动 113 | z[1] = -z[1] + gxc_sunLat(T); //z数组为太阳地心黄道视坐标 114 | MSC::sHJ = z[0]; 115 | MSC::sHW = z[1]; 116 | MSC::sR = z[2]; //太阳视黄经,视黄纬,日地质心距 117 | 118 | //太阳赤道坐标 119 | z = llrConv(z, MSC::E); //转为赤道坐标 120 | MSC::sCJ = z[0]; 121 | MSC::sCW = z[1]; //太阳视赤经,视赤纬 122 | 123 | //太阳时角计算 124 | MSC::sShiJ = rad2mrad(MSC::gst + L - z[0]); //得到此刻天体时角 125 | if (MSC::sShiJ > _pi) 126 | MSC::sShiJ -= pi2; 127 | 128 | //修正了视差的赤道坐标 129 | z = parallax(z, MSC::sShiJ, fa, high); //视差修正 130 | MSC::sCJ2 = z[0], MSC::sCW2 = z[1], MSC::sR2 = z[2]; 131 | 132 | //太阳时角坐标 133 | z[0] += _pi / 2 - MSC::gst - L; //转到相对于地平赤道分点的赤道坐标 134 | 135 | //太阳地平坐标 136 | z = llrConv(z, _pi / 2 - fa); 137 | z[0] = rad2mrad(-_pi / 2 - z[0]); 138 | //z[1] -= 8.794/rad/z[2]*cos(z[1]); //直接在地平坐标中视差修正(这里把地球看为球形,精度比parallax()稍差一些) 139 | MSC::sDJ = z[0]; 140 | MSC::sDW = z[1]; //方位角,高度角 141 | 142 | if (z[1] > 0) 143 | z[1] += MQC(z[1]); //大气折射修正 144 | MSC::sPJ = z[0]; 145 | MSC::sPW = z[1]; //方位角,高度角 146 | 147 | //=======其它======== 148 | //时差计算 149 | double t = T / 10, t2 = t * t, t3 = t2 * t, t4 = t3 * t, t5 = t4 * t; 150 | double Lon = (1753470142 + 6283319653318 * t + 529674 * t2 + 432 * t3 - 1124 * t4 - 9 * t5) / 1000000000 + _pi - 20.5 / rad; //修正了光行差的太阳平黄经 151 | Lon = rad2mrad(Lon - (MSC::sCJ - MSC::dL * cos(MSC::E))); //(修正了光行差的平黄经)-(不含dL*cos(E)的视赤经) 152 | if (Lon > _pi) 153 | Lon -= pi2; //得到时差,单位是弧度 154 | MSC::sc = Lon / pi2; //时差(单位:日) 155 | 156 | //真太阳与平太阳 157 | MSC::pty = MSC::jd + L / pi2; //平太阳时 158 | MSC::zty = MSC::jd + L / pi2 + MSC::sc; //真太阳时 159 | 160 | //视半径 161 | // MSC::mRad = moonRad(MSC::mR,MSC::mDW); //月亮视半径(角秒) 162 | MSC::mRad = cs_sMoon / MSC::mR2; //月亮视半径(角秒) 163 | MSC::sRad = 959.63 / MSC::sR2; //太阳视半径(角秒) 164 | MSC::e_mRad = cs_sMoon / MSC::mR; //月亮地心视半径(角秒) 165 | MSC::eShadow = (cs_rEarA / MSC::mR * rad - (959.63 - 8.794) / MSC::sR) * 51 / 50; //地本影在月球向径处的半径(角秒),式中51/50是大气厚度补偿 166 | MSC::eShadow2 = (cs_rEarA / MSC::mR * rad + (959.63 + 8.794) / MSC::sR) * 51 / 50; //地半影在月球向径处的半径(角秒),式中51/50是大气厚度补偿 167 | MSC::mIll = moonIll(T); //月亮被照面比例 168 | 169 | //中心食计算 170 | if (fabs(rad2rrad(MSC::mCJ - MSC::sCJ)) < 50.0 / 180.0 * _pi) 171 | { 172 | COORDP pp = lineEar({ MSC::mCJ, MSC::mCW, MSC::mR } 173 | , { MSC::sCJ, MSC::sCW, MSC::sR * cs_AU } 174 | , MSC::gst); 175 | MSC::zx_J = pp.J; 176 | MSC::zx_W = pp.W; //无解返回值是100 177 | } 178 | else 179 | MSC::zx_J = MSC::zx_W = 100; 180 | } 181 | 182 | mystl::string MSC::toStr(bool fs) 183 | { 184 | mystl::string s; 185 | s = "-------------------------------------------\n"; 186 | s = s + "平太阳 " + timeStr(MSC::pty) + " 真太阳 " + timeStr(MSC::zty) + "\n"; 187 | s = s + "时差 " + m2fm(MSC::sc * 86400, 2, 1) + " 月亮被照亮 " + to_str(MSC::mIll * 100, 2) + "% "; 188 | s = s + "\n"; 189 | 190 | s = s + "-------------------------------------------\n表一 月亮 太阳\n"; 191 | s = s + "视黄经 " + rad2str(MSC::mHJ, 0) + " " + rad2str(MSC::sHJ, 0) + "\n"; 192 | s = s + "视黄纬 " + rad2str(MSC::mHW, 0) + " " + rad2str(MSC::sHW, 0) + "\n"; 193 | s = s + "视赤经 " + rad2str(MSC::mCJ, 1) + " " + rad2str(MSC::sCJ, 1) + "\n"; 194 | s = s + "视赤纬 " + rad2str(MSC::mCW, 0) + " " + rad2str(MSC::sCW, 0) + "\n"; 195 | s = s + "距离 " + to_str(MSC::mR, 2) + "千米 " + to_str(MSC::sR, 8) + "AU" + "\n"; 196 | 197 | s = s + "-------------------------------------------\n表二 月亮 太阳\n"; 198 | s = s + "方位角 " + rad2str(MSC::mPJ, 0) + " " + rad2str(MSC::sPJ, 0) + "\n"; 199 | s = s + "高度角 " + rad2str(MSC::mPW, 0) + " " + rad2str(MSC::sPW, 0) + "\n"; 200 | s = s + "时角 " + rad2str(MSC::mShiJ, 0) + " " + rad2str(MSC::sShiJ, 0) + "\n"; 201 | s = s + "视半径 " + m2fm(MSC::mRad, 2, 0) + " " + m2fm(MSC::sRad, 2, 0) + " (观测点)\n"; 202 | 203 | if (fs) 204 | { 205 | s = s + "-------------------------------------------\n"; 206 | s = s + "力学时" + JD2str(MSC::T + J2000); 207 | s = s + " ΔT=" + to_str(MSC::dt * 86400, 1) + "秒\n"; 208 | s = s + "黄经章 " + to_str(MSC::dL / pi2 * 360 * 3600, 2) + "\" "; 209 | s = s + "交角章 " + to_str(MSC::dE / pi2 * 360 * 3600, 2) + "\" "; 210 | s = s + "ε=" + rad2str(MSC::E, 0) + "\n"; 211 | s = s + "-------------------------------------------\n"; 212 | 213 | } 214 | return s; 215 | } -------------------------------------------------------------------------------- /mylib/mystl/set_algo.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_SET_ALGO_H_ 2 | #define MYTINYSTL_SET_ALGO_H_ 3 | 4 | // 这个头文件包含 set 的四种算法: union, intersection, difference, symmetric_difference 5 | // 所有函数都要求序列有序 6 | 7 | #include "algobase.h" 8 | #include "iterator.h" 9 | 10 | namespace mystl 11 | { 12 | 13 | /*****************************************************************************************/ 14 | // set_union 15 | // 计算 S1∪S2 的结果并保存到 result 中,返回一个迭代器指向输出结果的尾部 16 | /*****************************************************************************************/ 17 | template 18 | OutputIter set_union(InputIter1 first1, InputIter1 last1, 19 | InputIter2 first2, InputIter2 last2, 20 | OutputIter result) 21 | { 22 | while (first1 != last1 && first2 != last2) 23 | { 24 | if (*first1 < *first2) 25 | { 26 | *result = *first1; 27 | ++first1; 28 | } 29 | else if (*first2 < *first1) 30 | { 31 | *result = *first2; 32 | ++first2; 33 | } 34 | else 35 | { 36 | *result = *first1; 37 | ++first1; 38 | ++first2; 39 | } 40 | ++result; 41 | } 42 | // 将剩余元素拷贝到 result 43 | return mystl::copy(first2, last2, mystl::copy(first1, last1, result)); 44 | } 45 | 46 | // 重载版本使用函数对象 comp 代替比较操作 47 | template 48 | OutputIter set_union(InputIter1 first1, InputIter1 last1, 49 | InputIter2 first2, InputIter2 last2, 50 | OutputIter result, Compared comp) 51 | { 52 | while (first1 != last1 && first2 != last2) 53 | { 54 | if (comp(*first1, *first2)) 55 | { 56 | *result = *first1; 57 | ++first1; 58 | } 59 | else if (comp(*first2, *first1)) 60 | { 61 | *result = *first2; 62 | ++first2; 63 | } 64 | else 65 | { 66 | *result = *first1; 67 | ++first1; 68 | ++first2; 69 | } 70 | ++result; 71 | } 72 | // 将剩余元素拷贝到 result 73 | return mystl::copy(first2, last2, mystl::copy(first1, last1, result)); 74 | } 75 | 76 | /*****************************************************************************************/ 77 | // set_intersection 78 | // 计算 S1∩S2 的结果并保存到 result 中,返回一个迭代器指向输出结果的尾部 79 | /*****************************************************************************************/ 80 | template 81 | OutputIter set_intersection(InputIter1 first1, InputIter1 last1, 82 | InputIter2 first2, InputIter2 last2, 83 | OutputIter result) 84 | { 85 | while (first1 != last1 && first2 != last2) 86 | { 87 | if (*first1 < *first2) 88 | { 89 | ++first1; 90 | } 91 | else if (*first2 < *first1) 92 | { 93 | ++first2; 94 | } 95 | else 96 | { 97 | *result = *first1; 98 | ++first1; 99 | ++first2; 100 | ++result; 101 | } 102 | } 103 | return result; 104 | } 105 | 106 | // 重载版本使用函数对象 comp 代替比较操作 107 | template 108 | OutputIter set_intersection(InputIter1 first1, InputIter1 last1, 109 | InputIter2 first2, InputIter2 last2, 110 | OutputIter result, Compared comp) 111 | { 112 | while (first1 != last1 && first2 != last2) 113 | { 114 | if (comp(*first1, *first2)) 115 | { 116 | ++first1; 117 | } 118 | else if (comp(*first2, *first1)) 119 | { 120 | ++first2; 121 | } 122 | else 123 | { 124 | *result = *first1; 125 | ++first1; 126 | ++first2; 127 | ++result; 128 | } 129 | } 130 | return result; 131 | } 132 | 133 | /*****************************************************************************************/ 134 | // set_difference 135 | // 计算 S1-S2 的结果并保存到 result 中,返回一个迭代器指向输出结果的尾部 136 | /*****************************************************************************************/ 137 | template 138 | OutputIter set_difference(InputIter1 first1, InputIter1 last1, 139 | InputIter2 first2, InputIter2 last2, 140 | OutputIter result) 141 | { 142 | while (first1 != last1 && first2 != last2) 143 | { 144 | if (*first1 < *first2) 145 | { 146 | *result = *first1; 147 | ++first1; 148 | ++result; 149 | } 150 | else if (*first2 < *first1) 151 | { 152 | ++first2; 153 | } 154 | else 155 | { 156 | ++first1; 157 | ++first2; 158 | } 159 | } 160 | return mystl::copy(first1, last1, result); 161 | } 162 | 163 | // 重载版本使用函数对象 comp 代替比较操作 164 | template 165 | OutputIter set_difference(InputIter1 first1, InputIter1 last1, 166 | InputIter2 first2, InputIter2 last2, 167 | OutputIter result, Compared comp) 168 | { 169 | while (first1 != last1 && first2 != last2) 170 | { 171 | if (comp(*first1, *first2)) 172 | { 173 | *result = *first1; 174 | ++first1; 175 | ++result; 176 | } 177 | else if (comp(*first2, *first1)) 178 | { 179 | ++first2; 180 | } 181 | else 182 | { 183 | ++first1; 184 | ++first2; 185 | } 186 | } 187 | return mystl::copy(first1, last1, result); 188 | } 189 | 190 | /*****************************************************************************************/ 191 | // set_symmetric_difference 192 | // 计算 (S1-S2)∪(S2-S1) 的结果并保存到 result 中,返回一个迭代器指向输出结果的尾部 193 | /*****************************************************************************************/ 194 | template 195 | OutputIter set_symmetric_difference(InputIter1 first1, InputIter1 last1, 196 | InputIter2 first2, InputIter2 last2, 197 | OutputIter result) 198 | { 199 | while (first1 != last1 && first2 != last2) 200 | { 201 | if (*first1 < *first2) 202 | { 203 | *result = *first1; 204 | ++first1; 205 | ++result; 206 | } 207 | else if (*first2 < *first1) 208 | { 209 | *result = *first2; 210 | ++first2; 211 | ++result; 212 | } 213 | else 214 | { 215 | ++first1; 216 | ++first2; 217 | } 218 | } 219 | return mystl::copy(first2, last2, mystl::copy(first1, last1, result)); 220 | } 221 | 222 | // 重载版本使用函数对象 comp 代替比较操作 223 | template 224 | OutputIter set_symmetric_difference(InputIter1 first1, InputIter1 last1, 225 | InputIter2 first2, InputIter2 last2, 226 | OutputIter result, Compared comp) 227 | { 228 | while (first1 != last1 && first2 != last2) 229 | { 230 | if (comp(*first1, *first2)) 231 | { 232 | *result = *first1; 233 | ++first1; 234 | ++result; 235 | } 236 | else if (comp(*first2, *first1)) 237 | { 238 | *result = *first2; 239 | ++first2; 240 | ++result; 241 | } 242 | else 243 | { 244 | ++first1; 245 | ++first2; 246 | } 247 | } 248 | return mystl::copy(first2, last2, mystl::copy(first1, last1, result)); 249 | } 250 | 251 | } // namespace mystl 252 | #endif // !MYTINYSTL_SET_ALGO_H_ 253 | 254 | -------------------------------------------------------------------------------- /mylib/mystl/functional.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_FUNCTIONAL_H_ 2 | #define MYTINYSTL_FUNCTIONAL_H_ 3 | 4 | // 这个头文件包含了 mystl 的函数对象与哈希函数 5 | 6 | #include 7 | 8 | namespace mystl 9 | { 10 | 11 | // 定义一元函数的参数型别和返回值型别 12 | template 13 | struct unarg_function 14 | { 15 | typedef Arg argument_type; 16 | typedef Result result_type; 17 | }; 18 | 19 | // 定义二元函数的参数型别的返回值型别 20 | template 21 | struct binary_function 22 | { 23 | typedef Arg1 first_argument_type; 24 | typedef Arg2 second_argument_type; 25 | typedef Result result_type; 26 | }; 27 | 28 | // 函数对象:加法 29 | template 30 | struct plus :public binary_function 31 | { 32 | T operator()(const T& x, const T& y) const { return x + y; } 33 | }; 34 | 35 | // 函数对象:减法 36 | template 37 | struct minus :public binary_function 38 | { 39 | T operator()(const T& x, const T& y) const { return x - y; } 40 | }; 41 | 42 | // 函数对象:乘法 43 | template 44 | struct multiplies :public binary_function 45 | { 46 | T operator()(const T& x, const T& y) const { return x * y; } 47 | }; 48 | 49 | // 函数对象:除法 50 | template 51 | struct divides :public binary_function 52 | { 53 | T operator()(const T& x, const T& y) const { return x / y; } 54 | }; 55 | 56 | // 函数对象:模取 57 | template 58 | struct modulus :public binary_function 59 | { 60 | T operator()(const T& x, const T& y) const { return x % y; } 61 | }; 62 | 63 | // 函数对象:否定 64 | template 65 | struct negate :public unarg_function 66 | { 67 | T operator()(const T& x) const { return -x; } 68 | }; 69 | 70 | // 加法的证同元素 71 | template 72 | T identity_element(plus) { return T(0); } 73 | 74 | // 乘法的证同元素 75 | template 76 | T identity_element(multiplies) { return T(1); } 77 | 78 | // 函数对象:等于 79 | template 80 | struct equal_to :public binary_function 81 | { 82 | bool operator()(const T& x, const T& y) const { return x == y; } 83 | }; 84 | 85 | // 函数对象:不等于 86 | template 87 | struct not_equal_to :public binary_function 88 | { 89 | bool operator()(const T& x, const T& y) const { return x != y; } 90 | }; 91 | 92 | // 函数对象:大于 93 | template 94 | struct greater :public binary_function 95 | { 96 | bool operator()(const T& x, const T& y) const { return x > y; } 97 | }; 98 | 99 | // 函数对象:小于 100 | template 101 | struct less :public binary_function 102 | { 103 | bool operator()(const T& x, const T& y) const { return x < y; } 104 | }; 105 | 106 | // 函数对象:大于等于 107 | template 108 | struct greater_equal :public binary_function 109 | { 110 | bool operator()(const T& x, const T& y) const { return x >= y; } 111 | }; 112 | 113 | // 函数对象:小于等于 114 | template 115 | struct less_equal :public binary_function 116 | { 117 | bool operator()(const T& x, const T& y) const { return x <= y; } 118 | }; 119 | 120 | // 函数对象:逻辑与 121 | template 122 | struct logical_and :public binary_function 123 | { 124 | bool operator()(const T& x, const T& y) const { return x && y; } 125 | }; 126 | 127 | // 函数对象:逻辑或 128 | template 129 | struct logical_or :public binary_function 130 | { 131 | bool operator()(const T& x, const T& y) const { return x || y; } 132 | }; 133 | 134 | // 函数对象:逻辑非 135 | template 136 | struct logical_not :public unarg_function 137 | { 138 | bool operator()(const T& x) const { return !x; } 139 | }; 140 | 141 | // 证同函数:不会改变元素,返回本身 142 | template 143 | struct identity :public unarg_function 144 | { 145 | const T& operator()(const T& x) const { return x; } 146 | }; 147 | 148 | // 选择函数:接受一个 pair,返回第一个元素 149 | template 150 | struct selectfirst :public unarg_function 151 | { 152 | const typename Pair::first_type& operator()(const Pair& x) const 153 | { 154 | return x.first; 155 | } 156 | }; 157 | 158 | // 选择函数:接受一个 pair,返回第二个元素 159 | template 160 | struct selectsecond :public unarg_function 161 | { 162 | const typename Pair::second_type& operator()(const Pair& x) const 163 | { 164 | return x.second; 165 | } 166 | }; 167 | 168 | // 投射函数:返回第一参数 169 | template 170 | struct projectfirst :public binary_function 171 | { 172 | Arg1 operator()(const Arg1& x, const Arg2&) const { return x; } 173 | }; 174 | 175 | // 投射函数:返回第二参数 176 | template 177 | struct projectsecond :public binary_function 178 | { 179 | Arg2 operator()(const Arg1&, const Arg2& y) const { return y; } 180 | }; 181 | 182 | /*****************************************************************************************/ 183 | // 哈希函数对象 184 | 185 | // 对于大部分类型,hash function 什么都不做 186 | template 187 | struct hash {}; 188 | 189 | // 针对指针的偏特化版本 190 | template 191 | struct hash 192 | { 193 | size_t operator()(T* p) const noexcept 194 | { return reinterpret_cast(p); } 195 | }; 196 | 197 | // 对于整型类型,只是返回原值 198 | #define MYSTL_TRIVIAL_HASH_FCN(Type) \ 199 | template <> struct hash \ 200 | { \ 201 | size_t operator()(Type val) const noexcept \ 202 | { return static_cast(val); } \ 203 | }; 204 | 205 | MYSTL_TRIVIAL_HASH_FCN(bool) 206 | 207 | MYSTL_TRIVIAL_HASH_FCN(char) 208 | 209 | MYSTL_TRIVIAL_HASH_FCN(signed char) 210 | 211 | MYSTL_TRIVIAL_HASH_FCN(unsigned char) 212 | 213 | MYSTL_TRIVIAL_HASH_FCN(wchar_t) 214 | 215 | MYSTL_TRIVIAL_HASH_FCN(char16_t) 216 | 217 | MYSTL_TRIVIAL_HASH_FCN(char32_t) 218 | 219 | MYSTL_TRIVIAL_HASH_FCN(short) 220 | 221 | MYSTL_TRIVIAL_HASH_FCN(unsigned short) 222 | 223 | MYSTL_TRIVIAL_HASH_FCN(int) 224 | 225 | MYSTL_TRIVIAL_HASH_FCN(unsigned int) 226 | 227 | MYSTL_TRIVIAL_HASH_FCN(long) 228 | 229 | MYSTL_TRIVIAL_HASH_FCN(unsigned long) 230 | 231 | MYSTL_TRIVIAL_HASH_FCN(long long) 232 | 233 | MYSTL_TRIVIAL_HASH_FCN(unsigned long long) 234 | 235 | #undef MYSTL_TRIVIAL_HASH_FCN 236 | 237 | // 对于浮点数,逐位哈希 238 | inline size_t bitwise_hash(const unsigned char* first, size_t count) 239 | { 240 | #if (_MSC_VER && _WIN64) || ((__GNUC__ || __clang__) &&__SIZEOF_POINTER__ == 8) 241 | const size_t fnv_offset = 14695981039346656037ull; 242 | const size_t fnv_prime = 1099511628211ull; 243 | #else 244 | const size_t fnv_offset = 2166136261u; 245 | const size_t fnv_prime = 16777619u; 246 | #endif 247 | size_t result = fnv_offset; 248 | for (size_t i = 0; i < count; ++i) 249 | { 250 | result ^= (size_t)first[i]; 251 | result *= fnv_prime; 252 | } 253 | return result; 254 | } 255 | 256 | template <> 257 | struct hash 258 | { 259 | size_t operator()(const float& val) 260 | { 261 | return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(float)); 262 | } 263 | }; 264 | 265 | template <> 266 | struct hash 267 | { 268 | size_t operator()(const double& val) 269 | { 270 | return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(double)); 271 | } 272 | }; 273 | 274 | template <> 275 | struct hash 276 | { 277 | size_t operator()(const long double& val) 278 | { 279 | return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(long double)); 280 | } 281 | }; 282 | 283 | } // namespace mystl 284 | #endif // !MYTINYSTL_FUNCTIONAL_H_ 285 | 286 | -------------------------------------------------------------------------------- /mylib/mystl/heap_algo.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_HEAP_ALGO_H_ 2 | #define MYTINYSTL_HEAP_ALGO_H_ 3 | 4 | // 这个头文件包含 heap 的四个算法 : push_heap, pop_heap, sort_heap, make_heap 5 | 6 | #include "iterator.h" 7 | 8 | namespace mystl 9 | { 10 | 11 | /*****************************************************************************************/ 12 | // push_heap 13 | // 该函数接受两个迭代器,表示一个 heap 容器的首尾,并且新元素已经插入到底部容器的最尾端,调整 heap 14 | /*****************************************************************************************/ 15 | template 16 | void push_heap_aux(RandomIter first, Distance holeIndex, Distance topIndex, T value) 17 | { 18 | auto parent = (holeIndex - 1) / 2; 19 | while (holeIndex > topIndex && *(first + parent) < value) 20 | { 21 | // 使用 operator<,所以 heap 为 max-heap 22 | *(first + holeIndex) = *(first + parent); 23 | holeIndex = parent; 24 | parent = (holeIndex - 1) / 2; 25 | } 26 | *(first + holeIndex) = value; 27 | } 28 | 29 | template 30 | void push_heap_d(RandomIter first, RandomIter last, Distance*) 31 | { 32 | mystl::push_heap_aux(first, (last - first) - 1, static_cast(0), *(last - 1)); 33 | } 34 | 35 | template 36 | void push_heap(RandomIter first, RandomIter last) 37 | { // 新元素应该已置于底部容器的最尾端 38 | mystl::push_heap_d(first, last, distance_type(first)); 39 | } 40 | 41 | // 重载版本使用函数对象 comp 代替比较操作 42 | template 43 | void push_heap_aux(RandomIter first, Distance holeIndex, Distance topIndex, T value, 44 | Compared comp) 45 | { 46 | auto parent = (holeIndex - 1) / 2; 47 | while (holeIndex > topIndex && comp(*(first + parent), value)) 48 | { 49 | *(first + holeIndex) = *(first + parent); 50 | holeIndex = parent; 51 | parent = (holeIndex - 1) / 2; 52 | } 53 | *(first + holeIndex) = value; 54 | } 55 | 56 | template 57 | void push_heap_d(RandomIter first, RandomIter last, Distance*, Compared comp) 58 | { 59 | mystl::push_heap_aux(first, (last - first) - 1, static_cast(0), 60 | *(last - 1), comp); 61 | } 62 | 63 | template 64 | void push_heap(RandomIter first, RandomIter last, Compared comp) 65 | { 66 | mystl::push_heap_d(first, last, distance_type(first), comp); 67 | } 68 | 69 | /*****************************************************************************************/ 70 | // pop_heap 71 | // 该函数接受两个迭代器,表示 heap 容器的首尾,将 heap 的根节点取出放到容器尾部,调整 heap 72 | /*****************************************************************************************/ 73 | template 74 | void adjust_heap(RandomIter first, Distance holeIndex, Distance len, T value) 75 | { 76 | // 先进行下溯(percolate down)过程 77 | auto topIndex = holeIndex; 78 | auto rchild = 2 * holeIndex + 2; 79 | while (rchild < len) 80 | { 81 | if (*(first + rchild) < *(first + rchild - 1)) 82 | --rchild; 83 | *(first + holeIndex) = *(first + rchild); 84 | holeIndex = rchild; 85 | rchild = 2 * (rchild + 1); 86 | } 87 | if (rchild == len) 88 | { // 如果没有右子节点 89 | *(first + holeIndex) = *(first + (rchild - 1)); 90 | holeIndex = rchild - 1; 91 | } 92 | // 再执行一次上溯(percolate up)过程 93 | mystl::push_heap_aux(first, holeIndex, topIndex, value); 94 | } 95 | 96 | template 97 | void pop_heap_aux(RandomIter first, RandomIter last, RandomIter result, T value, 98 | Distance*) 99 | { 100 | // 先将首值调至尾节点,然后调整[first, last - 1)使之重新成为一个 max-heap 101 | *result = *first; 102 | mystl::adjust_heap(first, static_cast(0), last - first, value); 103 | } 104 | 105 | template 106 | void pop_heap(RandomIter first, RandomIter last) 107 | { 108 | mystl::pop_heap_aux(first, last - 1, last - 1, *(last - 1), distance_type(first)); 109 | } 110 | 111 | // 重载版本使用函数对象 comp 代替比较操作 112 | template 113 | void adjust_heap(RandomIter first, Distance holeIndex, Distance len, T value, 114 | Compared comp) 115 | { 116 | // 先进行下溯(percolate down)过程 117 | auto topIndex = holeIndex; 118 | auto rchild = 2 * holeIndex + 2; 119 | while (rchild < len) 120 | { 121 | if (comp(*(first + rchild), *(first + rchild - 1))) --rchild; 122 | *(first + holeIndex) = *(first + rchild); 123 | holeIndex = rchild; 124 | rchild = 2 * (rchild + 1); 125 | } 126 | if (rchild == len) 127 | { 128 | *(first + holeIndex) = *(first + (rchild - 1)); 129 | holeIndex = rchild - 1; 130 | } 131 | // 再执行一次上溯(percolate up)过程 132 | mystl::push_heap_aux(first, holeIndex, topIndex, value, comp); 133 | } 134 | 135 | template 136 | void pop_heap_aux(RandomIter first, RandomIter last, RandomIter result, 137 | T value, Distance*, Compared comp) 138 | { 139 | *result = *first; // 先将尾指设置成首值,即尾指为欲求结果 140 | mystl::adjust_heap(first, static_cast(0), last - first, value, comp); 141 | } 142 | 143 | template 144 | void pop_heap(RandomIter first, RandomIter last, Compared comp) 145 | { 146 | mystl::pop_heap_aux(first, last - 1, last - 1, *(last - 1), 147 | distance_type(first), comp); 148 | } 149 | 150 | /*****************************************************************************************/ 151 | // sort_heap 152 | // 该函数接受两个迭代器,表示 heap 容器的首尾,不断执行 pop_heap 操作,直到首尾最多相差1 153 | /*****************************************************************************************/ 154 | template 155 | void sort_heap(RandomIter first, RandomIter last) 156 | { 157 | // 每执行一次 pop_heap,最大的元素都被放到尾部,直到容器最多只有一个元素,完成排序 158 | while (last - first > 1) 159 | { 160 | mystl::pop_heap(first, last--); 161 | } 162 | } 163 | 164 | // 重载版本使用函数对象 comp 代替比较操作 165 | template 166 | void sort_heap(RandomIter first, RandomIter last, Compared comp) 167 | { 168 | while (last - first > 1) 169 | { 170 | mystl::pop_heap(first, last--, comp); 171 | } 172 | } 173 | 174 | /*****************************************************************************************/ 175 | // make_heap 176 | // 该函数接受两个迭代器,表示 heap 容器的首尾,把容器内的数据变为一个 heap 177 | /*****************************************************************************************/ 178 | template 179 | void make_heap_aux(RandomIter first, RandomIter last, Distance*) 180 | { 181 | if (last - first < 2) 182 | return; 183 | auto len = last - first; 184 | auto holeIndex = (len - 2) / 2; 185 | while (true) 186 | { 187 | // 重排以 holeIndex 为首的子树 188 | mystl::adjust_heap(first, holeIndex, len, *(first + holeIndex)); 189 | if (holeIndex == 0) 190 | return; 191 | holeIndex--; 192 | } 193 | } 194 | 195 | template 196 | void make_heap(RandomIter first, RandomIter last) 197 | { 198 | mystl::make_heap_aux(first, last, distance_type(first));; 199 | } 200 | 201 | // 重载版本使用函数对象 comp 代替比较操作 202 | template 203 | void make_heap_aux(RandomIter first, RandomIter last, Distance*, Compared comp) 204 | { 205 | if (last - first < 2) 206 | return; 207 | auto len = last - first; 208 | auto holeIndex = (len - 2) / 2; 209 | while (true) 210 | { 211 | // 重排以 holeIndex 为首的子树 212 | mystl::adjust_heap(first, holeIndex, len, *(first + holeIndex), comp); 213 | if (holeIndex == 0) 214 | return; 215 | holeIndex--; 216 | } 217 | } 218 | 219 | template 220 | void make_heap(RandomIter first, RandomIter last, Compared comp) 221 | { 222 | mystl::make_heap_aux(first, last, distance_type(first), comp); 223 | } 224 | 225 | } // namespace mystl 226 | #endif // !MYTINYSTL_HEAP_ALGO_H_ 227 | 228 | -------------------------------------------------------------------------------- /mylib/mystl/uninitialized.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_UNINITIALIZED_H_ 2 | #define MYTINYSTL_UNINITIALIZED_H_ 3 | 4 | // 这个头文件用于对未初始化空间构造元素 5 | 6 | #include "algobase.h" 7 | #include "construct.h" 8 | #include "iterator.h" 9 | #include "type_traits.h" 10 | #include "util.h" 11 | 12 | namespace mystl 13 | { 14 | 15 | /*****************************************************************************************/ 16 | // uninitialized_copy 17 | // 把 [first, last) 上的内容复制到以 result 为起始处的空间,返回复制结束的位置 18 | /*****************************************************************************************/ 19 | template 20 | ForwardIter 21 | unchecked_uninit_copy(InputIter first, InputIter last, ForwardIter result, std::true_type) 22 | { 23 | return mystl::copy(first, last, result); 24 | } 25 | 26 | template 27 | ForwardIter 28 | unchecked_uninit_copy(InputIter first, InputIter last, ForwardIter result, std::false_type) 29 | { 30 | auto cur = result; 31 | try 32 | { 33 | for (; first != last; ++first, ++cur) 34 | { 35 | mystl::construct(&*cur, *first); 36 | } 37 | } 38 | catch (...) 39 | { 40 | for (; result != cur; --cur) 41 | mystl::destroy(&*cur); 42 | } 43 | return cur; 44 | } 45 | 46 | template 47 | ForwardIter uninitialized_copy(InputIter first, InputIter last, ForwardIter result) 48 | { 49 | return mystl::unchecked_uninit_copy(first, last, result, 50 | std::is_trivially_copy_assignable< 51 | typename iterator_traits:: 52 | value_type>{}); 53 | } 54 | 55 | /*****************************************************************************************/ 56 | // uninitialized_copy_n 57 | // 把 [first, first + n) 上的内容复制到以 result 为起始处的空间,返回复制结束的位置 58 | /*****************************************************************************************/ 59 | template 60 | ForwardIter 61 | unchecked_uninit_copy_n(InputIter first, Size n, ForwardIter result, std::true_type) 62 | { 63 | return mystl::copy_n(first, n, result).second; 64 | } 65 | 66 | template 67 | ForwardIter 68 | unchecked_uninit_copy_n(InputIter first, Size n, ForwardIter result, std::false_type) 69 | { 70 | auto cur = result; 71 | try 72 | { 73 | for (; n > 0; --n, ++cur, ++first) 74 | { 75 | mystl::construct(&*cur, *first); 76 | } 77 | } 78 | catch (...) 79 | { 80 | for (; result != cur; --cur) 81 | mystl::destroy(&*cur); 82 | } 83 | return cur; 84 | } 85 | 86 | template 87 | ForwardIter uninitialized_copy_n(InputIter first, Size n, ForwardIter result) 88 | { 89 | return mystl::unchecked_uninit_copy_n(first, n, result, 90 | std::is_trivially_copy_assignable< 91 | typename iterator_traits:: 92 | value_type>{}); 93 | } 94 | 95 | /*****************************************************************************************/ 96 | // uninitialized_fill 97 | // 在 [first, last) 区间内填充元素值 98 | /*****************************************************************************************/ 99 | template 100 | void 101 | unchecked_uninit_fill(ForwardIter first, ForwardIter last, const T& value, std::true_type) 102 | { 103 | mystl::fill(first, last, value); 104 | } 105 | 106 | template 107 | void 108 | unchecked_uninit_fill(ForwardIter first, ForwardIter last, const T& value, std::false_type) 109 | { 110 | auto cur = first; 111 | try 112 | { 113 | for (; cur != last; ++cur) 114 | { 115 | mystl::construct(&*cur, value); 116 | } 117 | } 118 | catch (...) 119 | { 120 | for (;first != cur; ++first) 121 | mystl::destroy(&*first); 122 | } 123 | } 124 | 125 | template 126 | void uninitialized_fill(ForwardIter first, ForwardIter last, const T& value) 127 | { 128 | mystl::unchecked_uninit_fill(first, last, value, 129 | std::is_trivially_copy_assignable< 130 | typename iterator_traits:: 131 | value_type>{}); 132 | } 133 | 134 | /*****************************************************************************************/ 135 | // uninitialized_fill_n 136 | // 从 first 位置开始,填充 n 个元素值,返回填充结束的位置 137 | /*****************************************************************************************/ 138 | template 139 | ForwardIter 140 | unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::true_type) 141 | { 142 | return mystl::fill_n(first, n, value); 143 | } 144 | 145 | template 146 | ForwardIter 147 | unchecked_uninit_fill_n(ForwardIter first, Size n, const T& value, std::false_type) 148 | { 149 | auto cur = first; 150 | try 151 | { 152 | for (; n > 0; --n, ++cur) 153 | { 154 | mystl::construct(&*cur, value); 155 | } 156 | } 157 | catch (...) 158 | { 159 | for (; first != cur; ++first) 160 | mystl::destroy(&*first); 161 | } 162 | return cur; 163 | } 164 | 165 | template 166 | ForwardIter uninitialized_fill_n(ForwardIter first, Size n, const T& value) 167 | { 168 | return mystl::unchecked_uninit_fill_n(first, n, value, 169 | std::is_trivially_copy_assignable< 170 | typename iterator_traits:: 171 | value_type>{}); 172 | } 173 | 174 | /*****************************************************************************************/ 175 | // uninitialized_move 176 | // 把[first, last)上的内容移动到以 result 为起始处的空间,返回移动结束的位置 177 | /*****************************************************************************************/ 178 | template 179 | ForwardIter 180 | unchecked_uninit_move(InputIter first, InputIter last, ForwardIter result, std::true_type) 181 | { 182 | return mystl::move(first, last, result); 183 | } 184 | 185 | template 186 | ForwardIter 187 | unchecked_uninit_move(InputIter first, InputIter last, ForwardIter result, std::false_type) 188 | { 189 | ForwardIter cur = result; 190 | try 191 | { 192 | for (; first != last; ++first, ++cur) 193 | { 194 | mystl::construct(&*cur, mystl::move(*first)); 195 | } 196 | } 197 | catch (...) 198 | { 199 | mystl::destroy(result, cur); 200 | } 201 | return cur; 202 | } 203 | 204 | template 205 | ForwardIter uninitialized_move(InputIter first, InputIter last, ForwardIter result) 206 | { 207 | return mystl::unchecked_uninit_move(first, last, result, 208 | std::is_trivially_move_assignable< 209 | typename iterator_traits:: 210 | value_type>{}); 211 | } 212 | 213 | /*****************************************************************************************/ 214 | // uninitialized_move_n 215 | // 把[first, first + n)上的内容移动到以 result 为起始处的空间,返回移动结束的位置 216 | /*****************************************************************************************/ 217 | template 218 | ForwardIter 219 | unchecked_uninit_move_n(InputIter first, Size n, ForwardIter result, std::true_type) 220 | { 221 | return mystl::move(first, first + n, result); 222 | } 223 | 224 | template 225 | ForwardIter 226 | unchecked_uninit_move_n(InputIter first, Size n, ForwardIter result, std::false_type) 227 | { 228 | auto cur = result; 229 | try 230 | { 231 | for (; n > 0; --n, ++first, ++cur) 232 | { 233 | mystl::construct(&*cur, mystl::move(*first)); 234 | } 235 | } 236 | catch (...) 237 | { 238 | for (; result != cur; ++result) 239 | mystl::destroy(&*result); 240 | throw; 241 | } 242 | return cur; 243 | } 244 | 245 | template 246 | ForwardIter uninitialized_move_n(InputIter first, Size n, ForwardIter result) 247 | { 248 | return mystl::unchecked_uninit_move_n(first, n, result, 249 | std::is_trivially_move_assignable< 250 | typename iterator_traits:: 251 | value_type>{}); 252 | } 253 | 254 | } // namespace mystl 255 | #endif // !MYTINYSTL_UNINITIALIZED_H_ 256 | 257 | -------------------------------------------------------------------------------- /mylib/mystl/util.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_UTIL_H_ 2 | #define MYTINYSTL_UTIL_H_ 3 | 4 | // 这个文件包含一些通用工具,包括 move, forward, swap 等函数,以及 pair 等 5 | 6 | #include 7 | 8 | #include "type_traits.h" 9 | 10 | namespace mystl 11 | { 12 | 13 | // move 14 | 15 | template 16 | typename std::remove_reference::type&& move(T&& arg) noexcept 17 | { 18 | return static_cast::type&&>(arg); 19 | } 20 | 21 | // forward 22 | 23 | template 24 | T&& forward(typename std::remove_reference::type& arg) noexcept 25 | { 26 | return static_cast(arg); 27 | } 28 | 29 | template 30 | T&& forward(typename std::remove_reference::type&& arg) noexcept 31 | { 32 | static_assert(!std::is_lvalue_reference::value, "bad forward"); 33 | return static_cast(arg); 34 | } 35 | 36 | // swap 37 | 38 | template 39 | void swap(Tp& lhs, Tp& rhs) 40 | { 41 | auto tmp(mystl::move(lhs)); 42 | lhs = mystl::move(rhs); 43 | rhs = mystl::move(tmp); 44 | } 45 | 46 | template 47 | ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2) 48 | { 49 | for (; first1 != last1; ++first1, (void) ++first2) 50 | mystl::swap(*first1, *first2); 51 | return first2; 52 | } 53 | 54 | template 55 | void swap(Tp(&a)[N], Tp(&b)[N]) 56 | { 57 | mystl::swap_range(a, a + N, b); 58 | } 59 | 60 | // -------------------------------------------------------------------------------------- 61 | // pair 62 | 63 | // 结构体模板 : pair 64 | // 两个模板参数分别表示两个数据的类型 65 | // 用 first 和 second 来分别取出第一个数据和第二个数据 66 | template 67 | struct pair 68 | { 69 | typedef Ty1 first_type; 70 | typedef Ty2 second_type; 71 | 72 | first_type first; // 保存第一个数据 73 | second_type second; // 保存第二个数据 74 | 75 | // default constructiable 76 | template ::value && 79 | std::is_default_constructible::value, void>::type> 80 | constexpr pair() 81 | : first(), second() 82 | { 83 | } 84 | 85 | // implicit constructiable for this type 86 | template ::value && 89 | std::is_copy_constructible::value && 90 | std::is_convertible::value && 91 | std::is_convertible::value, int>::type = 0> 92 | constexpr pair(const Ty1& a, const Ty2& b) 93 | : first(a), second(b) 94 | { 95 | } 96 | 97 | // explicit constructible for this type 98 | template ::value && 101 | std::is_copy_constructible::value && 102 | (!std::is_convertible::value || 103 | !std::is_convertible::value), int>::type = 0> 104 | explicit constexpr pair(const Ty1& a, const Ty2& b) 105 | : first(a), second(b) 106 | { 107 | } 108 | 109 | pair(const pair& rhs) = default; 110 | pair(pair&& rhs) = default; 111 | 112 | // implicit constructiable for other type 113 | template ::value && 116 | std::is_constructible::value && 117 | std::is_convertible::value && 118 | std::is_convertible::value, int>::type = 0> 119 | constexpr pair(Other1&& a, Other2&& b) 120 | : first(mystl::forward(a)), 121 | second(mystl::forward(b)) 122 | { 123 | } 124 | 125 | // explicit constructiable for other type 126 | template ::value && 129 | std::is_constructible::value && 130 | (!std::is_convertible::value || 131 | !std::is_convertible::value), int>::type = 0> 132 | explicit constexpr pair(Other1&& a, Other2&& b) 133 | : first(mystl::forward(a)), 134 | second(mystl::forward(b)) 135 | { 136 | } 137 | 138 | // implicit constructiable for other pair 139 | template ::value && 142 | std::is_constructible::value && 143 | std::is_convertible::value && 144 | std::is_convertible::value, int>::type = 0> 145 | constexpr pair(const pair& other) 146 | : first(other.first), 147 | second(other.second) 148 | { 149 | } 150 | 151 | // explicit constructiable for other pair 152 | template ::value && 155 | std::is_constructible::value && 156 | (!std::is_convertible::value || 157 | !std::is_convertible::value), int>::type = 0> 158 | explicit constexpr pair(const pair& other) 159 | : first(other.first), 160 | second(other.second) 161 | { 162 | } 163 | 164 | // implicit constructiable for other pair 165 | template ::value && 168 | std::is_constructible::value && 169 | std::is_convertible::value && 170 | std::is_convertible::value, int>::type = 0> 171 | constexpr pair(pair&& other) 172 | : first(mystl::forward(other.first)), 173 | second(mystl::forward(other.second)) 174 | { 175 | } 176 | 177 | // explicit constructiable for other pair 178 | template ::value && 181 | std::is_constructible::value && 182 | (!std::is_convertible::value || 183 | !std::is_convertible::value), int>::type = 0> 184 | explicit constexpr pair(pair&& other) 185 | : first(mystl::forward(other.first)), 186 | second(mystl::forward(other.second)) 187 | { 188 | } 189 | 190 | // copy assign for this pair 191 | pair& operator=(const pair& rhs) 192 | { 193 | if (this != &rhs) 194 | { 195 | first = rhs.first; 196 | second = rhs.second; 197 | } 198 | return *this; 199 | } 200 | 201 | // move assign for this pair 202 | pair& operator=(pair&& rhs) 203 | { 204 | if (this != &rhs) 205 | { 206 | first = mystl::move(rhs.first); 207 | second = mystl::move(rhs.second); 208 | } 209 | return *this; 210 | } 211 | 212 | // copy assign for other pair 213 | template 214 | pair& operator=(const pair& other) 215 | { 216 | first = other.first; 217 | second = other.second; 218 | return *this; 219 | } 220 | 221 | // move assign for other pair 222 | template 223 | pair& operator=(pair&& other) 224 | { 225 | first = mystl::forward(other.first); 226 | second = mystl::forward(other.second); 227 | return *this; 228 | } 229 | 230 | ~pair() = default; 231 | 232 | void swap(pair& other) 233 | { 234 | if (this != &other) 235 | { 236 | mystl::swap(first, other.first); 237 | mystl::swap(second, other.second); 238 | } 239 | } 240 | 241 | }; 242 | 243 | // 重载比较操作符 244 | template 245 | bool operator==(const pair& lhs, const pair& rhs) 246 | { 247 | return lhs.first == rhs.first && lhs.second == rhs.second; 248 | } 249 | 250 | template 251 | bool operator<(const pair& lhs, const pair& rhs) 252 | { 253 | return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); 254 | } 255 | 256 | template 257 | bool operator!=(const pair& lhs, const pair& rhs) 258 | { 259 | return !(lhs == rhs); 260 | } 261 | 262 | template 263 | bool operator>(const pair& lhs, const pair& rhs) 264 | { 265 | return rhs < lhs; 266 | } 267 | 268 | template 269 | bool operator<=(const pair& lhs, const pair& rhs) 270 | { 271 | return !(rhs < lhs); 272 | } 273 | 274 | template 275 | bool operator>=(const pair& lhs, const pair& rhs) 276 | { 277 | return !(lhs < rhs); 278 | } 279 | 280 | // 重载 mystl 的 swap 281 | template 282 | void swap(pair& lhs, pair& rhs) 283 | { 284 | lhs.swap(rhs); 285 | } 286 | 287 | // 全局函数,让两个数据成为一个 pair 288 | template 289 | pair make_pair(Ty1&& first, Ty2&& second) 290 | { 291 | return pair(mystl::forward(first), mystl::forward(second)); 292 | } 293 | 294 | } 295 | 296 | #endif // !MYTINYSTL_UTIL_H_ 297 | 298 | -------------------------------------------------------------------------------- /mylib/mystl/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_QUEUE_H_ 2 | #define MYTINYSTL_QUEUE_H_ 3 | 4 | // 这个头文件包含了两个模板类 queue 和 priority_queue 5 | // queue : 队列 6 | // priority_queue : 优先队列 7 | 8 | #include "deque.h" 9 | #include "vector.h" 10 | #include "functional.h" 11 | #include "heap_algo.h" 12 | 13 | namespace mystl 14 | { 15 | 16 | // 模板类 queue 17 | // 参数一代表数据类型,参数二代表底层容器类型,缺省使用 mystl::deque 作为底层容器 18 | template > 19 | class queue 20 | { 21 | public: 22 | typedef Container container_type; 23 | // 使用底层容器的型别 24 | typedef typename Container::value_type value_type; 25 | typedef typename Container::size_type size_type; 26 | typedef typename Container::reference reference; 27 | typedef typename Container::const_reference const_reference; 28 | 29 | static_assert(std::is_same::value, 30 | "the value_type of Container should be same with T"); 31 | private: 32 | container_type c_; // 用底层容器表现 queue 33 | 34 | public: 35 | // 构造、复制、移动函数 36 | 37 | queue() = default; 38 | 39 | explicit queue(size_type n) 40 | :c_(n) 41 | { 42 | } 43 | queue(size_type n, const value_type& value) 44 | :c_(n, value) 45 | { 46 | } 47 | 48 | template 49 | queue(IIter first, IIter last) 50 | :c_(first, last) 51 | { 52 | } 53 | 54 | queue(std::initializer_list ilist) 55 | :c_(ilist.begin(), ilist.end()) 56 | { 57 | } 58 | 59 | queue(const Container& c) 60 | :c_(c) 61 | { 62 | } 63 | queue(Container&& c) noexcept(std::is_nothrow_move_constructible::value) 64 | :c_(mystl::move(c)) 65 | { 66 | } 67 | 68 | queue(const queue& rhs) 69 | :c_(rhs.c_) 70 | { 71 | } 72 | queue(queue&& rhs) noexcept(std::is_nothrow_move_constructible::value) 73 | :c_(mystl::move(rhs.c_)) 74 | { 75 | } 76 | 77 | queue& operator=(const queue& rhs) 78 | { 79 | c_ = rhs.c_; 80 | return *this; 81 | } 82 | queue& operator=(queue&& rhs) noexcept(std::is_nothrow_move_assignable::value) 83 | { 84 | c_ = mystl::move(rhs.c_); 85 | return *this; 86 | } 87 | 88 | queue& operator=(std::initializer_list ilist) 89 | { 90 | c_ = ilist; 91 | return *this; 92 | } 93 | 94 | ~queue() = default; 95 | 96 | // 访问元素相关操作 97 | reference front() { return c_.front(); } 98 | const_reference front() const { return c_.front(); } 99 | reference back() { return c_.back(); } 100 | const_reference back() const { return c_.back(); } 101 | 102 | // 容量相关操作 103 | bool empty() const noexcept { return c_.empty(); } 104 | size_type size() const noexcept { return c_.size(); } 105 | 106 | // 修改容器相关操作 107 | template 108 | void emplace(Args&& ...args) 109 | { c_.emplace_back(mystl::forward(args)...); } 110 | 111 | void push(const value_type& value) 112 | { c_.push_back(value); } 113 | void push(value_type&& value) 114 | { c_.emplace_back(mystl::move(value)); } 115 | 116 | void pop() 117 | { c_.pop_front(); } 118 | 119 | void clear() 120 | { 121 | while (!empty()) 122 | pop(); 123 | } 124 | 125 | void swap(queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_))) 126 | { mystl::swap(c_, rhs.c_); } 127 | 128 | public: 129 | friend bool operator==(const queue& lhs, const queue& rhs) { return lhs.c_ == rhs.c_; } 130 | friend bool operator< (const queue& lhs, const queue& rhs) { return lhs.c_ < rhs.c_; } 131 | }; 132 | 133 | // 重载比较操作符 134 | template 135 | bool operator==(const queue& lhs, const queue& rhs) 136 | { 137 | return lhs == rhs; 138 | } 139 | 140 | template 141 | bool operator!=(const queue& lhs, const queue& rhs) 142 | { 143 | return !(lhs == rhs); 144 | } 145 | 146 | template 147 | bool operator<(const queue& lhs, const queue& rhs) 148 | { 149 | return lhs < rhs; 150 | } 151 | 152 | template 153 | bool operator>(const queue& lhs, const queue& rhs) 154 | { 155 | return rhs < lhs; 156 | } 157 | 158 | template 159 | bool operator<=(const queue& lhs, const queue& rhs) 160 | { 161 | return !(rhs < lhs); 162 | } 163 | 164 | template 165 | bool operator>=(const queue& lhs, const queue& rhs) 166 | { 167 | return !(lhs < rhs); 168 | } 169 | 170 | // 重载 mystl 的 swap 171 | template 172 | void swap(queue& lhs, queue& rhs) noexcept(noexcept(lhs.swap(rhs))) 173 | { 174 | lhs.swap(rhs); 175 | } 176 | 177 | /*****************************************************************************************/ 178 | 179 | // 模板类 priority_queue 180 | // 参数一代表数据类型,参数二代表容器类型,缺省使用 mystl::vector 作为底层容器 181 | // 参数三代表比较权值的方式,缺省使用 mystl::less 作为比较方式 182 | template , 183 | class Compare = mystl::less> 184 | class priority_queue 185 | { 186 | public: 187 | typedef Container container_type; 188 | typedef Compare value_compare; 189 | // 使用底层容器的型别 190 | typedef typename Container::value_type value_type; 191 | typedef typename Container::size_type size_type; 192 | typedef typename Container::reference reference; 193 | typedef typename Container::const_reference const_reference; 194 | 195 | static_assert(std::is_same::value, 196 | "the value_type of Container should be same with T"); 197 | 198 | private: 199 | container_type c_; // 用底层容器来表现 priority_queue 200 | value_compare comp_; // 权值比较的标准 201 | 202 | public: 203 | // 构造、复制、移动函数 204 | priority_queue() = default; 205 | 206 | priority_queue(const Compare& c) 207 | :c_(), comp_(c) 208 | { 209 | } 210 | 211 | explicit priority_queue(size_type n) 212 | :c_(n) 213 | { 214 | mystl::make_heap(c_.begin(), c_.end(), comp_); 215 | } 216 | priority_queue(size_type n, const value_type& value) 217 | :c_(n, value) 218 | { 219 | mystl::make_heap(c_.begin(), c_.end(), comp_); 220 | } 221 | 222 | template 223 | priority_queue(IIter first, IIter last) 224 | :c_(first, last) 225 | { 226 | mystl::make_heap(c_.begin(), c_.end(), comp_); 227 | } 228 | 229 | priority_queue(std::initializer_list ilist) 230 | :c_(ilist) 231 | { 232 | mystl::make_heap(c_.begin(), c_.end(), comp_); 233 | } 234 | 235 | priority_queue(const Container& s) 236 | :c_(s) 237 | { 238 | mystl::make_heap(c_.begin(), c_.end(), comp_); 239 | } 240 | priority_queue(Container&& s) 241 | :c_(mystl::move(s)) 242 | { 243 | mystl::make_heap(c_.begin(), c_.end(), comp_); 244 | } 245 | 246 | priority_queue(const priority_queue& rhs) 247 | :c_(rhs.c_), comp_(rhs.comp_) 248 | { 249 | mystl::make_heap(c_.begin(), c_.end(), comp_); 250 | } 251 | priority_queue(priority_queue&& rhs) 252 | :c_(mystl::move(rhs.c_)), comp_(rhs.comp_) 253 | { 254 | mystl::make_heap(c_.begin(), c_.end(), comp_); 255 | } 256 | 257 | priority_queue& operator=(const priority_queue& rhs) 258 | { 259 | c_ = rhs.c_; 260 | comp_ = rhs.comp_; 261 | mystl::make_heap(c_.begin(), c_.end(), comp_); 262 | return *this; 263 | } 264 | priority_queue& operator=(priority_queue&& rhs) 265 | { 266 | c_ = mystl::move(rhs.c_); 267 | comp_ = rhs.comp_; 268 | mystl::make_heap(c_.begin(), c_.end(), comp_); 269 | return *this; 270 | } 271 | priority_queue& operator=(std::initializer_list ilist) 272 | { 273 | c_ = ilist; 274 | comp_ = value_compare(); 275 | mystl::make_heap(c_.begin(), c_.end(), comp_); 276 | return *this; 277 | } 278 | 279 | ~priority_queue() = default; 280 | 281 | public: 282 | 283 | // 访问元素相关操作 284 | const_reference top() const { return c_.front(); } 285 | 286 | // 容量相关操作 287 | bool empty() const noexcept { return c_.empty(); } 288 | size_type size() const noexcept { return c_.size(); } 289 | 290 | // 修改容器相关操作 291 | template 292 | void emplace(Args&& ...args) 293 | { 294 | c_.emplace_back(mystl::forward(args)...); 295 | mystl::push_heap(c_.begin(), c_.end(), comp_); 296 | } 297 | 298 | void push(const value_type& value) 299 | { 300 | c_.push_back(value); 301 | mystl::push_heap(c_.begin(), c_.end(), comp_); 302 | } 303 | void push(value_type&& value) 304 | { 305 | c_.push_back(mystl::move(value)); 306 | mystl::push_heap(c_.begin(), c_.end(), comp_); 307 | } 308 | 309 | void pop() 310 | { 311 | mystl::pop_heap(c_.begin(), c_.end(), comp_); 312 | c_.pop_back(); 313 | } 314 | 315 | void clear() 316 | { 317 | while (!empty()) 318 | pop(); 319 | } 320 | 321 | void swap(priority_queue& rhs) noexcept(noexcept(mystl::swap(c_, rhs.c_)) && 322 | noexcept(mystl::swap(comp_, rhs.comp_))) 323 | { 324 | mystl::swap(c_, rhs.c_); 325 | mystl::swap(comp_, rhs.comp_); 326 | } 327 | 328 | public: 329 | friend bool operator==(const priority_queue& lhs, const priority_queue& rhs) 330 | { 331 | return lhs.c_ == rhs.c_; 332 | } 333 | friend bool operator!=(const priority_queue& lhs, const priority_queue& rhs) 334 | { 335 | return lhs.c_ != rhs.c_; 336 | } 337 | }; 338 | 339 | // 重载比较操作符 340 | template 341 | bool operator==(priority_queue& lhs, 342 | priority_queue& rhs) 343 | { 344 | return lhs == rhs; 345 | } 346 | 347 | template 348 | bool operator!=(priority_queue& lhs, 349 | priority_queue& rhs) 350 | { 351 | return lhs != rhs; 352 | } 353 | 354 | // 重载 mystl 的 swap 355 | template 356 | void swap(priority_queue& lhs, 357 | priority_queue& rhs) noexcept(noexcept(lhs.swap(rhs))) 358 | { 359 | lhs.swap(rhs); 360 | } 361 | 362 | } // namespace mystl 363 | #endif // !MYTINYSTL_QUEUE_H_ 364 | 365 | -------------------------------------------------------------------------------- /eph/eph_rspl.cpp: -------------------------------------------------------------------------------- 1 | #include "eph.h" 2 | #include "eph0.h" 3 | #include "eph_rsgs.h" 4 | #include "eph_rspl.h" 5 | #include "../mylib/tool.h" 6 | #include "../mylib/math_patch.h" 7 | 8 | bool RS_PL::nasa_r = 0;//为1表示采用NASA的视径比 9 | mystl::array5 RS_PL::sT;//地方日食时间表 10 | mystl::string RS_PL::LX; 11 | double RS_PL::sf; 12 | double RS_PL::sf2; 13 | double RS_PL::sf3; 14 | mystl::string RS_PL::sflx; 15 | double RS_PL::b1; 16 | double RS_PL::dur; 17 | double RS_PL::sun_s; 18 | double RS_PL::sun_j; 19 | double RS_PL::P1; 20 | double RS_PL::V1; 21 | double RS_PL::P2; 22 | double RS_PL::V2; 23 | 24 | mystl::array3 RS_PL::A={}; 25 | mystl::array3 RS_PL::B={}; //本半影锥顶点坐标 26 | _ZB RS_PL::P={};//t1时刻的日月坐标,g为恒星时 27 | _ZB RS_PL::Q={};//t2时刻的日月坐标 28 | mystl::array10 RS_PL::V;//食界表 29 | mystl::string RS_PL::Vc = ""; 30 | mystl::string RS_PL::Vb = ""; //食中心类型,本影南北距离 31 | 32 | void RS_PL::secXY(double jd,double L,double fa,double high,_SECXY &re) 33 | { //日月xy坐标计算。参数:jd是力学时,站点经纬L,fa,海拔high(千米) 34 | //基本参数计算 35 | double deltat = dt_T(jd); //TD-UT 36 | mystl::array2 zd=nutation2(jd/36525.0); 37 | double gst= pGST(jd-deltat,deltat) + zd[0]*cos(hcjj(jd/36525.0) + zd[1]); //真恒星时(不考虑非多项式部分) 38 | 39 | mystl::array3 z; 40 | //=======月亮======== 41 | z=RS_GS::moon(jd); re.mCJ=z[0]; re.mCW=z[1]; re.mR=z[2]; //月亮视赤经,月球赤纬 42 | double mShiJ = rad2rrad(gst + L - z[0]); //得到此刻月亮时角 43 | z=parallax(z, mShiJ,fa, high); re.mCJ2=z[0], re.mCW2=z[1], re.mR2=z[2]; //修正了视差的赤道坐标 44 | 45 | //=======太阳======== 46 | z=RS_GS::sun(jd); re.sCJ=z[0]; re.sCW=z[1]; re.sR=z[2]; //太阳视赤经,太阳赤纬 47 | double sShiJ = rad2rrad(gst + L - z[0]); //得到此刻太阳时角 48 | z=parallax(z,sShiJ,fa,high); re.sCJ2=z[0], re.sCW2=z[1], re.sR2=z[2]; //修正了视差的赤道坐标 49 | 50 | //=======视半径======== 51 | re.mr = cs_sMoon/re.mR2/rad; 52 | re.sr = 959.63/re.sR2/rad*cs_AU; 53 | if(RS_PL::nasa_r) re.mr*=cs_sMoon2/cs_sMoon; //0.99925; 54 | //=======日月赤经纬差转为日面中心直角坐标(用于日食)============== 55 | re.x = rad2rrad(re.mCJ2-re.sCJ2) * cos((re.mCW2+re.sCW2)/2.0); 56 | re.y = re.mCW2-re.sCW2; 57 | re.t = jd; 58 | } 59 | 60 | double RS_PL::lineT(_SECXY G, double v,double u, double r, bool n) 61 | {//已知t1时刻星体位置、速度,求x*x+y*y=r*r时,t的值 62 | double b=G.y*v-G.x*u; 63 | double A=u*u+v*v; 64 | double B=u*b; 65 | double C=b*b-r*r*v*v; 66 | double D=B*B-A*C; 67 | if(D<0) return 0; 68 | D=sqrt(D); if(!n) D=-D; 69 | return G.t+((-B+D)/A-G.x)/v; 70 | } 71 | 72 | void RS_PL::secMax(double jd,double L,double fa,double high) 73 | { //日食的食甚计算(jd为近朔的力学时,误差几天不要紧) 74 | int i; 75 | for(i=0;i<5;i++) RS_PL::sT[i]=0; //分别是:食甚,初亏,复圆,食既,生光 76 | RS_PL::LX=""; //类型 77 | RS_PL::sf=0; //食分 78 | RS_PL::b1=1.0; //月日半径比(食甚时刻) 79 | RS_PL::dur = 0; //持续时间 80 | RS_PL::P1 = RS_PL::V1 = 0; //初亏方位,P北点起算,V顶点起算 81 | RS_PL::P2 = RS_PL::V2 = 0; //复圆方位,P北点起算,V顶点起算 82 | RS_PL::sun_s = RS_PL::sun_j = 0; //日出日没 83 | RS_PL::sf2=0; //食分(日出食分) 84 | RS_PL::sf3=0; //食分(日没食分) 85 | RS_PL::sflx = " "; //食分类型 86 | RS_GS::init(jd,7); 87 | jd=RS_GS::Zjd; //食甚初始估值为插值表中心时刻(粗朔) 88 | 89 | _SECXY G={}, g={}; 90 | RS_PL::secXY(jd,L,fa,high,G); 91 | jd -= G.x/0.2128; //与食甚的误差在20分钟以内 92 | 93 | double u,v,dt=60/86400.0,dt2; 94 | for (i=0;i<2;i++) 95 | { 96 | RS_PL::secXY(jd,L,fa,high,G); 97 | RS_PL::secXY(jd+dt,L,fa,high,g); 98 | u = (g.y-G.y)/dt; 99 | v = (g.x-G.x)/dt; 100 | dt2 = -(G.y*u+G.x*v)/(u*u+v*v); 101 | jd += dt2; //极值时间 102 | } 103 | 104 | // 求直线到太阳中心的最小值 105 | // double x=G.x+dt2*v, y=G.y+dt2*u, rmin=sqrt(x*x+y*y); 106 | // js v5.10更新计算公式 107 | double maxsf = 0, maxjd = jd, rmin, ls, tt; 108 | for (i = -30; i < 30; i += 6) { 109 | tt = jd + i / 86400.0; 110 | RS_PL::secXY(tt, L, fa, high, g); 111 | ls = (g.mr + g.sr - sqrt(g.x * g.x + g.y * g.y)) / g.sr / 2; 112 | if (ls > maxsf) maxsf = ls, maxjd = tt; 113 | } 114 | jd = maxjd; 115 | for (i = -5; i < 5; i += 1) { 116 | tt = jd + i / 86400.0; 117 | RS_PL::secXY(tt, L, fa, high, g); 118 | ls = (g.mr + g.sr - sqrt(g.x * g.x + g.y * g.y)) / g.sr / 2; 119 | if (ls > maxsf) maxsf = ls, maxjd = tt; 120 | } 121 | jd = maxjd; 122 | RS_PL::secXY(jd, L, fa, high, G); 123 | rmin = sqrt(G.x * G.x + G.y * G.y); 124 | 125 | RS_PL::sun_s = sunShengJ(jd-dt_T(jd)+L/pi2,L,fa,-1) + dt_T(jd);; //日出 统一用力学时 126 | RS_PL::sun_j = sunShengJ(jd-dt_T(jd)+L/pi2,L,fa, 1) + dt_T(jd);; //日没 统一用力学时 127 | 128 | if(rmin<=G.mr+G.sr) 129 | { //偏食计算 130 | RS_PL::sT[1] = jd; //食甚 131 | RS_PL::LX="偏"; 132 | RS_PL::sf=(G.mr+G.sr-rmin)/G.sr/2.0; //食分 133 | RS_PL::b1=G.mr/G.sr; 134 | 135 | RS_PL::secXY(RS_PL::sun_s,L,fa,high,g); //日出食分 136 | RS_PL::sf2=(g.mr+g.sr-sqrt(g.x*g.x+g.y*g.y))/g.sr/2; //日出食分 137 | if(RS_PL::sf2<0) RS_PL::sf2=0; 138 | 139 | RS_PL::secXY(RS_PL::sun_j,L,fa,high,g); //日没食分 140 | RS_PL::sf3=(g.mr+g.sr-sqrt(g.x*g.x+g.y*g.y))/g.sr/2; //日没食分 141 | if(RS_PL::sf3<0) RS_PL::sf3=0; 142 | 143 | RS_PL::sT[0] = RS_PL::lineT(G,v,u, G.mr+G.sr, 0); //初亏 144 | for(i=0;i<3;i++) 145 | { //初亏再算3次 146 | RS_PL::secXY(RS_PL::sT[0],L,fa,high,g); 147 | RS_PL::sT[0] = RS_PL::lineT(g,v,u, g.mr+g.sr, 0); 148 | } 149 | 150 | RS_PL::P1 = rad2mrad(atan2(g.x,g.y)); //初亏位置角 151 | RS_PL::V1 = rad2mrad(RS_PL::P1-shiChaJ(pGST2(RS_PL::sT[0]),L,fa,g.sCJ,g.sCW)); //这里g.sCJ与g.sCW对应的时间与sT[0]还差了一点,所以有一小点误差,不采用真恒星时也误差一点 152 | 153 | RS_PL::sT[2] = RS_PL::lineT(G,v,u, G.mr+G.sr, 1); //复圆 154 | for(i=0;i<3;i++) 155 | { //复圆再算3次 156 | RS_PL::secXY(RS_PL::sT[2],L,fa,high,g); 157 | RS_PL::sT[2] = RS_PL::lineT(g,v,u, g.mr+g.sr, 1); 158 | } 159 | RS_PL::P2 = rad2mrad(atan2(g.x,g.y)); 160 | RS_PL::V2 = rad2mrad(RS_PL::P2-shiChaJ(pGST2(RS_PL::sT[2]),L,fa,g.sCJ,g.sCW)); //这里g.sCJ与g.sCW对应的时间与sT[2]还差了一点,所以有一小点误差,不采用真恒星时也误差一点 161 | } 162 | if(rmin<=G.mr-G.sr){ //全食计算 163 | RS_PL::LX="全"; 164 | RS_PL::sT[3] = RS_PL::lineT(G,v,u, G.mr-G.sr, 0); //食既 165 | RS_PL::secXY(RS_PL::sT[3],L,fa,high,g); 166 | RS_PL::sT[3] = RS_PL::lineT(g,v,u, g.mr-g.sr, 0); //食既再算1次 167 | 168 | RS_PL::sT[4] = RS_PL::lineT(G,v,u, G.mr-G.sr, 1); //生光 169 | RS_PL::secXY(RS_PL::sT[4],L,fa,high,g); 170 | RS_PL::sT[4] = RS_PL::lineT(g,v,u, g.mr-g.sr, 1); //生光再算1次 171 | RS_PL::dur = RS_PL::sT[4]-RS_PL::sT[3]; 172 | } 173 | if(rmin<=G.sr-G.mr){ //环食计算 174 | RS_PL::LX="环"; 175 | RS_PL::sT[3] = RS_PL::lineT(G,v,u, G.sr-G.mr, 0); //食既 176 | RS_PL::secXY(RS_PL::sT[3],L,fa,high,g); 177 | RS_PL::sT[3] = RS_PL::lineT(g,v,u, g.sr-g.mr, 0); //食既再算1次 178 | 179 | RS_PL::sT[4] = RS_PL::lineT(G,v,u, G.sr-G.mr, 1); //生光 180 | RS_PL::secXY(RS_PL::sT[4],L,fa,high,g); 181 | RS_PL::sT[4] = RS_PL::lineT(g,v,u, g.sr-g.mr, 1); //生光再算1次 182 | RS_PL::dur = RS_PL::sT[4]-RS_PL::sT[3]; 183 | } 184 | 185 | 186 | if(RS_PL::sT[1]0 ) RS_PL::sf=RS_PL::sf2,RS_PL::sflx="#"; //食甚在日出前,取日出食分 187 | if(RS_PL::sT[1]>RS_PL::sun_j && RS_PL::sf3>0 ) RS_PL::sf=RS_PL::sf3,RS_PL::sflx="*"; //食甚在日没后,取日没食分 188 | 189 | for(i=0;i<5;i++){ 190 | if(RS_PL::sT[i]RS_PL::sun_j) RS_PL::sT[i]=0; //升降时间之外的日食算值无效,因为地球不是透明的 191 | } 192 | 193 | RS_PL::sun_s -= dt_T(jd); 194 | RS_PL::sun_j -= dt_T(jd); 195 | } 196 | 197 | //以下涉及南北界计算 198 | mystl::array3 A={}, B={}; //本半影锥顶点坐标 199 | _ZB P={};//t1时刻的日月坐标,g为恒星时 200 | _ZB Q={};//t2时刻的日月坐标 201 | mystl::array10 V;//食界表 202 | mystl::string Vc = "", Vb = ""; //食中心类型,本影南北距离 203 | 204 | void RS_PL::zb0(double jd) 205 | { 206 | //基本参数计算 207 | double deltat = dt_T(jd); //TD-UT 208 | double E=hcjj(jd/36525.0); 209 | mystl::array2 zd=nutation2(jd/36525.0); 210 | 211 | RS_PL::P.g = pGST(jd-deltat, deltat) + zd[0]*cos(E+zd[1]); //真恒星时(不考虑非多项式部分) 212 | RS_PL::P.S=RS_GS::sun(jd); 213 | RS_PL::P.M=RS_GS::moon(jd); 214 | 215 | double t2=jd+60/86400.0; 216 | RS_PL::Q.g = pGST(t2-deltat,deltat) + zd[0]*cos(E+zd[1]); 217 | RS_PL::Q.S=RS_GS::sun(t2); 218 | RS_PL::Q.M=RS_GS::moon(t2); 219 | 220 | //转为直角坐标 221 | mystl::array3 z1={}, z2={}; 222 | z1 = llr2xyz(RS_PL::P.S); 223 | z2 = llr2xyz(RS_PL::P.M); 224 | 225 | double k=959.63/cs_sMoon*cs_AU; //k为日月半径比 226 | //本影锥顶点坐标计算 227 | mystl::array3 F = { 228 | (z1[0]-z2[0])/(1-k)+z2[0], 229 | (z1[1]-z2[1])/(1-k)+z2[1], 230 | (z1[2]-z2[2])/(1-k)+z2[2]}; 231 | RS_PL::A = xyz2llr(F); 232 | //半影锥顶点坐标计算 233 | mystl::array3 FF = { 234 | (z1[0]-z2[0])/(1+k)+z2[0], 235 | (z1[1]-z2[1])/(1+k)+z2[1], 236 | (z1[2]-z2[2])/(1+k)+z2[2]}; 237 | RS_PL::B = xyz2llr(FF); 238 | } 239 | 240 | void RS_PL::zbXY(_ZB &p,double L,double fa) 241 | { 242 | mystl::array3 s = {p.S[0],p.S[1],p.S[2]}; 243 | mystl::array3 m = {p.M[0],p.M[1],p.M[2]}; 244 | s=parallax(s, p.g+L-p.S[0],fa, 0); //修正了视差的赤道坐标 245 | m=parallax(m, p.g+L-p.M[0],fa, 0); //修正了视差的赤道坐标 246 | //=======视半径======== 247 | p.mr = cs_sMoon/m[2]/rad; 248 | p.sr = 959.63/s[2]/rad*cs_AU; 249 | //=======日月赤经纬差转为日面中心直角坐标(用于日食)============== 250 | p.x = rad2rrad(m[0]-s[0]) * cos((m[1]+s[1])/2.0); 251 | p.y = m[1]-s[1]; 252 | } 253 | 254 | void RS_PL::p2p(double L,double fa,_GJW &re,bool fAB,int f) 255 | { //f取+-1 256 | _ZB &p=RS_PL::P, &q=RS_PL::Q; 257 | RS_PL::zbXY(RS_PL::P,L,fa); 258 | RS_PL::zbXY(RS_PL::Q,L,fa); 259 | 260 | double u=q.y-p.y, v=q.x-p.x, a=sqrt(u*u+v*v),r=959.63/p.S[2]/rad*cs_AU; 261 | double W=p.S[1]+f*r*v/a, J=p.S[0]-f*r*u/a/cos((W+p.S[1])/2.0), R=p.S[2]; 262 | mystl::array3 AA = fAB ? RS_PL::A : RS_PL::B; 263 | 264 | COORDP pp = lineEar({J,W,R}, AA, p.g ); 265 | re.J = pp.J; 266 | re.W = pp.W; 267 | } 268 | void RS_PL::pp0(_GJW &re) 269 | { //食中心点计算 270 | _ZB p=RS_PL::P; 271 | COORDP pp = lineEar( p.M, p.S, p.g ); 272 | re.J = pp.J; 273 | re.W = pp.W; //无解返回值是100 274 | 275 | if(re.W==100) { re.c = ""; return; } 276 | re.c="全"; 277 | RS_PL::zbXY(p,re.J,re.W); 278 | if(p.sr>p.mr) re.c="环"; 279 | } 280 | void RS_PL::nbj(double jd) 281 | { //南北界计算 282 | RS_GS::init(jd,7); 283 | _GJW G={}; 284 | mystl::array10 &V=RS_PL::V; 285 | int i; 286 | for(i=0;i<10;i++) V[i]=100; RS_PL::Vc="",RS_PL::Vb=""; //返回初始化,纬度值为100表示无解,经度100也是无解,但在以下程序中经度会被转为-PI到+PI 287 | 288 | RS_PL::zb0(jd); 289 | RS_PL::pp0(G); V[0]=G.J, V[1]=G.W, RS_PL::Vc=G.c; //食中心 290 | 291 | G.J=G.W=0; for(i=0;i<2;i++) RS_PL::p2p(G.J,G.W,G,1, 1); V[2]=G.J, V[3]=G.W; //本影北界,环食为南界(本影区之内,变差u,v基本不变,所以计算两次足够) 292 | G.J=G.W=0; for(i=0;i<2;i++) RS_PL::p2p(G.J,G.W,G,1,-1); V[4]=G.J, V[5]=G.W; //本影南界,环食为北界 293 | G.J=G.W=0; for(i=0;i<3;i++) RS_PL::p2p(G.J,G.W,G,0,-1); V[6]=G.J, V[7]=G.W; //半影北界 294 | G.J=G.W=0; for(i=0;i<3;i++) RS_PL::p2p(G.J,G.W,G,0, 1); V[8]=G.J, V[9]=G.W; //半影南界 295 | 296 | if(V[3]!=100&&V[5]!=100) 297 | { //粗算本影南北距离 298 | double x=(V[2]-V[4])*cos((V[3]+V[5])/2. ), y=V[3]-V[5]; 299 | RS_PL::Vb = to_str(cs_rEarA*sqrt(x*x+y*y),0)+"千米"; 300 | } 301 | } -------------------------------------------------------------------------------- /eph/eph_show.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "eph_show.h" 3 | #include "eph0.h" 4 | #include "eph.h" 5 | #include "eph_msc.h" 6 | #include "eph_szj.h" 7 | #include "eph_yspl.h" 8 | #include "eph_rsgs.h" 9 | #include "eph_rspl.h" 10 | #include "../mylib/lat_lon_data.h" 11 | #include "../mylib/tool.h" 12 | #include "../mylib/mystl/static_array.h" 13 | #include "../mylib/mystl/map.h" 14 | 15 | mystl::string rysCalc(Date d, bool is_utc, bool nasa_r) 16 | { 17 | double vJ=jw.J/radd; 18 | double vW=jw.W/radd; 19 | double jd = toJD(d)-J2000; 20 | if (is_utc) { 21 | jd+=-8/24.0+dt_T(jd); 22 | } 23 | 24 | MSC::calc(jd,vJ,vW,0); 25 | mystl::string s = "",s2; 26 | double J1, W1, J2, W2; 27 | double sr, mr, er, Er, d0, d1, d2; 28 | double msHJ = rad2mrad(MSC::mHJ - MSC::sHJ); 29 | int i; 30 | 31 | if (msHJ < 3 / radd || msHJ > 357 / radd) 32 | {// 日食图表放大计算 33 | J1 = MSC::mCJ2, W1 = MSC::mCW2, J2 = MSC::sCJ2, W2 = MSC::sCW2; // 用未做大气折射的来计算日食 34 | sr = MSC::sRad, mr = MSC::mRad; 35 | d1 = j1_j2(J1, W1, J2, W2) * rad, d0 = mr + sr; 36 | s2 = "此刻月亮本影中心线不经过地球。"; 37 | if (MSC::zx_W != 100) 38 | { 39 | mystl::string zxsJ = to_str(MSC::zx_J / _pi * 180, 5); 40 | mystl::string zxsW = to_str(MSC::zx_W / _pi * 180, 5); 41 | s2 = "食中心地标:经 " + zxsJ + " 纬 " + zxsW; 42 | } 43 | s = "日月站心视半径 " + m2fm(sr, 2, 0) + "及" + m2fm(mr, 2, 0) + " \n\033[31m" + s2 + "\033[0m\n" 44 | + "日月中心视距 " + m2fm(d1, 2, 0) + " 日月半径和 " + m2fm(d0, 2, 0) + "\n半径差 " + m2fm(sr - mr, 2, 0) + "\t距外切 " + m2fm(d1 - d0, 2, 0); 45 | 46 | // 显示南北界数据 47 | RS_PL::nasa_r = nasa_r; // 视径选择 48 | s = s + "\n--------------------------------------\n" + JD2str(jd + J2000) + " TD\n--------------------------------------\n" + "南北界点:经度    纬度\n"; 49 | mystl::static_array mc = 50 | { 51 | "食中心点", "本影北界", "本影南界", "半影北界", "半影南界"}; 52 | RS_PL::nbj(jd); 53 | for (i = 0; i < 5; i++) 54 | { 55 | s += mc[i] + ":"; 56 | if (RS_PL::V[i * 2 + 1] == 100) 57 | { 58 | s += "无     无\n"; 59 | continue; 60 | } 61 | s += to_str(RS_PL::V[i * 2] * radd, 5) + " " + to_str(RS_PL::V[i * 2 + 1] * radd, 5) + "\n"; 62 | } 63 | s += "中心类型:" + RS_PL::Vc + "食\n"; 64 | s += "本影南北界距约" + RS_PL::Vb; 65 | 66 | // 显示食甚等时间 67 | mystl::string td = " TD"; 68 | mc = {"初亏", "食甚", "复圆", "食既", "生光"}; 69 | RS_PL::secMax(jd, vJ, vW, 0); 70 | if (RS_PL::LX == "环") 71 | mc[3] = "环食始", mc[4] = "环食终"; // 环食没有食既和生光 72 | s = s + "\n--------------------------------------\n" + "时间表 (日" + RS_PL::LX + "食)\n"; 73 | for (i = 0; i < 5; i++) 74 | { 75 | jd = RS_PL::sT[i]; 76 | if (!jd) 77 | continue; 78 | if (is_utc) 79 | jd -= -8 / 24.0 + dt_T(jd), td = " UTC"; // 转为UTC(本地时间) 80 | s += mc[i] + ":" + JD2str(jd + J2000) + td + "\n"; 81 | } 82 | s += "时长: " + m2fm(RS_PL::dur * 86400, 1, 1) + "\n"; 83 | s += "食分: " + to_str(RS_PL::sf, 5) + "\n"; 84 | s += "月日视径比: " + to_str(RS_PL::b1, 5) + "(全或环食分)\n"; 85 | s += "是否NASA径比(1是,0否): " + to_str(RS_PL::nasa_r) + "\n"; 86 | s += "食分指日面直径被遮比例\n\n"; 87 | } 88 | 89 | if (msHJ > 170 / radd && msHJ < 190 / radd) 90 | {// 月食图表放大计算 91 | J1 = MSC::mCJ, W1 = MSC::mCW, J2 = MSC::sCJ + _pi, W2 = -MSC::sCW; 92 | er = MSC::eShadow, Er = MSC::eShadow2, mr = MSC::e_mRad; // 用未做大气折射的来计算日食 93 | d1 = j1_j2(J1, W1, J2, W2) * rad, d0 = mr + er, d2 = mr + Er; 94 | s = "本影半径 " + m2fm(er, 2, 0) + " 半影半径 " + m2fm(Er, 2,0) + 95 | " 月亮地心视半径 " + m2fm(mr, 2, 0) + "\n" + "影月中心距 " + m2fm(d1, 2, 0) + 96 | " 影月半径和 " + m2fm(d0, 2, 0) + " \n距相切 \033[31m" + m2fm(d1 - d0, 2, 0) + "\033[0m 距第二相切 " + m2fm(d1 - d2, 2, 0); 97 | 98 | mystl::string td = " TD"; 99 | mystl::static_array mc = {"初亏", "食甚", "复圆", "半影食始", "半影食终", "食既", "生光"}; 100 | YS_PL::lecMax(jd); 101 | s = s + "\n\n时间表(月" + YS_PL::LX + "食)\n"; 102 | for (int i = 0; i < 7; i++) 103 | { 104 | jd = YS_PL::lT[i]; 105 | if (!jd) 106 | continue; 107 | if (is_utc) 108 | jd -= -8 / 24.0 + dt_T(jd), td = " UTC"; // 转为UTC(本地时间) 109 | s = s+mc[i] + ":" + JD2str(jd + J2000) + td + "\n"; 110 | } 111 | s += "食分:" + to_str(YS_PL::sf, 5) + "\n"; 112 | s += "食分指月面直径被遮比例\n\n"; 113 | } 114 | s += MSC::toStr(true); 115 | return s; 116 | } 117 | 118 | mystl::string rs_search(int Y, int M, int n, bool fs) 119 | { 120 | //查找日食 121 | int i, k; 122 | _ECFAST r; 123 | mystl::string s = "", s2 = ""; 124 | double jd = toJD({ Y, M, 1, 0, 0, 0 }) - J2000; //取屏幕时间 125 | jd = MS_aLon_t2(int2((jd + 8) / 29.5306) * _pi * 2) * 36525; //定朔 126 | for (i = 0, k = 0; i < n; i++) { 127 | r = ecFast(jd); //低精度高速搜索 128 | if (r.lx == "NN") { 129 | jd += 29.5306; 130 | continue; 131 | } 132 | //排除不可能的情况,加速计算 133 | if (!r.ac) { 134 | if (fs == 0) RS_GS::init(jd, 2); //低精度 135 | if (fs == 1) RS_GS::init(jd, 7); //高精度 136 | _FEATURE rr = RS_GS::feature(jd); 137 | r = {rr.jd, rr.jdSuo, r.ac, rr.lx}; 138 | } 139 | if (r.lx != "N") { 140 | s += JD2str(r.jd + J2000).substr(0, 11); 141 | s += r.lx; 142 | k++; 143 | if (k % 5 == 0) 144 | s += "\n"; 145 | if (k % 100 == 0) 146 | s2 += s, s = ""; 147 | } 148 | jd = r.jd + 29.5306; 149 | } 150 | return s2 + s; 151 | } 152 | 153 | 154 | mystl::string rs2_calc(uint8_t fs,double jd0, double step) 155 | { 156 | if (fs==0) return ""; 157 | static double jd = 2454679.926741-J2000; //取屏幕时间 158 | 159 | if(fs==1) jd = jd0; 160 | // if(fs==2) ; //保持时间不变 161 | if(fs==3) jd -= step; 162 | if(fs==4) jd += step; 163 | jd = MS_aLon_t2( int2((jd+8)/29.5306)*_pi*2 )*36525.0; //归朔 164 | //Cp10_jd.value = Cp10_jd2.value = (jd+J2000),6); //保存在屏幕上 165 | std::cout< lxb={{"T","全食"},{"A","环食"},{"P","偏食"},{"T0","无中心全食"},{"T1","部分本影有中心全食"},{"A0","无中心环食"},{"A1","部分伪本影有中心全食"},{"H","全环全"},{"H2","全全环"},{"H3","环全全"}}; 168 | 169 | mystl::string s=""; 170 | //计算单个日食 171 | if(fs==1||fs==2||fs==3||fs==4) 172 | { 173 | RS_GS::init(jd,7); 174 | _FEATURE r = RS_GS::feature(jd); //特征计算 175 | if (r.lx == "N") { s = "无日食"; } 176 | else { 177 | s = s + "\n" 178 | + "\033[1m本次日食概述(力学时)\033[0m\n" 179 | 180 | + "偏食始:" + JD2str(r.gk3[2] + J2000) + " " + rad2str2(r.gk3[0]) + "," + rad2str2(r.gk3[1]) + "\n" 181 | + "中心始:" + JD2str(r.gk1[2] + J2000) + " " + rad2str2(r.gk1[0]) + "," + rad2str2(r.gk1[1]) + "\n" 182 | + (r.gk5[1] != 100 ? 183 | "视午食:" + JD2str(r.gk5[2] + J2000) + " " + rad2str2(r.gk5[0]) + "," + rad2str2(r.gk5[1]) + "\n" : "") 184 | + "中心终:" + JD2str(r.gk2[2] + J2000) + " " + rad2str2(r.gk2[0]) + "," + rad2str2(r.gk2[1]) + "\n" 185 | + "偏食终:" + JD2str(r.gk4[2] + J2000) + " " + rad2str2(r.gk4[0]) + "," + rad2str2(r.gk4[1]) + "\n" 186 | 187 | + "\033[1m中心点特征\033[0m\n" 188 | + "影轴地心距 γ = " + to_str(r.D, 4) + "\n" 189 | + "中心地标 (经,纬) = " + to_str((r.zxJ * radd), 2) + "," + to_str((r.zxW * radd), 2) + "\n" 190 | + "中心时刻 tm = " + JD2str(r.jd + J2000) + "\n" 191 | + "太阳方位 (经,纬) = " + to_str((r.Sdp[0] * radd), 0) + "," + to_str((r.Sdp[1] * radd), 0) + "\n" 192 | + "日食类型 LX = " + r.lx + " " + lxb[r.lx] + "\n" 193 | + "食分=" + to_str(r.sf, 4) + ", 食延=" + m2fm(r.tt * 86400, 0, 2) + ", 食带=" + to_str(r.dw, 0) + "km\n" 194 | + "\n"; 195 | } 196 | // std::cout< struct _cond { using type = F; }; 76 | template struct _cond { using type = T; }; 77 | template using cond = typename _cond::type; 78 | 79 | template 80 | #if defined(_MSC_VER) 81 | __forceinline 82 | #else 83 | inline 84 | #endif 85 | char* to_text_from_integer(char* b, T i) 86 | { 87 | constexpr auto q = sizeof(T); 88 | using U = cond>>; 89 | 90 | // convert bool to int before test with unary + to silence warning if T happens to be bool 91 | U const n = +i < 0 ? *b++ = '-', U(0) - U(i) : U(i); 92 | 93 | if (n < u32(1e2)) 94 | { 95 | *reinterpret_cast(b) = digits.fd[n]; 96 | return n < 10 ? b + 1 : b + 2; 97 | } 98 | if (n < u32(1e6)) 99 | { 100 | if (n < u32(1e4)) 101 | { 102 | auto f0 = u32(10 * (1 << 24) / 1e3 + 1) * n; 103 | *reinterpret_cast(b) = digits.fd[f0 >> 24]; 104 | b -= n < u32(1e3); 105 | auto f2 = (f0 & mask24) * 100; 106 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 24]; 107 | return b + 4; 108 | } 109 | auto f0 = u64(10 * (1ull << 32ull)/ 1e5 + 1) * n; 110 | *reinterpret_cast(b) = digits.fd[f0 >> 32]; 111 | b -= n < u32(1e5); 112 | auto f2 = (f0 & mask32) * 100; 113 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 114 | auto f4 = (f2 & mask32) * 100; 115 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 116 | return b + 6; 117 | } 118 | if (n < u64(1ull << 32ull)) 119 | { 120 | if (n < u32(1e8)) 121 | { 122 | auto f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * n >> 16; 123 | *reinterpret_cast(b) = digits.fd[f0 >> 32]; 124 | b -= n < u32(1e7); 125 | auto f2 = (f0 & mask32) * 100; 126 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 127 | auto f4 = (f2 & mask32) * 100; 128 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 129 | auto f6 = (f4 & mask32) * 100; 130 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 32]; 131 | return b + 8; 132 | } 133 | auto f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * n; 134 | *reinterpret_cast(b) = digits.fd[f0 >> 57]; 135 | b -= n < u32(1e9); 136 | auto f2 = (f0 & mask57) * 100; 137 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 57]; 138 | auto f4 = (f2 & mask57) * 100; 139 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 57]; 140 | auto f6 = (f4 & mask57) * 100; 141 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 57]; 142 | auto f8 = (f6 & mask57) * 100; 143 | *reinterpret_cast(b + 8) = digits.dd[f8 >> 57]; 144 | return b + 10; 145 | } 146 | 147 | // if we get here U must be u64 but some compilers don't know that, so reassign n to a u64 to avoid warnings 148 | u32 z = n % u32(1e8); 149 | u64 u = n / u32(1e8); 150 | 151 | if (u < u32(1e2)) 152 | { 153 | // u can't be 1 digit (if u < 10 it would have been handled above as a 9 digit 32bit number) 154 | *reinterpret_cast(b) = digits.dd[u]; 155 | b += 2; 156 | } 157 | else if (u < u32(1e6)) 158 | { 159 | if (u < u32(1e4)) 160 | { 161 | auto f0 = u32(10 * (1 << 24) / 1e3 + 1) * u; 162 | *reinterpret_cast(b) = digits.fd[f0 >> 24]; 163 | b -= u < u32(1e3); 164 | auto f2 = (f0 & mask24) * 100; 165 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 24]; 166 | b += 4; 167 | } 168 | else 169 | { 170 | auto f0 = u64(10 * (1ull << 32ull) / 1e5 + 1) * u; 171 | *reinterpret_cast(b) = digits.fd[f0 >> 32]; 172 | b -= u < u32(1e5); 173 | auto f2 = (f0 & mask32) * 100; 174 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 175 | auto f4 = (f2 & mask32) * 100; 176 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 177 | b += 6; 178 | } 179 | } 180 | else if (u < u32(1e8)) 181 | { 182 | auto f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * u >> 16; 183 | *reinterpret_cast(b) = digits.fd[f0 >> 32]; 184 | b -= u < u32(1e7); 185 | auto f2 = (f0 & mask32) * 100; 186 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 187 | auto f4 = (f2 & mask32) * 100; 188 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 189 | auto f6 = (f4 & mask32) * 100; 190 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 32]; 191 | b += 8; 192 | } 193 | else if (u < u64(1ull << 32ull)) 194 | { 195 | auto f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * u; 196 | *reinterpret_cast(b) = digits.fd[f0 >> 57]; 197 | b -= u < u32(1e9); 198 | auto f2 = (f0 & mask57) * 100; 199 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 57]; 200 | auto f4 = (f2 & mask57) * 100; 201 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 57]; 202 | auto f6 = (f4 & mask57) * 100; 203 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 57]; 204 | auto f8 = (f6 & mask57) * 100; 205 | *reinterpret_cast(b + 8) = digits.dd[f8 >> 57]; 206 | b += 10; 207 | } 208 | else 209 | { 210 | u32 y = u % u32(1e8); 211 | u /= u32(1e8); 212 | 213 | // u is 2, 3, or 4 digits (if u < 10 it would have been handled above) 214 | if (u < u32(1e2)) 215 | { 216 | *reinterpret_cast(b) = digits.dd[u]; 217 | b += 2; 218 | } 219 | else 220 | { 221 | auto f0 = u32(10 * (1 << 24) / 1e3 + 1) * u; 222 | *reinterpret_cast(b) = digits.fd[f0 >> 24]; 223 | b -= u < u32(1e3); 224 | auto f2 = (f0 & mask24) * 100; 225 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 24]; 226 | b += 4; 227 | } 228 | // do 8 digits 229 | auto f0 = (u64((1ull << 48ull) / 1e6 + 1) * y >> 16) + 1; 230 | *reinterpret_cast(b) = digits.dd[f0 >> 32]; 231 | auto f2 = (f0 & mask32) * 100; 232 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 233 | auto f4 = (f2 & mask32) * 100; 234 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 235 | auto f6 = (f4 & mask32) * 100; 236 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 32]; 237 | b += 8; 238 | } 239 | // do 8 digits 240 | auto f0 = (u64((1ull << 48ull) / 1e6 + 1) * z >> 16) + 1; 241 | *reinterpret_cast(b) = digits.dd[f0 >> 32]; 242 | auto f2 = (f0 & mask32) * 100; 243 | *reinterpret_cast(b + 2) = digits.dd[f2 >> 32]; 244 | auto f4 = (f2 & mask32) * 100; 245 | *reinterpret_cast(b + 4) = digits.dd[f4 >> 32]; 246 | auto f6 = (f4 & mask32) * 100; 247 | *reinterpret_cast(b + 6) = digits.dd[f6 >> 32]; 248 | return b + 8; 249 | } 250 | } 251 | #endif // JEAIII_TO_TEXT_H_ 252 | -------------------------------------------------------------------------------- /mylib/mystl/iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef MYTINYSTL_ITERATOR_H_ 2 | #define MYTINYSTL_ITERATOR_H_ 3 | 4 | // 这个头文件用于迭代器设计,包含了一些模板结构体与全局函数, 5 | 6 | #include 7 | 8 | #include "type_traits.h" 9 | 10 | namespace mystl 11 | { 12 | 13 | // 五种迭代器类型 14 | struct input_iterator_tag {}; 15 | struct output_iterator_tag {}; 16 | struct forward_iterator_tag : public input_iterator_tag {}; 17 | struct bidirectional_iterator_tag : public forward_iterator_tag {}; 18 | struct random_access_iterator_tag : public bidirectional_iterator_tag {}; 19 | 20 | // iterator 模板 21 | template 23 | struct iterator 24 | { 25 | typedef Category iterator_category; 26 | typedef T value_type; 27 | typedef Pointer pointer; 28 | typedef Reference reference; 29 | typedef Distance difference_type; 30 | }; 31 | 32 | // iterator traits 33 | 34 | template 35 | struct has_iterator_cat 36 | { 37 | private: 38 | struct two { char a; char b; }; 39 | template static two test(...); 40 | template static char test(typename U::iterator_category* = 0); 41 | public: 42 | static const bool value = sizeof(test(0)) == sizeof(char); 43 | }; 44 | 45 | template 46 | struct iterator_traits_impl {}; 47 | 48 | template 49 | struct iterator_traits_impl 50 | { 51 | typedef typename Iterator::iterator_category iterator_category; 52 | typedef typename Iterator::value_type value_type; 53 | typedef typename Iterator::pointer pointer; 54 | typedef typename Iterator::reference reference; 55 | typedef typename Iterator::difference_type difference_type; 56 | }; 57 | 58 | template 59 | struct iterator_traits_helper {}; 60 | 61 | template 62 | struct iterator_traits_helper 63 | : public iterator_traits_impl::value || 65 | std::is_convertible::value> 66 | { 67 | }; 68 | 69 | // 萃取迭代器的特性 70 | template 71 | struct iterator_traits 72 | : public iterator_traits_helper::value> {}; 73 | 74 | // 针对原生指针的偏特化版本 75 | template 76 | struct iterator_traits 77 | { 78 | typedef random_access_iterator_tag iterator_category; 79 | typedef T value_type; 80 | typedef T* pointer; 81 | typedef T& reference; 82 | typedef ptrdiff_t difference_type; 83 | }; 84 | 85 | template 86 | struct iterator_traits 87 | { 88 | typedef random_access_iterator_tag iterator_category; 89 | typedef T value_type; 90 | typedef const T* pointer; 91 | typedef const T& reference; 92 | typedef ptrdiff_t difference_type; 93 | }; 94 | 95 | template >::value> 96 | struct has_iterator_cat_of 97 | : public m_bool_constant::iterator_category, U>::value> 99 | { 100 | }; 101 | 102 | // 萃取某种迭代器 103 | template 104 | struct has_iterator_cat_of : public m_false_type {}; 105 | 106 | template 107 | struct is_input_iterator : public has_iterator_cat_of {}; 108 | 109 | template 110 | struct is_output_iterator : public has_iterator_cat_of {}; 111 | 112 | template 113 | struct is_forward_iterator : public has_iterator_cat_of {}; 114 | 115 | template 116 | struct is_bidirectional_iterator : public has_iterator_cat_of {}; 117 | 118 | template 119 | struct is_random_access_iterator : public has_iterator_cat_of {}; 120 | 121 | template 122 | struct is_iterator : 123 | public m_bool_constant::value || 124 | is_output_iterator::value> 125 | { 126 | }; 127 | 128 | // 萃取某个迭代器的 category 129 | template 130 | typename iterator_traits::iterator_category 131 | iterator_category(const Iterator&) 132 | { 133 | typedef typename iterator_traits::iterator_category Category; 134 | return Category(); 135 | } 136 | 137 | // 萃取某个迭代器的 distance_type 138 | template 139 | typename iterator_traits::difference_type* 140 | distance_type(const Iterator&) 141 | { 142 | return static_cast::difference_type*>(0); 143 | } 144 | 145 | // 萃取某个迭代器的 value_type 146 | template 147 | typename iterator_traits::value_type* 148 | value_type(const Iterator&) 149 | { 150 | return static_cast::value_type*>(0); 151 | } 152 | 153 | // 以下函数用于计算迭代器间的距离 154 | 155 | // distance 的 input_iterator_tag 的版本 156 | template 157 | typename iterator_traits::difference_type 158 | distance_dispatch(InputIterator first, InputIterator last, input_iterator_tag) 159 | { 160 | typename iterator_traits::difference_type n = 0; 161 | while (first != last) 162 | { 163 | ++first; 164 | ++n; 165 | } 166 | return n; 167 | } 168 | 169 | // distance 的 random_access_iterator_tag 的版本 170 | template 171 | typename iterator_traits::difference_type 172 | distance_dispatch(RandomIter first, RandomIter last, 173 | random_access_iterator_tag) 174 | { 175 | return last - first; 176 | } 177 | 178 | template 179 | typename iterator_traits::difference_type 180 | distance(InputIterator first, InputIterator last) 181 | { 182 | return distance_dispatch(first, last, iterator_category(first)); 183 | } 184 | 185 | // 以下函数用于让迭代器前进 n 个距离 186 | 187 | // advance 的 input_iterator_tag 的版本 188 | template 189 | void advance_dispatch(InputIterator& i, Distance n, input_iterator_tag) 190 | { 191 | while (n--) 192 | ++i; 193 | } 194 | 195 | // advance 的 bidirectional_iterator_tag 的版本 196 | template 197 | void advance_dispatch(BidirectionalIterator& i, Distance n, bidirectional_iterator_tag) 198 | { 199 | if (n >= 0) 200 | while (n--) ++i; 201 | else 202 | while (n++) --i; 203 | } 204 | 205 | // advance 的 random_access_iterator_tag 的版本 206 | template 207 | void advance_dispatch(RandomIter& i, Distance n, random_access_iterator_tag) 208 | { 209 | i += n; 210 | } 211 | 212 | template 213 | void advance(InputIterator& i, Distance n) 214 | { 215 | advance_dispatch(i, n, iterator_category(i)); 216 | } 217 | 218 | /*****************************************************************************************/ 219 | 220 | // 模板类 : reverse_iterator 221 | // 代表反向迭代器,使前进为后退,后退为前进 222 | template 223 | class reverse_iterator 224 | { 225 | private: 226 | Iterator current; // 记录对应的正向迭代器 227 | 228 | public: 229 | // 反向迭代器的五种相应型别 230 | typedef typename iterator_traits::iterator_category iterator_category; 231 | typedef typename iterator_traits::value_type value_type; 232 | typedef typename iterator_traits::difference_type difference_type; 233 | typedef typename iterator_traits::pointer pointer; 234 | typedef typename iterator_traits::reference reference; 235 | 236 | typedef Iterator iterator_type; 237 | typedef reverse_iterator self; 238 | 239 | public: 240 | // 构造函数 241 | reverse_iterator() {} 242 | explicit reverse_iterator(iterator_type i) :current(i) {} 243 | reverse_iterator(const self& rhs) :current(rhs.current) {} 244 | 245 | public: 246 | // 取出对应的正向迭代器 247 | iterator_type base() const 248 | { return current; } 249 | 250 | // 重载操作符 251 | reference operator*() const 252 | { // 实际对应正向迭代器的前一个位置 253 | auto tmp = current; 254 | return *--tmp; 255 | } 256 | pointer operator->() const 257 | { 258 | return &(operator*()); 259 | } 260 | 261 | // 前进(++)变为后退(--) 262 | self& operator++() 263 | { 264 | --current; 265 | return *this; 266 | } 267 | self operator++(int) 268 | { 269 | self tmp = *this; 270 | --current; 271 | return tmp; 272 | } 273 | // 后退(--)变为前进(++) 274 | self& operator--() 275 | { 276 | ++current; 277 | return *this; 278 | } 279 | self operator--(int) 280 | { 281 | self tmp = *this; 282 | ++current; 283 | return tmp; 284 | } 285 | 286 | self& operator+=(difference_type n) 287 | { 288 | current -= n; 289 | return *this; 290 | } 291 | self operator+(difference_type n) const 292 | { 293 | return self(current - n); 294 | } 295 | self& operator-=(difference_type n) 296 | { 297 | current += n; 298 | return *this; 299 | } 300 | self operator-(difference_type n) const 301 | { 302 | return self(current + n); 303 | } 304 | 305 | reference operator[](difference_type n) const 306 | { 307 | return *(*this + n); 308 | } 309 | }; 310 | 311 | // 重载 operator- 312 | template 313 | typename reverse_iterator::difference_type 314 | operator-(const reverse_iterator& lhs, 315 | const reverse_iterator& rhs) 316 | { 317 | return rhs.base() - lhs.base(); 318 | } 319 | 320 | // 重载比较操作符 321 | template 322 | bool operator==(const reverse_iterator& lhs, 323 | const reverse_iterator& rhs) 324 | { 325 | return lhs.base() == rhs.base(); 326 | } 327 | 328 | template 329 | bool operator<(const reverse_iterator& lhs, 330 | const reverse_iterator& rhs) 331 | { 332 | return rhs.base() < lhs.base(); 333 | } 334 | 335 | template 336 | bool operator!=(const reverse_iterator& lhs, 337 | const reverse_iterator& rhs) 338 | { 339 | return !(lhs == rhs); 340 | } 341 | 342 | template 343 | bool operator>(const reverse_iterator& lhs, 344 | const reverse_iterator& rhs) 345 | { 346 | return rhs < lhs; 347 | } 348 | 349 | template 350 | bool operator<=(const reverse_iterator& lhs, 351 | const reverse_iterator& rhs) 352 | { 353 | return !(rhs < lhs); 354 | } 355 | 356 | template 357 | bool operator>=(const reverse_iterator& lhs, 358 | const reverse_iterator& rhs) 359 | { 360 | return !(lhs < rhs); 361 | } 362 | 363 | } // namespace mystl 364 | 365 | #endif // !MYTINYSTL_ITERATOR_H_ 366 | 367 | -------------------------------------------------------------------------------- /test/test1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 寿星天文历v5.05js版 C++翻译工程 3 | API测试 4 | 2018-8-28 5 | */ 6 | #include 7 | #include 8 | #include 9 | 10 | #include "../mylib/tool.h" 11 | #include "../lunar/lunar.h" 12 | #include "../eph/eph_show.h" 13 | #include "../eph/eph0.h" 14 | #include "../eph/eph.h" 15 | #include "../mylib/lat_lon_data.h" 16 | #include "../mylib/tool.h" 17 | #include "../mylib/math_patch.h" 18 | #include "../mylib/mystl/static_array.h" 19 | #include "../mylib/mystl/map.h" 20 | 21 | #define MAP_H 18 22 | #define MAP_W 7 23 | std::array,MAP_H> strmap; 24 | 25 | #include 26 | #include 27 | 28 | mystl::string txFormatT(double t) 29 | { //天象时间格式化输出 30 | double t1 = t * 36525 + J2000; 31 | double t2 = t1 - dt_T(t1 - J2000) + 8.0 / 24; 32 | return JD2str(t1) + " TD " + JD2str(t2).substr(9, 11) + " UT "; 33 | } 34 | 35 | void tianXiang(int xm, int xm2, Date dat,int n=10) 36 | { 37 | mystl::static_array xxName = {"地球","水星","金星","火星","木星","土星","天王星","海王星","冥王星"}; 38 | double jd = toJD({dat.Y, dat.M, dat.D}) - J2000;//取屏幕时间 39 | mystl::string s = ""; 40 | int i; 41 | double re0; 42 | mystl::array2 re; 43 | mystl::array4 re2; 44 | jd /= 36525.0; 45 | if (xm == 1 || xm == 2) 46 | { //求月亮近远点 47 | for (i = 0; i < n; i++, jd = re[0] + 27.555 / 36525.0) 48 | { 49 | if (xm == 1) 50 | re = moonMinR(jd, 1);//求近点 51 | if (xm == 2) 52 | re = moonMinR(jd, 0);//求远点 53 | s += txFormatT(re[0]) + to_str(re[1],2) + "千米\n"; 54 | } 55 | 56 | } 57 | if (xm == 3 || xm == 4) 58 | { //求月亮升降交点 59 | for (i = 0; i < n; i++, jd = re[0] + 27.555 / 36525.0) 60 | { 61 | if (xm == 3) 62 | re = moonNode(jd, 1);//求升 63 | if (xm == 4) 64 | re = moonNode(jd, 0);//求降 65 | s += txFormatT(re[0]) + rad2str(rad2mrad(re[1]), 0) + "\n"; 66 | } 67 | 68 | } 69 | if (xm == 5 || xm == 6) 70 | { //求地球近远点 71 | for (i = 0; i < n; i++, jd = re[0] + 365.259636 / 36525.0) 72 | { 73 | if (xm == 5) 74 | re = earthMinR(jd, 1);//求近点 75 | if (xm == 6) 76 | re = earthMinR(jd, 0);//求远点 77 | s += txFormatT(re[0]) + to_str(re[1],8) + " AU\n"; 78 | } 79 | } 80 | if (xm == 7 || xm == 8) 81 | { //大距计算 82 | for (i = 0; i < n; i++, jd = re[0] + 115.8774777586 / 36525.0) 83 | { 84 | if (xm == 7) 85 | re = daJu(1, jd, 1);//求水星东大距 86 | if (xm == 8) 87 | re = daJu(1, jd, 0);//求水星东西距 88 | s += txFormatT(re[0]) + to_str((re[1] / M_PI * 180),5) + "度\n"; 89 | } 90 | } 91 | if (xm == 9 || xm == 10) 92 | { //大距计算 93 | for (i = 0; i < n; i++, jd = re[0] + 583.9213708245 / 36525.0) 94 | { 95 | if (xm == 9) 96 | re = daJu(2, jd, 1);//求金星东大距 97 | if (xm == 10) 98 | re = daJu(2, jd, 0);//求金星东西距 99 | s += txFormatT(re[0]) + to_str((re[1] / M_PI * 180),5) + "度\n"; 100 | } 101 | } 102 | if (xm == 11) 103 | { //合月计算 104 | s += xxName[xm2] + "赤经合月\n"; 105 | s += "合月时间(TD UT) 星月赤纬差(小于1度可能月掩星,由视差决定)\n"; 106 | for (i = 0; i < n; i++, jd = re2[0] + 28.0 / 36525.0) 107 | { 108 | re2 = xingHY(xm2, jd); 109 | s += txFormatT(re2[0]) + to_str((-re2[1] / M_PI * 180),5) + "度\n"; 110 | } 111 | } 112 | if (xm == 12 || xm == 13) 113 | { 114 | if (xm == 12) 115 | s = xxName[xm2] + "合日(地内行星上合)\n"; 116 | if (xm == 13) 117 | s = xxName[xm2] + "冲日(地内行星下合)\n"; 118 | s += "黄经合/冲日时间(TD UT) 星日赤纬差\n"; 119 | for (i = 0; i < n; i++, jd = re[0] + cs_xxHH[xm2 - 1] / 36525.0) 120 | { 121 | if (xm == 12) 122 | re = xingHR(xm2, jd, 0); 123 | if (xm == 13) 124 | re = xingHR(xm2, jd, 1); 125 | s += txFormatT(re[0]) + to_str((-re[1] / M_PI * 180),5) + "度\n"; 126 | } 127 | } 128 | 129 | if (xm == 14 || xm == 15) 130 | { //顺留 131 | if (xm == 14) 132 | s = xxName[xm2] + "顺留\n"; 133 | if (xm == 15) 134 | s = xxName[xm2] + "逆留\n"; 135 | s += "留时间(TD UT)\n"; 136 | for (i = 0; i < n; i++, jd = re0 + cs_xxHH[xm2 - 1] / 36525.0) 137 | { 138 | if (xm == 14) 139 | re0 = xingLiu(xm2, jd, 1); 140 | if (xm == 15) 141 | re0 = xingLiu(xm2, jd, 0); 142 | s += txFormatT(re0) + "\n"; 143 | } 144 | } 145 | std::cout<< s< 1000) 156 | { 157 | std::cout<<"个数太多了"<>jiao; 179 | } 180 | int i; 181 | double r, T; 182 | mystl::string s = "月-日黄经差" + to_str(jiao) + "\n", s2 = ""; 183 | int n0 = int2(y * (365.2422 / 29.53058886));//截止当年首经历朔望的个数 184 | for (i = 0; i < n; i++) 185 | { 186 | T = MS_aLon_t((n0 + i + jiao / 360.0) * 2 * M_PI);//精确时间计算,入口参数是当年各朔望黄经 187 | r = XL1_calc(2, T, -1);//计算月亮 188 | s2 += JD2str(T * 36525 + J2000 + 8.0 / 24 - dt_T(T * 36525)) + " " + to_str(r,2) + "千米\n";//日期转为字串 189 | if (i % 50 == 0) 190 | s += s2, s2 = ""; 191 | } 192 | 193 | std::cout<< s + s2< maxT) 248 | maxT = T; 249 | } 250 | std::cout << "\033[31;1m" << to_str(2000 + y) << "年之后" << to_str(N) << "个朔日粗算与精算的最大差异:" << to_str(maxT) << "秒。\033[0m" << std::endl; 251 | } 252 | 253 | void dingSuo_cmp(int y=2000,int N=10) 254 | { //定朔测试函数 255 | y-=2000; 256 | int i; 257 | double T, maxT = 0,W; 258 | int n = int2(y * (365.2422 / 29.53058886));//截止当年首经历朔望的个数 259 | for (i = 0; i < N; i++) 260 | { 261 | W = (n + i / 24.0) * 2 * M_PI; 262 | T = MS_aLon_t2(W) - MS_aLon_t(W);//合塑粗算与精算的差异 263 | T = int2(abs(T * 36525 * 86400)); 264 | if (T > maxT) 265 | maxT = T; 266 | } 267 | std::cout<<"\033[31;1m"<< to_str(2000 + y) << "年之后" << to_str(N) << "个朔日粗算与精算的最大差异:" << to_str(maxT) << "秒。\033[0m"< (d2 - d1).count()*1000 << "毫秒/10千个\n" << "低精度:" << std::chrono::duration (d3 - d2).count()*1000 << "毫秒/10千个\n\033[0m"; 282 | } 283 | 284 | void dingSuo_v() 285 | { //定朔计算速度测试 286 | int i; 287 | auto d1 = std::chrono::system_clock::now(); 288 | for (i = 0; i < 10000; i++) 289 | MS_aLon_t(0); 290 | 291 | auto d2 = std::chrono::system_clock::now(); 292 | for (i = 0; i < 10000; i++) 293 | MS_aLon_t2(0); 294 | 295 | auto d3 = std::chrono::system_clock::now(); 296 | std::cout<< "\033[31;1m高精度:" << std::chrono::duration (d2 - d1).count()*1000 << "毫秒/10千个\n" << "低精度:" << std::chrono::duration (d3 - d2).count()*1000 << "毫秒/10千个\n\033[0m"; 297 | } 298 | 299 | void ML_calc(Date dat) 300 | { 301 | MLBZ ob={}; 302 | double jd = toJD({dat.Y,dat.M,dat.D,dat.h,dat.m,dat.s}); 303 | OBB::mingLiBaZi( jd+(-8.0)/24-J2000, jw.J/radd, ob ); //八字计算 304 | 305 | std::cout<< 306 | "\033[31;1m[日标]:\033[0m"<<"公历 "<tm_year + 1900, 321 | bjs->tm_mon+1, 322 | bjs->tm_mday, 323 | bjs->tm_hour, 324 | bjs->tm_min, 325 | (double)bjs->tm_sec 326 | }; 327 | return dat; 328 | } 329 | 330 | void initmap(OB_LUN lun) 331 | { 332 | mystl::map str_yx={{"望","\033[33m●"},{"上弦","\033[33m∪"},{"朔","\033[38;5;245m●"},{"下弦","\033[33m∩"}}; 333 | for (int i=0;i<18;i++) 334 | { 335 | for (int j=0;j<7;j++) 336 | { 337 | if (i%3==2) 338 | strmap[i][j]="\033[32m--------"; 339 | else 340 | strmap[i][j]=" "; 341 | } 342 | } 343 | for (int i = 0;i < lun.dn;i++) 344 | { 345 | int y=lun.day[i].weeki; 346 | int x=lun.day[i].week; 347 | int j=i<9?6:4; 348 | strmap[y*3][x] = x==0||x==6?"\033[31m":"\033[37m"; 349 | strmap[y*3][x] += str_rmc0[i]; 350 | if (lun.day[i].jqmc.length()) 351 | strmap[y*3][x]+="\033[32m◆",j--; 352 | if (lun.day[i].yxmc.length()) 353 | strmap[y*3][x]+=str_yx[lun.day[i].yxmc],j--; 354 | for (int k = 0;k < j;k++) 355 | strmap[y*3][x]+=" \033[0m\033[1m"; 356 | if (!j) 357 | strmap[y*3][x]+="\033[0m\033[1m"; 358 | if (lun.day[i].Ldi==0) 359 | strmap[y*3+1][x]="\033[38;5;123m",strmap[y*3+1][x] 360 | =strmap[y*3+1][x]+lun.day[i].Lleap+strmap[y*3+1][x] 361 | +lun.day[i].Lmc+strmap[y*3+1][x] 362 | +(lun.day[i].Lmc.length()==6?"":"月") 363 | +strmap[y*3+1][x]+str_dx[lun.day[i].Ldn-29]+strmap[y*3+1][x] 364 | +(lun.day[i].Lleap.length()?"\033[0m\033[1m":" \033[0m\033[1m"); 365 | else 366 | strmap[y*3+1][x]="",strmap[y*3+1][x]=strmap[y*3+1][x] 367 | +lun.day[i].Ldc+strmap[y*3+1][x]+" \033[0m\033[1m"; 368 | if (lun.day[i].Ljq.length()) 369 | strmap[y*3+1][x]="\033[34m",strmap[y*3+1][x]=strmap[y*3+1][x] 370 | +lun.day[i].Ljq+strmap[y*3+1][x]+=" \033[0m\033[1m"; 371 | 372 | } 373 | // printf("公元 %04d年%02d月%02d日 周%s %s座\n",lun.day[1].XiZ); 374 | } 375 | void drawmap() 376 | { 377 | for (int k=0;k i:strmap) 384 | { 385 | for (mystl::string j:i) 386 | std::cout<