Download presentation
Presentation is loading. Please wait.
Published byJordan Mathews Modified over 9 years ago
1
Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)
2
Tutorial: Binary Conversion Program Problem Analysis – Construct a program that allows the user to enter a character and see its decimal (base-10) and binary (base-2) representations – For example, if the user entered the letter ‘A’ Its binary representation 01000001 is displayed Its decimal representation 65 is displayed Programming with Visual C++: Concepts and Projects2
3
Problem Analysis Programming with Visual C++: Concepts and Projects3
4
Problem Analysis (continued) The ASCII code of the letter is a base-10 integer – no explicit conversion required! To calculate the binary representation: – Start with largest binary place value (128) – Determine how many whole times the place value goes into the integer in question (using integer division) – Display result – Capture remainder (using mod) Programming with Visual C++: Concepts and Projects4
5
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects5
6
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects6
7
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects7
8
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects8
9
Design Interface sketch requires numerous textboxes – Data entry ( txtChar ) – Data output Eight textboxes for the binary digits One textbox ( txtDec ) for the decimal version Control table Data table Algorithm for the button ( btnConvert ) Programming with Visual C++: Concepts and Projects9
10
Design (continued) Programming with Visual C++: Concepts and Projects10
11
Design (continued) Programming with Visual C++: Concepts and Projects11
12
Design (continued) Programming with Visual C++: Concepts and Projects12 Variables required – The character entered by the user in txtChar – The integer corresponding to that character (used to produce the binary representation)
13
Design (continued) Programming with Visual C++: Concepts and Projects13 The starting point for this solution is a high- level algorithm Step 3 needs much more development
14
Design (continued) Programming with Visual C++: Concepts and Projects14 Low-level algorithm for Step 3
15
Design (continued) Programming with Visual C++: Concepts and Projects15
16
Development Create the interface – Create, resize and position multiple textboxes – Set MaxLength property to 1 for txtChar – Set horizontal spacing – Add labels – Change ForeColor and BackColor – Assign names to match interface design – Add a textbox for instructions ( txtInstruct ) Programming with Visual C++: Concepts and Projects16
17
Development (continued) Programming with Visual C++: Concepts and Projects17 Create interface with textBox1 and label1 Set textBox1 properties – Change the size to 42, 62 – Change the Font to Microsoft Sans Serif 16Change the MaxLength property of textBox1 to 1 so that the user can only enter 1 character into it
18
Development (continued) Programming with Visual C++: Concepts and Projects18
19
Development (continued) Programming with Visual C++: Concepts and Projects19
20
Development (continued) Programming with Visual C++: Concepts and Projects20 Create 8 more textboxes – Select them all using the Ctrl key – Change the sizes to 42, 62 – Change the Font to Microsoft Sans Serif 16 – Decrease the horizontal spacing – Change BackColor color to Black and ForeColor to LimeGreen
21
Development (continued) Programming with Visual C++: Concepts and Projects21
22
Development (continued) Programming with Visual C++: Concepts and Projects22
23
Development (continued) Programming with Visual C++: Concepts and Projects23
24
Development (continued) Programming with Visual C++: Concepts and Projects24
25
Development (continued) Programming with Visual C++: Concepts and Projects25 Rename all textBoxes – txtChar – txt128, txt64, txt32, txt16, txt8, txt4, txt2, txt1 Add additional controls and set properties
26
Development (continued) Programming with Visual C++: Concepts and Projects26
27
Development (continued) Code Form1_Load() event handler – Put 0’s in all binary digit textBoxes – Put “ “ in txtChar – Put instructions in txtInstruct Programming with Visual C++: Concepts and Projects27
28
Development (continued) Programming with Visual C++: Concepts and Projects28
29
Development (continued) Programming with Visual C++: Concepts and Projects29
30
Development (continued) Code btnConvert_Click() – Assign first character in txtChar to variable Programming with Visual C++: Concepts and Projects30
31
Development (continued) Code btnConvert_Click() – Assign character to an integer variable – Display the base-10 integer Programming with Visual C++: Concepts and Projects31
32
Development (continued) Code btnConvert_Click() – Display groups of 128 – Calculate remainder Programming with Visual C++: Concepts and Projects32
33
Development (continued) Code btnConvert_Click() – Display other place values Programming with Visual C++: Concepts and Projects33
34
Development (continued) Programming with Visual C++: Concepts and Projects34
35
Testing Demonstrate that your program works with: – A-Z characters – a-z characters – 0-9 numerals – Punctuation marks and special characters Use the ASCII table in Appendix B to verify correctness Programming with Visual C++: Concepts and Projects35
36
Testing (continued) Programming with Visual C++: Concepts and Projects36
37
On Your Own Demonstrate your understanding – Create a diagram like Figure 3-28 to show how your program works with various decimal numbers – Replace ToString() with Convert::ToString() – Use shorthand operators /= %= Programming with Visual C++: Concepts and Projects37
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.