📄 hugecalc_example.cpp
字号:
// HugeCalcDemo.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include "../HugeCalc_Dll_Import/HugeCalc.h" // 公共接口
#include "../HugeCalc_Dll_Import/HugeInt.h" // 10进制系统
#include "../HugeCalc_Dll_Import/HugeIntX.h" // 16进制系统
#ifndef _UNICODE
#pragma comment( lib, "../HugeCalc_Dll_Import/HugeCalc.lib" )
#else
#pragma comment( lib, "../HugeCalc_Dll_Import/HugeCalcU.lib" )
#endif
// Project -> Setting -> C/C++ -> Code Generation --> Use run-time library:
// Win32 Debug: Debug Multithreaded DLL
// Win32 Release: Multithreaded DLL
int main(int argc, char* argv[])
{
// printf("Hello World!\n");
cout << HugeCalc::GetVersion() << endl << endl;
// printf( HugeCalc::GetVersion());
// printf( "\r\n\r\n" );
if ( HC_LICENSE_NONE == HugeCalc::GetLicenseLevel() )
{
cout << endl << "警告:您未通过 HugeCalc.dll 的许可认证!" \
<< endl << endl << "解决方案可选下列方案之一:" \
<< endl << " 一、请移动和[或]修改文件名为:../CopyrightByGuoXianqiang/[../]HugeCalc.exe;" \
<< endl << " 二、请在 HugeCalc.chm 中进行注册(一劳永逸)。" \
<< flush << flush;
}
else
{
// http://www.swox.com/gmp/#TRY
// 1 + gcd(87324,78263148,7896) * (10^1989879887 mod 471!)
system( "pause" );
U32_VECTOR v32Num;
v32Num.push_back( 87324 );
v32Num.push_back( 78263148 );
v32Num.push_back( 7896 );
// 初始化
HugeCalc::SetTerminate( FALSE );
HugeCalc::ResetTimer();
HugeCalc::EnableTimer();
// 下面测试:由16进制内核计算,然后将结果转换成10进制内核系统输出
// 测试:由16进制内核计算
CHugeIntX HugeIntX;
++( HugeIntX.PowMod( (UINT32)10, (UINT32)1989879887, CHugeIntX().Factorial( 471 ) ) *= Gcd( v32Num ));
// 记录计算耗时
UINT32 u32Timer = HugeCalc::GetTimer( TRUE );
// 测试:将结果转换成10进制内核输出
CHugeInt HugeInt( HugeIntX );
LPCTSTR lpShow = HugeInt.ConvertToStr( FS_NORMAL );
// 记录总耗时,并终止计时器
UINT32 u32Timer_Total = HugeCalc::GetTimer( FALSE );
// 输出
// printf( "\r\nThe result of executing 1 + gcd(87324,78263148,7896) * (10^1989879887 mod 471!) is:\r\n\r\n" \
// "%s (%u digitals)\r\n\r\n" \
// "computation took %u ms\r\n" \
// "output conversion took %u ms\r\n\r\n", lpShow, HugeInt.GetDigitals(), u32Timer, u32Timer_Total - u32Timer );
cout << endl << "The result of executing 1 + gcd(87324,78263148,7896) * (10^1989879887 mod 471!) is:" \
<< endl << endl << lpShow << "(" << HugeInt.GetDigitals() << " digitals)" \
<< endl << endl << "computation took " << u32Timer << " ms" \
<< endl << "output conversion took " << u32Timer_Total - u32Timer << " ms" \
<< endl << endl;
// 清空输出字符空间
HugeInt.FreeStrBuffer();
system( "pause" );
// 下面再测试计算 Fibonacci 数列及进制转换的效率,
// 有条件的朋友可与著名的数学工具软件 Mathematica 等进行对比:
// 注:之所以选择用 Fibonacci 数列测试,是因为其尾部不会产生大片的连续“0”
// (如果出现这种情况,乘法可优化,使测试“缩水”);且大家容易得到。
UINT32 u32Num = 100000;
CONST UINT32 u32Num_max = u32Num * 10;
// 初始化
HugeInt = 0;
HugeIntX = 0;
HugeCalc::ResetTimer();
HugeCalc::EnableTimer();
UINT32 u32Digitals = 0;
while ( u32Num_max >= u32Num )
{
// 在16进制系统计算
u32Timer_Total = HugeCalc::GetTimer( TRUE );
HugeIntX.Fibonacci( u32Num );
cout << endl << "Hex: Computation Fibonacci(" << u32Num << ") took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << HugeIntX.GetBits() << " bits)." << endl;
u32Timer = u32Timer_Total;
// 直接转换成10进制输出
u32Timer_Total = HugeCalc::GetTimer( TRUE );
/*lpShow = */HugeIntX.ConvertToStr( 10, &u32Digitals, FS_NORMAL );
cout << "Dec: Output conversion took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << u32Digitals << " digitals)." \
<< endl;
// 复位,可回收资源
HugeIntX = 0;
// 在10进制系统计算
u32Timer_Total = HugeCalc::GetTimer( TRUE );
HugeInt.Fibonacci( u32Num );
cout << endl << "Dec: computation Fibonacci(" << u32Num << ") took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << HugeInt.GetDigitals() << " digitals)." << endl;
// 直接转换成16进制输出
u32Timer_Total = HugeCalc::GetTimer( TRUE );
/*lpShow = */HugeInt.ConvertToStr( 16, &u32Digitals, FS_NORMAL );
cout << "Hex: output conversion took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << u32Digitals << " digitals)." \
<< endl << endl;
// 复位,可回收资源
HugeInt = 0;
u32Num <<= 1;
}
}
system( "pause" );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -