Kalkulator liczb rzeczywistych

0

Witam. Czy jest tu ktoś kto ogarnia wxWidgets? Napisałem program, który wyświetla w oknie kalkulator z przyciskami natomiast wynik wyświetlany jest tylko jako liczby całkowite, bez przecinków. Czy ktoś wie, co muszę zmienić w kodzie?

/***************************************************************
 * Name:      Zad_2Main.cpp
 * Purpose:   Code for Application Frame
 * Author:     ()
 * Created:   2023-05-10
 * Copyright:  ()
 * License:
 **************************************************************/

#include "Zad_2Main.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(Zad_2Frame)
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(Zad_2Frame)
const long Zad_2Frame::ID_TEXTCTRL1 = wxNewId();
const long Zad_2Frame::ID_BUTTON1 = wxNewId();
const long Zad_2Frame::ID_BUTTON2 = wxNewId();
const long Zad_2Frame::ID_BUTTON3 = wxNewId();
const long Zad_2Frame::ID_BUTTON4 = wxNewId();
const long Zad_2Frame::ID_BUTTON5 = wxNewId();
const long Zad_2Frame::ID_BUTTON6 = wxNewId();
const long Zad_2Frame::ID_BUTTON7 = wxNewId();
const long Zad_2Frame::ID_BUTTON8 = wxNewId();
const long Zad_2Frame::ID_BUTTON9 = wxNewId();
const long Zad_2Frame::ID_BUTTON10 = wxNewId();
const long Zad_2Frame::ID_BUTTON11 = wxNewId();
const long Zad_2Frame::ID_BUTTON12 = wxNewId();
const long Zad_2Frame::ID_BUTTON13 = wxNewId();
const long Zad_2Frame::ID_BUTTON15 = wxNewId();
const long Zad_2Frame::ID_BUTTON17 = wxNewId();
const long Zad_2Frame::ID_BUTTON18 = wxNewId();
const long Zad_2Frame::ID_BUTTON14 = wxNewId();
const long Zad_2Frame::idMenuQuit = wxNewId();
const long Zad_2Frame::idMenuAbout = wxNewId();
const long Zad_2Frame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Zad_2Frame,wxFrame)
    //(*EventTable(Zad_2Frame)
    //*)
END_EVENT_TABLE()

Zad_2Frame::Zad_2Frame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(Zad_2Frame)
    wxMenu* Menu1;
    wxMenu* Menu2;
    wxMenuBar* MenuBar1;
    wxMenuItem* MenuItem1;
    wxMenuItem* MenuItem2;

    Create(parent, id, _("Calculator"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(400,450));
    Move(wxPoint(100,100));
    wxFont thisFont(25,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    SetFont(thisFont);
    TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, _("0"), wxPoint(8,32), wxSize(384,48), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    wxFont TextCtrl1Font(26,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    TextCtrl1->SetFont(TextCtrl1Font);
    Button1 = new wxButton(this, ID_BUTTON1, _("1"), wxPoint(8,96), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON1"));
    wxFont Button1Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button1->SetFont(Button1Font);
    Button2 = new wxButton(this, ID_BUTTON2, _("2"), wxPoint(88,96), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON2"));
    wxFont Button2Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button2->SetFont(Button2Font);
    Button3 = new wxButton(this, ID_BUTTON3, _("3"), wxPoint(168,96), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON3"));
    wxFont Button3Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button3->SetFont(Button3Font);
    Button4 = new wxButton(this, ID_BUTTON4, _("4"), wxPoint(248,96), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON4"));
    wxFont Button4Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button4->SetFont(Button4Font);
    Button5 = new wxButton(this, ID_BUTTON5, _("+"), wxPoint(328,96), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON5"));
    wxFont Button5Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button5->SetFont(Button5Font);
    Button6 = new wxButton(this, ID_BUTTON6, _("5"), wxPoint(8,184), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON6"));
    wxFont Button6Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button6->SetFont(Button6Font);
    Button7 = new wxButton(this, ID_BUTTON7, _("6"), wxPoint(88,184), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON7"));
    wxFont Button7Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button7->SetFont(Button7Font);
    Button8 = new wxButton(this, ID_BUTTON8, _("7"), wxPoint(168,184), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON8"));
    wxFont Button8Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button8->SetFont(Button8Font);
    Button9 = new wxButton(this, ID_BUTTON9, _("8"), wxPoint(248,184), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON9"));
    wxFont Button9Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button9->SetFont(Button9Font);
    Button10 = new wxButton(this, ID_BUTTON10, _("-"), wxPoint(328,184), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON10"));
    wxFont Button10Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button10->SetFont(Button10Font);
    Button11 = new wxButton(this, ID_BUTTON11, _("9"), wxPoint(8,272), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON11"));
    wxFont Button11Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button11->SetFont(Button11Font);
    Button12 = new wxButton(this, ID_BUTTON12, _("0"), wxPoint(88,272), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON12"));
    wxFont Button12Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button12->SetFont(Button12Font);
    Button13 = new wxButton(this, ID_BUTTON13, _("="), wxPoint(168,272), wxSize(144,64), 0, wxDefaultValidator, _T("ID_BUTTON13"));
    wxFont Button13Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button13->SetFont(Button13Font);
    Button15 = new wxButton(this, ID_BUTTON15, _("x"), wxPoint(328,272), wxSize(64,64), 0, wxDefaultValidator, _T("ID_BUTTON15"));
    wxFont Button15Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button15->SetFont(Button15Font);
    Button17 = new wxButton(this, ID_BUTTON17, _("Clear"), wxPoint(8,360), wxSize(216,56), 0, wxDefaultValidator, _T("ID_BUTTON17"));
    wxFont Button17Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button17->SetFont(Button17Font);
    Button18 = new wxButton(this, ID_BUTTON18, _("/"), wxPoint(328,360), wxSize(64,56), 0, wxDefaultValidator, _T("ID_BUTTON18"));
    wxFont Button18Font(14,wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button18->SetFont(Button18Font);
    Button14 = new wxButton(this, ID_BUTTON14, _("."), wxPoint(240,360), wxSize(72,56), 0, wxDefaultValidator, _T("ID_BUTTON14"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&Zad_2Frame::OnTextCtrl1Text);
    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton1Click);
    Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton2Click);
    Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton3Click);
    Connect(ID_BUTTON4,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton4Click);
    Connect(ID_BUTTON5,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton5Click);
    Connect(ID_BUTTON6,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton6Click);
    Connect(ID_BUTTON7,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton7Click);
    Connect(ID_BUTTON8,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton8Click);
    Connect(ID_BUTTON9,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton9Click);
    Connect(ID_BUTTON10,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton10Click);
    Connect(ID_BUTTON11,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton11Click);
    Connect(ID_BUTTON12,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton12Click);
    Connect(ID_BUTTON13,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton13Click);
    Connect(ID_BUTTON15,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton15Click);
    Connect(ID_BUTTON17,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton17Click);
    Connect(ID_BUTTON18,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton18Click);
    Connect(ID_BUTTON14,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Zad_2Frame::OnButton14Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Zad_2Frame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Zad_2Frame::OnAbout);
    //*)
}

Zad_2Frame::~Zad_2Frame()
{
    //(*Destroy(Zad_2Frame)
    //*)
}

void Zad_2Frame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void Zad_2Frame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}
//------------------------------------------------------------------------------------------------------------------
wxString fast, last, Res;
int Operand1, Operand2, Operators;
double Answer;
//------------------------------------------------------------------------------------------------------------------
void Zad_2Frame::OnButton6Click(wxCommandEvent& event)
{
}

void Zad_2Frame::OnButton5Click(wxCommandEvent& event)
{
    fast = TextCtrl1->GetValue();
    Operand1 = wxAtoi(fast);
    Operators = 1; //+
    TextCtrl1->SetValue("0");
}

void Zad_2Frame::OnTextCtrl1Text(wxCommandEvent& event)
{
}

void Zad_2Frame::OnButton1Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button1->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button1->GetLabel());
    }
}

void Zad_2Frame::OnButton2Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button2->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button2->GetLabel());
    }
}

void Zad_2Frame::OnButton3Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button3->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button3->GetLabel());
    }
}

void Zad_2Frame::OnButton4Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button4->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button4->GetLabel());
    }
}

void Zad_2Frame::OnButton7Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button7->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button7->GetLabel());
    }
}

void Zad_2Frame::OnButton8Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button8->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button8->GetLabel());
    }
}

void Zad_2Frame::OnButton9Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button9->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button9->GetLabel());
    }
}

void Zad_2Frame::OnButton11Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button11->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button11->GetLabel());
    }
}

void Zad_2Frame::OnButton12Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button12->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button12->GetLabel());
    }
}

void Zad_2Frame::OnButton17Click(wxCommandEvent& event)
{
    TextCtrl1->SetValue("0");
}

void Zad_2Frame::OnButton10Click(wxCommandEvent& event)
{
    fast = TextCtrl1->GetValue();
    Operand1 = wxAtoi(fast);
    Operators = 2; //-
    TextCtrl1->SetValue("0");
}

void Zad_2Frame::OnButton15Click(wxCommandEvent& event)
{
    fast = TextCtrl1->GetValue();
    Operand1 = wxAtoi(fast);
    Operators = 3; //*
    TextCtrl1->SetValue("0");
}

void Zad_2Frame::OnButton18Click(wxCommandEvent& event)
{
    fast = TextCtrl1->GetValue();
    Operand1 = wxAtoi(fast);
    Operators = 4; //
    TextCtrl1->SetValue("0");
}

void Zad_2Frame::OnButton13Click(wxCommandEvent& event)
{
    last = TextCtrl1->GetValue();
    Operand2 = wxAtoi(last);
    switch(Operators)
    {
    case 1:
        Answer = double(Operand1) + double(Operand2);
        Res = wxString::Format(wxT("%g"),Answer);
        TextCtrl1->SetValue(Res);
        break;
    case 2:
        Answer = double(Operand1) - double(Operand2);
        Res = wxString::Format(wxT("%g"),Answer);
        TextCtrl1->SetValue(Res);
        break;
    case 3:
        Answer = double(Operand1) * double(Operand2);
        Res = wxString::Format(wxT("%g"),Answer);
        TextCtrl1->SetValue(Res);
        break;
    case 4:
        Answer = double(Operand1) / double(Operand2);
        Res = wxString::Format(wxT("%g"),Answer);
        TextCtrl1->SetValue(Res);
        break;
    }
}

void Zad_2Frame::OnButton14Click(wxCommandEvent& event)
{
    if (TextCtrl1->GetValue() == "0")
    {
        TextCtrl1->SetValue(Button14->GetLabel());
    }
    else
    {
        TextCtrl1->AppendText(Button14->GetLabel());
    }
}
0

Podaj przykład działania, które wykonujesz, a nie tylko sam wynik, który wynosi 0.
Jeśli wykonujesz np. 1.2 + 2.3, to wynik będzie całkowity, ponieważ przed wykonaniem działania wywołujesz wxAtoi() na tych wartościach.
Spróbuj wykonać 1 / 2.

1

wxAtoi odnosi się do wartości INT. Spróbuj wxAtof

3
GitExe64 napisał(a):

wxAtoi odnosi się do wartości INT. Spróbuj wxAtof

W jego kodzie jest

int Operand1, Operand2, Operators;

Zmiennoprzecinkowych używa tylko przy wyniku końcowym.
Ergo najpierw powinien ogarnąć ten problem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1