C语言入门题库——分段函数

C语言入门题库——分段函数

Description:按下表计算y值,x值由键盘输入。(x,y均为float类型)

x y
0<=x<10 sinx
10<=x<20 cosx
20<=x<30 ex-1
30<=x<40 ln(x+1)
其它值 no definition

Input:输入仅一行,输入一个数值。
Output:输出仅一行,输出相应的数据值(小数点后保留2位有效位)或no definition(两单词中间有一空格)。
Sample Input:1
Sample Output:y=0.84

//分段函数
#include<stdio.h>
#include<math.h>
int main()
{
    float x, y;
    scanf("%f", &x);
    if(x >= 0 && x < 40)
    {
        if(x < 10)
            y = sin(x);
        if(x >= 10 && x < 20)
            y = cos(x);
        if(x >= 20 && x < 30)
            y = exp(x)-1;
        if(x >= 30 && x < 40)
            y = log(x + 1);
        printf("y=%.2f", y);
    }
    else 
        printf("no definition");
    return 0;
} 
发布了27 篇原创文章 · 获赞 0 · 访问量 946

猜你喜欢

转载自blog.csdn.net/qq_43479432/article/details/104449908