On-Machine content: Solving piecewise functions with an if statement
The purpose of the machine: branch structure programming
/*
* Copyright (c) 2012, Computer College, Yantai University
* All rights reserved.
* Author: Xu Benxi
* Date of Completion: October 25, 2012
* Version Number: v1.0
*
* Input Description: X,x is not a negative number
* Problem Description: To find the value of the independent variable x as a piecewise function. Y=x (0<=X<2), y=x^2+2 (2<=x<6)
, y= (x+1) ^ (1/2) (6<=x<10), y=1/(x+1) (x>=10)
* Program output: value of y
* Problem Analysis: First determine the value of x and then do the processing
* Algorithm design: Using the IF statement to deal with
*/
My Code:
#include <iostream>
#include <Cmath>
using namespace std;
int main ()
{
double x,y;
cout<< "Please enter the value of x:";
cin>>x;
if (x<0)
cout<< "Input error!" <<endl;
else
{if (x<2&&x>=0)
y=x;
else
if (x<6&&x>=2)
Y=1+pow (x,2);
else
if (x<10&&x>=6)
y=sqrt (x+1);
else
if (x>=10)
y=1/(x+1);
cout<< "y=" <<Y<<ENDL;}
return 0;
}
Results screenshot:
My summary is not skilled enough to keep the passion