📄 wndmain.h
字号:
this->Name = S"WinCalc";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = S"Calculator";
this->paBase->ResumeLayout(false);
this->gbCalc->ResumeLayout(false);
this->paTitle->ResumeLayout(false);
this->ResumeLayout(false);
}
private:
mu::ParserBase *m_pParser;
mu::Parser *m_pParserDbl;
mu::ParserInt *m_pParserInt;
double *m_pfVar;
StringCollection *m_History;
int m_iHistLine;
int m_iVarCount;
WndError *dlgError;
// Container for a ComboBox entry
// consisting of a name and a pointer
// to a parser object
__gc class BoxItem : public System::Object
{
public:
BoxItem(const char *a_szName, mu::ParserBase *a_pParser)
:System::Object()
,m_pParser(a_pParser)
,m_strName(new System::String(a_szName))
{
};
String* ToString()
{
return m_strName;
}
mu::ParserBase* GetParser()
{
return m_pParser;
};
private:
mu::ParserBase *m_pParser;
String *m_strName;
};
//---------------------------------------------------------------------------
void ScrollToEnd()
{
reMain->Select(reMain->Text->Length, 0);
reMain->Focus();
reMain->ScrollToCaret();
edInput->Focus();
}
//---------------------------------------------------------------------------
void Calc(const char *pszFormula)
{
reMain->SelectionColor = System::Drawing::Color::Blue;
reMain->AppendText(pszFormula);
reMain->AppendText("\r\n");
reMain->SelectionColor = System::Drawing::Color::Black;
try
{
using mu::Parser::exception_type;
// 1.) does the formula contain a equal sign?
char szName[256], szEqn[256];
int iStat = std::sscanf(pszFormula, "%255[^=]=%255s", szName, szEqn);
// There is no "=" present in the string, thus it cant be a variable declaration
// and must be a normal formula
if (iStat!=2)
{
m_pParser->SetExpr(pszFormula);
if (cbShowVar->Checked)
DumpUsedVariables();
m_pfVar[0] = m_pParser->Eval(); // Calculate result and store in variable "ans"
}
else
{
if (m_iVarCount>=99)
throw mu::Parser::exception_type("Maximum number of variables exceeded");
// Variable name is empty or contains invalid charakters
if (!std::strlen(szName) ||
std::strspn(szName, m_pParser->ValidNameChars())!=std::strlen(szName))
{
std::ostringstream os;
os << "Invalid token name: \"" << szName << "\"\n";
throw mu::Parser::exception_type(os.str().c_str());
}
m_pParser->SetExpr(szEqn);
// Calculate the new variable value
m_pfVar[0] = m_pfVar[m_iVarCount] = m_pParser->Eval();
// Add to parser
m_pParser->DefineVar(szName, &m_pfVar[m_iVarCount++] );
}
// Print the result
String *pResult = Convert::ToString( m_pfVar[0] );
reMain->AppendText("ans = ");
reMain->AppendText(pResult);
reMain->AppendText("\r\n");
}
catch(Parser::exception_type &e)
{
m_pfVar[0] = 0;
reMain->SelectionColor = System::Drawing::Color::Red;
reMain->AppendText(e.GetMsg().c_str());
reMain->AppendText("\r\n");
reMain->SelectionColor = System::Drawing::Color::Black;
}
ScrollToEnd();
}
//---------------------------------------------------------------------------
void Reset()
{
delete [] m_pfVar;
m_pfVar = new double[VARCOUNT];
m_pfVar[0] = 0;
// Reinitialize the parser
m_pParserInt->ClearVar();
m_pParserInt->DefineVar("ans", &m_pfVar[0]);
m_pParserDbl->ClearVar();
m_pParserDbl->DefineVar("ans", &m_pfVar[0]);
cbParser->SelectedIndex = 0;
// Clear history and text fields
m_History->Clear();
reMain->ResetText();
edInput->Text = "";
}
//---------------------------------------------------------------------------
void DumpUsedVariables()
{
// Query the used variables (must be done after calc)
mu::varmap_type UsedVar = m_pParser->GetUsedVar();
if (UsedVar.size())
{
std::ostringstream os;
os << "Used variables:\n";
os << " Number: " << (int)UsedVar.size() << "\n";
mu::varmap_type::const_iterator item = UsedVar.begin();
for (; item!=UsedVar.end(); ++item)
os << " Name: " << item->first << " Address: [0x" << item->second << "]\n";
reMain->SelectionColor = System::Drawing::Color::Green;
reMain->AppendText(os.str().c_str());
reMain->SelectionColor = System::Drawing::Color::Black;
reMain->Invalidate();
}
}
private: System::Void btnReset_Click(System::Object*, System::EventArgs*)
{
Reset();
}
private: System::Void btnUnit_Click(System::Object*, System::EventArgs*)
{
Reset();
std::ostringstream os;
os << "------------------------------------------------------------\n";
os << " Math Parser sample application\n";
os << "------------------------------------------------------------\n";
os << "\nUnit testing in progress...\n";
reMain->AppendText(os.str().c_str());
reMain->Update();
os.str(std::string());
mu::Test::ParserTester pt;
pt.SetStream(&os);
pt.Run();
reMain->AppendText(os.str().c_str());
ScrollToEnd();
}
private: System::Void edInput_KeyDown(System::Object*, System::Windows::Forms::KeyEventArgs *e)
{
enum Keys iCode = e->KeyCode;
int iHistLen = m_History->Count;
switch(iCode)
{
case Return:
{
char *pszFormula = (char*)(void*)Marshal::StringToHGlobalAnsi(edInput->Text);
try
{
Calc(pszFormula);
// Add Formula to history
if ( pszFormula && std::strlen(pszFormula) )
m_iHistLine = m_History->Add(pszFormula);
edInput->Text = "";
}
catch(...)
{
Marshal::FreeHGlobal(pszFormula);
throw;
}
Marshal::FreeHGlobal(pszFormula);
}
break;
case Up:
case Down:
m_iHistLine = System::Math::Max(0, System::Math::Min(iHistLen-1, m_iHistLine) );
if (!m_History->Count)
break;
edInput->Text = static_cast<String*>(m_History->Item[m_iHistLine]);
edInput->Select(edInput->Text->Length,0);
m_iHistLine += (iCode==Up) ? -1 : 1;
break;
}
}
private: System::Void btnListFun_Click(System::Object*, System::EventArgs*)
{
// Make the type of the map containing function prototypes visible
using mu::funmap_type;
funmap_type funmap = m_pParser->GetFunDef();
funmap_type::const_iterator item;
std::ostringstream os;
os << "\nFunctions available:\n";
// iterate over all function definitions
for (item=funmap.begin(); item!=funmap.end(); ++item)
{
os << " " << item->first; // This is the Function Name
os << "(";
// Deal with different argument numbers
int iArgc = item->second.GetArgc();
if (iArgc>=0)
{
// Indicate number of input variables
for (int i=0; i<iArgc; ++i)
{
char cVar[] = "val ";
cVar[3] = '1' + (char)i;
os << cVar;
if (i!=iArgc-1)
os << ",";
}
}
else
{
os << "..."; // Multiargument function
}
os << ")\r\n";
}
// output
reMain->Focus();
reMain->SelectionColor = System::Drawing::Color::Green;
reMain->AppendText(os.str().c_str());
reMain->SelectionColor = System::Drawing::Color::Black;
ScrollToEnd();
}
private: System::Void btnListConst_Click(System::Object*, System::EventArgs*)
{
// Make the type of the map containing function prototypes visible
using mu::valmap_type;
valmap_type cvalmap = m_pParser->GetConst();
valmap_type::const_iterator item;
std::ostringstream os;
os << "\nConstants available:\n";
// iterate over all function definitions
if (!cvalmap.size())
{
os << " none\n";
}
else
{
for (item=cvalmap.begin(); item!=cvalmap.end(); ++item)
{
os << " " << item->first; // This is the Function Name
os << " = ";
os << item->second;
os << "\n";
}
}
// output
reMain->Focus();
reMain->SelectionColor = System::Drawing::Color::Green;
reMain->AppendText(os.str().c_str());
reMain->SelectionColor = System::Drawing::Color::Black;
ScrollToEnd();
}
private: System::Void btnListVar_Click(System::Object*, System::EventArgs*)
{
mu::varmap_type UsedVar = m_pParser->GetVar();
if (UsedVar.size())
{
std::ostringstream os;
os << "\nVariables available:\n";
mu::varmap_type::const_iterator item = UsedVar.begin();
for (; item!=UsedVar.end(); ++item)
os << " " << item->first << " = " << *item->second << "\n";
reMain->SelectionColor = System::Drawing::Color::Green;
reMain->AppendText(os.str().c_str());
reMain->SelectionColor = System::Drawing::Color::Black;
reMain->Invalidate();
ScrollToEnd();
}
}
private: System::Void cbParser_SelectedIndexChanged(System::Object*, System::EventArgs*)
{
BoxItem *pItem = static_cast<BoxItem*>(cbParser->SelectedItem);
m_pParser = pItem->GetParser();
reMain->SelectionColor = System::Drawing::Color::Green;
reMain->AppendText( "\nSetting parser to: " );
reMain->AppendText( pItem->ToString() );
reMain->AppendText( "\n" );
reMain->SelectionColor = System::Drawing::Color::Black;
ScrollToEnd();
}
}; // end of class
} // end of namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -