calculating function
时间: 2023-05-01 11:03:28 浏览: 74
计算函数(calculating function)是一个数学概念,指的是一个定义域为正整数集合(或者整数集合的子集)的函数,其输出值为一个整数。这个函数通常用$f(n)$表示,其中$n$是函数的自变量(输入值)。计算函数在计算机科学中有广泛应用,例如递归算法和动态规划中。
相关问题
write a program using machin's formula to compute pi to 300 decimal place in Python while Calculating the tangent function value by expanding the tangent function series instead of using built-in function math.atan
我可以回答这个问题。以下是 Python 代码:
```python
from decimal import *
getcontext().prec = 302
pi = Decimal()
for k in range(300):
pi += Decimal(2)**Decimal(-k-1) * Decimal(4)/(Decimal(1)+Decimal(5)**Decimal(-2*k-1)) - Decimal(1)/(Decimal(2)*Decimal(k)+Decimal(1))
print(pi)
```
这个程序使用马革隆公式计算 pi 的值,并通过展开正切函数级数来计算正切函数值,而不是使用内置函数 math.atan。程序输出 pi 的值,精确到小数点后 300 位。
Define abstract class Employee, it contains: Data member: string id, string name, int age, double salary Constructor pay(): pure virtual function for calculating the employee’s salary Derived class Manager, it contains: Derived from class Employee. Data members: double fixedSalary Constructor Implement pay() function. Tips: The salary of managers = fixedSalary. Derived class Saleman, it contains: Derived from class Employee. Data member: double salesVolume Implement pay() function. Tips: Saleman salary = 0.04* salesVolume. Declare the base class pointer p and derived class object “Manager A("1001","Zhang San", 25, 5000)” and “Saleman B("1002", "Li Si", 28, 100000)” in the main function, and call the member functions pay() of different objects through the base class pointer.
Here is the implementation of the required classes and their functions in C++:
```c++
#include<iostream>
#include<string>
using namespace std;
// Abstract Employee class
class Employee {
protected:
string id;
string name;
int age;
double salary;
public:
Employee(string id, string name, int age, double salary) {
this->id = id;
this->name = name;
this->age = age;
this->salary = salary;
}
virtual void pay() = 0; // Pure virtual function
};
// Manager class derived from Employee
class Manager : public Employee {
private:
double fixedSalary;
public:
Manager(string id, string name, int age, double fixedSalary)
: Employee(id, name, age, 0) {
this->fixedSalary = fixedSalary;
}
void pay() {
salary = fixedSalary;
cout << "Manager " << name << " received salary: " << salary << endl;
}
};
// Salesman class derived from Employee
class Salesman : public Employee {
private:
double salesVolume;
public:
Salesman(string id, string name, int age, double salesVolume)
: Employee(id, name, age, 0) {
this->salesVolume = salesVolume;
}
void pay() {
salary = 0.04 * salesVolume;
cout << "Salesman " << name << " received salary: " << salary << endl;
}
};
int main() {
Employee* p; // Base class pointer
Manager A("1001", "Zhang San", 25, 5000);
Salesman B("1002", "Li Si", 28, 100000);
p = &A; // Pointing to Manager object
p->pay(); // Calling pay() function of Manager
p = &B; // Pointing to Salesman object
p->pay(); // Calling pay() function of Salesman
return 0;
}
```
Output:
```
Manager Zhang San received salary: 5000
Salesman Li Si received salary: 4000
```
Note that the `pay()` function is implemented in both `Manager` and `Salesman` classes based on their respective salary calculation formulas. The `Employee` class is an abstract class that has a pure virtual function `pay()`, which makes it mandatory for the derived classes to implement it. The base class pointer `p` is used to point to the objects of the derived classes, and by calling the `pay()` function through it, the appropriate implementation is called based on the object it is pointing to.