1. 程式人生 > >程式設計基石與實踐之標記控制和計數器控制的迴圈

程式設計基石與實踐之標記控制和計數器控制的迴圈

為了說明程式設計師是如何開發演算法,本博文以求全班平均成績問題的兩種具體形式為例,

演算法詳述:計數器控制的迴圈

本節考慮的問題陳述是:一個有10名學生的班級進行了一次測驗,測驗成績(1~100中的一個整數)現在都已經出來了.請計算並列印本班學生的總成績及班級的平均成績.我們知道,班級的平均成績等於總成績除以學生的人數.

計數器控制的迴圈的虛擬碼演算法

下面用程式碼列出要執行的動作,並指定這些動作執行的順序.使用計數器控制的迴圈以一次一個的方式輸入每個學生的成績.這種方法用一個稱為計數器的變數控制一組語句執行次數.在此,給出了一個已完全開發好的虛擬碼演算法以及GradeBook類的一個版本,其中,GradeBook類在一個C++成員函式中實現了演算法.

採用計數器控制的迴圈來解決求全班平均成績問題的虛擬碼演算法


採用計數器控制的迴圈來解決求全班平均成績問題:GradeBook的標頭檔案
// 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 std::string;

// 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
   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 
採用計數器控制的迴圈來解決求全班平均成績問題:GradeBook的原始碼檔案
// GradeBook.cpp
// Member-function definitions for class GradeBook that solves the 
// class average program with counter-controlled repetition.
#include <iostream>
using namespace std;

#include "GradeBook.h" // include definition of class GradeBook

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
   setCourseName( name ); // validate and store courseName
} // end GradeBook constructor

// function 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
string GradeBook::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 entered 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 input
      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

增強GradeBook有效性確認

考慮一下對GradeBook類的增強工作.成員函式setCourseName將驗證課程名稱的合法性.它首先用一條if語句測試課程名稱的的長度是否小於或者等於25個字元.如果是這樣,接受此課程名稱.

// 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
   return 0; // indicate successful termination
} // end main

演算法詳述:標記控制的迴圈

虛擬碼演算法可以解決更一般化的求全班平均成績的問題.如下所示.

採用標記控制的迴圈求全班平均成績問題的虛擬碼演算法


在GradeBook類中實現標記控制的迴圈

採用標記控制的迴圈求全班平均成績問題:GradeBook標頭檔案
//  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 std::string;

// 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
   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 
採用標記控制的迴圈求全班平均成績問題:GradeBook原始碼檔案
// GradeBook.cpp Member-function definitions for class GradeBook that solves the 
// class average program with sentinel-controlled repetition.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed; // ensures that decimal point is displayed

#include <iomanip> // parameterized stream manipulators  
using std::setprecision; // sets numeric output precision

// include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
   setCourseName( name ); // validate and store courseName
} // end GradeBook constructor

// function 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
string GradeBook::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 grades entered
   int grade; // grade value
   double average; // number with decimal point for average

   // initialization phase
   total = 0; // initialize total
   gradeCounter = 0; // initialize loop counter

   // processing phase
   // prompt for input and read grade from user  
   cout << "Enter grade or -1 to quit: ";        
   cin >> grade; // input grade or sentinel value

   // loop until sentinel value read from user   
   while ( grade != -1 ) // while grade is not -1
   {                  
      total = total + grade; // add grade to total
      gradeCounter = gradeCounter + 1; // increment counter
      
      // prompt for input and read next grade from user
      cout << "Enter grade or -1 to quit: ";           
      cin >> grade; // input grade or sentinel value   
   } // end while

   // termination phase
   if ( gradeCounter != 0 ) // if user entered at least one grade...
   {
      // calculate average of all grades entered              
      average = static_cast< double >( total ) / gradeCounter;
      
      // display total and average (with two digits of precision)
      cout << "\nTotal of all " << gradeCounter << " grades entered is " 
         << total << endl;
      cout << "Class average is " << setprecision( 2 ) << fixed << average 
         << endl;
   } // end if
   else // no grades were entered, so output appropriate message
      cout << "No grades were entered" << endl;
} // end function determineClassAverage
採用標記控制的迴圈求全班平均成績問題:建立GradeBook類的一個物件
// Create GradeBook object and invoke its determineClassAverage function.

// include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"

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
   return 0; // indicate successful termination
} // end main