⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex9_03.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Exercise 9.3 Accessing the trig functions via a pointer to function. 
/********************************************************************************
The trig functions in the cmath header file expect the argument to be in radians;
you could extend the program by having the user enter a value in degrees
and converting it. 
*********************************************************************************/
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;

double calc(const double& value, double (*pTrigFun)(double));

int main() {
  double value = 0;
  
  cout << "Enter a value, in radians: ";
  cin >> value;

  double (*pTrigFunctions[])(double) = {std::sin, std::cos, std::tan};
  
  cout << " sin(" << value << ") = " << calc(value, pTrigFunctions[0]) << endl;
  cout << " cos(" << value << ") = " << calc(value, pTrigFunctions[1]) << endl;
  cout << " tan(" << value << ") = " << calc(value, pTrigFunctions[2]) << endl;

  return 0;
}

double calc(const double& value, double (*pTrigFun)(double)) {
  return pTrigFun(value);  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -