#include?iostream

10多年的旌陽網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整旌陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“旌陽網(wǎng)站設(shè)計”,“旌陽網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
int?main()
{
int?a?=?0,b?=?0;
printf("請輸入一次方程的系數(shù)a和b(以逗號隔開):");
scanf("%d,%d",a,b);
double?c?=?(double)-b?/?a;
printf("一次方程?%dx+%d=0?的根是:x?=?%lf\n",a,b,c);
system("pause");
return?0;
一元多項式簡單的計算器的功能:
1)輸入并建立多項式;
2)輸出多項式;
3)兩個多項式相加,輸出和多項式;
4)兩個多項式相減,輸出差多項式。
例程
#include?dos.h?/*DOS接口函數(shù)*/
#include?math.h?/*數(shù)學(xué)函數(shù)的定義*/
#include?conio.h?/*屏幕操作函數(shù)*/
#include?stdio.h?/*I/O函數(shù)*/
#include?stdlib.h?/*庫函數(shù)*/
#include?stdarg.h?/*變量長度參數(shù)表*/
#include?graphics.h?/*圖形函數(shù)*/
#include?string.h?/*字符串函數(shù)*/
#include?ctype.h?/*字符操作函數(shù)*/
#define?UP?0x48?/*光標(biāo)上移鍵*/
#define?DOWN?0x50?/*光標(biāo)下移鍵*/
#define?LEFT?0x4b?/*光標(biāo)左移鍵*/
#define?RIGHT?0x4d?/*光標(biāo)右移鍵*/
#define?ENTER?0x0d?/*回車鍵*/
void?*rar;?/*全局變量,保存光標(biāo)圖象*/
struct?palettetype?palette;?/*使用調(diào)色板信息*/
int?GraphDriver;?/*?圖形設(shè)備驅(qū)動*/
int?GraphMode;?/*?圖形模式值*/
int?ErrorCode;?/*?錯誤代碼*/
int?MaxColors;?/*?可用顏色的最大數(shù)值*/
int?MaxX,?MaxY;?/*?屏幕的最大分辨率*/
double?AspectRatio;?/*?屏幕的像素比*/
void?drawboder(void);?/*畫邊框函數(shù)*/
void?initialize(void);?/*初始化函數(shù)*/
void?computer(void);?/*計算器計算函數(shù)*/
void?changetextstyle(int?font,?int?direction,?int?charsize);?/*改變文本樣式函數(shù)*/
void?mwindow(char?*header);?/*窗口函數(shù)*/
int?specialkey(void)?;?/*獲取特殊鍵函數(shù)*/
int?arrow();?/*設(shè)置箭頭光標(biāo)函數(shù)*/
/*主函數(shù)*/
int?main()
{
initialize();/*?設(shè)置系統(tǒng)進(jìn)入圖形模式?*/
computer();?/*運行計算器?*/
closegraph();/*系統(tǒng)關(guān)閉圖形模式返回文本模式*/
return(0);?/*結(jié)束程序*/
}
/*?設(shè)置系統(tǒng)進(jìn)入圖形模式?*/
void?initialize(void)
{
int?xasp,?yasp;?/*?用于讀x和y方向縱橫比*/
GraphDriver?=?DETECT;?/*?自動檢測顯示器*/
initgraph(?GraphDriver,?GraphMode,?""?);
/*初始化圖形系統(tǒng)*/
ErrorCode?=?graphresult();?/*讀初始化結(jié)果*/
if(?ErrorCode?!=?grOk?)?/*如果初始化時出現(xiàn)錯誤*/
{
printf("Graphics?System?Error:?%s\n",
grapherrormsg(?ErrorCode?)?);?/*顯示錯誤代碼*/
exit(?1?);?/*退出*/
}
getpalette(?palette?);?/*?讀面板信息*/
MaxColors?=?getmaxcolor()?+?1;?/*?讀取顏色的最大值*/
MaxX?=?getmaxx();?/*?讀屏幕尺寸?*/
MaxY?=?getmaxy();?/*?讀屏幕尺寸?*/
getaspectratio(?xasp,?yasp?);?/*?拷貝縱橫比到變量中*/
AspectRatio?=?(double)xasp/(double)yasp;/*?計算縱橫比值*/
}
/*計算器函數(shù)*/
void?computer(void)
{
struct?viewporttype?vp;?/*定義視口類型變量*/
int?color,?height,?width;
int?x,?y,x0,y0,?i,?j,v,m,n,act,flag=1;
float?num1=0,num2=0,result;?/*操作數(shù)和計算結(jié)果變量*/
char?cnum[5],str2[20]={""},c,temp[20]={""};
char?str1[]="1230.456+-789*/Qc=^%";/*?定義字符串在按鈕圖形上顯示的符號?*/
mwindow(?"Calculator"?);?/*?顯示主窗口?*/
color?=?7;?/*設(shè)置灰顏色值*/
getviewsettings(?vp?);?/*?讀取當(dāng)前窗口的大小*/
width=(vp.right+1)/10;?/*?設(shè)置按鈕寬度?*/
height=(vp.bottom-10)/10?;?/*設(shè)置按鈕高度?*/
x?=?width?/2;?/*設(shè)置x的坐標(biāo)值*/
y?=?height/2;?/*設(shè)置y的坐標(biāo)值*/
setfillstyle(SOLID_FILL,?color+3);
bar(?x+width*2,?y,?x+7*width,?y+height?);
/*畫一個二維矩形條顯示運算數(shù)和結(jié)果*/
setcolor(?color+3?);?/*設(shè)置淡綠顏色邊框線*/
rectangle(?x+width*2,?y,?x+7*width,?y+height?);
/*畫一個矩形邊框線*/
setcolor(RED);?/*設(shè)置顏色為紅色*/
outtextxy(x+3*width,y+height/2,"0.");?/*輸出字符串"0."*/
x?=2*width-width/2;?/*設(shè)置x的坐標(biāo)值*/
y?=2*height+height/2;?/*設(shè)置y的坐標(biāo)值*/
for(?j=0?;?j4?;?++j?)?/*畫按鈕*/
{
for(?i=0?;?i5?;?++i?)
{
setfillstyle(SOLID_FILL,?color);
setcolor(RED);
bar(?x,?y,?x+width,?y+height?);?/*畫一個矩形條*/
rectangle(?x,?y,?x+width,?y+height?);
sprintf(str2,"%c",str1[j*5+i]);
/*將字符保存到str2中*/
outtextxy(?x+(width/2),?y+height/2,?str2);
x?=x+width+?(width?/?2)?;?/*移動列坐標(biāo)*/
}
y?+=(height/2)*3;?/*?移動行坐標(biāo)*/
x?=2*width-width/2;?/*復(fù)位列坐標(biāo)*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y);?/*移動光標(biāo)到x,y位置*/
arrow();?/*顯示光標(biāo)*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,"");?/*設(shè)置str2為空串*/
while((v=specialkey())!=45)?/*當(dāng)壓下Alt+x鍵結(jié)束程序,否則執(zhí)行下面的循環(huán)*/
{
while((v=specialkey())!=ENTER)?/*當(dāng)壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT);?/*顯示光標(biāo)圖象*/
if(v==RIGHT)?/*右移箭頭時新位置計算*/
if(x=x0+6*width)
/*如果右移,移到尾,則移動到最左邊字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
}?/*否則,右移到下一個字符位置*/
if(v==LEFT)?/*左移箭頭時新位置計算*/
if(x=x0)
{
x=x0+6*width;
m=4;
}?/*如果移到頭,再左移,則移動到最右邊字符位置*/
else
{
x=x-width-width/2;
m--;
}?/*否則,左移到前一個字符位置*/
if(v==UP)?/*上移箭頭時新位置計算*/
if(y=y0)
{
y=y0+4*height+height/2;
n=3;
}?/*如果移到頭,再上移,則移動到最下邊字符位置*/
else
{
y=y-height-height/2;
n--;
}?/*否則,移到上邊一個字符位置*/
if(v==DOWN)?/*下移箭頭時新位置計算*/
if(y=7*height)
{
y=y0;
n=0;
}?/*如果移到尾,再下移,則移動到最上邊字符位置*/
else
{
y=y+height+height/2;
n++;
}?/*否則,移到下邊一個字符位置*/
putimage(x,y,rar,XOR_PUT);?/*在新的位置顯示光標(biāo)箭頭*/
}
c=str1[n*5+m];?/*將字符保存到變量c中*/
if(isdigit(c)||c=='.')?/*判斷是否是數(shù)字或小數(shù)點*/
{
if(flag==-1)?/*如果標(biāo)志為-1,表明為負(fù)數(shù)*/
{
strcpy(str2,"-");?/*將負(fù)號連接到字符串中*/
flag=1;
}?/*將標(biāo)志值恢復(fù)為1*/
sprintf(temp,"%c",c);?/*將字符保存到字符串變量temp中*/
strcat(str2,temp);?/*將temp中的字符串連接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2);?/*顯示字符串*/
}
if(c=='+')
{
num1=atof(str2);?/*將第一個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=1;?/*做計算加法標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0)?/*如果str2為空,說明是負(fù)號,而不是減號*/
flag=-1;?/*設(shè)置負(fù)數(shù)標(biāo)志*/
else
{
num1=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=2;?/*做計算減法標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);?/*畫矩形*/
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
}
if(c=='*')
{
num1=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=3;?/*做計算乘法標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);?bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='/')
{
num1=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=4;?/*做計算除法標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='^')
{
num1=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=5;?/*做計算乘方標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);?/*設(shè)置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);?/*畫矩形*/
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='%')
{
num1=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,"");?/*將str2清空*/
act=6;?/*做計算模運算乘方標(biāo)志值*/
setfillstyle(SOLID_FILL,color+3);?/*設(shè)置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);?/*畫矩形*/
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='=')
{
num2=atof(str2);?/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
switch(act)?/*根據(jù)運算符號計算*/
{
case?1:result=num1+num2;break;?/*做加法*/
case?2:result=num1-num2;break;?/*做減法*/
case?3:result=num1*num2;break;?/*做乘法*/
case?4:result=num1/num2;break;?/*做除法*/
case?5:result=pow(num1,num2);break;?/*做x的y次方*/
case?6:result=fmod(num1,num2);break;?/*做模運算*/
}
setfillstyle(SOLID_FILL,color+3);?/*設(shè)置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);?/*覆蓋結(jié)果區(qū)*/
sprintf(temp,"%f",result);?/*將結(jié)果保存到temp中*/
outtextxy(5*width,height,temp);?/*顯示結(jié)果*/
}
if(c=='c')
{
num1=0;?/*將兩個操作數(shù)復(fù)位0,符號標(biāo)志為1*/
num2=0;
flag=1;
strcpy(str2,"");?/*將str2清空*/
setfillstyle(SOLID_FILL,color+3);?/*設(shè)置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2);?/*覆蓋結(jié)果區(qū)*/
outtextxy(5*width,height,"0.");?/*顯示字符串*/
}
if(c=='Q')exit(0);?/*如果選擇了q回車,結(jié)束計算程序*/
}
putimage(x,y,rar,XOR_PUT);?/*在退出之前消去光標(biāo)箭頭*/
return;?/*返回*/
}
/*窗口函數(shù)*/
void?mwindow(?char?*header?)
{
int?height;
cleardevice();?/*?清除圖形屏幕?*/
setcolor(?MaxColors?-?1?);?/*?設(shè)置當(dāng)前顏色為白色*/
setviewport(?20,?20,?MaxX/2,?MaxY/2,?1?);?/*?設(shè)置視口大小?*/
height?=?textheight(?"H"?);?/*?讀取基本文本大小?*/
settextstyle(?DEFAULT_FONT,?HORIZ_DIR,?1?);/*設(shè)置文本樣式*/
settextjustify(?CENTER_TEXT,?TOP_TEXT?);/*設(shè)置字符排列方式*/
outtextxy(?MaxX/4,?2,?header?);?/*輸出標(biāo)題*/
setviewport(?20,20+height+4,?MaxX/2+4,?MaxY/2+20,?1?);?/*設(shè)置視口大小*/
drawboder();?/*畫邊框*/
}
void?drawboder(void)?/*畫邊框*/
{
struct?viewporttype?vp;?/*定義視口類型變量*/
setcolor(?MaxColors?-?1?);?/*設(shè)置當(dāng)前顏色為白色?*/
setlinestyle(?SOLID_LINE,?0,?NORM_WIDTH?);/*設(shè)置畫線方式*/
getviewsettings(?vp?);/*將當(dāng)前視口信息裝入vp所指的結(jié)構(gòu)中*/
rectangle(?0,?0,?vp.right-vp.left,?vp.bottom-vp.top?);?/*畫矩形邊框*/
}
/*設(shè)計鼠標(biāo)圖形函數(shù)*/
int?arrow()
{
int?size;
int?raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4};?/*定義多邊形坐標(biāo)*/
setfillstyle(SOLID_FILL,2);?/*設(shè)置填充模式*/
fillpoly(8,raw);?/*畫出一光標(biāo)箭頭*/
size=imagesize(4,4,16,16);?/*測試圖象大小*/
rar=malloc(size);?/*分配內(nèi)存區(qū)域*/
getimage(4,4,16,16,rar);?/*存放光標(biāo)箭頭圖象*/
putimage(4,4,rar,XOR_PUT);?/*消去光標(biāo)箭頭圖象*/
return?0;
}
/*按鍵函數(shù)*/
int?specialkey(void)
{
int?key;
while(bioskey(1)==0);?/*等待鍵盤輸入*/
key=bioskey(0);?/*鍵盤輸入*/
key=key0xff??key0xff:key8;?/*只取特殊鍵的掃描值,其余為0*/
return(key);?/*返回鍵值*/
}
#include stdio.h
#include stdlib.h
//高精度計算s=1/n+1/(n+1)+1/(n+2)+……+1/m表達(dá)式的值
//求得的s是個分?jǐn)?shù),分子放在result[0]中,分母放在result[1]中
void func(double *result)
{
int m, n, i;
double *numerator; //分子
printf("Please input n and m (Separate by space and 0nm): \n");
while(scanf("%d%d", n, m))
{
if((0 n) (n m))
break;
printf("Value Invalid, please try again!\n");
printf("Please input n and m (Separate by space and 0nm): \n");
}
numerator = (double *)calloc(m, sizeof(double));
if(!numerator)
{
printf("malloc failed!\n");
exit(0);
}
result[1] = 1;
//下面兩個for循環(huán)是進(jìn)行通分
for(i=n; i=m; i++)
result[1] *= i;
for(i=n; i=m; i++)
numerator[i-n] = result[1]/i;
result[0] = 0;
//對分母進(jìn)行相加
for(i=n; i=m; i++)
result[0] += numerator[i-n];
for(i=n; im; i++)
printf("1/%d + ", i);
printf("1/%d = ", m);
free(numerator);
}
int main()
{
double result[2];
func(result);
printf("%g/%g\n", result[0], result[1]);
return 0;
}
//如果你還想對結(jié)果化為最簡分?jǐn)?shù)的話,可以告訴我,我會改程序
#includestdio.h 聲明部分:源代碼含有2個文件
#includemalloc.h
typedef struct pnode // 定義指針//
{int coef; //定義系數(shù)//
int exp; //定義指數(shù)//
struct pnode *next;
}pnode;
pnode * creat() //creat函數(shù)用來存放多項式//
{int m,n;
pnode *head,*rear,*s;
head=(pnode *)malloc(sizeof(pnode));
rear=head;
printf("\n輸入指數(shù)(按遞增順序輸入):");
scanf("%d",m);
printf("輸入一元式系數(shù)(0為退出):");
scanf("%d",n);
do
{
s=(pnode *)malloc(sizeof(pnode));
s-coef=n; //n為系數(shù)//
s-exp=m; //m為指數(shù)//
rear-next=s;
s-next=NULL;
rear=s;
printf("\n輸入指數(shù)(按遞增順序輸入):");
scanf("%d",m);
printf("輸入一元式系數(shù)(0為退出):");
scanf("%d",n);
}while(n);
return head;
}
pnode * add(pnode *heada,pnode *headb)
{pnode *headc,*a,*b,*s,*rearc;
int sum;
a=heada-next;b=headb-next;
headc=(pnode *)malloc(sizeof(pnode));
rearc=headc;
//多項式的存放//都放到s中里//
while(a!=NULLb!=NULL) //指數(shù)相等,則系數(shù)相加。//
{
if(a-exp==b-exp)
{ sum=a-coef+b-coef;
if(sum)
{s=(pnode *)malloc(sizeof(pnode));
s-coef=sum;
s-exp=a-exp;
rearc-next=s;
rearc=s;
a=a-next;
b=b-next;}
else
{a=a-next;
b=b-next;
}
}
else if(a-expb-exp)
//a指數(shù)如果小于b,則a放到s中//
{ s=(pnode *)malloc(sizeof(pnode));
s-coef=a-coef;
s-exp=a-exp;
rearc-next=s;
//用下一個結(jié)點s取代下一個c//
rearc=s;
a=a-next;
}
else //如果a的指數(shù)大,則b放到s中//
{ s=(pnode *)malloc(sizeof(pnode));
s-coef=b-coef;
s-exp=b-exp;
rearc-next=s;
rearc=s;
b=b-next;
}
}
if(a)
{while(a!=NULL) //b空了放a中的項//
{s=(pnode *)malloc(sizeof(pnode));
s-coef=a-coef;
s-exp=a-exp;
rearc-next=s;
s-next=NULL;
rearc=s;
a=a-next;
}
}
else if(b)
{while(b!=NULL) //a空了放b中的項//
{s=(pnode *)malloc(sizeof(pnode));
s-coef=b-coef;
s-exp=b-exp;
rearc-next=s;
s-next=NULL;
rearc=s;
b=b-next;
}}
return headc;
}
void main()
{pnode *a,*b,*c;
printf("建立A:");
a=creat();
printf("\n建立B:");
b=creat();
c=add(a,b);
c=c-next;
printf("%dx^%d",c-coef,c-exp);
c=c-next;
while(c!=NULL)
{printf("+%dx^%d",c-coef,c-exp);
c=c-next;
}
}
兩種方法:
一、定義求根公式,根據(jù)a、b、c的值計算結(jié)果。
二、暴力枚舉,每隔 0.001 取一個值,當(dāng)abs(f(x)-0) 0.00001 時視為得出結(jié)果。
網(wǎng)站標(biāo)題:C語言簡單一元函數(shù)求值,用c語言求函數(shù)的值
網(wǎng)站路徑:http://chinadenli.net/article38/dsihjsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、響應(yīng)式網(wǎng)站、網(wǎng)站收錄、網(wǎng)站制作、云服務(wù)器、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)