Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)
Jan 02, 2016
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 01000001 is displayed• Its decimal representation 65 is displayed
Programming with Visual C++: Concepts and Projects 2
Problem Analysis
Programming with Visual C++: Concepts and Projects 3
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 Projects 4
Problem Analysis (continued)
Programming with Visual C++: Concepts and Projects 5
Problem Analysis (continued)
Programming with Visual C++: Concepts and Projects 6
Problem Analysis (continued)
Programming with Visual C++: Concepts and Projects 7
Problem Analysis (continued)
Programming with Visual C++: Concepts and Projects 8
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 Projects 9
Design (continued)
Programming with Visual C++: Concepts and Projects 10
Design (continued)
Programming with Visual C++: Concepts and Projects 11
Design (continued)
Programming with Visual C++: Concepts and Projects 12
• 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 Projects 13
• 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 Projects 14
• Low-level algorithm for Step 3
Design (continued)
Programming with Visual C++: Concepts and Projects 15
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 Projects 16
Development (continued)
Programming with Visual C++: Concepts and Projects 17
• 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 Projects 18
Development (continued)
Programming with Visual C++: Concepts and Projects 19
Development (continued)
Programming with Visual C++: Concepts and Projects 20
• 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 Projects 21
Development (continued)
Programming with Visual C++: Concepts and Projects 22
Development (continued)
Programming with Visual C++: Concepts and Projects 23
Development (continued)
Programming with Visual C++: Concepts and Projects 24
Development (continued)
Programming with Visual C++: Concepts and Projects 25
• 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 Projects 26
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 Projects 27
Development (continued)
Programming with Visual C++: Concepts and Projects 28
Development (continued)
Programming with Visual C++: Concepts and Projects 29
Development (continued)
• Code btnConvert_Click()– Assign first character in txtChar to variable
Programming with Visual C++: Concepts and Projects 30
Development (continued)
• Code btnConvert_Click()– Assign character to an integer variable
– Display the base-10 integer
Programming with Visual C++: Concepts and Projects 31
Development (continued)
• Code btnConvert_Click()– Display groups of 128
– Calculate remainder
Programming with Visual C++: Concepts and Projects 32
Development (continued)
• Code btnConvert_Click()– Display other place values
Programming with Visual C++: Concepts and Projects 33
Development (continued)
Programming with Visual C++: Concepts and Projects 34
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 Projects 35
Testing (continued)
Programming with Visual C++: Concepts and Projects 36
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 Projects 37