gpt4 book ai didi

c++ - 从 wxString 转换为 double 时,小数位被解释为零

转载 作者:行者123 更新时间:2023-11-28 02:38:35 24 4
gpt4 key购买 nike

我正在为 GUI 使用 wxWidgets,我需要将文本框中的字符串转换为 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;
}
}

最佳答案

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 函数称为 CRT 函数包装器 的文档很少,因为每个这样的函数都包装了一些 CRT 函数。您可以在 C++ 引用中找到完整的 CRT 函数文档。例如,对于 wxAtoi,请参见 atoi 函数:http://www.cplusplus.com/reference/cstdlib/atoi/ ,对于 wxAtof 参见 atof 函数:http://www.cplusplus.com/reference/cstdlib/atof/

关于c++ - 从 wxString 转换为 double 时,小数位被解释为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26696892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com