Top Banner
Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)
37

Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Jan 02, 2016

Download

Documents

Jordan Mathews
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Programming with Visual C++: Concepts and Projects

Chapter 3B: Integral Data (Tutorial)

Page 2: 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

Page 3: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Problem Analysis

Programming with Visual C++: Concepts and Projects 3

Page 4: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 5: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 5

Page 6: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 6

Page 7: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 7

Page 8: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 8

Page 9: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 10: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Design (continued)

Programming with Visual C++: Concepts and Projects 10

Page 11: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Design (continued)

Programming with Visual C++: Concepts and Projects 11

Page 12: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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)

Page 13: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 14: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Design (continued)

Programming with Visual C++: Concepts and Projects 14

• Low-level algorithm for Step 3

Page 15: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Design (continued)

Programming with Visual C++: Concepts and Projects 15

Page 16: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 17: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 18: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 18

Page 19: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 19

Page 20: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 21: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 21

Page 22: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 22

Page 23: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 23

Page 24: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 24

Page 25: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 26: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 26

Page 27: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 28: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 28

Page 29: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 29

Page 30: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

• Code btnConvert_Click()– Assign first character in txtChar to variable

Programming with Visual C++: Concepts and Projects 30

Page 31: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

• Code btnConvert_Click()– Assign character to an integer variable

– Display the base-10 integer

Programming with Visual C++: Concepts and Projects 31

Page 32: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

• Code btnConvert_Click()– Display groups of 128

– Calculate remainder

Programming with Visual C++: Concepts and Projects 32

Page 33: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

• Code btnConvert_Click()– Display other place values

Programming with Visual C++: Concepts and Projects 33

Page 34: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Development (continued)

Programming with Visual C++: Concepts and Projects 34

Page 35: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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

Page 36: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

Testing (continued)

Programming with Visual C++: Concepts and Projects 36

Page 37: Programming with Visual C++: Concepts and Projects Chapter 3B: Integral Data (Tutorial)

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