Enter the first score : -30

Download Report

Transcript Enter the first score : -30

Computer Programming in
C++
黃鐘揚 教授
Prof. Chung-Yang (Ric) Huang
Department of Electrical Engineering
National Taiwan University
2007/06/26
Course Info


8 Meetings, 3 hours each
Text Book


Grading



1-2 homeworks (TBD)
1 final exam or project
Contact




C++ How to Program, 5e, Deitel & Deitel
EE-II 444
02-3366-3644
[email protected]
TA: 葉護熺

[email protected]
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
2
Course Outline
1.
2.
3.
4.
5.
6.
7.
8.
Introduction to Computers, the Internet and
WWW
Intro to C++ Programming
Intro to Classes and Objects
Control Statements: Part I
Control Statements: Part II
Functions and an Intro to Recursion
Array and Vectors
Pointers and Pointer-Based Strings
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
3
Course Outline
9.
10.
11.
12.
13.
14.
15.
16.
Classes: A Deeper Look, Part I
Classes: A Deeper Look, Part II
Operator Overloading: String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream I/O
Exception Handling
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
4
Course Outline
17.
18.
19.
20.
21.
22.
23.
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Standard Template Library (STL)
Other Topics
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
5
Components in C++
1.
2.
3.
4.
5.
6.
Keyword
Function
Variable (object)
Data type
Name space
Comments
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
6
Components in C++
1. Keyword

Procedure control
(e.g. if, else, for, while, do, break, continue, return, using, try, throw,
catch…)

Symbols
(e.g. { }, ( ), &, |, ^, &&, ||, ~, !, *, ->, >, <, =, ==, !=, +, -, *, /,
%, ++, --, >>, <<, “”, ‘’, \, ,…)

Type declaration
(e.g. class, struct, union, public, protected, private, virtual…)



Attribute (e.g. const, static…)
Constant (e.g. 1, 23, 456, 0xff08, true, false, string “xxxx”, ….)
Predefined types
(e.g. int, char, unsigned, short int, long int, float, double…)

Preprocessor deritives (e.g. #include, #ifdef,…)
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
7
Components in C++
2. Function



main
From standard library (e.g. exit, printf…)
User defined
3. Variable (object)


From standard library (e.g. cout, cin…)
User defined
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
8
Components in C++
4. Data type


Predefined types (also treated as keywaords)
From standard library


(e.g. iostream, fstream, vector<T>…)
User defined
5. Name space


std
User defined
6. Comments


Single-line (started with // )
Multiple-line (enclosed by /* */)
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
9
Practice Prog-2.1
Write a program… just use cin and cout
> Enter the first score : 80
> Enter the second score: 90
> Enter the thrid score : 85
-----------------------------> The average is
: 85

06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
10
Practice Prog-2.2
Follow Prog-2.1
> Enter the first name : Sam
> Enter the first score : 80
> Enter the second name : Claire
> Enter the second score: 90
-----------------------------> Sam’s score is 80
> Claire’s score is 90

06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
11
Practice Prog-2.3
Continued from Prog-2.2. However, check
if the entered score is between 0 and 100.
If not issue an error message as follows:
> Enter the first score : -30

Error: “-30” is not a legal score !!
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
12
13
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
1.12
1.13
1.14
1.15
1.16
Introduction
What Is a Computer?
Computer Organization
Early Operating Systems
Personal, Distributed and Client/Server Computing
The Internet and the World Wide Web
Machine Languages, Assembly Languages and High-Level Languages
History of C and C++
C++ Standard Library
History of Java
FORTRAN, COBOL, Pascal and Ada
Basic, Visual Basic, Visual C++, C# and .NET
Key Software Trend: Object Technology
Typical C++ Development Environment
Notes About C++ and C++ How to Program, 5/e
Test-Driving a C++ Application
1.17
Software Engineering Case Study: Introduction to Object Technology and
the UML (Required)
Wrap-Up
Web Resources
1.18
1.19
 2006 Pearson Education, Inc. All rights reserved.
14
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
Introduction
First Program in C++: Printing a Line of Text
Modifying Our First C++ Program
Another C++ Program: Adding Integers
Memory Concepts
Arithmetic
Decision Making: Equality and Relational Operators
(Optional) Software Engineering Case Study:
Examining the ATM Requirements Document
Wrap-Up
 2006 Pearson Education, Inc. All rights reserved.
15
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10
3.11
Introduction
Classes, Objects, Member Functions and Data Members
Overview of the Chapter Examples
Defining a Class with a Member Function
Defining a Member Function with a Parameter
Data Members, set Functions and get Functions
Initializing Objects with Constructors
Placing a Class in a Separate File for Reusability
Separating Interface from Implementation
Validating Data with set Functions
(Optional) Software Engineering Case Study: Identifying
the Classes in the ATM Requirements Document
3.12 Wrap-Up
 2006 Pearson Education, Inc. All rights reserved.
16
4.1
Introduction
4.2
Algorithms
4.3
Pseudocode
4.4
Control Structures
4.5
if
4.6
if...else
4.7
while
4.8
Formulating Algorithms: Counter-Controlled Repetition
4.9
Formulating Algorithms: Sentinel-Controlled Repetition
4.10
Formulating Algorithms: Nested Control Statements
4.11
Assignment Operators
4.12
Increment and Decrement Operators
4.13
(Optional) Software Engineering Case Study: Identifying Class
Attributes in the ATM System
4.14
Wrap-Up
Selection Statement
Double-Selection Statement
Repetition Statement
 2006 Pearson Education, Inc. All rights reserved.
17
5.1
Introduction
5.2
Essentials of Counter-Controlled Repetition
5.3
for Repetition Statement
5.4
Examples Using the for Statement
5.5
do…while Repetition Statement
5.6
switch Multiple-Selection Statement
5.7
break and continue Statements
5.8
Logical Operators
5.9
Confusing Equality (==) and Assignment (=) Operators
5.10
Structured Programming Summary
5.11
(Optional) Software Engineering Case Study: Identifying
Objects’ States and Activities in the ATM System
5.12
Wrap-Up
 2006 Pearson Education, Inc. All rights reserved.
Practice Prog-3.1
Similar to Prog-2.1, but create a class called
“Student” and store the scores and average as its
data members. Use member functions to set and
get.
> Enter the first score : 80
> Enter the second score: 90
> Enter the third score : 85
-----------------------------> The average is
: 85

06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
18
Practice Prog-4.1

>
>
>
>
>
>
>
>
>
Guess Number:
Enter the range [1 ~ ?]: 100
Please guess a number: 50
=> Too small!!
Please guess a number: 75
=> Too large!!
Please guess a number: 63
=> You are right!!
Play again? (Y/N) N
*** Good-Bye!! Have a nice day!! ***
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
19
Tips & Issues about Prog. 4-1

How to generate random numbers?


How to generate random numbers between 1 and 100 ?





y = rand() % 100 + 1;
Is the random number the same every time you play?
How to fix it?


Calling function “rand()” (for Windows)
(Homework) Try to google this problem.
Do you check whether the input number is within the
legal range?
What if the player makes a stupid guess?
For “Play again? (Y/N)”, can we accept “default value” (i.e.
no enter)?
Do you create any “class” for this program? Why yes and
why not?
06/27/07
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
20
Admin Info 07/18/07
A class website will be created





http://cc.ee.ntu.edu.tw/~ric/teaching/ComputerProg
ramming/S07-tl
Practice programs
This file
Announcements
Final homework/project will be announced
online next week




06/27/07
You are encouraged to think about the problem as
early as possible
Tentative due date: 08/13 (Monday)
Submission method: TBA
Chung-Yang (Ric) Huang
[email protected]
+886-2-3366-3644
21