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

📄 prog8_11.cpp

📁 一本语言类编程书籍
💻 CPP
字号:
// Program 8.11 Using a static variable
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

long next_Fibonacci();

int main() {
  cout << endl << "The Fibonacci Series" << endl;
  for(int i = 0 ; i < 30 ; i++) {
    if(i % 5 == 0)                      // Every fifth number...
      cout << endl;                     // ...start a new line
    cout << std::setw(12) << next_Fibonacci ();
  }
  cout << endl;
  return 0;
}

// Function to generate the next number in the Fibonacci series
long next_Fibonacci () {
  static long last = 0;                 // Last number in sequence
  static long last_but_one = 1;         // Last but one number

  long next = last + last_but_one;      // Next is sum of the last two
  last_but_one = last;                  // Update last but one
  last = next;                          // Last is new one
  return last;                          // Return the new one
}

⌨️ 快捷键说明

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