Week 9 piecewise function evaluate (if-else Statement)

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.