問題及代碼:
/*
*Copyright(c)2014,煙台大學計算機學院
*Allrights reserved.
*檔案名稱:MADE6.cpp
*作 者:孫化龍
*完成日期:2014年10月16日
*版 本 号:v1.0
*
*問題描述:多分段函數求值
*輸入描述: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>=2&&x<6)
y=x*x+1;
else if(x>=6&&x<10)
y=sqrt(x+1);
else
y=1/(x+1);
cout<<"y="<<y<<endl;
return 0;
}
運作結果:
學習心得:
第一次使用if語句的嵌套,過程遇到很多問題,終于在嘗試、改正中攻破。