bool UserSaysYes();//当用户肯定回答(yes)时,返回true,用户否定回答(no)时,返回false // 函数模板
template
void Swap(ElemType &e1, ElemType &e2); // 交换e1, e2之值 template
void Display(ElemType elem[], int n); // 显示数组elem的各数据元素值 // 实用类 class Timer; class Error; class Rand;
// 计时器类Timer // 通用异常类
// 随机数类Rand
// 从输入流in中跳过空格及制表符获取一字符
char GetChar(istream &in) {
char ch;
// 临时变量
&& ((ch = in.get()) == ' '|| ch == '\\t'));
while ((ch = in.peek()) != EOF
// 文件结束符(peek()函数从输入流中接受1字符,流的当前位置不变); 空格(get()函数从输入流中接受1字符,流的当前位置向后移1个位置);制表符 }
bool UserSaysYes() // 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false {
char ch;
// 用户回答字符
// 初始回答
return ch;
// 返回字符
bool initialResponse = true; do
{ // 循环直到用户输入恰当的回答为止
if (initialResponse) cout << \初始回答 else cout << \用y或n回答:\
// 非初始回答
while ((ch = GetChar()) == '\\n'); // 跳过空格,制表符及换行符获取一字符 initialResponse = false;
// 非初始回答
} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N'); while (GetChar() != '\\n');
// 跳过当前行后面的字符
31
}
if (ch == 'y' || ch == 'Y') return true; // 肯定回答返回true else return false;
// 否定回答返回false
template
void Swap(ElemType &e1, ElemType &e2) // 交换e1, e2之值 { }
template
void Show(ElemType elem[], int n) { }
class Timer // 计时器类Timer { private:
clock_t startTime; // 数据成员 for (int i = 0; i < n; i++) { // 显示数组elem }
cout << endl;
// 换行
cout << elem[i] << \
// 显示elem[i]
// 显示数组elem的各数据元素值
ElemType temp;
// 临时变量
// 循环赋值实现交换e1, e2
temp = e1; e1 = e2; e2 = temp;
public: // 方法声明
Timer(){ startTime = clock(); }
// 构造函数, 由当前时间作为开始时间
构造对象
double ElapsedTime() const { }
// 返回已过的时间
clock_t endTime = clock(); // 结束时间
return (double)(endTime - startTime) / (double)CLK_TCK; // 计算已过时间
void Reset(){ startTime = clock(); } // 重置开始时间
32
};
#define MAX_ERROR_MESSAGE_LEN 100 class Error // 通用异常类Error {
private: // 数据成员 char message[MAX_ERROR_MESSAGE_LEN];
public: // 方法声明 };
class Rand // 随机数类Rand {
public: // 方法声明
static void SetRandSeed(){ srand((unsigned)time(NULL)); } // 设置时间为随机数种子static int GetRand(int n){ return rand() % n; } }; #endif
33
// 异常信息
Error(char mes[] = \一般性异常!\ // 构造函数 void Show() const{ cout << message << endl; }
// 显示异常信息
// 生成0 ~ n-1之间的随机数 // 生成0 ~ n-1之间的随机数
static int GetRand(){ return rand(); }
附件二:
计算机与通信学院C++语言课程设计评分表
课程名称: C++ 语言课程设计
项 目 评 价 设计方案的合理性与创造性 设计与调试结果 设计说明书的质量 答辩陈述与回答问题情况 课程设计周表现情况 综合成绩
教师签名:
日 期:
(注:1.此页附在课程设计报告后;2.综合成绩按优、良、中、及格和不及格五级评定。)
34