Witam
Zrobiłem program kalkulator w Visual c++ 2010 i podczas dzielenia np. 13/2 wyświetla zamiast 6,5 to 6.
Proszę o pomoc.
- Jak zrobić aby można było obsługiwać kalkulator z klawiatury? (Wpisywanie licz, * to mnożenie, = równa się itp).
Kod programu:
#pragma once
namespace kalkulator_Kamil {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ lblDisplay;
protected:
protected:
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button7;
private: System::Windows::Forms::Button^ button8;
private: System::Windows::Forms::Button^ button9;
private: System::Windows::Forms::Button^ button10;
private: System::Windows::Forms::Button^ button11;
private: System::Windows::Forms::Button^ button12;
private: System::Windows::Forms::Button^ button13;
private: System::Windows::Forms::Button^ button14;
private: System::Windows::Forms::Button^ button15;
private: System::Windows::Forms::Button^ button16;
private: System::Windows::Forms::Button^ button17;
private: System::Windows::Forms::Button^ button18;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ plikToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ zamknijToolStripMenuItem;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->lblDisplay = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button7 = (gcnew System::Windows::Forms::Button());
this->button8 = (gcnew System::Windows::Forms::Button());
this->button9 = (gcnew System::Windows::Forms::Button());
this->button10 = (gcnew System::Windows::Forms::Button());
this->button11 = (gcnew System::Windows::Forms::Button());
this->button12 = (gcnew System::Windows::Forms::Button());
this->button13 = (gcnew System::Windows::Forms::Button());
this->button14 = (gcnew System::Windows::Forms::Button());
this->button15 = (gcnew System::Windows::Forms::Button());
this->button16 = (gcnew System::Windows::Forms::Button());
this->button17 = (gcnew System::Windows::Forms::Button());
this->button18 = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->plikToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->zamknijToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// lblDisplay
//
this->lblDisplay->BackColor = System::Drawing::SystemColors::ControlLightLight;
this->lblDisplay->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->lblDisplay->Location = System::Drawing::Point(48, 36);
this->lblDisplay->Name = L"lblDisplay";
this->lblDisplay->Size = System::Drawing::Size(286, 55);
this->lblDisplay->TabIndex = 0;
this->lblDisplay->Text = L"0";
this->lblDisplay->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
//
// button1
//
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button1->Location = System::Drawing::Point(12, 105);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(67, 67);
this->button1->TabIndex = 1;
this->button1->Text = L"7";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button2->Location = System::Drawing::Point(85, 105);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(67, 67);
this->button2->TabIndex = 2;
this->button2->Text = L"8";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button3->Location = System::Drawing::Point(158, 105);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(67, 67);
this->button3->TabIndex = 3;
this->button3->Text = L"9";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button4
//
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button4->Location = System::Drawing::Point(12, 178);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(67, 67);
this->button4->TabIndex = 4;
this->button4->Text = L"4";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// button5
//
this->button5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button5->Location = System::Drawing::Point(85, 178);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(67, 67);
this->button5->TabIndex = 5;
this->button5->Text = L"5";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// button6
//
this->button6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button6->Location = System::Drawing::Point(158, 178);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(67, 67);
this->button6->TabIndex = 6;
this->button6->Text = L"6";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// button7
//
this->button7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button7->Location = System::Drawing::Point(12, 251);
this->button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(67, 67);
this->button7->TabIndex = 7;
this->button7->Text = L"1";
this->button7->UseVisualStyleBackColor = true;
this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);
//
// button8
//
this->button8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button8->Location = System::Drawing::Point(85, 251);
this->button8->Name = L"button8";
this->button8->Size = System::Drawing::Size(67, 67);
this->button8->TabIndex = 8;
this->button8->Text = L"2";
this->button8->UseVisualStyleBackColor = true;
this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click);
//
// button9
//
this->button9->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button9->Location = System::Drawing::Point(158, 251);
this->button9->Name = L"button9";
this->button9->Size = System::Drawing::Size(67, 67);
this->button9->TabIndex = 9;
this->button9->Text = L"3";
this->button9->UseVisualStyleBackColor = true;
this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);
//
// button10
//
this->button10->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button10->Location = System::Drawing::Point(12, 324);
this->button10->Name = L"button10";
this->button10->Size = System::Drawing::Size(140, 67);
this->button10->TabIndex = 10;
this->button10->Text = L"0";
this->button10->UseVisualStyleBackColor = true;
this->button10->Click += gcnew System::EventHandler(this, &Form1::button10_Click);
//
// button11
//
this->button11->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button11->Location = System::Drawing::Point(158, 324);
this->button11->Name = L"button11";
this->button11->Size = System::Drawing::Size(67, 67);
this->button11->TabIndex = 11;
this->button11->Text = L"C";
this->button11->UseVisualStyleBackColor = true;
this->button11->Click += gcnew System::EventHandler(this, &Form1::button11_Click);
//
// button12
//
this->button12->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button12->Location = System::Drawing::Point(230, 105);
this->button12->Name = L"button12";
this->button12->Size = System::Drawing::Size(67, 67);
this->button12->TabIndex = 12;
this->button12->Text = L"+";
this->button12->UseVisualStyleBackColor = true;
this->button12->Click += gcnew System::EventHandler(this, &Form1::button12_Click);
//
// button13
//
this->button13->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button13->Location = System::Drawing::Point(231, 178);
this->button13->Name = L"button13";
this->button13->Size = System::Drawing::Size(67, 67);
this->button13->TabIndex = 13;
this->button13->Text = L"-";
this->button13->UseVisualStyleBackColor = true;
this->button13->Click += gcnew System::EventHandler(this, &Form1::button13_Click);
//
// button14
//
this->button14->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button14->Location = System::Drawing::Point(231, 251);
this->button14->Name = L"button14";
this->button14->Size = System::Drawing::Size(67, 67);
this->button14->TabIndex = 14;
this->button14->Text = L"*";
this->button14->UseVisualStyleBackColor = true;
this->button14->Click += gcnew System::EventHandler(this, &Form1::button14_Click);
//
// button15
//
this->button15->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button15->Location = System::Drawing::Point(231, 324);
this->button15->Name = L"button15";
this->button15->Size = System::Drawing::Size(67, 67);
this->button15->TabIndex = 15;
this->button15->Text = L"/";
this->button15->UseVisualStyleBackColor = true;
this->button15->Click += gcnew System::EventHandler(this, &Form1::button15_Click);
//
// button16
//
this->button16->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button16->Location = System::Drawing::Point(307, 105);
this->button16->Name = L"button16";
this->button16->Size = System::Drawing::Size(67, 67);
this->button16->TabIndex = 16;
this->button16->Text = L"sqrt";
this->button16->UseVisualStyleBackColor = true;
this->button16->Click += gcnew System::EventHandler(this, &Form1::button16_Click);
//
// button17
//
this->button17->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button17->Location = System::Drawing::Point(307, 178);
this->button17->Name = L"button17";
this->button17->Size = System::Drawing::Size(67, 67);
this->button17->TabIndex = 17;
this->button17->Text = L"^2";
this->button17->UseVisualStyleBackColor = true;
this->button17->Click += gcnew System::EventHandler(this, &Form1::button17_Click);
//
// button18
//
this->button18->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 21.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->button18->ForeColor = System::Drawing::Color::Crimson;
this->button18->Location = System::Drawing::Point(307, 251);
this->button18->Name = L"button18";
this->button18->Size = System::Drawing::Size(67, 140);
this->button18->TabIndex = 18;
this->button18->Text = L"=";
this->button18->UseVisualStyleBackColor = true;
this->button18->Click += gcnew System::EventHandler(this, &Form1::button18_Click);
//
// richTextBox1
//
this->richTextBox1->BackColor = System::Drawing::SystemColors::Control;
this->richTextBox1->Location = System::Drawing::Point(148, 397);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->ReadOnly = true;
this->richTextBox1->Size = System::Drawing::Size(94, 33);
this->richTextBox1->TabIndex = 19;
this->richTextBox1->Text = L"By Kamil";
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->plikToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(386, 26);
this->menuStrip1->TabIndex = 20;
this->menuStrip1->Text = L"menuStrip1";
//
// plikToolStripMenuItem
//
this->plikToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->zamknijToolStripMenuItem});
this->plikToolStripMenuItem->Font = (gcnew System::Drawing::Font(L"Courier New", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
this->plikToolStripMenuItem->Name = L"plikToolStripMenuItem";
this->plikToolStripMenuItem->Size = System::Drawing::Size(60, 22);
this->plikToolStripMenuItem->Text = L"Plik";
//
// zamknijToolStripMenuItem
//
this->zamknijToolStripMenuItem->Name = L"zamknijToolStripMenuItem";
this->zamknijToolStripMenuItem->Size = System::Drawing::Size(152, 22);
this->zamknijToolStripMenuItem->Text = L"Zamknij";
this->zamknijToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::zamknijToolStripMenuItem_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(386, 431);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->button18);
this->Controls->Add(this->button17);
this->Controls->Add(this->button16);
this->Controls->Add(this->button15);
this->Controls->Add(this->button14);
this->Controls->Add(this->button13);
this->Controls->Add(this->button12);
this->Controls->Add(this->button11);
this->Controls->Add(this->button10);
this->Controls->Add(this->button9);
this->Controls->Add(this->button8);
this->Controls->Add(this->button7);
this->Controls->Add(this->button6);
this->Controls->Add(this->button5);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->lblDisplay);
this->Controls->Add(this->menuStrip1);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->MainMenuStrip = this->menuStrip1;
this->Name = L"Form1";
this->Text = L"Kalkulator";
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
int firstnum;
int secondnum;
int Result;
char operation;
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "7";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "7";
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "8";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "8";
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "9";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "9";
}
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "4";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "4";
}
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "5";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "5";
}
}
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "6";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "6";
}
}
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "1";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "1";
}
}
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "2";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "2";
}
}
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "3";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "3";
}
}
private: System::Void button10_Click(System::Object^ sender, System::EventArgs^ e) {
if(lblDisplay->Text == "0"){
lblDisplay->Text = "0";
}
else{
lblDisplay->Text = Convert::ToInt32(lblDisplay->Text) + "0";
}
}
private: System::Void button11_Click(System::Object^ sender, System::EventArgs^ e) {
lblDisplay->Text = "";
lblDisplay->Text ="0";
}
private: System::Void button12_Click(System::Object^ sender, System::EventArgs^ e) {
firstnum = Convert::ToInt32(lblDisplay->Text);
lblDisplay->Text="0";
operation = '+';
}
private: System::Void button13_Click(System::Object^ sender, System::EventArgs^ e) {
firstnum = Convert::ToInt32(lblDisplay->Text);
lblDisplay->Text="0";
operation = '-';
}
private: System::Void button14_Click(System::Object^ sender, System::EventArgs^ e) {
firstnum = Convert::ToInt32(lblDisplay->Text);
lblDisplay->Text="0";
operation = 'x';
}
private: System::Void button15_Click(System::Object^ sender, System::EventArgs^ e) {
firstnum = Convert::ToInt32(lblDisplay->Text);
lblDisplay->Text="0";
operation = '/';
}
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
secondnum = Convert::ToInt32(lblDisplay->Text);
switch (operation)
{
case '+':
Result= firstnum + secondnum;
lblDisplay->Text = System::Convert::ToString(Result);
break;
case 'x':
Result= firstnum * secondnum;
lblDisplay->Text = System::Convert::ToString(Result);
break;
case '-':
Result= firstnum - secondnum;
lblDisplay->Text = System::Convert::ToString(Result);
break;
case '/':
Result= firstnum / secondnum;
lblDisplay->Text = System::Convert::ToString(Result);
break;
case '^':
break;
}
}
private: System::Void button16_Click(System::Object^ sender, System::EventArgs^ e) {
float Result = 0;
Result = Single::Parse(lblDisplay->Text);
lblDisplay->Text = (Math::Sqrt(Result).ToString());
}
private: System::Void button17_Click(System::Object^ sender, System::EventArgs^ e) {
float Result = 0;
Result = Single::Parse(lblDisplay->Text);
lblDisplay->Text = (Math::Pow(Result, (1.0 * 2.0)).ToString());
}
private: System::Void zamknijToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
};
}