Hi,
Has anyone seen the code simple calculator?
I need to convert the simplest expression to a number.
For example 3+4+5,5 should be converted to 12,5
It's for personal financial app
http://sourceforge.net/projects/moneymanagerex/
Thanks
a simple calculator needed Topic is solved
Re: a simple calculator needed
It may be a little overkill for your purpose, but i use muParser:
http://muparser.sourceforge.net/
http://muparser.sourceforge.net/
Use the source, Luke!
Re: a simple calculator needed
or bison, or antlr. Or just a parser you would write yourself. It really depends on how complete you want it, i.e. do you want * and / to be prioritary over + and -, do you want the ability to enter parentheses, etc.
"Keyboard not detected. Press F1 to continue"
-- Windows
-- Windows
Re: a simple calculator needed
Yes, I'll prefer parser if it created myself, but I am beginner in c++. I prefer transform available code to my needed. Can't write code from scratch.Auria wrote: Or just a parser you would write yourself.
It seems that there are no so simple calculators and I will write my.
Without parentheses, it's simple.
M$, VS2017, C++
Re: a simple calculator needed
I've created it by myself.
It support only main operations like + - / * and ()
It support only main operations like + - / * and ()
Code: Select all
bool mmCalculator(wxString sInput, wxString& sOutput)
{
//TODO: Fix Non standart number format
sInput.Replace(wxT(")("), wxT(")*("));
bool bResult = true;
int a = sInput.Replace(wxT("("), wxT("("));
int b = sInput.Replace(wxT(")"), wxT(")"));
if (a != b) return false;
if (a > 0)
{
for (size_t i = 0; i < sInput.Len(); i++)
{
if (sInput[i] == '(') a += i;
if (sInput[i] == ')') b += i;
if (sInput[i] == '(' && i > 0) bResult = bResult && (wxString(wxT("(+-*/")).Contains(sInput[i-1]));
if (sInput[i] == ')' && i < sInput.Len()-1) bResult = bResult && (wxString(wxT(")+-*/")).Contains(sInput[i+1]));
}
if (a >= b || !bResult) return false;
}
wxString sTemp = sInput.Trim().Prepend(wxT("(")).Append(wxT(")"));
wxString midBrackets, sToCalc;
double dAmount = 0;
while (sTemp.Contains(wxT("(")) && bResult)
{
dAmount = 0;
size_t leftPos = sTemp.Find('(', true);
size_t rightPos = sTemp.find(wxT(")"), leftPos);
midBrackets = sTemp.SubString(leftPos, rightPos);
midBrackets.Replace(wxT("(-"), wxT("(N"));
sToCalc = midBrackets.SubString(1, midBrackets.Len()-2);
if (sToCalc.IsEmpty()) bResult = false;
double dTempAmount;
sToCalc.Replace(wxT("*-"), wxT("M"));
sToCalc.Replace(wxT("/-"), wxT("D"));
sToCalc.Replace(wxT("+-"), wxT("-"));
sToCalc.Replace(wxT("-+"), wxT("-"));
sToCalc.Replace(wxT("+"), wxT("|"));
sToCalc.Replace(wxT("-"), wxT("|-"));
midBrackets.Replace(wxT("(N"), wxT("(-"));
wxStringTokenizer token(sToCalc, wxT("|"));
while (token.HasMoreTokens() && bResult)
{
double dSubtotal = 1;
wxString sToken = token.GetNextToken();
sToken.Replace(wxT("M"), wxT("|M"));
sToken.Replace(wxT("D"), wxT("|D"));
sToken.Replace(wxT("*"), wxT("|*"));
sToken.Replace(wxT("/"), wxT("|/"));
sToken.Replace(wxT("N"), wxT("-"));
sToken.Prepend(wxT("*"));
wxStringTokenizer token2(sToken, wxT("|"));
while (token2.HasMoreTokens() && bResult)
{
wxString sElement = token2.GetNextToken();
wxString sSign = sElement.Mid(0,1);
sElement.Remove(0,1);
if (sElement.ToDouble(&dTempAmount))
{
if (sSign == wxT("*")) dSubtotal = dSubtotal*dTempAmount;
else if (sSign == wxT("M")) dSubtotal = -dSubtotal*dTempAmount;
else if (sSign == wxT("/") && dTempAmount != 0) dSubtotal = dSubtotal/dTempAmount;
else if (sSign == wxT("D") && dTempAmount != 0) dSubtotal = -dSubtotal/dTempAmount;
else bResult = false;
}
else
bResult = false;
}
dAmount += dSubtotal;
}
sTemp.Replace(midBrackets, wxString()<<dAmount);
}
if (sTemp.Contains(wxT("("))||sTemp.Contains(wxT(")"))) bResult = false;
//if (bResult) mmex::formatDoubleToCurrencyEdit(dAmount, sOutput);
if (bResult) sOutput = wxString()<<dAmount;
return bResult;
}
- Attachments
-
- mmCalculator.cpp
- (3.3 KiB) Downloaded 937 times
M$, VS2017, C++
Re: a simple calculator needed
This code seems is popular.
It should be validator used for wxTextCtrl
https://sourceforge.net/p/moneymanagere ... lidators.h
I became to understand classes and will redesign this code.
It should be validator used for wxTextCtrl
https://sourceforge.net/p/moneymanagere ... lidators.h
Code: Select all
textAmount_ = new mmTextCtrl( this, ID_DIALOG_TRANS_TEXTAMOUNT, "",
wxDefaultPosition, wxSize(110, -1),
wxALIGN_RIGHT|wxTE_PROCESS_ENTER, mmCalcValidator());
M$, VS2017, C++
Re: a simple calculator needed
it has been updated
how to use
how to use
Code: Select all
#include "mmCalculator.h"
....
textAmount_ = new wxTextCtrl( this, wxID_ANY, "",
wxDefaultPosition, wxSize(110, -1),
wxALIGN_RIGHT|wxTE_PROCESS_ENTER, mmCalcValidator());
....
mmCalculator calc;
if (calc.is_ok(textAmount_->GetValue()))
textAmount_->SetValue(calc.get_result());
- Attachments
-
- mmCalculator.zip
- src
- (1.46 KiB) Downloaded 872 times
M$, VS2017, C++
Re: a simple calculator needed
Is it allowed to include your code into own code? If yes, under which license?