2007-02-08 18:54:37 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of TTCalc - a mathematical calculator
|
|
|
|
* and is distributed under the (new) BSD licence.
|
2009-11-04 18:59:42 +01:00
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
2007-02-08 18:54:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-01-30 18:30:10 +01:00
|
|
|
* Copyright (c) 2006-2011, Tomasz Sowa
|
2007-02-08 18:54:37 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-01-22 12:12:01 +01:00
|
|
|
#ifndef headerfilecalculation
|
|
|
|
#define headerfilecalculation
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\file parsermanager.h
|
|
|
|
\brief object of type ParserManager we're using during calculating
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "resource.h"
|
|
|
|
#include "programresources.h"
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief object of type ParserManager we're using during calculating
|
|
|
|
|
|
|
|
In our program we're using three kind of precisions. First is the smallest
|
|
|
|
, and the third is the biggest. Because precision is established during
|
|
|
|
compilation (templates) we need three different objects. ParserManager
|
|
|
|
helps us to maintain these objects.
|
|
|
|
*/
|
|
|
|
class ParserManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ParserManager();
|
|
|
|
ParserManager(const ParserManager & p);
|
|
|
|
ParserManager & operator=(const ParserManager & p);
|
|
|
|
~ParserManager();
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
the main method which call parserX.Parse(...)
|
|
|
|
*/
|
|
|
|
ttmath::ErrorCode Parse();
|
|
|
|
|
|
|
|
|
2007-02-08 18:54:37 +01:00
|
|
|
/*
|
|
|
|
it returns the last error code which was during parsing
|
|
|
|
*/
|
|
|
|
ttmath::ErrorCode GetLastCode();
|
|
|
|
|
|
|
|
|
2007-01-22 12:12:01 +01:00
|
|
|
/*!
|
|
|
|
we call this method directly after when we have left
|
|
|
|
WaitForCalculatingAndBlockForStop() method from the ThreadController
|
|
|
|
|
|
|
|
only in this method we can read variables which can be changed
|
|
|
|
by the first thread
|
|
|
|
*/
|
|
|
|
void MakeCopyOfVariables();
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
it prepares our three parsers to work, it should be called only once
|
|
|
|
*/
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
this method prints result
|
|
|
|
(a correct value if was or an error instead)
|
|
|
|
*/
|
2007-10-29 22:42:34 +01:00
|
|
|
int PrintResult();
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
/*!
|
|
|
|
this method prints the info about converting (on the converting tab)
|
|
|
|
*/
|
|
|
|
void ShowConvertingInfo();
|
|
|
|
|
|
|
|
|
2007-01-22 12:12:01 +01:00
|
|
|
private:
|
|
|
|
|
2007-06-24 22:11:14 +02:00
|
|
|
#ifndef TTCALC_PORTABLE
|
2008-10-13 06:17:57 +02:00
|
|
|
ttmath::Parser<TTMathBig1> parser1;
|
|
|
|
ttmath::Parser<TTMathBig2> parser2;
|
|
|
|
ttmath::Parser<TTMathBig3> parser3;
|
2007-06-24 22:11:14 +02:00
|
|
|
#else
|
2008-10-13 06:17:57 +02:00
|
|
|
ttmath::Parser<TTMathBig1> parser1;
|
2007-06-24 22:11:14 +02:00
|
|
|
#endif
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
ttmath::Objects variables, functions;
|
2009-04-02 07:13:27 +02:00
|
|
|
int last_variables_id;
|
|
|
|
int last_functions_id;
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
const unsigned int buffer_len;
|
|
|
|
char * buffer;
|
|
|
|
|
|
|
|
int base_input, base_output;
|
2007-11-04 22:56:55 +01:00
|
|
|
int conv_type, conv_input_unit, conv_output_unit;
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
bool always_scientific;
|
|
|
|
int when_scientific;
|
|
|
|
int rounding;
|
2008-04-12 10:52:21 +02:00
|
|
|
bool remove_zeroes;
|
2007-01-22 12:12:01 +01:00
|
|
|
int precision;
|
|
|
|
Languages::Country country;
|
|
|
|
ttmath::ErrorCode code;
|
2009-03-28 19:26:20 +01:00
|
|
|
int angle_deg_rad_grad;
|
2009-12-05 19:30:33 +01:00
|
|
|
|
|
|
|
char decimal_point;
|
|
|
|
char grouping;
|
|
|
|
char input_comma1;
|
|
|
|
char input_comma2;
|
|
|
|
char param_sep;
|
2007-01-22 12:12:01 +01:00
|
|
|
|
2011-01-30 18:30:10 +01:00
|
|
|
int grouping_digits;
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
/*
|
|
|
|
some buffers which we use in some method in the second thread,
|
|
|
|
it's better to have those buffers outside those methods -
|
|
|
|
if the buffers were in the methods there would be still allocating
|
|
|
|
and deallocating memory
|
|
|
|
*/
|
|
|
|
std::string buffer1, buffer2;
|
|
|
|
|
2007-10-29 22:42:34 +01:00
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
bool CanWeConvert() const
|
|
|
|
{
|
|
|
|
if( conv_type != 0 )
|
|
|
|
if( conv_input_unit!=-1 && conv_output_unit!=-1 &&
|
|
|
|
conv_input_unit!=conv_output_unit )
|
|
|
|
return true;
|
2007-10-29 22:42:34 +01:00
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-29 22:42:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
void AddOutputSuffix(std::string & result)
|
|
|
|
{
|
|
|
|
if( CanWeConvert() )
|
|
|
|
{
|
|
|
|
result += " ";
|
2008-10-13 06:17:57 +02:00
|
|
|
result += GetPrgRes()->GetConvert()->GetUnitAbbr(country, conv_output_unit);
|
2007-11-04 22:56:55 +01:00
|
|
|
}
|
|
|
|
}
|
2007-10-29 22:42:34 +01:00
|
|
|
|
2007-12-09 21:05:15 +01:00
|
|
|
|
2007-10-29 22:42:34 +01:00
|
|
|
// 1 if carry
|
2007-01-22 12:12:01 +01:00
|
|
|
template<class ValueType>
|
2007-10-29 22:42:34 +01:00
|
|
|
int PrintResult(ttmath::Parser<ValueType> & matparser)
|
2007-01-22 12:12:01 +01:00
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
buffer1.erase();
|
|
|
|
buffer2.erase();
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
for(i=0 ; i<matparser.stack.size() ; ++i)
|
|
|
|
{
|
changed: on win9x the configuration file is in
"c:\windows\data application\ttcalc\" now
changed: the program uses the TTMath 0.8.1 now
added: from TTMath 0.8.1:
root(x, index), bitand(x,y), bitor(x,y), bitxor(x,y)
/band(x,y), bor(x,y), bxor(x,y)/
asinh(x), acosh(x), atanh(x), acoth(x) /atgh(x), actgh(x)/
changed: from TTMath 0.8.1: calculations are much faster now
especially the factorial() function
changed: from TTMath 0.8.1: the way of parsing operators
git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@37 e52654a7-88a9-db11-a3e9-0013d4bc506e
2007-04-17 15:41:38 +02:00
|
|
|
try
|
|
|
|
{
|
2007-10-29 22:42:34 +01:00
|
|
|
ValueType result = matparser.stack[i].value;
|
2008-10-13 06:17:57 +02:00
|
|
|
|
|
|
|
if( CanWeConvert() )
|
2007-10-29 22:42:34 +01:00
|
|
|
{
|
2008-10-13 06:17:57 +02:00
|
|
|
if( GetPrgRes()->GetConvert()->Conversion(conv_input_unit, conv_output_unit, result) )
|
|
|
|
{
|
|
|
|
code = ttmath::err_overflow;
|
|
|
|
return 1;
|
|
|
|
}
|
2007-10-29 22:42:34 +01:00
|
|
|
}
|
|
|
|
|
2009-11-01 02:52:13 +01:00
|
|
|
ttmath::Conv conv;
|
|
|
|
|
|
|
|
conv.base = base_output;
|
|
|
|
conv.scient = always_scientific;
|
|
|
|
conv.scient_from = when_scientific;
|
2009-11-24 08:40:36 +01:00
|
|
|
conv.round = rounding;
|
2009-11-01 02:52:13 +01:00
|
|
|
conv.trim_zeroes = remove_zeroes;
|
|
|
|
conv.comma = decimal_point;
|
|
|
|
conv.group = grouping;
|
2011-01-30 18:30:10 +01:00
|
|
|
conv.group_digits = grouping_digits;
|
2009-11-01 02:52:13 +01:00
|
|
|
|
|
|
|
if( result.ToString(buffer2, conv) )
|
changed: on win9x the configuration file is in
"c:\windows\data application\ttcalc\" now
changed: the program uses the TTMath 0.8.1 now
added: from TTMath 0.8.1:
root(x, index), bitand(x,y), bitor(x,y), bitxor(x,y)
/band(x,y), bor(x,y), bxor(x,y)/
asinh(x), acosh(x), atanh(x), acoth(x) /atgh(x), actgh(x)/
changed: from TTMath 0.8.1: calculations are much faster now
especially the factorial() function
changed: from TTMath 0.8.1: the way of parsing operators
git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@37 e52654a7-88a9-db11-a3e9-0013d4bc506e
2007-04-17 15:41:38 +02:00
|
|
|
{
|
2007-06-24 22:11:14 +02:00
|
|
|
// we shouldn't have had this error in the new version of ToStrign(...)
|
|
|
|
// (where we're using a bigger type for calculating)
|
2007-11-04 22:56:55 +01:00
|
|
|
buffer2 = GetPrgRes()->GetLanguages()->GuiMessage(country, Languages::overflow_during_printing);
|
changed: on win9x the configuration file is in
"c:\windows\data application\ttcalc\" now
changed: the program uses the TTMath 0.8.1 now
added: from TTMath 0.8.1:
root(x, index), bitand(x,y), bitor(x,y), bitxor(x,y)
/band(x,y), bor(x,y), bxor(x,y)/
asinh(x), acosh(x), atanh(x), acoth(x) /atgh(x), actgh(x)/
changed: from TTMath 0.8.1: calculations are much faster now
especially the factorial() function
changed: from TTMath 0.8.1: the way of parsing operators
git-svn-id: svn://ttmath.org/publicrep/ttcalc/trunk@37 e52654a7-88a9-db11-a3e9-0013d4bc506e
2007-04-17 15:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(...)
|
2007-01-22 12:12:01 +01:00
|
|
|
{
|
2007-11-04 22:56:55 +01:00
|
|
|
buffer2 = GetPrgRes()->GetLanguages()->ErrorMessage(country, ttmath::err_internal_error);
|
2007-01-22 12:12:01 +01:00
|
|
|
}
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
buffer1 += buffer2;
|
|
|
|
AddOutputSuffix(buffer1);
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
if( i < matparser.stack.size()-1 )
|
2009-11-01 02:52:13 +01:00
|
|
|
buffer1 += " ; ";
|
2007-01-22 12:12:01 +01:00
|
|
|
}
|
|
|
|
|
2007-11-04 22:56:55 +01:00
|
|
|
SetDlgItemText(GetPrgRes()->GetMainWindow(),IDC_OUTPUT_EDIT,buffer1.c_str());
|
2007-10-29 22:42:34 +01:00
|
|
|
|
|
|
|
return 0;
|
2007-01-22 12:12:01 +01:00
|
|
|
}
|
|
|
|
|
2009-12-05 19:30:33 +01:00
|
|
|
template<class ValueType>
|
|
|
|
void Parse(ttmath::Parser<ValueType> & matparser)
|
|
|
|
{
|
|
|
|
matparser.SetBase(base_input);
|
|
|
|
matparser.SetDegRadGrad(angle_deg_rad_grad);
|
|
|
|
matparser.SetComma(input_comma1, input_comma2);
|
|
|
|
matparser.SetGroup(grouping);
|
|
|
|
matparser.SetParamSep(param_sep);
|
|
|
|
code = matparser.Parse(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-22 12:12:01 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|