Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)
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 is displayed Its decimal representation 65 is displayed Programming with Visual C++: Concepts and Projects2
Problem Analysis Programming with Visual C++: Concepts and Projects3
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
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects5
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects6
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects7
Problem Analysis (continued) Programming with Visual C++: Concepts and Projects8
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
Design (continued) Programming with Visual C++: Concepts and Projects10
Design (continued) Programming with Visual C++: Concepts and Projects11
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)
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
Design (continued) Programming with Visual C++: Concepts and Projects14 Low-level algorithm for Step 3
Design (continued) Programming with Visual C++: Concepts and Projects15
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
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
Development (continued) Programming with Visual C++: Concepts and Projects18
Development (continued) Programming with Visual C++: Concepts and Projects19
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
Development (continued) Programming with Visual C++: Concepts and Projects21
Development (continued) Programming with Visual C++: Concepts and Projects22
Development (continued) Programming with Visual C++: Concepts and Projects23
Development (continued) Programming with Visual C++: Concepts and Projects24
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
Development (continued) Programming with Visual C++: Concepts and Projects26
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
Development (continued) Programming with Visual C++: Concepts and Projects28
Development (continued) Programming with Visual C++: Concepts and Projects29
Development (continued) Code btnConvert_Click() – Assign first character in txtChar to variable Programming with Visual C++: Concepts and Projects30
Development (continued) Code btnConvert_Click() – Assign character to an integer variable – Display the base-10 integer Programming with Visual C++: Concepts and Projects31
Development (continued) Code btnConvert_Click() – Display groups of 128 – Calculate remainder Programming with Visual C++: Concepts and Projects32
Development (continued) Code btnConvert_Click() – Display other place values Programming with Visual C++: Concepts and Projects33
Development (continued) Programming with Visual C++: Concepts and Projects34
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
Testing (continued) Programming with Visual C++: Concepts and Projects36
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