Добавил:
больше работ здесь: https://github.com/alisadex Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчёт по простому калькулятору.docx
Скачиваний:
27
Добавлен:
10.01.2023
Размер:
668.72 Кб
Скачать

3. Текст программы

bool Protect() {

if ((txt1->Text->Length == 0) || (txt2->Text->Length == 0)) { return false; }

for (int i = 0; i < txt1->Text->Length; ++i) {

if ((txt1->Text[i] < '0' || txt1->Text[i] > '9') && (txt1->Text[i] != '.') &&

(txt1->Text[i] != '-')) { return false; }

else {

int counter = 0;

for (int i = 0; i < txt1->Text->Length; ++i) {

if (txt1->Text[i] == '.') {

++counter;

if (counter > 1) { return false; }

}

}

int countMinus = 0;

for (int i = 0; i < txt1->Text->Length; ++i) {

if (txt1->Text[i] == '-') {

countMinus++;

if (countMinus > 1) {

return false;

}

else {

for (int i = 0; i < txt1->Text->Length; ++i) {

if (txt1->Text[0] != '-') {

return false;

}

}

}

}

}

}

}

for (int i = 0; i < txt2->Text->Length; ++i) {

if ((txt2->Text[i] < '0' || txt2->Text[i] > '9') && (txt2->Text[i] != '.') && (txt2->Text[i] != '-')) { return false; }

else {

int counter = 0;

for (int i = 0; i < txt2->Text->Length; ++i) {

if (txt2->Text[i] == '.') {

++counter;

if (counter > 1) { return false; }

}

}

int countMinus = 0;

for (int i = 0; i < txt2->Text->Length; ++i) {

if (txt2->Text[i] == '-') {

countMinus++;

if (countMinus > 1) {

return false;

}

else {

for (int i = 0; i < txt2->Text->Length; ++i {

if (txt2->Text[0] != '-') {

return false;

}

}

}

}

}

}

}

return true;

}

private: System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e) {

this->Close();

}

private: System::Void btnReset_Click(System::Object^ sender, System::EventArgs^ e) {

this->lblOper->Text = L"";

this->txt1->Text = L"";

this->txt2->Text = L"";

this->txtResult->Text = L"";

}

private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e) {

if (Protect()) {

this->txtResult->Text = L"";

double i1, i2, i3;

this->lblOper->Text = L"Сложение";

i1 = System::Double::Parse(txt1->Text);

i2 = System::Double::Parse(txt2->Text);

i3 = i1 + i2;

this->txt1->Text = Convert::ToString(i1);

this->txt2->Text = Convert::ToString(i2);

this->txtResult->Text = Convert::ToString(i3);

}

else {

this->lblInfo->Text = L"Ошибка ввода! Введите число.";

}

}

private: System::Void btnSubtract_Click(System::Object^ sender, System::EventArgs^ e) {

if (Protect()) {

this->txtResult->Text = L"";

double i1, i2, i3;

this->lblOper->Text = L"Вычитание";

i1 = System::Double::Parse(txt1->Text);

i2 = System::Double::Parse(txt2->Text);

i3 = i1 - i2;

this->txt1->Text = Convert::ToString(i1);

this->txt2->Text = Convert::ToString(i2);

this->txtResult->Text = Convert::ToString(i3);

}

else {

this->lblInfo->Text = L"Ошибка ввода! Введите число.";

}

}

private: System::Void btnMultiply_Click(System::Object^ sender, System::EventArgs^ e) {

if (Protect()) {

this->txtResult->Text = L"";

double i1, i2, i3;

this->lblOper->Text = L"Умножение";

i1 = System::Double::Parse(txt1->Text);

i2 = System::Double::Parse(txt2->Text);

i3 = i1 * i2;

this->txt1->Text = Convert::ToString(i1);

this->txt2->Text = Convert::ToString(i2);

this->txtResult->Text = System::Convert::ToString(i3);

}

else {

this->lblInfo->Text = L"Ошибка ввода! Введите число.";

}

}

private: System::Void btnDivide_Click(System::Object^ sender, System::EventArgs^ e) {

if (Protect()) {

this->txtResult->Text = L"";

double i1, i2, i3;

this->lblOper->Text = L"Деление";

i1 = System::Double::Parse(txt1->Text);

i2 = System::Double::Parse(txt2->Text);

if (i2 != 0) {

i3 = i1 / i2;

this->txt1->Text = Convert::ToString(i1);

this->txt2->Text = Convert::ToString(i2);

this->txtResult->Text = Convert::ToString(i3);

}

else {

this->lblInfo->Text = L"На ноль делить нельзя.";

}

}

else {

this->lblInfo->Text = L"Ошибка ввода! Введите число.";

}

}