0% found this document useful (0 votes)
36 views2 pages

Visual C++ Calculator Exercise

The document describes code for a visual C++ calculator application. It includes code for a button click event that calculates results based on operator and operand values from text boxes, displays the result, and clears values. It also includes code for button click events that set the operator value and clear the result.

Uploaded by

LeakhanakNguon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Visual C++ Calculator Exercise

The document describes code for a visual C++ calculator application. It includes code for a button click event that calculates results based on operator and operand values from text boxes, displays the result, and clears values. It also includes code for button click events that set the operator value and clear the result.

Uploaded by

LeakhanakNguon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Visual C++ Calculator Exercise

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


double a, b, r;
char sw;
sw = System::Convert::ToChar(lblOperator->Text);
a = System::Convert::ToDouble(textBox1->Text);
b = System::Convert::ToDouble(textBox2->Text);
switch (sw)
{
case '+':
r = a + b;
break;
.
.
.
.
break;
}
lblResult->Text = System::Convert::ToString(r);
}

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


lblOperator->Text = "+";
}

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


lblResult->Text = "";

}
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
char grade = 'D';

switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}

#include <iostream>
using namespace std;

int main()
{
char o;
float num1, num2;

cout << "Enter an operator (+, -, *, /): ";


cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;

switch (o)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! operator is not correct";
break;
}

return 0;
}

You might also like