C++大学基础教程_4_9标记控制的循环_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > C++大学基础教程_4_9标记控制的循环

C++大学基础教程_4_9标记控制的循环

 2014/5/9 0:11:47  _jia_shun  程序员俱乐部  我要评论(0)
  • 摘要://GradeBook.h#include<string>usingstd::string;classGradeBook{public:GradeBook(string);//重载构造函数voidsetCourseName(string);//设置课程名称stringgetCourseName();//返回课程名称voiddisPlayMessage();//欢迎信息voiddetermineClassAverage();//统计全班学生某一门课程的平均分以及总分private
  • 标签:c++ 大学 教程 循环
class="GradeBook.h">//GradeBook.h
#include<string>
using  std::string;

class GradeBook
{
public:
	GradeBook(string );//重载构造函数
	void setCourseName(string );//设置课程名称
	string getCourseName();//返回课程名称
	void disPlayMessage();//欢迎信息
	void determineClassAverage();//统计全班学生某一门课程的平均分以及总分
private:
	string courseName;//course name for this GradeBook class
};

?

?

//GradeBook.cpp
#include<iomanip>
#include <iostream>
using namespace std;
#include "GradeBook.h"  //两个include头文件的顺序还对报错有影响额,好神奇!!!!!



GradeBook::GradeBook(string name)
{
	setCourseName(name);
}

//进行判断,如果输入coursename字符大于25,则取前25个字符
void GradeBook::setCourseName(string name)
{
	if(name.length()<=25)
		courseName = name;
	else 
	{
		courseName = name.substr(0,25);//select first 25 characters
		cout << "Name\"" << name << "\"exceeds maximum length(25).\n"
			   << "limiting courseName to first 25characters.\n" << endl;
	}
}

string  GradeBook::getCourseName()
{
	return courseName;
}

void GradeBook::disPlayMessage()
{
	cout << "welcome to the grade book for\n" << getCourseName() << "!\n"<< endl;
	
}

void GradeBook::determineClassAverage()
{
	int total = 0;//学生总成绩
	int grade;//输入的学生成绩
	int gradeCounter = 0 ;//输入的学生成绩个数
	double average ;//学生的平均成绩

	cout << "Enter the first grade or -1 to quit :" << endl;
	cin >> grade ;
	
	while(grade!=-1)
	{
		total = total +grade ;
		gradeCounter = gradeCounter +1 ;//计数器加一
		cout << "Enter the " << gradeCounter +1<< "th grade or -1 to quit :" << endl;
	    cin >> grade ;
	}
	if(gradeCounter != 0)
	{
		average = static_cast<double>(total)/gradeCounter ;
		cout << "\n Total of all " << gradeCounter << " grades entered is :" << total << endl;
		cout << "Class average is :" << setprecision(4) << fixed << average << endl;
	}
	else
		cout << "No grades were entered !" << endl;
	
}

?

?

?

?

// main.cpp
#include "GradeBook.h"

int main()
{
	GradeBook myGradeBook("C++大学基础教程");
	myGradeBook.disPlayMessage();
	myGradeBook.determineClassAverage();
	system("pause >>  cout ");
	return 0;
}

?

发表评论
用户名: 匿名