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

📄 pr0510.cpp

📁 《C++编程习题与解答》书中所有例题与习题的源代码
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 5.10 on page 112
//  The Average of at Most Four Function

#include <iostream>  // defines the cin and cout objects
using namespace std;
double ave(double,double=0,double=0,double=0);

double main()
{ cout << "Enter four non-zero numbers: ";
  double w, x, y, z;
  cin >> w >> x >> y >> z;
  cout << "The average of the first one is " << ave(w) << endl;
  cout << "The average of the first two is " << ave(w,x) << endl;
  cout << "The average of the first three is " << ave(w,x,y)<<endl;
  cout << "The average of all four is " << ave(w,x,y,z) << endl;
}

double ave(double x1, double x2, double x3, double x4)
{ double sum = x1 + x2 + x3 + x4;
  if (x2 == 0) return sum;
  if (x3 == 0) return sum/2.0;
  if (x4 == 0) return sum/3.0;
  return sum/4.0;
}

⌨️ 快捷键说明

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