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

📄 prog8_02.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Program 8.2 Calculating powers - rearranged
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

double power(double x, int n);   // Prototype for power function

int main() {
  cout << endl;

  // Calculate powers of 8 from -3 to +3
  for(int i = -3 ; i <= 3 ; i++) 
    cout << std::setw(10) << power(8.0, i);

  cout << endl;
  return 0;
}

// Function to calculate x to the power n
double power(double x, int n) {
  double result = 1.0;
  if(n >= 0)
    for(int i = 0 ; i < n ; i++)
      result *= x;
  else
    for(int i = 0 ; i < -n ; i++)
      result /= x;
  return result;
}

⌨️ 快捷键说明

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