用C语言计算分段函数

1、 计算下列分段函数
f(x,y)= 2x2+3x+1/x+y (x>=0,y>0)
f(x,y)=2x2+3x+1/1+y2 (x>=0,y<=0)
f(x,y)=3sin(x+y)/ 2x2+3x+1 (x<0)
具体要求如下:
(1) 用if语句实现多路分支,自变量x,y与函数值均用双精度类型。
(2) 自变量x,y用scanf函数输入,且输入前要有提示。结果的输出采用以下形式:
x=具体值,y=具体值,f(x)=具体值
(3) 分别输入x=3,y=8;x=5,y=-9;x=-7,y=2运行该程序
tt

第1个回答  2011-06-28
#include "stdio.h"
#include<math.h>
void main()
{
double x,y,f,h;
printf("请输入x:\n");
scanf("%lf",&x);
printf("请输入y:\n");
scanf("%lf",&y);
if((x>=0)&&(y>0))
f=2*pow(x,2)+3*x+1/x+y;
else if((x>=0)&&(y<=0))
f=2*x*x+3*x+1/x+y*y;
else
f=3*sin(x+y)/2/pow(x,2)+3*x+1;
printf("x=%lf,y=%lf,f=%lf\n",x,y,f);
h=pow(x,2);
printf("%lf",h);
}本回答被提问者采纳
第2个回答  2011-06-28
你那个变量后面加一个数字是指幂吗? 还有
f(x,y)=2x2+3x+1/1+y2,这个中的1/1是什么意思,
f(x,y)=3sin(x+y)/ 2x2+3x+1 这个中的2x2是当做一个整体做除数吗?
你题目都没写清楚,我看着就来气。
第3个回答  2011-06-28
f(x,y)= 2x2+3x+1/x+y (x>=0,y>0)
f(x,y)=2x2+3x+1/1+y2 (x>=0,y<=0)
f(x,y)=3sin(x+y)/ 2x2+3x+1 (x<0)

公式都对吗?怎么还有1/1呢

2*2 还是其他意思 分不清 * X

#include <stdio.h>
#include <math.h>

double f(int x, int y)
{
double result = 0.0;

if (x >= 0 && y > 0)
{
result = 2*2+3*x+1.0/x+y;
}
else if (x >= 0 && y <= 0)
{
result = 2*2+3*x+1/1+2*y;
}
else if (x < 0)
{
result = 3*sin(x+y)/2.0*2.0+3*x+1;
}
return result;
}

int main(void)
{
int a[3] = {3, 8, -7};
int b[3] = {8, -9, 2};
int i;

for (i = 0; i < 3; ++i)
printf("x[%d] y[%d] f=%g\n", a[i], b[i], f(a[i], b[i]));

return 0;
}追问

“/”表示除法 '2X2'是指X的2次幂 不是乘号

追答

double f(int x, int y)
{
double result = 0.0;

if (x >= 0 && y > 0)
{
result = 2*x*x+3*x+1.0/x+y;
}
else if (x >= 0 && y <= 0)
{
result = 2*x*x+3*x+1/1+y*y;
}
else if (x < 0)
{
result = 3*sin(x+y)/2*x*x+3*x+1;
}
return result;
}

第4个回答  2011-06-28
# include <stdio.h>
# include <math.h>
main()
{
double x,y,f;
printf("x=");
scanf("%lf",&x);
printf("y=");
scanf("%lf",&y);
if(x>=0&&y>0)
f=2*x*x+3*x+1/x+y;
else
if(x>=0&&y<=0)
f=2*x*x+3*x+1/1+y*y;
else
f=3*sin(x+y)/(2*x*x)+3*x+1;
printf("x=%lf,y=%lf,f(x,y)=%lf\n",x,y,f);
}

c语言中如何将分段函数值计算出来?
include <stdio.h> include <math.h> int main(){ float x, result;printf("请输入x的值:");scanf("%f",&x);if(x <= 0){ result = 0;} else if(x <= 10){ result = sqrt(x);} else { result = 2 * x + 3;} printf("%f",result);return 0;} ...

c语言 计算分段函数,测试数据分别是-2,3,7
计算下列分段函数 f(x,y)= 2X2+3x+1\/(x+y) (x>=0,y>0)f(x,y)=2X2+3x+1\/(1+y2)(x>=0,y<=0)f(x,y)=3sin(x+y)\/ 2X2+3X+1 (x<0)

C 语言 编写程序,计算分段函数:
\/\/100分制 include <stdio.h> void main(){ int score,t;printf("输入成绩:");scanf("%d",&score);t=score\/10;\/\/t的取值0,1,2,3,4,5,6,7,8,9,10 switch(t){ case 0:case 1:case 2:case 3:case 4:case 5:printf("不及格\\n");break;case 6:printf("及格\\n")...

c语言设计 分段函数
int main(){ double x,y;scanf("%lf",&x);if (x<0)y=0.5*(-x);else if (x<10)y=exp(x)+3;else if(x<20)y=log10(x);else if (x<30)y=pow(x,1.5);else if (x<50)y=pow (x,0.5)-1;else y=3*cos(x);printf("y=%lf\\n",y);return 0;} ...

c语言求下面的分段函数,多种方法
void main(){ int x,y;printf("Please type in x:");scanf("%d",&x);if(x<0)y=x;else if(x<10)y=x-10;else y=x+10;printf("y=%d\\n",y);} 2嵌套的if语句 include <stdio.h> void main(){ int x,y;printf("Please type in x:");scanf("%d",&x);if(x<0)y=x;...

C语言计算分段函数
int main(void){ double x, y, f;printf("Please input 2 double number in the form of x y:\\n");scanf("%lf%lf", &x, &y);if(x>=0 && y>0)f = 2*x*x + 3*x +1\/(x+y);else if(x>=0 && y<=0)f = 2*x*x + 3*x +1\/(1+y*y);else f = 3*sin(x+...

c语言求分段函数
include<stdio.h> int main(){ float x,y;\/\/根据给定的测试用例,x,y应该为float型 scanf("%f",&x);\/\/x为float型,所以改为%f if(x<20){ y=x+100;} else if(x>=20&&x<=100){ y=x;} else y=x-100;printf("x=%f,y=%f\\n",x,y);return 0;\/\/缺少分号 } ...

C语言写计算分段函数
include <stdio.h> include <math.h> int main(void){ int repeat, ri;double x, y;scanf("%d", &repeat);for(ri = 1; ri <= repeat; ri++){ scanf("%lf",&x);y=x>=0?sqrt(x):pow(x+1,2)+2*x+1\/x;printf("f(%.2f) = %.2f\\n", x, y);} } ...

C语言:计算图片中的分段函数;x由键盘输入,实数。咋编?
代码文本:include "stdio.h"include "math.h"int main(int argc,char *argv[]){ double x;printf("Enter x(R:)...\\nx=");scanf("%lf",&x);printf("y = %g\\n",(x ? sin(x)+100 : cos(x)-10*x+3));return 0;}

c语言:编写程序可计算如下分段函数
inlude<math.h>#include<fstream.h>#define pi=3.1415926int main(){float x,y;cin>>x;if(x>0){ y=(pow(x,0.5)+exp(x))\/(5*x+5);}else{ y=(2\/3.0+sin(60*pi\/180.0))*fabs(x);}cout<<"y="<<y<<endl;return 0;} 具体的你再调试下 ...

相似回答