2017-11-04 07:03:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* calculator.cpp - A simple calculator with trigonometric functions *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
2018-09-20 23:59:01 +02:00
|
|
|
* Copyright 2016-2018 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
|
|
|
* The Final Cut is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* The Final Cut is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this program. If not, see *
|
|
|
|
* <http://www.gnu.org/licenses/>. *
|
|
|
|
***********************************************************************/
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
#include <cfloat>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <limits>
|
2017-09-11 03:06:02 +02:00
|
|
|
#include <map>
|
2018-12-19 22:04:02 +01:00
|
|
|
#include <memory>
|
2015-06-22 00:28:06 +02:00
|
|
|
#include <stack>
|
|
|
|
|
2017-10-31 00:41:59 +01:00
|
|
|
#include <final/final.h>
|
2015-06-22 00:28:06 +02:00
|
|
|
|
2015-06-27 23:00:12 +02:00
|
|
|
const lDouble PI = 3.141592653589793238L;
|
2015-06-22 00:28:06 +02:00
|
|
|
|
2017-10-02 07:32:33 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class Button
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
class Button : public finalcut::FButton
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Constructor
|
2018-12-10 01:48:26 +01:00
|
|
|
explicit Button (FWidget* = nullptr);
|
2016-09-30 04:55:28 +02:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Method
|
|
|
|
void setChecked(bool);
|
2016-09-30 04:55:28 +02:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Event handler
|
2018-09-24 04:02:35 +02:00
|
|
|
virtual void onKeyPress (finalcut::FKeyEvent*);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
private:
|
|
|
|
// Data Member
|
2018-12-03 03:22:36 +01:00
|
|
|
bool checked{false};
|
2015-06-22 00:28:06 +02:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
Button::Button (finalcut::FWidget* parent)
|
|
|
|
: finalcut::FButton(parent)
|
2015-09-20 05:44:50 +02:00
|
|
|
{ }
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-22 23:50:10 +01:00
|
|
|
void Button::setChecked (bool enable)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-12-22 23:50:10 +01:00
|
|
|
if ( checked == enable )
|
2016-10-17 08:44:38 +02:00
|
|
|
return;
|
|
|
|
|
2018-12-22 23:50:10 +01:00
|
|
|
checked = enable;
|
2016-10-17 08:44:38 +02:00
|
|
|
|
|
|
|
if ( checked )
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
setBackgroundColor(finalcut::fc::Cyan);
|
|
|
|
setFocusForegroundColor(finalcut::fc::White);
|
|
|
|
setFocusBackgroundColor(finalcut::fc::Cyan);
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
2016-10-17 08:44:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
setBackgroundColor(wc.button_active_bg);
|
|
|
|
setFocusForegroundColor(wc.button_active_focus_fg);
|
|
|
|
setFocusBackgroundColor(wc.button_active_focus_bg);
|
|
|
|
}
|
|
|
|
|
|
|
|
redraw();
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Button::onKeyPress (finalcut::FKeyEvent* ev)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey key = ev->key();
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
// catch the enter key
|
2018-09-20 23:59:01 +02:00
|
|
|
if ( key == finalcut::fc::Fkey_return
|
|
|
|
|| key == finalcut::fc::Fkey_enter )
|
2015-06-22 00:28:06 +02:00
|
|
|
return;
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FButton::onKeyPress(ev);
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class Calc
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
class Calc : public finalcut::FDialog
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Constructor
|
2018-12-10 01:48:26 +01:00
|
|
|
explicit Calc (finalcut::FWidget* parent = nullptr);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Destructor
|
|
|
|
~Calc();
|
|
|
|
|
|
|
|
// Event handlers
|
2018-09-24 04:02:35 +02:00
|
|
|
virtual void onKeyPress (finalcut::FKeyEvent*);
|
|
|
|
virtual void onAccel (finalcut::FAccelEvent*);
|
|
|
|
virtual void onClose (finalcut::FCloseEvent*);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Callback method
|
2018-09-24 04:02:35 +02:00
|
|
|
void cb_buttonClicked (finalcut::FWidget*, data_ptr);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
private:
|
2017-12-30 21:27:17 +01:00
|
|
|
// Typedef and Enumeration
|
|
|
|
typedef void (Calc::*keyFunction)(lDouble&); // Method pointer
|
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
enum button
|
|
|
|
{
|
|
|
|
Sine,
|
|
|
|
Cosine,
|
|
|
|
Tangent,
|
|
|
|
Reciprocal,
|
|
|
|
On,
|
|
|
|
Natural_logarithm,
|
|
|
|
Powers_of_e,
|
|
|
|
Power,
|
|
|
|
Square_root,
|
|
|
|
Divide,
|
|
|
|
Common_logarithm,
|
|
|
|
Powers_of_ten,
|
|
|
|
Parenthese_l,
|
|
|
|
Parenthese_r,
|
|
|
|
Multiply,
|
|
|
|
Hyperbolic,
|
|
|
|
Seven,
|
|
|
|
Eight,
|
|
|
|
Nine,
|
|
|
|
Subtract,
|
|
|
|
Arcus,
|
|
|
|
Four,
|
|
|
|
Five,
|
|
|
|
Six,
|
|
|
|
Add,
|
|
|
|
Pi,
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
Percent,
|
|
|
|
Zero,
|
|
|
|
Decimal_point,
|
|
|
|
Change_sign,
|
|
|
|
Equals,
|
|
|
|
NUM_OF_BUTTONS
|
|
|
|
};
|
|
|
|
|
2017-12-08 23:51:56 +01:00
|
|
|
// Methods
|
2017-12-30 21:27:17 +01:00
|
|
|
void drawDispay();
|
|
|
|
virtual void draw();
|
|
|
|
void clear (lDouble&);
|
|
|
|
void zero (lDouble&);
|
|
|
|
void one (lDouble&);
|
|
|
|
void two (lDouble&);
|
|
|
|
void three (lDouble&);
|
|
|
|
void four (lDouble&);
|
|
|
|
void five (lDouble&);
|
|
|
|
void six (lDouble&);
|
|
|
|
void seven (lDouble&);
|
|
|
|
void eight (lDouble&);
|
|
|
|
void nine (lDouble&);
|
|
|
|
void add (lDouble&);
|
|
|
|
void subtract (lDouble&);
|
|
|
|
void multiply (lDouble&);
|
|
|
|
void divide (lDouble&);
|
|
|
|
void equals (lDouble&);
|
|
|
|
void change_sign (lDouble&);
|
|
|
|
void radix_point(lDouble&);
|
|
|
|
void reciprocal (lDouble&);
|
|
|
|
void percent (lDouble&);
|
|
|
|
void pi (lDouble&);
|
|
|
|
void open_bracket (lDouble&);
|
|
|
|
void close_bracket (lDouble&);
|
|
|
|
void log_e (lDouble&);
|
|
|
|
void power_e (lDouble&);
|
|
|
|
void log_10 (lDouble&);
|
|
|
|
void power_10 (lDouble&);
|
|
|
|
void power (lDouble&);
|
|
|
|
void square_root (lDouble&);
|
|
|
|
void hyperbolic (lDouble&);
|
|
|
|
void arcus (lDouble&);
|
|
|
|
void sine (lDouble&);
|
|
|
|
void cosine (lDouble&);
|
|
|
|
void tangent (lDouble&);
|
|
|
|
bool isDataEntryKey (int);
|
|
|
|
bool isOperatorKey (int);
|
|
|
|
lDouble& getValue();
|
|
|
|
void setDisplay (lDouble);
|
|
|
|
void setInfixOperator (char);
|
|
|
|
void clearInfixOperator();
|
|
|
|
void calcInfixOperator();
|
2018-09-24 04:02:35 +02:00
|
|
|
virtual void adjustSize();
|
2017-12-30 21:27:17 +01:00
|
|
|
const wchar_t* getButtonText (int);
|
|
|
|
void mapKeyFunctions();
|
2017-12-08 23:51:56 +01:00
|
|
|
|
|
|
|
// Data Members
|
2018-12-03 03:22:36 +01:00
|
|
|
bool error{false};
|
|
|
|
bool arcus_mode{false};
|
|
|
|
bool hyperbolic_mode{false};
|
|
|
|
lDouble a{0.0L};
|
|
|
|
lDouble b{0.0L};
|
|
|
|
lDouble infinity{std::numeric_limits<lDouble>::infinity()};
|
|
|
|
uInt max_char{33};
|
|
|
|
int last_key{-1};
|
|
|
|
char infix_operator{'\0'};
|
|
|
|
char last_infix_operator{'\0'};
|
|
|
|
finalcut::FString input{""};
|
|
|
|
int button_no[Calc::NUM_OF_BUTTONS]{};
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
struct stack_data
|
|
|
|
{
|
|
|
|
lDouble term;
|
|
|
|
char infix_operator;
|
|
|
|
};
|
|
|
|
|
2018-12-03 03:22:36 +01:00
|
|
|
std::stack<stack_data> bracket_stack{};
|
2018-12-19 22:04:02 +01:00
|
|
|
std::map<Calc::button, std::shared_ptr<Button> > calculator_buttons{};
|
2018-12-03 03:22:36 +01:00
|
|
|
std::map<Calc::button, keyFunction> key_map{};
|
2015-06-22 00:28:06 +02:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
Calc::Calc (FWidget* parent)
|
2018-09-20 23:59:01 +02:00
|
|
|
: finalcut::FDialog(parent)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-12-30 21:27:17 +01:00
|
|
|
mapKeyFunctions();
|
2015-06-22 00:28:06 +02:00
|
|
|
clearInfixOperator();
|
2016-10-06 23:15:09 +02:00
|
|
|
std::setlocale(LC_NUMERIC, "C");
|
2017-12-08 23:51:56 +01:00
|
|
|
setText ("Calculator");
|
2015-06-22 00:28:06 +02:00
|
|
|
setGeometry (19, 6, 37, 18);
|
2017-12-08 23:51:56 +01:00
|
|
|
addAccelerator('q'); // Press 'q' to quit
|
2015-06-22 00:28:06 +02:00
|
|
|
|
2018-12-19 22:04:02 +01:00
|
|
|
for (std::size_t key = 0; key < Calc::NUM_OF_BUTTONS; key++)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-12-19 22:04:02 +01:00
|
|
|
auto btn = std::make_shared<Button>(this);
|
2015-06-22 00:28:06 +02:00
|
|
|
button_no[key] = key;
|
|
|
|
|
|
|
|
if ( key == Equals )
|
|
|
|
btn->setGeometry(30, 15, 5, 3);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int x, y, n;
|
2017-09-17 21:32:46 +02:00
|
|
|
( key <= Three ) ? n = 0 : n = 1;
|
2017-08-27 09:50:30 +02:00
|
|
|
x = (key + n) % 5 * 7 + 2;
|
|
|
|
y = (key + n) / 5 * 2 + 3;
|
2015-06-22 00:28:06 +02:00
|
|
|
btn->setGeometry(x, y, 5, 1);
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
btn->setFlat();
|
|
|
|
btn->setNoUnderline();
|
2017-12-30 21:27:17 +01:00
|
|
|
btn->setText(getButtonText(key));
|
2018-09-20 23:59:01 +02:00
|
|
|
btn->setDoubleFlatLine(finalcut::fc::top);
|
|
|
|
btn->setDoubleFlatLine(finalcut::fc::bottom);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-07-01 22:34:40 +02:00
|
|
|
if ( isNewFont() )
|
|
|
|
btn->unsetClickAnimation();
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
btn->addCallback
|
|
|
|
(
|
|
|
|
"clicked",
|
2017-04-09 20:08:53 +02:00
|
|
|
F_METHOD_CALLBACK (this, &Calc::cb_buttonClicked),
|
2015-06-22 00:28:06 +02:00
|
|
|
&button_no[key]
|
|
|
|
);
|
|
|
|
|
|
|
|
calculator_buttons[button(key)] = btn;
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
calculator_buttons[On]->addAccelerator(finalcut::fc::Fkey_dc); // Del key
|
2015-06-22 00:28:06 +02:00
|
|
|
calculator_buttons[On]->setFocus();
|
|
|
|
calculator_buttons[Pi]->addAccelerator('p');
|
|
|
|
calculator_buttons[Power]->addAccelerator('^');
|
|
|
|
calculator_buttons[Divide]->addAccelerator('/');
|
|
|
|
calculator_buttons[Powers_of_ten]->addAccelerator('d');
|
|
|
|
calculator_buttons[Multiply]->addAccelerator('*');
|
|
|
|
calculator_buttons[Decimal_point]->addAccelerator(',');
|
|
|
|
calculator_buttons[Change_sign]->addAccelerator('#');
|
2018-09-20 23:59:01 +02:00
|
|
|
calculator_buttons[Equals]->addAccelerator(finalcut::fc::Fkey_return);
|
|
|
|
calculator_buttons[Equals]->addAccelerator(finalcut::fc::Fkey_enter);
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Calc::~Calc()
|
2018-11-13 02:51:41 +01:00
|
|
|
{ }
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::drawDispay()
|
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FString display = input;
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
if ( display.isNull() || display.isEmpty() )
|
|
|
|
display = L'0';
|
|
|
|
|
|
|
|
if ( display.right(3) == L"-0." )
|
|
|
|
display = L'0';
|
|
|
|
|
|
|
|
if ( display.back() == L'.' && display.getLength() > 1 )
|
|
|
|
display = display.left(display.getLength() - 1);
|
|
|
|
|
|
|
|
if ( ! display.isEmpty() && display.getLength() < max_char )
|
2018-09-20 23:59:01 +02:00
|
|
|
display.insert(finalcut::FString(max_char - display.getLength(), L' '), 0);
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
if ( display.getLength() > max_char )
|
|
|
|
display = display.left(max_char);
|
|
|
|
|
|
|
|
if ( infix_operator )
|
|
|
|
display[1] = infix_operator;
|
|
|
|
|
|
|
|
if ( error )
|
|
|
|
display = " Error ";
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
setColor(finalcut::fc::Black, finalcut::fc::LightGray);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-10-11 21:56:16 +02:00
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(false);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (3, 3);
|
2015-06-22 00:28:06 +02:00
|
|
|
print(display);
|
|
|
|
print(L' ');
|
2015-07-09 23:29:51 +02:00
|
|
|
setColor(wc.dialog_fg, wc.dialog_bg);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-10-11 21:56:16 +02:00
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(true);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
if ( isNewFont() )
|
|
|
|
{
|
2018-12-06 02:28:24 +01:00
|
|
|
wchar_t bottom_line = finalcut::fc::NF_border_line_bottom;
|
|
|
|
wchar_t top_bottom_line = finalcut::fc::NF_border_line_up_and_down;
|
|
|
|
wchar_t top_line = finalcut::fc::NF_border_line_upper;
|
|
|
|
wchar_t right_line = finalcut::fc::NF_rev_border_line_right;
|
|
|
|
wchar_t left_line = finalcut::fc::NF_border_line_left;
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (3, 2);
|
2018-09-20 23:59:01 +02:00
|
|
|
print (finalcut::FString(33, bottom_line));
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (2, 3);
|
2018-09-20 23:59:01 +02:00
|
|
|
print (right_line);
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (36, 3);
|
2018-09-20 23:59:01 +02:00
|
|
|
print (left_line);
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (3, 4);
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FString top_bottom_line_5 (5, top_bottom_line);
|
|
|
|
finalcut::FString top_line_2 (2, top_line);
|
2015-06-22 00:28:06 +02:00
|
|
|
print ( top_bottom_line_5 + top_line_2
|
|
|
|
+ top_bottom_line_5 + top_line_2
|
|
|
|
+ top_bottom_line_5 + top_line_2
|
|
|
|
+ top_bottom_line_5 + top_line_2
|
2018-09-20 23:59:01 +02:00
|
|
|
+ top_bottom_line_5 );
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 02:28:24 +01:00
|
|
|
wchar_t vertical_and_right = finalcut::fc::BoxDrawingsVerticalAndRight;
|
|
|
|
wchar_t horizontal = finalcut::fc::BoxDrawingsHorizontal;
|
|
|
|
wchar_t vertical_and_left = finalcut::fc::BoxDrawingsVerticalAndLeft;
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FString separator = finalcut::FString(vertical_and_right)
|
|
|
|
+ finalcut::FString(35, horizontal)
|
|
|
|
+ finalcut::FString(vertical_and_left);
|
2017-09-11 03:06:02 +02:00
|
|
|
setPrintPos (1, 4);
|
2015-06-22 00:28:06 +02:00
|
|
|
print(separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:51:56 +01:00
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::clear (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
error = false;
|
|
|
|
arcus_mode = false;
|
|
|
|
hyperbolic_mode = false;
|
|
|
|
calculator_buttons[Arcus]->setChecked(false);
|
|
|
|
calculator_buttons[Hyperbolic]->setChecked(false);
|
|
|
|
input = "";
|
|
|
|
clearInfixOperator();
|
|
|
|
last_infix_operator = '\0';
|
|
|
|
a = b = 0.0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::zero (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '0';
|
|
|
|
else
|
|
|
|
input = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::one (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '1';
|
|
|
|
else
|
|
|
|
input = '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::two (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '2';
|
|
|
|
else
|
|
|
|
input = '2';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::three (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '3';
|
|
|
|
else
|
|
|
|
input = '3';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::four (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '4';
|
|
|
|
else
|
|
|
|
input = '4';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::five (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '5';
|
|
|
|
else
|
|
|
|
input = '5';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::six (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '6';
|
|
|
|
else
|
|
|
|
input = '6';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::seven (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '7';
|
|
|
|
else
|
|
|
|
input = '7';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::eight (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '8';
|
|
|
|
else
|
|
|
|
input = '8';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::nine (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key) )
|
|
|
|
input += '9';
|
|
|
|
else
|
|
|
|
input = '9';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::add (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( ! isOperatorKey(last_key) )
|
|
|
|
calcInfixOperator();
|
|
|
|
|
|
|
|
setDisplay(a);
|
|
|
|
setInfixOperator('+');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::subtract (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( ! isOperatorKey(last_key) )
|
|
|
|
calcInfixOperator();
|
|
|
|
|
|
|
|
setDisplay(a);
|
|
|
|
setInfixOperator('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::multiply (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
2018-11-20 21:11:04 +01:00
|
|
|
if ( ! isOperatorKey(last_key) )
|
|
|
|
calcInfixOperator();
|
2017-12-08 23:51:56 +01:00
|
|
|
|
|
|
|
setDisplay(a);
|
|
|
|
setInfixOperator('*');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::divide (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( ! isOperatorKey(last_key) )
|
|
|
|
calcInfixOperator();
|
|
|
|
|
|
|
|
setDisplay(a);
|
|
|
|
setInfixOperator('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::equals (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
infix_operator = last_infix_operator;
|
|
|
|
calcInfixOperator();
|
|
|
|
setDisplay(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::change_sign (lDouble& x)
|
|
|
|
{
|
|
|
|
x *= -1.0L;
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::radix_point (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
2017-12-30 21:27:17 +01:00
|
|
|
if ( input.getLength() >= max_char )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( isDataEntryKey(last_key)
|
|
|
|
&& ! input.isNull()
|
|
|
|
&& ! input.isEmpty()
|
|
|
|
&& ! input.includes('.') )
|
2017-12-08 23:51:56 +01:00
|
|
|
input += '.';
|
2017-12-30 21:27:17 +01:00
|
|
|
else
|
|
|
|
input = "0.";
|
2017-12-08 23:51:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::reciprocal (lDouble& x)
|
|
|
|
{
|
|
|
|
if ( std::fabs(x) < LDBL_EPSILON ) // x == 0
|
|
|
|
error = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = 1 / x;
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::percent (lDouble& x)
|
|
|
|
{
|
|
|
|
infix_operator = last_infix_operator;
|
|
|
|
x /= 100.0L;
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::pi (lDouble& x)
|
|
|
|
{
|
|
|
|
x = PI;
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::open_bracket (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
stack_data d = { a, infix_operator };
|
|
|
|
bracket_stack.push(d);
|
|
|
|
clearInfixOperator();
|
|
|
|
input = "";
|
|
|
|
a = b = 0.0L;
|
|
|
|
setDisplay(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-12-30 21:27:17 +01:00
|
|
|
void Calc::close_bracket (lDouble&)
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( bracket_stack.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
calcInfixOperator();
|
|
|
|
setDisplay(a);
|
|
|
|
stack_data d = bracket_stack.top();
|
|
|
|
bracket_stack.pop();
|
|
|
|
b = d.term;
|
|
|
|
infix_operator = d.infix_operator;
|
|
|
|
last_infix_operator = infix_operator;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::log_e (lDouble& x)
|
|
|
|
{
|
|
|
|
x = std::log(x);
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::power_e (lDouble& x)
|
|
|
|
{
|
|
|
|
x = std::exp(x);
|
|
|
|
|
|
|
|
if ( errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::log_10 (lDouble& x)
|
|
|
|
{
|
|
|
|
x = std::log10(x);
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::power_10 (lDouble& x)
|
|
|
|
{
|
|
|
|
x = std::pow(10, x);
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::power (lDouble& x)
|
|
|
|
{
|
|
|
|
if ( ! isOperatorKey(last_key) )
|
|
|
|
calcInfixOperator();
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
setInfixOperator('^');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::square_root (lDouble& x)
|
|
|
|
{
|
|
|
|
x = std::sqrt(x);
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::hyperbolic (lDouble& x)
|
|
|
|
{
|
|
|
|
hyperbolic_mode = ! hyperbolic_mode;
|
|
|
|
calculator_buttons[Hyperbolic]->setChecked(hyperbolic_mode);
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::arcus (lDouble& x)
|
|
|
|
{
|
|
|
|
arcus_mode = ! arcus_mode;
|
|
|
|
calculator_buttons[Arcus]->setChecked(arcus_mode);
|
|
|
|
setDisplay(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::sine (lDouble& x)
|
|
|
|
{
|
2018-11-20 21:11:04 +01:00
|
|
|
if ( hyperbolic_mode )
|
2017-12-08 23:51:56 +01:00
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
{
|
|
|
|
x = std::log(x + std::sqrt(x * x + 1));
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
if ( std::fabs(x - infinity) < LDBL_EPSILON ) // x = ∞
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
x = std::sinh(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
x = std::asin(x) * 180.0L / PI;
|
|
|
|
else if ( std::fabs(std::fmod(x, 180.0L)) < LDBL_EPSILON ) // x / 180 = 0
|
|
|
|
x = 0.0L;
|
|
|
|
else
|
|
|
|
x = std::sin(x * PI / 180.0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( errno == EDOM )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
arcus_mode = false;
|
|
|
|
hyperbolic_mode = false;
|
|
|
|
calculator_buttons[Arcus]->setChecked(false);
|
|
|
|
calculator_buttons[Hyperbolic]->setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::cosine (lDouble& x)
|
|
|
|
{
|
|
|
|
if ( hyperbolic_mode )
|
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
{
|
|
|
|
x = std::log(x + std::sqrt(x * x - 1));
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
if ( std::fabs(x - infinity) < LDBL_EPSILON ) // x = ∞
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
x = std::cosh(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
x = std::acos(x) * 180.0L / PI;
|
|
|
|
else if ( std::fabs(std::fmod(x - 90.0L, 180.0L)) < LDBL_EPSILON ) // (x - 90) / 180 == 0
|
|
|
|
x = 0.0L;
|
|
|
|
else
|
|
|
|
x = std::cos(x * PI / 180.0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( errno == EDOM )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
arcus_mode = false;
|
|
|
|
hyperbolic_mode = false;
|
|
|
|
calculator_buttons[Arcus]->setChecked(false);
|
|
|
|
calculator_buttons[Hyperbolic]->setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::tangent (lDouble& x)
|
|
|
|
{
|
|
|
|
if ( hyperbolic_mode )
|
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
if ( x < 1 )
|
|
|
|
{
|
|
|
|
x = 0.5L * std::log((1 + x) / (1 - x));
|
|
|
|
|
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
else
|
|
|
|
x = std::tanh(x);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( arcus_mode )
|
|
|
|
x = std::atan(x) * 180.0L / PI;
|
|
|
|
else
|
2018-11-20 21:11:04 +01:00
|
|
|
{
|
2017-12-08 23:51:56 +01:00
|
|
|
// Test if (x / 180) != 0 and x / 90 == 0
|
|
|
|
if ( std::fabs(std::fmod(x, 180.0L)) > LDBL_EPSILON
|
|
|
|
&& std::fabs(std::fmod(x, 90.0L)) < LDBL_EPSILON )
|
|
|
|
error = true;
|
|
|
|
else if ( std::fabs(std::fmod(x, 180.0L)) < LDBL_EPSILON ) // x / 180 == 0
|
|
|
|
x = 0.0L;
|
|
|
|
else
|
|
|
|
x = std::tan(x * PI / 180.0L);
|
2018-11-20 21:11:04 +01:00
|
|
|
}
|
2017-12-08 23:51:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( errno == EDOM )
|
|
|
|
error = true;
|
|
|
|
|
|
|
|
setDisplay(x);
|
|
|
|
arcus_mode = false;
|
|
|
|
hyperbolic_mode = false;
|
|
|
|
calculator_buttons[Arcus]->setChecked(false);
|
|
|
|
calculator_buttons[Hyperbolic]->setChecked(false);
|
|
|
|
}
|
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::draw()
|
|
|
|
{
|
2016-11-05 23:12:05 +01:00
|
|
|
setBold();
|
2018-09-20 23:59:01 +02:00
|
|
|
setColor (finalcut::fc::Blue, finalcut::fc::Cyan);
|
|
|
|
clearArea (vdesktop, finalcut::fc::MediumShade);
|
2016-11-05 23:12:05 +01:00
|
|
|
unsetBold();
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::draw();
|
2015-06-22 00:28:06 +02:00
|
|
|
drawDispay();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool Calc::isDataEntryKey (int key)
|
|
|
|
{
|
2017-12-08 23:51:56 +01:00
|
|
|
// Test if key is in {'.', '0'..'9'}
|
2015-06-22 00:28:06 +02:00
|
|
|
int data_entry_keys[] =
|
|
|
|
{
|
|
|
|
Decimal_point,
|
|
|
|
Zero,
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
Four,
|
|
|
|
Five,
|
|
|
|
Six,
|
|
|
|
Seven,
|
|
|
|
Eight,
|
|
|
|
Nine
|
|
|
|
};
|
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
int* iter = std::find (data_entry_keys, data_entry_keys + 11, key);
|
2015-06-22 00:28:06 +02:00
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
if ( iter != data_entry_keys + 11 )
|
2015-06-22 00:28:06 +02:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool Calc::isOperatorKey(int key)
|
|
|
|
{
|
2017-12-08 23:51:56 +01:00
|
|
|
// Test if key is in {'*', '/', '+', '-', '^', '='}
|
2015-06-22 00:28:06 +02:00
|
|
|
int operators[] =
|
|
|
|
{
|
|
|
|
Multiply,
|
|
|
|
Divide,
|
|
|
|
Add,
|
|
|
|
Subtract,
|
|
|
|
Power,
|
|
|
|
Equals
|
|
|
|
};
|
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
int* iter = std::find (operators, operators + 6, key);
|
2015-06-27 23:00:12 +02:00
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
if ( iter != operators + 6 )
|
2015-06-22 00:28:06 +02:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:51:56 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
lDouble& Calc::getValue()
|
|
|
|
{
|
|
|
|
if ( infix_operator )
|
|
|
|
return b;
|
|
|
|
else
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
2015-06-27 23:00:12 +02:00
|
|
|
void Calc::setDisplay (lDouble d)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2016-09-25 23:53:48 +02:00
|
|
|
char buffer[33];
|
2017-03-26 20:40:04 +02:00
|
|
|
snprintf (buffer, sizeof(buffer), "%32.11Lg", d);
|
2015-06-22 00:28:06 +02:00
|
|
|
input = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void Calc::setInfixOperator(char c)
|
|
|
|
{
|
|
|
|
infix_operator = c;
|
|
|
|
last_infix_operator = infix_operator;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void Calc::clearInfixOperator()
|
|
|
|
{
|
|
|
|
infix_operator = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::calcInfixOperator()
|
|
|
|
{
|
|
|
|
switch ( infix_operator )
|
|
|
|
{
|
|
|
|
case '*':
|
2016-10-06 23:15:09 +02:00
|
|
|
if ( std::fabs(a) > LDBL_EPSILON ) // a != 0.0L
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
|
|
|
// ln(a * b) = ln(a) + ln(b)
|
2016-10-06 23:15:09 +02:00
|
|
|
if ( std::log(std::abs(a)) + std::log(std::abs(b)) <= std::log(LDBL_MAX) )
|
2015-06-22 00:28:06 +02:00
|
|
|
a *= b;
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
b = 0.0L;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
2016-10-06 23:15:09 +02:00
|
|
|
if ( std::fabs(b) > LDBL_EPSILON ) // b != 0.0L
|
2015-06-22 00:28:06 +02:00
|
|
|
a /= b;
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
2016-10-06 23:15:09 +02:00
|
|
|
if ( std::fabs(a) > LDBL_EPSILON ) // a != 0.0L
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-08-27 09:50:30 +02:00
|
|
|
if ( std::log(std::abs(a)) + std::log(std::abs(1 + b / a)) <= std::log(LDBL_MAX) )
|
2015-06-22 00:28:06 +02:00
|
|
|
a += b;
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
a = b;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
2016-10-06 23:15:09 +02:00
|
|
|
if ( std::fabs(b) > LDBL_EPSILON ) // b != 0.0L
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-08-27 09:50:30 +02:00
|
|
|
if ( std::log(std::abs(a)) + std::log(std::abs(1 - b / a)) <= std::log(LDBL_MAX) )
|
2015-06-22 00:28:06 +02:00
|
|
|
a -= b;
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
a = b * (-1.0L);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '^':
|
2016-10-06 23:15:09 +02:00
|
|
|
a = std::pow(a, b);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
if ( errno == EDOM || errno == ERANGE )
|
|
|
|
error = true;
|
|
|
|
break;
|
2015-09-20 05:44:50 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
2017-12-30 21:27:17 +01:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
clearInfixOperator();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Calc::onKeyPress (finalcut::FKeyEvent* ev)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-11-21 20:07:08 +01:00
|
|
|
std::size_t len = input.getLength();
|
|
|
|
FKey key = ev->key();
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
switch ( key )
|
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
case finalcut::fc::Fkey_erase:
|
|
|
|
case finalcut::fc::Fkey_backspace:
|
2015-06-22 00:28:06 +02:00
|
|
|
if ( len > 0 )
|
|
|
|
{
|
2017-12-30 21:27:17 +01:00
|
|
|
lDouble& x = getValue();
|
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
if ( len == 1 )
|
2017-12-30 21:27:17 +01:00
|
|
|
{
|
2015-06-22 00:28:06 +02:00
|
|
|
input = "";
|
2017-12-30 21:27:17 +01:00
|
|
|
x = 0.0L;
|
|
|
|
}
|
2015-06-22 00:28:06 +02:00
|
|
|
else
|
2017-12-30 21:27:17 +01:00
|
|
|
{
|
2015-06-22 00:28:06 +02:00
|
|
|
input = input.left(input.getLength() - 1);
|
2017-12-30 21:27:17 +01:00
|
|
|
x = lDouble(std::atof(input.c_str()));
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
drawDispay();
|
|
|
|
updateTerminal();
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-09-20 05:44:50 +02:00
|
|
|
ev->accept();
|
2015-06-22 00:28:06 +02:00
|
|
|
break;
|
2015-06-27 23:00:12 +02:00
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
case finalcut::fc::Fkey_escape:
|
|
|
|
case finalcut::fc::Fkey_escape_mintty:
|
2015-09-20 05:44:50 +02:00
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FAccelEvent a_ev( finalcut::fc::Accelerator_Event
|
|
|
|
, getFocusWidget() );
|
2015-09-20 05:44:50 +02:00
|
|
|
calculator_buttons[On]->onAccel(&a_ev);
|
|
|
|
}
|
|
|
|
ev->accept();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::onKeyPress(ev);
|
2015-06-22 00:28:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Calc::onAccel (finalcut::FAccelEvent* ev)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
ev->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Calc::onClose (finalcut::FCloseEvent* ev)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication::closeConfirmationDialog (this, ev);
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Calc::cb_buttonClicked (finalcut::FWidget*, data_ptr data)
|
2015-06-22 00:28:06 +02:00
|
|
|
{
|
2017-12-08 23:51:56 +01:00
|
|
|
lDouble& x = getValue();
|
2017-12-30 21:27:17 +01:00
|
|
|
Calc::button key = *(static_cast<Calc::button*>(data));
|
2015-06-22 00:28:06 +02:00
|
|
|
|
2017-12-30 21:27:17 +01:00
|
|
|
// Call the key function
|
|
|
|
(this->*key_map[key])(x);
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
if ( ! input.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( isDataEntryKey(key) )
|
2017-12-08 23:51:56 +01:00
|
|
|
x = lDouble(input.toDouble());
|
2015-06-22 00:28:06 +02:00
|
|
|
else
|
|
|
|
{
|
2017-12-30 21:27:17 +01:00
|
|
|
// Remove trailing zeros
|
2015-06-22 00:28:06 +02:00
|
|
|
while ( ! input.includes(L'e')
|
2017-11-30 02:38:55 +01:00
|
|
|
&& input.includes(L'.')
|
|
|
|
&& input.back() == L'0' )
|
2017-08-27 09:50:30 +02:00
|
|
|
input = input.left(input.getLength() - 1);
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drawDispay();
|
|
|
|
updateTerminal();
|
|
|
|
|
|
|
|
if ( infix_operator && ! isDataEntryKey(key) )
|
|
|
|
input = "";
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
last_key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::adjustSize()
|
|
|
|
{
|
2018-10-14 06:25:33 +02:00
|
|
|
std::size_t pw = getParentWidget()->getWidth();
|
|
|
|
std::size_t ph = getParentWidget()->getHeight();
|
|
|
|
setX (1 + int(pw - getWidth()) / 2, false);
|
|
|
|
setY (1 + int(ph - getHeight()) / 2, false);
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::adjustSize();
|
2015-06-22 00:28:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:27:17 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
const wchar_t* Calc::getButtonText (int key)
|
|
|
|
{
|
|
|
|
static const wchar_t* const button_text[Calc::NUM_OF_BUTTONS] =
|
|
|
|
{
|
|
|
|
L"&Sin",
|
|
|
|
L"&Cos",
|
|
|
|
L"&Tan",
|
|
|
|
L"1/&x",
|
|
|
|
L"&On",
|
|
|
|
L"L&n",
|
|
|
|
L"&e\x02e3",
|
|
|
|
L"&y\x02e3",
|
|
|
|
L"Sq&r",
|
|
|
|
L"&\xf7",
|
|
|
|
L"&Lg",
|
|
|
|
L"10&\x02e3",
|
|
|
|
L"&(",
|
|
|
|
L"&)",
|
|
|
|
L"&\xd7",
|
|
|
|
L"&Hyp",
|
|
|
|
L"&7",
|
|
|
|
L"&8",
|
|
|
|
L"&9",
|
|
|
|
L"&-",
|
|
|
|
L"&Arc",
|
|
|
|
L"&4",
|
|
|
|
L"&5",
|
|
|
|
L"&6",
|
|
|
|
L"&+",
|
|
|
|
L"&\x03c0",
|
|
|
|
L"&1",
|
|
|
|
L"&2",
|
|
|
|
L"&3",
|
|
|
|
L"&%",
|
|
|
|
L"&0",
|
|
|
|
L"&.",
|
|
|
|
L"&±",
|
|
|
|
L"&="
|
|
|
|
};
|
|
|
|
|
|
|
|
return button_text[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Calc::mapKeyFunctions()
|
|
|
|
{
|
|
|
|
key_map[Sine] = &Calc::sine; // sin
|
|
|
|
key_map[Cosine] = &Calc::cosine; // cos
|
|
|
|
key_map[Tangent] = &Calc::tangent; // tan
|
|
|
|
key_map[Reciprocal] = &Calc::reciprocal; // 1/x
|
|
|
|
key_map[On] = &Calc::clear; // On
|
|
|
|
key_map[Natural_logarithm] = &Calc::log_e; // ln
|
|
|
|
key_map[Powers_of_e] = &Calc::power_e; // eˣ
|
|
|
|
key_map[Power] = &Calc::power; // yˣ
|
|
|
|
key_map[Square_root] = &Calc::square_root; // sqrt
|
|
|
|
key_map[Divide] = &Calc::divide; // ÷
|
|
|
|
key_map[Common_logarithm] = &Calc::log_10; // lg
|
|
|
|
key_map[Powers_of_ten] = &Calc::power_10; // 10ˣ
|
|
|
|
key_map[Parenthese_l] = &Calc::open_bracket; // (
|
|
|
|
key_map[Parenthese_r] = &Calc::close_bracket; // )
|
|
|
|
key_map[Multiply] = &Calc::multiply; // *
|
|
|
|
key_map[Hyperbolic] = &Calc::hyperbolic; // hyp
|
|
|
|
key_map[Seven] = &Calc::seven; // 7
|
|
|
|
key_map[Eight] = &Calc::eight; // 8
|
|
|
|
key_map[Nine] = &Calc::nine; // 9
|
|
|
|
key_map[Subtract] = &Calc::subtract; // -
|
|
|
|
key_map[Arcus] = &Calc::arcus; // arc
|
|
|
|
key_map[Four] = &Calc::four; // 4
|
|
|
|
key_map[Five] = &Calc::five; // 5
|
|
|
|
key_map[Six] = &Calc::six; // 6
|
|
|
|
key_map[Add] = &Calc::add; // +
|
|
|
|
key_map[Pi] = &Calc::pi; // π
|
|
|
|
key_map[One] = &Calc::one; // 1
|
|
|
|
key_map[Two] = &Calc::two; // 2
|
|
|
|
key_map[Three] = &Calc::three; // 3
|
|
|
|
key_map[Percent] = &Calc::percent; // %
|
|
|
|
key_map[Zero] = &Calc::zero; // 0
|
|
|
|
key_map[Decimal_point] = &Calc::radix_point; // .
|
|
|
|
key_map[Change_sign] = &Calc::change_sign; // ±
|
|
|
|
key_map[Equals] = &Calc::equals; // =
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:51:56 +01:00
|
|
|
|
2015-06-22 00:28:06 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// Create the application object
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication app(argc, argv);
|
2015-06-22 00:28:06 +02:00
|
|
|
|
|
|
|
// Create a calculator object
|
|
|
|
Calc calculator(&app);
|
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Set calculator object as main widget
|
2015-06-22 00:28:06 +02:00
|
|
|
app.setMainWidget(&calculator);
|
2017-09-19 06:18:03 +02:00
|
|
|
|
|
|
|
// Show and start the application
|
2015-06-22 00:28:06 +02:00
|
|
|
calculator.show();
|
|
|
|
return app.exec();
|
|
|
|
}
|