2014-11-02 17 views
0

我正在使用wxWidgets的GUI,我需要將文本框中的字符串轉換爲雙倍,所以我可以正確處理它們並將其用於計算,但它似乎是該程序將用0替換所有小數位。這是當我將它從wxString轉換爲double時。我評論了錯誤發生的地方。我想你可能會看到整個代碼。這是有點下面。下面的代碼:從wxString轉換爲小數位時解釋爲零

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

嗚嗚......你嘗試在發生問題的線路使用wxAtof代替wxAtoi? – audionuma 2014-11-02 07:25:35

回答

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)轉換wsStringint。您需要double wxAtof (const wxString &str)wxString轉換爲double

稱爲Wrappers of CRT functions的不良記錄wxWidgets的功能,因爲每一個這樣的功能包裝了一些CRT函數。您可以在C++參考中找到完整的CRT函數文檔。例如,對於wxAtoi看到atoi功能:http://www.cplusplus.com/reference/cstdlib/atoi/,爲wxAtof看到atof功能:http://www.cplusplus.com/reference/cstdlib/atof/

+0

謝謝! wxAtoi和wxAtof有什麼不同?這是否意味着我是爲int而f爲浮動的? – Jessie 2014-11-02 07:28:18

+1

答案是正確的,但我也建議使用[wxString :: ToDouble()](http://docs.wxwidgets.org/trunk/classwx_string.html#afca98ce954f669ec69696372e81d46ac)而不是'wxAtof()'來能夠檢測轉換失敗。 – 2014-11-02 11:44:47