/*
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作 者:张浩
* 完成日期:2012 年 11 月 2 日
* 版 本 号:v1.0
*
* 输入描述: 无
* 问题描述:分段函数的问题
* 程序输出:x与y的值
* 问题分析:根据定义域x的范围,求y对应的函数
* 算法设计:利用if-else语句
*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"请输入X的值:"<<endl;cin>>x;
if(x<0){cout<<"Input error!"<<endl;}
else{
if(x>=0 && x<2){
y=x;}
else{
if(x>=2 && x<6){
y=x*x+1;}
else{
if(x>=6 && x<10){
y=sqrt(x+1);}
else{y=1/(x+1);}}}}
cout<<"x="<<x<<",y="<<y<<endl;
return 0;
}
运行结果:
心得体会:对于在写程序时怎么用好括号上我还是有点不懂;恳请大家提提意见!