#include <math.h>
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
}
扩展资料return 0代表程序正常退出。return是C++预定义的语句,它提供了终止函数执行的一种方式。当return语句提供了一个值时,这个值就成为函数的返回值。
return语句用来结束循环,或返回一个函数的值。
1、return 0,说明程序正常退出,返回到主程序继续往下执行。
2、return 1,说明程序异常退出,返回主调函数来处理,继续往下执行。return 0或return 1对程序执行的顺序没有影响,只是大家习惯于使用return(0)退出子程序而已。
#include
int main()
{
int x,y
scanf("%d",&x)
if(0<x&&x<10) y=3*x+2
else
{if(x=0) y=0
else
{if (x<0) y=x*x
else printf("go die\n")
}
}
printf("%d",y)
return 0
}该程序的分段函数如下:
f(x)=3x+2 (0<x<10)
f(x)=1 (x=0)
f(x) = x*x (x<0)
#include <stdio.h>
#include <math.h>
void main()
{
float x
double y
printf("Please input the value of x:")
scanf("%f",&x)
if(x>=-10&&x<=4)
{
y=fabs(x-2)
printf("y=%.2f\n",y)
}
else if(x>=5&&x<=7)
{
y=x+10
printf("y=%.2f\n",y)
}
else if(x>=8&&x<=12)
{
y=pow(x,4)
printf("y=%.2f\n",y)
}
else
printf("No answer\n")
}
进行数值分段主要进行字符串分割,使用strtok函数即可实现字符串分割。这里引用一段strtok用法:The strtok() function returns a pointer to the next "token" in str1, where str2 contains the delimiters that determine the token. strtok() returns NULL if no token is found. In order to convert a string to tokens, the first call to strtok() should have str1 point to the string to be tokenized. All calls after this should have str1 be NULL.
For example:char str[] = "now # is the time for all # good men to come to the # aid of their country"
char delims[] = "#"
char *result = NULL
result = strtok( str, delims )
while( result != NULL ) {undefined
printf( "result is \"%s\"\n", result )
result = strtok( NULL, delims )
}
/* 何问起 hovertree.com */
The above code will display the following output:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)