大学时用c++做的计算器
源代码在线查看: function.cpp
#include "function.h"
#include
void Function::setRDState(RDState s)
{
__state = s;
}
ValueType Function::Sin(const ValueType& v)
{
if (__state == Degrees)
{
if (Abs(sin(v * Pi / 180)) < Minimum)
return 0.0;
else
return sin(v * Pi / 180);
}
else
{
if (Abs(sin(v)) < Minimum)
return 0.0;
else
return sin(v);
}
}
ValueType Function::Cos(const ValueType& v)
{
if (__state == Degrees)
{
if (Abs(cos(v * Pi / 180)) < Minimum)
return 0.0;
else
return sin((90 - v) * Pi / 180);
}
else
{
if (Abs(cos(v)) < Minimum)
return 0.0;
else
return sin(Pi / 2 * v);
}
}
ValueType Function::Tan(const ValueType& v)
{
double tmp;
if (__state == Degrees) {
if (modf(v, &tmp) == 0.0 && (Abs(v - 90) < Minimum))
throw TanError();
}
else if (modf(2 * v / Pi, &tmp) == 0.0)
throw TanError();
if (__state == Degrees)
{
if (Abs(tan(v * Pi / 180)) < Minimum)
return 0.0;
else
return tan(v * Pi / 180);
}
else
{
if (Abs(tan(v)) < Minimum)
return 0.0;
else
return tan(v);
}
}
ValueType Function::Asin(const ValueType& v)
{
if (v < -1.0 || 1.0 < v) throw AsinError();
if (__state == Degrees)
return asin(v) * 180 / Pi;
else
return asin(v);
}
ValueType Function::Acos(const ValueType& v)
{
if (v < -1.0 || 1.0 < v) throw AcosError();
if (__state == Degrees)
return acos(v) * 180 / Pi;
else
return acos(v);
}
ValueType Function::Atan(const ValueType& v)
{
if (__state == Degrees)
return atan(v) * 180 / Pi;
else
return atan(v);
}
ValueType Function::Sinh(const ValueType& v)
{
return sinh(v);
}
ValueType Function::Cosh(const ValueType& v)
{
return cosh(v);
}
ValueType Function::Tanh(const ValueType& v)
{
return tanh(v);
}
ValueType Function::Asinh(const ValueType& v)
{
return log(v + sqrt(v*v + 1));
}
ValueType Function::Acosh(const ValueType& v)
{
if (v < 1.0)
throw AcoshError();
return log(v + sqrt(v*v - 1));
}
ValueType Function::Atanh(const ValueType& v)
{
if (v throw AtanhError();
return (log(1 + v) - log(1 - v)) / 2;
}
ValueType Function::Exp(const ValueType& v)
{
return exp(v);
}
ValueType Function::Ln(const ValueType& v)
{
if (v
return log(v);
}
ValueType Function::Log(const ValueType& v)
{
if (v
return log10(v);
}
ValueType Function::Sqrt(const ValueType& v)
{
if (v < 0.0) throw SqrtError();
return sqrt(v);
}
ValueType Function::Fct(const ValueType& v)
{
long double res = 1;
for (unsigned long i = 1; i res *= i;
return res;
}
ValueType Function::Abs(const ValueType& v)
{
if (v >= 0)
return v;
else
return -v;
}