7-1 Calculate the piecewise function [2] (10 points)
This topic requires calculating the value of the following piecewise function f(x):
Note: You can include math.h in the header file, and call the sqrt function to find the square root, and call the pow function to find the exponentiation.
Input format:
Input a real number x in one line.
Output format: output in the format of
"f(x) = result" in one line, where x and result both retain two decimal places.
Input example 1:
10
Output example 1:
f(10.00) = 3.16
Input example 2:
-0.5
Output example 2:
f(-0.50) = -2.75
c language:
#include <stdio.h>
int main()
{
float x,result;
scanf("%f",&x);
if(x>=0)
{
result=sqrt(x);
printf("f(%.2f) = %.2f\n",x,result);
}else
{
result=pow(x+1,2)+(2*x)+(1/x);
printf("f(%.2f) = %.2f\n",x,result);
}
}
java:
//待更新