Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

С ++, помогите с созданием простого калькулятора с использованием Windows Forms.

Дмитрий Висков Ученик (46), закрыт 8 месяцев назад
Напишите несколько строк кода на C++ для создания простого калькулятора с использованием Windows Forms. Реализуйте базовые арифметические операции (сложение, вычитание, умножение, деление) и отображение результата на форме.
Лучший ответ
TheDarkGhostIvan Мудрец (15792) 9 месяцев назад
 #include  
#include

using namespace System;
using namespace System::Windows::Forms;

public ref class CalculatorForm : public Form
{
private:
TextBox^ txtInput;
Button^ btnAdd;
Button^ btnSubtract;
Button^ btnMultiply;
Button^ btnDivide;
Button^ btnEquals;
double total1;
double total2;

public:
CalculatorForm()
{
txtInput = gcnew TextBox();
txtInput->Location = Point(10, 10);
txtInput->Width = 200;
Controls->Add(txtInput);

btnAdd = gcnew Button();
btnAdd->Text = L"+";
btnAdd->Location = Point(10, 40);
btnAdd->Click += gcnew EventHandler(this, &CalculatorForm::btnAdd_Click);
Controls->Add(btnAdd);

// Добавьте остальные кнопки и обработчики событий аналогично

btnEquals = gcnew Button();
btnEquals->Text = L"=";
btnEquals->Location = Point(10, 160);
btnEquals->Click += gcnew EventHandler(this, &CalculatorForm::btnEquals_Click);
Controls->Add(btnEquals);
}

void btnAdd_Click(Object^ sender, EventArgs^ e)
{
total1 += Double::Parse(txtInput->Text);
txtInput->Clear();
}

// Реализуйте остальные обработчики событий для вычитания, умножения и деления

void btnEquals_Click(Object^ sender, EventArgs^ e)
{
total2 = total1;
txtInput->Text = total2.ToString();
total1 = 0;
}
};

[STAThread]
void Main(array^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew CalculatorForm());
}
Остальные ответы
МАКС ВВ ВЧВ Профи (547) 9 месяцев назад
#include <Windows.h>
#include <string>
#include <msclr/marshal_cppstd.h>

using namespace System;
using namespace System::Windows::Forms;

ref class CalculatorForm : public Form
{
private:
TextBox^ textBox;
Button^ addButton;
Button^ subtractButton;
Button^ multiplyButton;
Button^ divideButton;
Button^ equalsButton;
Button^ clearButton;

double firstOperand;
double secondOperand;
char operation;

public:
CalculatorForm()
{
this->Text = "Simple Calculator";
this->Size = Drawing::Size(300, 400);

textBox = gcnew TextBox();
textBox->Location = Drawing::Point(10, 10);
textBox->Size = Drawing::Size(270, 40);
textBox->ReadOnly = true;
this->Controls->Add(textBox);

addButton = gcnew Button();
addButton->Text = "+";
addButton->Location = Drawing::Point(10, 60);
addButton->Size = Drawing::Size(60, 60);
addButton->Click += gcnew EventHandler(this, &CalculatorForm::Operator_Click);
this->Controls->Add(addButton);

// Создайте кнопки для других операций аналогичным образом

equalsButton = gcnew Button();
equalsButton->Text = "=";
equalsButton->Location = Drawing::Point(80, 180);
equalsButton->Size = Drawing::Size(60, 60);
equalsButton->Click += gcnew EventHandler(this, &CalculatorForm::Equals_Click);
this->Controls->Add(equalsButton);

clearButton = gcnew Button();
clearButton->Text = "C";
clearButton->Location = Drawing::Point(150, 180);
clearButton->Size = Drawing::Size(60, 60);
clearButton->Click += gcnew EventHandler(this, &CalculatorForm::Clear_Click);
this->Controls->Add(clearButton);
}

private:
void Operator_Click(Object^ sender, EventArgs^ e)
{
Button^ button = dynamic_cast<Button^>(sender);
operation = button->Text[0];
firstOperand = Convert::ToDouble(textBox->Text);
textBox->Text = "";
}

// Добавьте методы для других операций и обработчиков событий

void Equals_Click(Object^ sender, EventArgs^ e)
{
secondOperand = Convert::ToDouble(textBox->Text);
double result = 0;

switch (operation)
{
case '+':
result = firstOperand + secondOperand;
break;
// Добавьте обработку других операций
}

textBox->Text = result.ToString();
}

void Clear_Click(Object^ sender, EventArgs^ e)
{
textBox->Text = "";
}
};

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

CalculatorForm^ form = gcnew CalculatorForm();
Application::Run(form);

return 0;
}

короче, обрабатывает базовые арифметические операции (сложение, вычитание, умножение, деление).
DR ZOIDBERG Профи (691) 9 месяцев назад
}

void Clear_Click(Object^ sender, EventArgs^ e)
{
textBox->Text = "";
}
};

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

CalculatorForm^ form = gcnew CalculatorForm();
Application::Run(form);

return 0;
}
Это есть сложение вычитание деление. База.
Дима Скоркин Профи (828) 9 месяцев назад
#include <windows.h>
#include <System.Windows.Forms.h>

using namespace System;
using namespace System::Windows::Forms;

public ref class CalculatorForm : public Form
{
public:
CalculatorForm()
{
this->InitializeComponent();
}

private:
TextBox^ textBox1;
TextBox^ textBox2;
TextBox^ resultBox;
Button^ addButton;
Button^ subButton;
Button^ mulButton;
Button^ divButton;

void InitializeComponent()
{
// Initialize components here
this->textBox1 = (gcnew TextBox());
this->textBox2 = (gcnew TextBox());
this->resultBox = (gcnew TextBox());

this->addButton = (gcnew Button());
this->subButton = (gcnew Button());
this->mulButton = (gcnew Button());
this->divButton = (gcnew Button());

// Set properties for the components (Location, Text, etc.)
// Example for addButton:
this->addButton->Text = "Add";
this->addButton->Location = Point(10, 100);

// Add events for the buttons
this->addButton->Click += gcnew EventHandler(this, &CalculatorForm::OnAdd);
// Similarly, add for subButton, mulButton, and divButton

// Add components to the form
this->Controls->Add(this->textBox1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->resultBox);
this->Controls->Add(this->addButton);
// Similarly, add for other buttons
}

void OnAdd(Object^ sender, EventArgs^ e)
{
double a = Convert::ToDouble(textBox1->Text);
double b = Convert::ToDouble(textBox2->Text);
double result = a + b;
resultBox->Text = result.ToString();
}

// Similarly, implement OnSubtract, OnMultiply, OnDivide
};

[STAThread]
void Main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew CalculatorForm());
}
Илларион Кутенко Ученик (5) 9 месяцев назад
Я непонимаю ВИНДОУВСНИКИ ВЫ ЭХ ???
Похожие вопросы