Download presentation
Presentation is loading. Please wait.
1
Speaker: Liu Yu-Jiun Date: 2009/04/08
Recitation Course 0408 Speaker: Liu Yu-Jiun Date: 2009/04/08
2
Outline Ch 6 重點整理 HW2 pseudo code HW2 評分新制
3
Functions Called methods or procedures in other languages
Allow programmers to modularize a program by separating its tasks into self-contained units Statements in function bodies are written only once Reused from perhaps several locations in a program Hidden from other functions Avoid repeating code Enable the divide-and-conquer approach Reusable in other programs User-defined or programmer-defined functions
4
Function (Cont.) A function is invoked by a function call
Called function either returns a result or simply returns to the caller Function calls form hierarchical relationships
5
<cmath> Global functions,不屬於任何的類別,只要把標頭檔include進來就可以在程式任何地方使用這些函式
6
Return control Three ways to return control to the calling statement:
If the function does not return a result: Program flow reaches the function-ending right brace (}) or Program executes the statement return; If the function does return a result: Program executes the statement return expression; expression is evaluated and its value is returned to the caller
7
Function Prototypes and Argument Coercion
7 Function Prototypes and Argument Coercion Function prototype Also called a function declaration Indicates to the compiler: Name of the function Type of data returned by the function Parameters the function expects to receive Number of parameters Types of those parameters Order of those parameters 7
8
Outline (1 of 4) #include and using for function time
8 Outline fig06_11.cpp (1 of 4) #include and using for function time Enumeration to keep track of the game status Declaring a variable of the user-defined enumeration type Seeding the random number generator with the current time 8
9
Outline (2 of 4) Assigning an enumeration constant to gameStatus
9 Outline fig06_11.cpp (2 of 4) Assigning an enumeration constant to gameStatus Comparing a variable of an enumeration type to an enumeration constant 9
10
Function that performs the task of rolling the dice
10 Outline fig06_11.cpp (3 of 4) Function that performs the task of rolling the dice 10
11
11 Outline fig06_11.cpp (4 of 4) 11
12
HW2 pseudo code
13
#include <iostream> using namespace std; class RationalNumber{
// rational.h #include <iostream> using namespace std; class RationalNumber{ friend ostream& operator<< (ostream &, const RationaNumber&); friend istream& operator>> (istream &, RationalNumber&); public: RationalNumber(); RationalNumber(int = 0, int = 1); RationalNumber operator+(const RationalNumber&); RationalNumber operator-(const RationalNumber&); RationalNumber operator*(const RationalNumber&); RationalNumber operator%(const RationalNumber&); private: int numerator; int denominator; bool sign; void reduction(void); int gcd(int, int); }; 可以存取類別中的成員與成員函式 1.是哪個類別的朋友,就宣告在哪個類別中 2.不是成員函式,所以寫函式內容時,不用加”類別名稱::” 3.呼叫friend函式時,不用”object.” const : constant 修飾後面的東西,確保後面的東西不會被更改。
14
課本習題6.45 // rational.cpp // 運算部分
RationalNumber RationalNumber::operator+(const RationalNumber& in){ n1 = (sign)?(this->numerator):(0 - this->numerator); n2 = (in.sign)?(in.numerator):(0 - in.numerator); tmp_d = this->denominator * in.denominator; tmp_n = n1 * in.denominator + n2 * this->denominator; RationalNumber rev(tmp_n, tmp_d); rev.reduction(); //把結果約個分 return rev; } RationalNumber RationalNumber::operator-(const RationalNumber& in){ .... RationalNumber RationalNumber::operator*(const RationalNumber& in){ RationalNumber RationalNumber::operator%(const RationalNumber& in){ // 約分部份,把最後算出來的結果約個分 void reduction(){ ..... int gcd(int x, int y){ return ((y != 0) ? gcd(y, x%y) : x); ?: (c++內唯一的三元運算子) (condition) ? (value if true) : (value if false) 誰呼叫它,誰就是”this”。 “this”是一個指標,指向物件。 課本習題6.45
15
這兩個運算子多載函式是friend,所以前面不用加上”Rational::”
// rational.cpp // 輸入、輸出部分 istream& operator>>(istream& in, RationalNumber& val){ int n, d; char ch; if (!(in >> n)) return in; ch = in.get(); if (ch != '/'){ do something.... } else{ in >> d; val.numerator = abs(n); val.denominator = abs(d); ostream& operator<<(ostream& out, const RationaNumber& val){ if (sign == 0){ out << "-"; if (val.denominator == 1){ out << val.numerator; else { out << val.numerator << "/" << val.denominator; return out; 這兩個運算子多載函式是friend,所以前面不用加上”Rational::” >>的部份比較需要處理,應該還需要記錄sign值
16
#include <iostream>
#include "rational.h" using namespace std; int main(){ RationalNumber r1, r2, ans; char opr; while(cin >> r1){ cin >> opr; cin >> r2; // 可以在這邊就檢查是否有除以零的情況... if (opr == '+'){ ans = r1 + r2; } else if (opr == '-'){ ans = r1 - r2; cout << ans; 一堆的if...else if...else if...else...,可以換成switch。 switch(opr){ case ‘+’: ans = r1 + r2; break; case ‘-’: ans = r1 – r2; default: // 這裡就是非四則運算符號的情況處理 }
17
HW2 評分新制 Upload to system Demo Final score = max (upload, demo)
Deadline: 4/15 11:59 p.m. Demo Time: 4/15 7:30~10:00 p.m. Explain your code to get 2 points. Answer 1 question to get 1 point, total 3 questions. Pass 1 example to get 1 point, total 3 examples. The highest score of demo is 8 points. Final score = max (upload, demo)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.