C++ How to Program 7/E 筆記
第四章 控制敘述:第一部份
學習目標
|
|
- 演算法、虛擬碼
- 控制結構、if 、if … else、while
- 規劃演算法
- 指定運算子
- 遞增和遞減運算子
任何一個可解的運算問題,均可用特定順序執行一連串的動作來解決。解決問題的程序(procedure)是由以下兩個部分組成。
- 要執行的動作(action)
- 執行這些動作的順序(order)
⇒⇒ 這種程序就稱為演算法(algorithm)
指定電腦程式中敘述式(動作)執行的順序,即程式控制(program control)。
虛擬碼
Pseudocode(虛擬碼,假碼)是人工的非正式語言,可協助開法演算法,而不必操心程式語法的嚴謹性。虛擬碼通常只會描述可執行的敘述(executable statements),通常不會寫上變數宣告,不過有些程式設計師會在虛擬碼的開頭列出變數並說明用途。
|
加法程式的虛構碼
控制結構
1960年代,人們發現任意使用控制權轉換是造成軟體開法小苦問擾的根本原因,goto 敘述成了眾矢之的,因為它可以讓控制權轉換到程市中幾乎任何一個地方,把程式搞得複雜。結構化程式設計(structured programming)幾乎成了「消除goto(goto elimination)」的同義字。
1970以後,Böhm和Jacopini證明所有程式都可以只用三種控制結構(control structure)撰寫。
- 循序結構(sequence structure)
- 選擇結構(selection structure)
- 重複結構(repetition structure)
循序結構
選擇結構
主要有三種敘述:
if(單一選擇敘述)選擇敘述中,若條件true則會執行某動作,若條件false則會略過動作。
if … else(雙重選擇敘述)則是true執行某動作,false則執行另一個動作。
switch(多重選擇敘述)遠則結構則會一某個整數運算設值,執行不同動作的其中一個。
重複結構
特色是只要條件仍為true則會不斷地執行敘述。
同樣也有三種敘述:
while
do … while
for
while、 for兩項若一開始就是false就不會執行動作。
do … while 則至少會在主體內執行一次動作。
在C++程式的演算法僅由三種敘述組成,組成方法只有兩種:
三種敘述即:循序敘述、選擇敘述、重複敘述
兩種組成:堆疊控制敘述(control-statement stacking)、巢狀控制敘述(control-statement nesting)
if 選擇敘述
如果grade >=60,程市便會在螢幕印出「passed」,然後轉換到此活動的最終狀態;如 grade < 60程式則會自動轉換到最終狀態,不顯示任何訊息。
if … else 雙重選擇敘述
特色是條件true 或 false食,則會有不同的執行動作。
條件運算子(?:)
C++提供條件運算子(?:),是C++中唯一的三元運算子(ternary operator),包含三個三個運算元,當這些運算元加上條件運算子==>==>條件運算式。
巢狀if ...else 敘述
朝逛if ...else敘述的執行速度會比一連串單一選擇if 敘述快許多,因為前者只要條件一符合就可能提早離開。
區塊
if ...else的兩個部分中包含數行敘述,我們可將這些敘述放在大括號內({ 和}),放在大括號內的敘述稱為複合敘述(compond statement)或區塊(block)。有時若在其中不放入任何敘述則稱null 敘述(null statement)或空敘述(empty statement)。
while 重複敘述
在條件為true時會持續執行,直到條件是成為false才會停下來。
規劃演算法:技術控制重複
利用counter來限定重複
*在GradeBook類別中時做計數控制重複
題目:
某個班級有十位學生參加測驗。您已知道考試的分數(範圍從0到100的整數)計算並顯示所有學生的成績總和,以及班級小考平均成績
虛擬碼
技術控制重複也常稱為限定重複(definite repetition),因為執行迴圈前,程式就已知道迴圈執行的次數。
|
*total 與 counter概念說明,total 是用來將一串數值累加起來的變數,counter 是用來計算次數的變數。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fig.4_10.cpp | |
// Create GradeBook object and invoke its determineClassAverage function. | |
#include "GradeBook.h" // include definition of class GradeBook | |
int main() | |
{ | |
// create GradeBook object myGradeBook and | |
// pass course name to constructor | |
GradeBook myGradeBook( "CS101 C++ Programming" ); | |
myGradeBook.displayMessage(); // display welcome message | |
myGradeBOok.determineClassAverage();// find average of 10 grades | |
} //end main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GradeBook.cpp | |
// Member-function definitions for class GradeBook that solves the | |
// class average program with counter controlled repetition | |
#include <iostream> | |
#include "GradeBook.h" // include definition of class GradeBook | |
using namespace std; | |
//constructor initializes courseNmae with string supplied as argument | |
GradeBook::GradeBook ( string name ) | |
{ | |
setCOurseName( name ); // validate and store courseName | |
} // end GradeBook constructor | |
// fucntion to set the course name; | |
// ensures that the course name has at most 25 characters | |
void GradeBook::setCourseName( string name ) | |
{ | |
if ( name.length() <= 25 ) // if name has 25 or fewer characters | |
courseName = name; // store the course name in the object | |
else // if name is longer than 25 characters | |
{ // set courseName to first 25 characters of parameter name | |
courseName = name.substr( 0,25 ); // select first 25 characters | |
cout << "Name\""<< name << "\"exceeds maximum length (25).\n" | |
<< "Limiting courseName to first 25 characters. \n" << endl; | |
} // end if... else | |
} // end function setCourseName | |
//function to retrieve the course name | |
sting Grade::getCourseName() | |
{ | |
return courseName; | |
} //end function getCourseName | |
// display a welcome message to the GradeBook user | |
void GradeBook::displayMessage() | |
{ | |
cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" | |
<< endl; | |
} // end function displayMessage | |
//determine class average based on 10 grades entered by user | |
void GradeBook::determineClassAverage() | |
{ | |
int total; // sum of grades entered by user | |
int gradeCounter; // number of the grade to be enter next | |
int grade; // grade value entered by user | |
int average; // average of grades | |
//initialization phase | |
total = 0; //initialize total | |
gradeCounter = 1; //initialize loop counter | |
//processing phase | |
while ( gradeCounter <= 10 ) // loop 10 times | |
{ | |
cout << "Enter grade:" ; // prompt for imput | |
cin >> grade; // input next grade | |
total = total + grade; // add grade to total | |
gradeCounter = gradeCounter + 1; // increment counter by 1 | |
} // end while | |
// termination phase | |
average = total / 10; // integer division yields integer result | |
// display total and average of grades | |
cout << "\nTotal of all 10 grades is " << total << endl; | |
cout << "Class average is " << average <<endl; | |
} // end function determineClassAverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GradeBook.h | |
// Definition of class GradeBook that determines a class average. | |
// Member functions are defined in GradeBook.cpp | |
#include <string> //program uses C++ standard string class | |
using namespace std; | |
// GradeBook class definition | |
class GradeBook | |
{ | |
public: | |
GradeBook( string ); // constructor initializes course name | |
void setCourseName( string ); // function to set the course name | |
string getCourseName(); // function to retrieve the course name | |
sting getCOurseName(); //function to retrieve the course name | |
void displayMessage(); // display a welcome message | |
void determineClassAverage(); // averages grades entered by the user | |
private: | |
string courseName; // course name for this GradeBook | |
}; // end class GradeBook | |
} |
留言
張貼留言