From d33a6925b180e4199cf18bdecef720fd9a0649f6 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 29 Oct 2007 21:42:34 +0000 Subject: [PATCH] partial work on the convertion tab git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@54 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/calculation.cpp | 5 ++++- src/languages.cpp | 25 +++++++++++++++++----- src/languages.h | 4 +++- src/parsermanager.cpp | 5 ++++- src/parsermanager.h | 49 ++++++++++++++++++++++++++++++++++++++++--- src/tabs.cpp | 9 ++++++++ 6 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/calculation.cpp b/src/calculation.cpp index 3cff508..c3a3c9a 100644 --- a/src/calculation.cpp +++ b/src/calculation.cpp @@ -74,7 +74,10 @@ unsigned __stdcall CalculationsProcedure(void *) // at the end we're printing the result // if there was a stop signal we continue the main loop without printing any values if( ! GetPrgRes()->GetThreadController()->WasStopSignal() ) - parser_manager.PrintResult(); + if( parser_manager.PrintResult() ) + PostMessage(main_window, WM_SET_LAST_ERROR, (WPARAM)parser_manager.GetLastCode(), 0); + + } diff --git a/src/languages.cpp b/src/languages.cpp index 96ccdad..9fcc95f 100644 --- a/src/languages.cpp +++ b/src/languages.cpp @@ -446,7 +446,7 @@ void Languages::ConvertingAddType(const char * name) } -void Languages::ConvertingAddUnit(const char * name) +void Languages::ConvertingAddUnit(const char * name, double v) { if( converting_type.empty() || converting_type.back().empty() || converting_unit.empty() ) return; @@ -454,6 +454,7 @@ void Languages::ConvertingAddUnit(const char * name) ConvUnit temp; temp.name = name; temp.type = static_cast(converting_type.back().size()-1); + temp.value = v; converting_unit.back().push_back(temp); } @@ -468,15 +469,16 @@ void Languages::InitConverting() ConvertingAddType("none"); ConvertingAddType("length"); - ConvertingAddUnit("metre"); - ConvertingAddUnit("mile"); + ConvertingAddUnit("metre", 1); + ConvertingAddUnit("mile", 1600); ConvertingAddType("area"); - ConvertingAddUnit("are"); - ConvertingAddUnit("square metre"); + ConvertingAddUnit("are", 10); + ConvertingAddUnit("square metre", 1); // pl + // the 'value' param we're using only from the english converting_type.push_back( std::vector() ); converting_unit.push_back( std::vector() ); @@ -596,3 +598,16 @@ int Languages::ConvertingUnitType(int id) { return ConvertingUnitType(current_country, id); } + +// value is taken only from english +double Languages::ConvertingUnitValue(int id) +{ + if( converting_unit.empty() ) + InitConverting(); + + if( id >= static_cast(converting_unit[0].size()) ) + return -1.0; + +return converting_unit[0][id].value; +} + diff --git a/src/languages.h b/src/languages.h index 48d2116..0ab0c55 100644 --- a/src/languages.h +++ b/src/languages.h @@ -157,6 +157,7 @@ public: std::string name; // int id; int type; + double value; }; @@ -177,7 +178,7 @@ private: std::vector > converting_unit; void ConvertingAddType(const char * name); - void ConvertingAddUnit(const char * name); + void ConvertingAddUnit(const char * name, double v = 1); void InitConverting(); @@ -215,6 +216,7 @@ public: int ConvertingTypeSize(); int ConvertingUnitSize(); + double ConvertingUnitValue(int id); }; diff --git a/src/parsermanager.cpp b/src/parsermanager.cpp index fa91197..27885d6 100644 --- a/src/parsermanager.cpp +++ b/src/parsermanager.cpp @@ -174,7 +174,7 @@ void ParserManager::Init() } -void ParserManager::PrintResult() +int ParserManager::PrintResult() { if( code == ttmath::err_ok ) { @@ -222,4 +222,7 @@ void ParserManager::PrintResult() GetPrgRes()->GetLanguages()->ErrorMessage(country, code) ); */ } + + + return 0; } diff --git a/src/parsermanager.h b/src/parsermanager.h index 2de065e..9002c2e 100644 --- a/src/parsermanager.h +++ b/src/parsermanager.h @@ -100,7 +100,7 @@ public: this method prints result (a correct value if was or an error instead) */ - void PrintResult(); + int PrintResult(); private: @@ -128,8 +128,42 @@ private: ttmath::ErrorCode code; + + + + template - void PrintResult(ttmath::Parser & matparser) + int Conversion(ValueType & result) + { + int convtype = GetPrgRes()->GetConvType(); + int c = 0; + + if( convtype != 0 ) + { + int input, output; + GetPrgRes()->GetConvUnit(convtype, input, output); + + if( input!=-1 && output!=-1 && input!=output ) + { + double vinput = GetPrgRes()->GetLanguages()->ConvertingUnitValue(input); + double voutput = GetPrgRes()->GetLanguages()->ConvertingUnitValue(output); + + ValueType vinputbig, voutputbig; + vinputbig.FromDouble(vinput); + voutputbig.FromDouble(voutput); + + c += result.Mul(voutputbig); + c += result.Div(vinputbig); + } + } + + return c; + } + + + // 1 if carry + template + int PrintResult(ttmath::Parser & matparser) { std::string result, part; unsigned int i = 0; @@ -139,7 +173,14 @@ private: { try { - if( matparser.stack[i].value.ToString(part,base_output, always_scientific, when_scientific, rounding) ) + ValueType result = matparser.stack[i].value; + if( Conversion(result) ) + { + code = ttmath::err_overflow; + return 1; + } + + if( result.ToString(part,base_output, always_scientific, when_scientific, rounding) ) { // we shouldn't have had this error in the new version of ToStrign(...) // (where we're using a bigger type for calculating) @@ -158,6 +199,8 @@ private: } SetDlgItemText(GetPrgRes()->GetMainWindow(),IDC_OUTPUT_EDIT,result.c_str()); + + return 0; } diff --git a/src/tabs.cpp b/src/tabs.cpp index e5da7fa..d1a1219 100644 --- a/src/tabs.cpp +++ b/src/tabs.cpp @@ -538,7 +538,10 @@ HWND list1 = GetDlgItem(hWnd, IDC_LIST_UNIT_TYPE); if( !ListView_GetItem(list1, &item) ) return; + GetPrgRes()->GetThreadController()->StopCalculating(); GetPrgRes()->SetConvType( (int)item.lParam ); + GetPrgRes()->GetThreadController()->StartCalculating(); + TabConvertFillUpUnitLists(hWnd); } @@ -564,8 +567,11 @@ HWND list2b = GetDlgItem(hWnd, IDC_LIST_UNIT_INPUT2); int convtype = GetPrgRes()->GetConvType(); int input, output; + + GetPrgRes()->GetThreadController()->StopCalculating(); GetPrgRes()->GetConvUnit(convtype, input, output); GetPrgRes()->SetConvUnit(convtype, (int)item.lParam, output ); + GetPrgRes()->GetThreadController()->StartCalculating(); } @@ -591,8 +597,11 @@ HWND list3b = GetDlgItem(hWnd, IDC_LIST_UNIT_OUTPUT2); int convtype = GetPrgRes()->GetConvType(); int input, output; + + GetPrgRes()->GetThreadController()->StopCalculating(); GetPrgRes()->GetConvUnit(convtype, input, output); GetPrgRes()->SetConvUnit(convtype, input, (int)item.lParam ); + GetPrgRes()->GetThreadController()->StartCalculating(); }