Content on the machine: Use the IF-else statement and switch statement to solve the segmentation function.
Objective: To learn the Branch StructureProgramDesign
Program:
/** Copyright (c) 2012, School of Computer Science, Yantai University * All Rights Reserved. * Author: Qiu xuewei * Completion Date: July 15, October 24, 2012 * version number: V1.0 ** input Description: X, Y is not negative * Problem description: returns the value of the piecewise function with X as the independent variable. 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: Y value * Problem Analysis: This task is completed using the if Condition Statement. *AlgorithmDESIGN: After Entering X, determine whether it is a non-negative number. When it is not a negative number, separate the processing according to the above segmentation method. */# Include <iostream> # include <cmath> using namespace STD; int main () {float X, Y; cout <"Enter the value of x (x> = 0):"; CIN> X; If (x <0) cout <"does not meet the requirements. "<Endl; {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 <"x =" <x <"Y =" <Y <Endl;} return 0 ;}
Running result: