从键盘上输入x和y的值,计算y = ln( 2x – y )的值,要求用异常处理“负数求对数”的情况。
#include <iostream> #include <cmath> using namespace std; double f( double x,double y ); int main() { double x,y; try { cout << "输入x和y的值:"; cin >> x >> y; cout << f( x,y ) << endl; } catch( char * ) { cout << "负数不能求对数!" << endl; } } double f( double x,double y ) { if( 2*x-y < 0 ) throw "error"; else return log( 2*x - y ); }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题