Friday, June 4, 2010

How to create a Calculator in VC++ 2008 [Windows Forms Application]

Here is the code

//First declare these variables
char c; double x,y,d;

//Button '+'
this->label1->Text = "+"; c='+';

//Button '-'
this->label1->Text = "-"; c='-';

//Button 'x'
this->label1->Text = "x"; c='x';

//Button '/'
this->label1->Text = "/"; c='/';

//Button '='
x = System::Convert::ToInt32(this->textBox1->Text);
y = System::Convert::ToInt32(this->textBox2->Text);
switch(c)
{
case '+': d = x + y; break;
case '-': d = x - y; break;
case 'x': d = x * y; break;
case '/': d = x / y; break;
}
this->label2->Text = System::Convert::ToString(d);

//Button 'Clear'
this->textBox1->Text = ""; this->textBox2->Text = "";
this->label1->Text = ""; this->label2->Text = "";

1 comment: