我正在使用wxWidgets进行GUI,我需要将文本框中的字符串转换为double,以便我可以正确处理它们并将其用于计算,但似乎程序用0替换所有小数位。这是当我把它从wxString转换为double。我评论了错误发生的位置。我想你可能会看到整个代码。它有点像下面。这是代码:
C ++代码:
#include <wx/wx.h>
#include "determinantsFrame.h"
#include "starter.h"
#include <cmath>
#include <ctype.h>
using namespace std;
BEGIN_EVENT_TABLE(determinantsFrame, wxFrame)
EVT_MENU(wxID_ABOUT, determinantsFrame::OnAbout)
EVT_MENU(wxID_EXIT, determinantsFrame::OnQuit)
EVT_CLOSE(determinantsFrame::OnClose)
EVT_BUTTON(ID_Calculate, determinantsFrame::OnCalculateClick)
EVT_BUTTON(ID_Reset, determinantsFrame::OnResetClick)
EVT_BUTTON(ID_Fill, determinantsFrame::OnFillEmptyCellsWithZeroClick)
END_EVENT_TABLE()
int n;
wxTextCtrl *numbers[100][100];
const long determinantsFrame::ID_Calculate = wxNewId();
const long determinantsFrame::ID_Reset = wxNewId();
const long determinantsFrame::ID_Fill = wxNewId();
void determinantsFrame::OnClose(wxCloseEvent &event)
{
exit(1);
}
void determinantsFrame::OnAbout(wxCommandEvent &event)
{
wxString msg;
msg.Printf(wxT("Toolkit used: %s"), wxVERSION_STRING);
wxMessageBox(msg, wxT("About Determinants Calculator"));
}
void determinantsFrame::OnQuit(wxCommandEvent &event)
{
Close();
}
determinantsFrame::~determinantsFrame()
{
Destroy();
}
determinantsFrame::determinantsFrame() : wxFrame(NULL, wxID_ANY, wxT("Determinants Calculator"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE)
{
fileMenu = new wxMenu;
helpMenu = new wxMenu;
menuBar = new wxMenuBar();
helpMenu->Append(wxID_ABOUT, wxT("&About"), wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("&Exit"), wxT("Quit this program"));
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));
SetMenuBar(menuBar);
while(true)
{
td = new wxTextEntryDialog(this, wxT("Enter the number of dimensions: "), wxGetTextFromUserPromptStr, wxT("2"));
td->ShowModal();
str = td->GetValue();
n = wxAtoi(str);
if (n != 0)
break;
else
wxMessageBox(wxT("The dimension is n x n. You may have entered zero or an invalid character. Only an integer will be accepted such that n \nrepresents the dimension."));
}
mainBox = new wxBoxSizer(wxVERTICAL);
MainPanel = new wxPanel(this, wxID_ANY);
matrix = new wxStaticBoxSizer(wxVERTICAL, MainPanel, "Matrix: ");
cells = new wxFlexGridSizer(0, n, 0, 0);
subBox = new wxBoxSizer(wxVERTICAL);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
numbers[i][j] = new wxTextCtrl(MainPanel, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(50,20), 0, wxDefaultValidator, _T("ID_TextCtrl"));
cells->Add(numbers[i][j], 0, wxALL | wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
}
matrix->Add(cells, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 2);
subBox->Add(matrix, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
wxBoxSizer *enb = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *rnb = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *bc = new wxBoxSizer(wxHORIZONTAL);
Enter = new wxButton(MainPanel, ID_Calculate, _("Calculate"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_Calculate"));
Reset = new wxButton(MainPanel, ID_Reset, _("Reset"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_Reset"));
enb->Add(Enter, 1, wxALL |wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
rnb->Add(Reset, 1, wxALL |wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
bc->Add(enb, 0, wxALL | wxEXPAND);
bc->Add(rnb, 0, wxALL | wxEXPAND);
Enter->Bind(wxEVT_BUTTON, &determinantsFrame::OnCalculateClick, this);
Reset->Bind(wxEVT_BUTTON, &determinantsFrame::OnResetClick, this);
subBox->Add(bc, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL);
FillEmptyCells = new wxButton(MainPanel, ID_Fill, _("Fill Empty Cells With Zero"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_Fill"));
subBox->Add(FillEmptyCells, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
FillEmptyCells->Bind(wxEVT_BUTTON, &determinantsFrame::OnFillEmptyCellsWithZeroClick, this);
MainPanel->SetSizer(subBox);
mainBox->Add(MainPanel);
mainBox->Fit(this);
this->SetSizer(mainBox);
CentreOnScreen();
}
void determinantsFrame::OnResetClick(wxCommandEvent &event)
{
determinantsFrame *df;
this->Show(false);
df = new determinantsFrame();
df->Show(true);
}
void determinantsFrame::OnFillEmptyCellsWithZeroClick(wxCommandEvent &event)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (numbers[i][j]->GetValue() == wxEmptyString)
numbers[i][j]->SetValue(wxT("0"));
}
void determinantsFrame::OnCalculateClick(wxCommandEvent &event)
{
double elem[MAX][MAX]; double det; string test; bool doIt = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
test = (numbers[i][j]->GetValue()).mb_str();
if (test == "")
{
doIt = false;
break;
}
for (int k = 0; k < test.length(); k++)
if (isalpha(test[k]) || test[k] == ' ')
{
doIt = false;
break;
}
if (doIt == false)
break;
}
if (doIt == false)
break;
}
if (doIt)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
elem[i][j] = static_cast<double>(wxAtoi(numbers[i][j]->GetValue())); //here's where the problem occurs
wxMessageBox(wxString::Format(wxT("The determinant is: %.2lf"), elem[i][j])); // here's what I use to test the values inside.
}
det = determinant(elem, n);
wxMessageBox(wxString::Format(wxT("The determinant is: %.2lf"),det));
}
else
wxMessageBox(wxT("You may have entered an invalid character. Please try again"));
}
double determinantsFrame::determinant(double matrix[MAX][MAX], int order)
{
double det = 0; double temp[MAX][MAX]; int row, col;
if (order == 1)
return matrix[0][0];
else if (order == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]));
else
{
for (int r = 0; r < order; r++)
{
col = 0; row = 0;
for (int i = 1; i < order; i++)
{
for (int j = 0; j < order; j++)
{
if (j == r)
continue;
temp[row][col] = matrix[i][j];
col++;
if (col == order - 1)
col = 0;
}
row++;
}
det = det + (matrix[0][r] * pow(-1, r) * determinant(temp, order - 1));
}
wxMessageBox(wxString::Format(wxT("The determinant is: %.2lf"),matrix[0][0]));
return det;
}
}
答案 0 :(得分:2)
elem[i][j] = static_cast<double>(wxAtoi(numbers[i][j]->GetValue()));
替换为:
elem[i][j] = wxAtof(numbers[i][j]->GetValue());
功能int wxAtoi (const wxString &str)
将wsString
转换为int
。您需要double wxAtof (const wxString &str)
将wxString
转换为double
wxWidgets函数名为Wrappers of CRT functions
的文档记录很少,因为每个这样的函数都包含一些CRT函数。您可以在C ++参考中找到完整的CRT功能文档。例如,对于wxAtoi
,请参阅atoi
函数:http://www.cplusplus.com/reference/cstdlib/atoi/,对于wxAtof
,请参阅atof
函数:http://www.cplusplus.com/reference/cstdlib/atof/