當前位置:編程學習大全網 - 編程軟體 - C語言,分別用if 語句和switch語句編寫程序求分段函數的值

C語言,分別用if 語句和switch語句編寫程序求分段函數的值

//if語句

#inlcude <stdio.h>

#include <math.h>

define a 10 //a自己取值

void main()

{

float x, y;

scanf("%f",&x);

if(x>=0.5 && x<1.5)

y = a*x*x;

if(x>=1.5 && x < 3.5)

y = exp(a*x);

if(x<=3.5 && x<7.5)

y = pow(sin(a+x), 2);

printf("%f", y);

}

//switch語句

#include <stdio.h>

#include <math.h>

define a 10 //a自己取值

void main()

{

float x, y;

scanf("%f",&x);

int n = x/0.5;

switch(n)

case(14):

case(13):

case(12):

case(11):

case(10):

case(9):

case(8):

case(7):

y = pow(sin(a+x), 2);break;

case(6):

case(5):

case(4):

case(3):

y = exp(a*x);break;

case(2):

case(1):

y = a*x*x;break;

default: break;

printf("%d", y);

}

  • 上一篇:c語言四舍五入的代碼怎麽打
  • 下一篇:北大青鳥怎麽試聯動
  • copyright 2024編程學習大全網