此文章是vip文章,如何查看?  

1,点击链接获取密钥 http://nicethemes.cn/product/view29882.html

2,在下方输入文章查看密钥即可立即查看当前vip文章


程序设计基石与实践之标记控制和计数器控制的循环

  • 时间:
  • 浏览:
  • 来源:互联网

为了说明程序员是如何开发算法,本博文以求全班平均成绩问题的两种具体形式为例,

算法详述:计数器控制的循环

本节考虑的问题陈述是:一个有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

关于Program Language更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.


本文链接http://element-ui.cn/news/show-577042.aspx