[关闭]
@chawuciren 2018-12-03T15:10:37.000000Z 字数 648 阅读 624

多文件编译

C++


这个放在头文件
head.h

  1. int Factorial(int);
  2. int fun2();
  3. int fun3(int n);

以下放在源文件
file1.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include"head.h"
  4. int Factorial(int n){
  5. if (n == 1)
  6. return 1;
  7. else return n * Factorial(n - 1);
  8. }

file2.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include"head.h"
  4. int fun2(){
  5. int res = 0;
  6. res = (Factorial(5) + Factorial(7)) / 8;
  7. return res;
  8. }

file3.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include"head.h"
  4. int fun3(int n){
  5. int res=0;
  6. for (int i = 1; i <= n; i++){
  7. res += Factorial(i);
  8. }
  9. return res;
  10. }

mainf.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include"head.h"
  4. int main(){
  5. int n1 = fun2();
  6. cout <<"5!+7!的和除8的结果是:"<< n1 << endl;
  7. int n2 = fun3(10);
  8. cout <<"1-10的阶乘的和是:"<< n2 << endl;
  9. system("pause");
  10. return 0;
  11. }

然后运行就好了

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注