C++:递归求阶乘

1661阅读 0评论2011-08-25 yinyuemi
分类:C/C++

本来想添加一个输入是否为数字的判断,没成功,以后再改进!

  1. #include <iostream>

  2. using namespace std;

  3. double f1( double a, double b=1){
  4.     b*=a;
  5.     if(a==1) return b;
  6.     else f1(a-1,b);
  7. }

  8. void f2(int& c){
  9.     double a,b;
  10.     cout << "Enter the number: ";
  11.     cin >> a;
  12.     b=f1(a);
  13.     cout << a << "!= "<< b <<endl;
  14.     cout <<'\n';
  15. }

  16. int main (){
  17.     int a(1);
  18.     while(a==1){
  19.         f2(p);
  20.         cout<<"Do you want to do another calculation? y/n \n";
  21.         char str;
  22.         cin>>str;
  23.         if(str=='Y'||str=='y')continue;
  24.         if(str=='N'||str=='n')a=0;
  25.     }
  26.     return 0;

  27. }
上一篇:sed 例子
下一篇:dd详解