Sunteți pe pagina 1din 6

Example Application: Algebra Page 1 of 6

Example Application: Algebra

Description
Active Vibration Control
This application demonstrates the use of the Standard/Custom Vibration Control
button control of Microsoft Windows. It does this Products. Request A Quote Online!
while calculating the factorial, the permutation, www.KineticSystems.com
and the combinatorial.
FlowChart.NET control
Diagramming and charting component
Windows Controls: for Windows Forms applications.
www.mindfusion.eu
z Label
Algebra for grades 1-6
z Button Practice variables, expressions,
z Text Box equations, exponents, much more!
www.ixl.com/math
z Tab Control

Practical Learning: Creating the Application

1. Start Microsoft Visual C# and create a Windows Application named Algebra1


2. On the main menu, click Project -> Add Class...
3. In the Templates list, make sure Class is selected.
Change the Name to Algebra and click Add
4. Change the file as follows:

using System;

namespace Algebra2
{
public class Algebra
{
public static long Factorial(long x)
{
if (x <= 1)
return 1;
else
return x * Factorial(x - 1);
}

public static long Permutation(long n, long r)


{

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009
Example Application: Algebra Page 2 of 6

if (r == 0)
return 0;
if (n == 0)
return 0;
if ((r >= 0) && (r <= n))
return Factorial(n) / Factorial(n - r);
else
return 0;
}

public static long Combinatorial(long a, long b)


{
if (a <= 1)
return 1;

return Factorial(a) / (Factorial(b) * Factorial(a - b));


}
}
}

5. In the Solution Explorer, right-click Form1.cs and click Rename


6. Type Exercise.cs and press Enter twice (to save and to open the form)
7. Click the body of the form to make sure it is selected.
In the Properties window, change the following characteristics
FormBorderStyle: FixedDialog
Text: Factorial, Permutation, and Combination
Size: 304, 208
StartPosition: CenterScreen
MaximizeBox: False
MinimizeBox: False
8. In the Containers section of the Toolbox, click TabControl and click the form
9. On the form, right-click the right side of tabPage2 and click Add Page
10. Based on what we learned in Lesson 24, design the form as follows:

Control Text Name Additional Properties


HotTrack: True
TabControl tclAlgebra Location: 12, 12
Size: 304, 235
TabPage Factorial tabFactorial
Label Number: Location: 22, 21
TextAlign: Right
TextBox txtNumber Location: 88, 18
Size: 50, 20
Label Result: Location: 22, 56
TextAlign: Right
TextBox txtFactorial Location: 88, 54
Size: 140, 20

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009
Example Application: Algebra Page 3 of 6

Control Text Name Location Size


TabPage Permutation tabPermutation
Label n: 22, 21
TextBox txtPermutationN 88, 18 50, 20
Label r: 22, 56
TextBox txtPermutationR 88, 54 50, 20
Label P(n, r): 22, 92
TextBox txtPermutation 88, 90 140, 20

Control Text Name Location Size


TabPage Combination tabCombination
Label n: 22, 21
TextBox txtCombinationN 88, 18 50, 20
Label r: 22, 56
TextBox txtCombinationR 88, 54 50, 20
Label C(n, r): 22, 92
TextBox txtCombination 88, 90 140, 20

11. In the combo box on top of the Properties window, select tabFactorial
12. From the Common Controls section of the Toolbox, click Button and click on the right side
of the top text box
13. Access each tab page and add a button to it
14. Add a button to the form and under the tab control
15. Complete the design of the form as follows:

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009
Example Application: Algebra Page 4 of 6

Control Text Name


Button Calculate btnCalcFactorial
Button Close btnClose
Simple Static
Elimination
Is static causing
problems in your
industrial process?
arcalian.com

Free .NET Chart


control
100% Managed
Charting for Visual
Studio, ASP.Net
www.teechart.net

DSkinLite GUI
Library
Control Text Name make fancy gui using
xml, easy to use, c++
Button Calculate btnCalcPermutation
library, for
mfc,wtl,win32
www.uieasy.com

ASP.NET
Ecommerce & Cart
shopping cart,
shipping, currency
credit card processing
for ASP.NET
www.dotnetecommerce.com

Fast Reporting
components
for Delphi 7, 2007,
2009 Royalty-free end
-user report design
www.fast-report.com
Control Text Name
Button Calculate btnCalcCombination

16. Access the Factorial tab page and double-click its Calculate button
17. Implement the event as follows:

private void btnCalcFactorial_Click(object sender, EventArgs e)


{
long number = 0;
long result;

try
{
number = long.Parse(txtFactNumber.Text);
result = Algebra.Factorial(number);
txtFactorial.Text = result.ToString();
}

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009
Example Application: Algebra Page 5 of 6

catch (FormatException)
{
MessageBox.Show("Invalid Number");
}
}

18. Return to the form


19. Access the Permutation tab page and double-click its Calculate button
20. Implement the event as follows:

private void btnCalcPermutation_Click(object sender, EventArgs e)


{
long n = 0, r = 0;
long result;

try
{
n = long.Parse(txtPermutationN.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Number");
}

try
{
r = long.Parse(txtPermutationR.Text);
result = Algebra.Permutation(n, r);
txtPermutation.Text = result.ToString();
}
catch (FormatException)
{
MessageBox.Show("Invalid Number");
}
}

21. Return to the form


22. Access the Combination tab page and double-click its Calculate button
23. Implement the event as follows:

private void btnCalcCombination_Click(object sender, EventArgs e)


{
long n = 0, r = 0;
long result;

try
{
n = long.Parse(txtCombinationN.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Number");
}

try
{
r = long.Parse(txtCombinationR.Text);
result = Algebra.Combinatorial(n, r);
txtCombination.Text = result.ToString();
}
catch (FormatException)
{
MessageBox.Show("Invalid Number");
}
}

24. Return to the form and double-click the Close button


25. Implement the event as follows:

private void btnClose_Click(object sender, EventArgs e)


{

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009
Example Application: Algebra Page 6 of 6

Close();
}

26. Execute the application to test the calculations

27. Close the form and return to your programming environment

Home Copyright © 2007 FunctionX, Inc.

http://www.functionx.com/vcsharp/applications/algebra.htm 12/30/2009

S-ar putea să vă placă și