?? 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 digits)\r\n\r\n" \
// "computation took %u ms\r\n" \
// "output conversion took %u ms\r\n\r\n", lpShow, HugeInt.GetDigits(), u32Timer, u32Timer_Total - u32Timer );
cout << endl << "The result of executing 1 + gcd(87324,78263148,7896) * (10^1989879887 mod 471!) is:" \
<< endl << endl << lpShow << "(" << HugeInt.GetDigits() << " digits)" \
<< 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 u32Digits = 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, &u32Digits, FS_NORMAL );
cout << "Dec: Output conversion took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << u32Digits << " digits)." \
<< 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.GetDigits() << " digits)." << endl;
// 直接轉換成16進制輸出
u32Timer_Total = HugeCalc::GetTimer( TRUE );
/*lpShow = */HugeInt.ConvertToStr( 16, &u32Digits, FS_NORMAL );
cout << "Hex: output conversion took " \
<< ( HugeCalc::GetTimer( TRUE ) - u32Timer_Total ) << " ms (" << u32Digits << " digits)." \
<< endl << endl;
// 復位,可回收資源
HugeInt = 0;
u32Num <<= 1;
}
}
system( "pause" );
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -