代碼如下所示,
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括簡陽網(wǎng)站建設(shè)、簡陽網(wǎng)站制作、簡陽網(wǎng)頁制作以及簡陽網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,簡陽網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到簡陽省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
#include
#include
#include
int main()
{
double x, y; // 定義函數(shù)的自變量、因變量,考慮到可能有小數(shù)的情況,設(shè)置數(shù)值類型為double型
int getBuff; // 用于最后清空輸入緩沖區(qū)殘留,了解即可
printf("請輸入自變量x的值(一個(gè)數(shù)字):\n");
scanf("%lf",x); // 輸入自變量的一個(gè)值
if(x-2)
y = 7-2*x;
else if(x=-2 x3)
y = 5 - fabs(3*x+2); // 注意,對浮點(diǎn)型數(shù)值取絕對值,要用fabs()函數(shù)
else
y = 3*x+4;
printf("自變量x為%lf時(shí),對應(yīng)的函數(shù)值y是:%lf\n",x,y);
while(getBuff=getchar()!='\n' getBuff!=EOF); // 清空輸入緩沖區(qū)殘留,了解即可
printf("按回車鍵結(jié)束...");
getchar();
return 0;
}為了避免手機(jī)上查看時(shí),代碼版式錯(cuò)亂,代碼的截圖如下,
如有幫助,煩請采納,謝謝!
有。C語言求絕對值的函數(shù)為abs( x )與fbs( x ),abs( x )包含于stdlib.h,且兩者均包含于math頭文件之下。
1、abs( x )函數(shù)
格式:int abs( int i );
作用:求整型數(shù)的絕對值
例子:
#includestdio.h
#include stdlib.h
#includemath.h
main(? ?)
{
int a = 1, b = -2 ;
printf("%d的絕對值是%d,%d的絕對值是%d\n", a, abs( a ), b, abs( b ));
}
運(yùn)行結(jié)果為:1的絕對值是1,-2的絕對值是2
2、fabs( x )函數(shù)
格式:float fabs( float i ); / double fabs( double x );
作用:求浮點(diǎn)數(shù)的絕對值
例子:
#includestdio.h
#includemath.h
main(? ?)
{
float a = 1.4, b = -2.7 ;
printf("%f的絕對值是%f,%f的絕對值是%f\n", a, fabs( a ), b, fabs( b ));
}
運(yùn)行結(jié)果為:1.400000的絕對值是1.400000,-2.700000的絕對值是2.700000
擴(kuò)展資料:
其他math.h頭文件包含函數(shù)介紹:
1、 三角函數(shù)
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函數(shù)
double asin (double); 結(jié)果介于[-PI/2,PI/2]
double acos (double); 結(jié)果介于[0,PI]
double atan (double); 反正切(主值),結(jié)果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圓值),結(jié)果介于[-PI,PI]
3 、雙曲三角函數(shù)
double sinh (double);
double cosh (double);
double tanh (double);
4 、指數(shù)與對數(shù)
double frexp(double value,int *exp);這是一個(gè)將value值拆分成小數(shù)部分f和(以2為底的)指數(shù)部分exp,并返回小數(shù)部分f,即f*2^exp。其中f取值在0.5~1.0范圍或者0。
double ldexp(double x,int exp);這個(gè)函數(shù)剛好跟上面那個(gè)frexp函數(shù)功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小數(shù)部分,iptr指向整數(shù)部分。
double log (double); 以e為底的對數(shù)
double log10 (double);以10為底的對數(shù)
double pow(double x,double y);計(jì)算x的y次冪
float powf(float x,float y); 功能與pow一致,只是輸入與輸出皆為單精度浮點(diǎn)數(shù)
double exp (double);求取自然數(shù)e的冪
double sqrt (double);開平方根
5 、取整
double ceil (double); 取上整,返回不比x小的最小整數(shù)
double floor (double); 取下整,返回不比x大的最大整數(shù),即高斯函數(shù)[x]
輸入數(shù)用scanf()函數(shù);
分段用switch()函數(shù);
1、絕對值用math庫里面的abs()函數(shù)
2、e^x用math庫里面的pow(e,x)函數(shù)
3、同理指數(shù)的都有pow()函數(shù),
4、cos函數(shù)也是math庫里面的double cos(double x)函數(shù)
補(bǔ)充:對于自變量x的不同的取值范圍,有著不同的對應(yīng)法則,這樣的函數(shù)通常叫做分段函數(shù)。它是一個(gè)函數(shù),而不是幾個(gè)函數(shù);分段函數(shù)的定義域是各段函數(shù)定義域的并集,值域也是各段函數(shù)值域的并集。
#include?"stdio.h"
#include?"math.h"
int?main(int?argc,char?*argv[]){
double?x,y;
printf("Input?x(R:)...\nx=");
scanf("%lf",x);
if(x5)
y=-x+3.5;
else?if(x=5??x10)
y=20-3.5*pow(x+3,7);//這里看著像7,是幾就把7改成幾
else
y=-3.5+sin(x);
printf("y?=?%g\t(x==%g)\n",y,x);
return?0;
}
運(yùn)行樣例:
1. C語言的庫函數(shù)中提供了求絕對值的函數(shù),函數(shù)名為 abs
2. 函數(shù)的頭文件:#include
3. 函數(shù)原型:int abs (int j);
4. 函數(shù)說明:abs()用來計(jì)算參數(shù)j 的絕對值,然后將結(jié)果返回。
5. 返回值:返回參數(shù)j 的絕對值結(jié)果。
c語言中取絕對值的函數(shù)
*?? ABS.C:?? This?? program?? computes?? and?? displays
*?? the?? absolute?? values?? of?? several?? numbers.
#include???? stdio.h
#include???? math.h
#include???? stdlib.h
void?? main(?? void?? )
{int???????? ix?? =?? -4,?? iy;
long?????? lx?? =?? -41567L,?? ly;
double?? dx?? =?? -3.141593,?? dy;
iy?? =?? abs(?? ix?? );
printf(?? "The?? absolute?? value?? of?? %d?? is?? %d/n",?? ix,?? iy);
ly?? =?? labs(?? lx?? );
printf(?? "The?? absolute?? value?? of?? %ld?? is?? %ld/n",?? lx,?? ly);
dy?? =?? fabs(?? dx?? );
printf(?? "The?? absolute?? value?? of?? %f?? is?? %f/n",?? dx,?? dy?? );
Output
The?? absolute?? value?? of?? -4?? is?? 4
The?? absolute?? value?? of?? -41567?? is?? 41567
The?? absolute?? value?? of?? -3.141593?? is?? 3.141593
本文名稱:c語言求絕對值分段函數(shù) c語言中求分段函數(shù)
新聞來源:http://jinyejixie.com/article38/hejhsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站改版、App設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)