//Jiecheng.h
class Jiecheng
{
public :
void Jiecheng_();//解决问题1
void caculateE();//解决问题2
void caculateEX();//解决问题3
int test(int);//递归算法求阶乘
double test_(double);//求阶乘的倒数
private:
int N ;//计算到阶乘的最大位
int I ;//阶乘结果
};
//Jiecheng.cpp
#include "Jiecheng.h"
#include<iostream>
using namespace std;
int Jiecheng::test(int number) //递归算法求阶乘 啊啊啊啊啊啊啊啊啊啊啊 啊啊
{
if(number == 1) return 1;
else
return number*test(number-1);
}
double Jiecheng::test_(double number)
{
int final = test(static_cast<int>(number));
return 1.00/(static_cast<double>(final));
}
void Jiecheng::Jiecheng_()
{
cout << "问题一、请输入一个非负整数:";
cin >> N ;
while(N<0) //至关重要额
{
cout << "阶乘中要求为非负数,请重新输入:";
cin >> N ;
}
if(N>0)
{
cout << N << "! = " << test(N) << endl;
}//end if
else
{
cout << "0! = 1" << endl;
}
}
void Jiecheng::caculateE()
{
cout << "问题二、请输入一个正整数n:" ;
cin >> N ;
while(N<=0)
{
cout << "题目要求N大于0,请重新输入N:" ;
cin >> N;
}
double L = 1.00; //L=e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + ······
for(double i=1.00;i<=static_cast<double>(N);i++)
{
L = L + test_(i);
}
cout << "e = " << L << endl;
}
void Jiecheng::caculateEX()
{
int x ;
cout << "问题三、请输入正整数N,以及e的系数x " ;
cin >> N >> x ;
while(N<=0)
{
cout << "题目要求N大于0,请重新输入N:" ;
cin >> N;
}
double L = 1.00;// exp(x)的解
double y = 1.00;//每个项上的x
int i=1;
for(int i=1;i<=N;i++)
{
y = y*static_cast<double>(x) ;
L =L + y*test_(static_cast<double>(i)); //注意啊,是i循环,不是N不变啊,!!!!
}
cout << "e(" << x << ") = " << L << endl;;
}
//caculate.cpp /main.cpp
#include"Jiecheng.h"
#include<string>
int main()
{
/*Jiecheng test1;
test1.Jiecheng_();
Jiecheng test2;
test2.caculateE();*/
Jiecheng test3;
test3.caculateEX();
system("pause >> cout ");
return 0;
}