1、
#include<iostream> using namespace std; double computeHarmonicMean(double x, double y); int main() { double x, y; cout << "Enter two numbers(x!=0 and y!=0): "; bool valid; valid = (bool)(cin >> x >> y); while (!valid) { cin.clear(); while (cin.get() != '\n'); cout << "Enter invalid! enter again!\n"; cout << "Please enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); } while (x != 0 and y != 0) { cout <<"Harmonic mean: " << computeHarmonicMean(x, y) << endl; cout << "Enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); while (!valid) { cin.clear(); while (cin.get() != '\n'); cout << "Enter invalid! enter again!\n"; cout << "Please enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); } } cout << "x==0 or y==0 holds to quit\n"; } double computeHarmonicMean(double x, double y) { return 2.0 * x * y / (x + y); }
2、
标签:PrimerPlus,cout,double,cin,C++,第六版,while From: https://www.cnblogs.com/xinmind/p/16900508.html