练习6.9:编写你自己的fact.cc和factMain.cc,这两个文件都应该包含上一小节的练习中编写的chapter6.h的头文件。通过这些文件,理解你的编译器是如何支持分离式编译的。
答:这些文件是在UNIX-LIKE这种系统中所特有的,为了能够说明意图,我定义了一个头文件fact1.h用于函数定义,chapter6.h用于函数声明,main函数写在别的地方。具体见云盘程序
fact1.h
/*
*练习6.8
*日期:2015/6/9
*问题描述:练习6.9:编写你自己的fact.cc和factMain.cc,这两个文件都应该包含上一小节的练习中编写的chapter6.h的头文件。通过这些文件,理解你的编译器是如何支持分离式编译的。
*功能; fact1()求阶乘
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
#ifndef FACT_1_H
#define FACT_1_H
#include "chapter6.h"
#include <stdexcept>
void fact1()
{
cout << "input a number(stop with 100): " << endl;
int val;
while (cin >> val && val != 100)
{
if(val > 0)
{
int Result = 1;
for (int j = val; j > 0; --j)
Result *= j;
cout << "The result is: " << Result << endl;
}
if (val == 0)
cout << "The result is: 1" << endl;
if (val < 0)
{
try{
throw runtime_error("Warning : val can not be less than zero !!!");
}catch(runtime_error err)
{
cout << err.what() << endl;
}
}
}
}
#endif
练习6.9 main函数
/*
*练习6.9
*日期:2015/6/9
*问题描述:练习6.9:编写你自己的fact.cc和factMain.cc,这两个文件都应该包含上一小节的练习中编写的chapter6.h的头文件。通过这些文件,理解你的编译器是如何支持分离式编译的。
*功能;调用fact1()函数,说明题目的意图
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
#include "chapter6.h"
#include "fact1.h"
int main()
{
fact1();
return 0;
}