您的位置:首页 > 其它

第8周项目3(1)-分段函数计算

2014-10-16 18:01 246 查看
/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week8-project3(1).cpp
*作者:高赞
*完成日期:2014年 10 月 16 日
*版本号:v1.0
*
*问题描述:计算函数值,x<2时,y=x;2<=x<6时,y=x*x+1;6<=x<10时,y=sqrt(x+1);否则y=1/(x+1)。使用else if语句
*输入描述:一个实数x
*程序输出:对应的y值
*/
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double x,y;
cout << "x=";
cin >> x;
if (x<2)
y=x;
else if (x<6)
y=x*x+1;
else if (x<10)
y=sqrt(x+1);
else y=1/(x+1);
cout << "y=" << y << endl;
return 0;
}


运算结果:



知识点总结:

运用多层内嵌if语句来实现多分支选择。

学习心得:

因为要用到数学函数sqrt ,所以要包含有头文件 cmath,不然无法识别。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: