C语言,分别用if 语句和switch语句编写程序求分段函数的值

分别用if 语句和switch语句编写程序,求下列分段函数的值。

第1个回答  2012-12-29
//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);
}追问

“define a 10  //a自己取值”显示Declaration syntax error

怎么办?

追答

#define a 10 //宏定义,写错了,前面加个#

本回答被提问者采纳