如何用C语言实现一元多项式简单计算器的设计 设计一个一元多项式简单的计算器(C语言)

\u6c42c\u8bed\u8a00\u8bfe\u7a0b\u8bbe\u8ba1\u3010\u4e00\u5143\u591a\u9879\u5f0f\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u3011

#include /*DOS\u63a5\u53e3\u51fd\u6570*/
#include /*\u6570\u5b66\u51fd\u6570\u7684\u5b9a\u4e49*/
#include /*\u5c4f\u5e55\u64cd\u4f5c\u51fd\u6570*/
#include /*I/O\u51fd\u6570*/
#include /*\u5e93\u51fd\u6570*/
#include /*\u53d8\u91cf\u957f\u5ea6\u53c2\u6570\u8868*/
#include /*\u56fe\u5f62\u51fd\u6570*/
#include /*\u5b57\u7b26\u4e32\u51fd\u6570*/
#include /*\u5b57\u7b26\u64cd\u4f5c\u51fd\u6570*/
#define UP 0x48 /*\u5149\u6807\u4e0a\u79fb\u952e*/
#define DOWN 0x50 /*\u5149\u6807\u4e0b\u79fb\u952e*/
#define LEFT 0x4b /*\u5149\u6807\u5de6\u79fb\u952e*/
#define RIGHT 0x4d /*\u5149\u6807\u53f3\u79fb\u952e*/
#define ENTER 0x0d /*\u56de\u8f66\u952e*/
void *rar; /*\u5168\u5c40\u53d8\u91cf\uff0c\u4fdd\u5b58\u5149\u6807\u56fe\u8c61*/
struct palettetype palette; /*\u4f7f\u7528\u8c03\u8272\u677f\u4fe1\u606f*/
int GraphDriver; /* \u56fe\u5f62\u8bbe\u5907\u9a71\u52a8*/
int GraphMode; /* \u56fe\u5f62\u6a21\u5f0f\u503c*/
int ErrorCode; /* \u9519\u8bef\u4ee3\u7801*/
int MaxColors; /* \u53ef\u7528\u989c\u8272\u7684\u6700\u5927\u6570\u503c*/
int MaxX, MaxY; /* \u5c4f\u5e55\u7684\u6700\u5927\u5206\u8fa8\u7387*/
double AspectRatio; /* \u5c4f\u5e55\u7684\u50cf\u7d20\u6bd4*/
void drawboder(void); /*\u753b\u8fb9\u6846\u51fd\u6570*/
void initialize(void); /*\u521d\u59cb\u5316\u51fd\u6570*/
void computer(void); /*\u8ba1\u7b97\u5668\u8ba1\u7b97\u51fd\u6570*/
void changetextstyle(int font, int direction, int charsize); /*\u6539\u53d8\u6587\u672c\u6837\u5f0f\u51fd\u6570*/
void mwindow(char *header); /*\u7a97\u53e3\u51fd\u6570*/
int specialkey(void) ; /*\u83b7\u53d6\u7279\u6b8a\u952e\u51fd\u6570*/
int arrow(); /*\u8bbe\u7f6e\u7bad\u5934\u5149\u6807\u51fd\u6570*/
/*\u4e3b\u51fd\u6570*/
int main()
{
initialize();/* \u8bbe\u7f6e\u7cfb\u7edf\u8fdb\u5165\u56fe\u5f62\u6a21\u5f0f */
computer(); /*\u8fd0\u884c\u8ba1\u7b97\u5668 */
closegraph();/*\u7cfb\u7edf\u5173\u95ed\u56fe\u5f62\u6a21\u5f0f\u8fd4\u56de\u6587\u672c\u6a21\u5f0f*/
return(0); /*\u7ed3\u675f\u7a0b\u5e8f*/
}
/* \u8bbe\u7f6e\u7cfb\u7edf\u8fdb\u5165\u56fe\u5f62\u6a21\u5f0f */
void initialize(void)
{
int xasp, yasp; /* \u7528\u4e8e\u8bfbx\u548cy\u65b9\u5411\u7eb5\u6a2a\u6bd4*/
GraphDriver = DETECT; /* \u81ea\u52a8\u68c0\u6d4b\u663e\u793a\u5668*/
initgraph( &GraphDriver, &GraphMode, "" );
/*\u521d\u59cb\u5316\u56fe\u5f62\u7cfb\u7edf*/
ErrorCode = graphresult(); /*\u8bfb\u521d\u59cb\u5316\u7ed3\u679c*/
if( ErrorCode != grOk ) /*\u5982\u679c\u521d\u59cb\u5316\u65f6\u51fa\u73b0\u9519\u8bef*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); /*\u663e\u793a\u9519\u8bef\u4ee3\u7801*/
exit( 1 ); /*\u9000\u51fa*/
}
getpalette( &palette ); /* \u8bfb\u9762\u677f\u4fe1\u606f*/
MaxColors = getmaxcolor() + 1; /* \u8bfb\u53d6\u989c\u8272\u7684\u6700\u5927\u503c*/
MaxX = getmaxx(); /* \u8bfb\u5c4f\u5e55\u5c3a\u5bf8 */
MaxY = getmaxy(); /* \u8bfb\u5c4f\u5e55\u5c3a\u5bf8 */
getaspectratio( &xasp, &yasp ); /* \u62f7\u8d1d\u7eb5\u6a2a\u6bd4\u5230\u53d8\u91cf\u4e2d*/
AspectRatio = (double)xasp/(double)yasp;/* \u8ba1\u7b97\u7eb5\u6a2a\u6bd4\u503c*/
}
/*\u8ba1\u7b97\u5668\u51fd\u6570*/
void computer(void)
{
struct viewporttype vp; /*\u5b9a\u4e49\u89c6\u53e3\u7c7b\u578b\u53d8\u91cf*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*\u64cd\u4f5c\u6570\u548c\u8ba1\u7b97\u7ed3\u679c\u53d8\u91cf*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* \u5b9a\u4e49\u5b57\u7b26\u4e32\u5728\u6309\u94ae\u56fe\u5f62\u4e0a\u663e\u793a\u7684\u7b26\u53f7 */
mwindow( "Calculator" ); /* \u663e\u793a\u4e3b\u7a97\u53e3 */
color = 7; /*\u8bbe\u7f6e\u7070\u989c\u8272\u503c*/
getviewsettings( &vp ); /* \u8bfb\u53d6\u5f53\u524d\u7a97\u53e3\u7684\u5927\u5c0f*/
width=(vp.right+1)/10; /* \u8bbe\u7f6e\u6309\u94ae\u5bbd\u5ea6 */
height=(vp.bottom-10)/10 ; /*\u8bbe\u7f6e\u6309\u94ae\u9ad8\u5ea6 */
x = width /2; /*\u8bbe\u7f6ex\u7684\u5750\u6807\u503c*/
y = height/2; /*\u8bbe\u7f6ey\u7684\u5750\u6807\u503c*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*\u753b\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u5f62\u6761\u663e\u793a\u8fd0\u7b97\u6570\u548c\u7ed3\u679c*/
setcolor( color+3 ); /*\u8bbe\u7f6e\u6de1\u7eff\u989c\u8272\u8fb9\u6846\u7ebf*/
rectangle( x+width*2, y, x+7*width, y+height );
/*\u753b\u4e00\u4e2a\u77e9\u5f62\u8fb9\u6846\u7ebf*/
setcolor(RED); /*\u8bbe\u7f6e\u989c\u8272\u4e3a\u7ea2\u8272*/
outtextxy(x+3*width,y+height/2,"0."); /*\u8f93\u51fa\u5b57\u7b26\u4e32"0."*/
x =2*width-width/2; /*\u8bbe\u7f6ex\u7684\u5750\u6807\u503c*/
y =2*height+height/2; /*\u8bbe\u7f6ey\u7684\u5750\u6807\u503c*/
for( j=0 ; j<4 ; ++j ) /*\u753b\u6309\u94ae*/
{
for( i=0 ; i<5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*\u753b\u4e00\u4e2a\u77e9\u5f62\u6761*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
/*\u5c06\u5b57\u7b26\u4fdd\u5b58\u5230str2\u4e2d*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*\u79fb\u52a8\u5217\u5750\u6807*/
}
y +=(height/2)*3; /* \u79fb\u52a8\u884c\u5750\u6807*/
x =2*width-width/2; /*\u590d\u4f4d\u5217\u5750\u6807*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*\u79fb\u52a8\u5149\u6807\u5230x,y\u4f4d\u7f6e*/
arrow(); /*\u663e\u793a\u5149\u6807*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*\u8bbe\u7f6estr2\u4e3a\u7a7a\u4e32*/
while((v=specialkey())!=45) /*\u5f53\u538b\u4e0bAlt+x\u952e\u7ed3\u675f\u7a0b\u5e8f\uff0c\u5426\u5219\u6267\u884c\u4e0b\u9762\u7684\u5faa\u73af*/
{
while((v=specialkey())!=ENTER) /*\u5f53\u538b\u4e0b\u952e\u4e0d\u662f\u56de\u8f66\u65f6*/
{
putimage(x,y,rar,XOR_PUT); /*\u663e\u793a\u5149\u6807\u56fe\u8c61*/
if(v==RIGHT) /*\u53f3\u79fb\u7bad\u5934\u65f6\u65b0\u4f4d\u7f6e\u8ba1\u7b97*/
if(x>=x0+6*width)
/*\u5982\u679c\u53f3\u79fb\uff0c\u79fb\u5230\u5c3e\uff0c\u5219\u79fb\u52a8\u5230\u6700\u5de6\u8fb9\u5b57\u7b26\u4f4d\u7f6e*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*\u5426\u5219\uff0c\u53f3\u79fb\u5230\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4f4d\u7f6e*/
if(v==LEFT) /*\u5de6\u79fb\u7bad\u5934\u65f6\u65b0\u4f4d\u7f6e\u8ba1\u7b97*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} /*\u5982\u679c\u79fb\u5230\u5934\uff0c\u518d\u5de6\u79fb\uff0c\u5219\u79fb\u52a8\u5230\u6700\u53f3\u8fb9\u5b57\u7b26\u4f4d\u7f6e*/
else
{
x=x-width-width/2;
m--;
} /*\u5426\u5219\uff0c\u5de6\u79fb\u5230\u524d\u4e00\u4e2a\u5b57\u7b26\u4f4d\u7f6e*/
if(v==UP) /*\u4e0a\u79fb\u7bad\u5934\u65f6\u65b0\u4f4d\u7f6e\u8ba1\u7b97*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} /*\u5982\u679c\u79fb\u5230\u5934\uff0c\u518d\u4e0a\u79fb\uff0c\u5219\u79fb\u52a8\u5230\u6700\u4e0b\u8fb9\u5b57\u7b26\u4f4d\u7f6e*/
else
{
y=y-height-height/2;
n--;
} /*\u5426\u5219\uff0c\u79fb\u5230\u4e0a\u8fb9\u4e00\u4e2a\u5b57\u7b26\u4f4d\u7f6e*/
if(v==DOWN) /*\u4e0b\u79fb\u7bad\u5934\u65f6\u65b0\u4f4d\u7f6e\u8ba1\u7b97*/
if(y>=7*height)
{
y=y0;
n=0;
} /*\u5982\u679c\u79fb\u5230\u5c3e\uff0c\u518d\u4e0b\u79fb\uff0c\u5219\u79fb\u52a8\u5230\u6700\u4e0a\u8fb9\u5b57\u7b26\u4f4d\u7f6e*/
else
{
y=y+height+height/2;
n++;
} /*\u5426\u5219\uff0c\u79fb\u5230\u4e0b\u8fb9\u4e00\u4e2a\u5b57\u7b26\u4f4d\u7f6e*/
putimage(x,y,rar,XOR_PUT); /*\u5728\u65b0\u7684\u4f4d\u7f6e\u663e\u793a\u5149\u6807\u7bad\u5934*/
}
c=str1[n*5+m]; /*\u5c06\u5b57\u7b26\u4fdd\u5b58\u5230\u53d8\u91cfc\u4e2d*/
if(isdigit(c)||c=='.') /*\u5224\u65ad\u662f\u5426\u662f\u6570\u5b57\u6216\u5c0f\u6570\u70b9*/
{
if(flag==-1) /*\u5982\u679c\u6807\u5fd7\u4e3a-1\uff0c\u8868\u660e\u4e3a\u8d1f\u6570*/
{
strcpy(str2,"-"); /*\u5c06\u8d1f\u53f7\u8fde\u63a5\u5230\u5b57\u7b26\u4e32\u4e2d*/
flag=1;
} /*\u5c06\u6807\u5fd7\u503c\u6062\u590d\u4e3a1*/
sprintf(temp,"%c",c); /*\u5c06\u5b57\u7b26\u4fdd\u5b58\u5230\u5b57\u7b26\u4e32\u53d8\u91cftemp\u4e2d*/
strcat(str2,temp); /*\u5c06temp\u4e2d\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u5230str2\u4e2d*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='+')
{
num1=atof(str2); /*\u5c06\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=1; /*\u505a\u8ba1\u7b97\u52a0\u6cd5\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='-')
{
if(strcmp(str2,"")==0) /*\u5982\u679cstr2\u4e3a\u7a7a\uff0c\u8bf4\u660e\u662f\u8d1f\u53f7\uff0c\u800c\u4e0d\u662f\u51cf\u53f7*/
flag=-1; /*\u8bbe\u7f6e\u8d1f\u6570\u6807\u5fd7*/
else
{
num1=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=2; /*\u505a\u8ba1\u7b97\u51cf\u6cd5\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*\u753b\u77e9\u5f62*/
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
}
if(c=='*')
{
num1=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=3; /*\u505a\u8ba1\u7b97\u4e58\u6cd5\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='/')
{
num1=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=4; /*\u505a\u8ba1\u7b97\u9664\u6cd5\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='^')
{
num1=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=5; /*\u505a\u8ba1\u7b97\u4e58\u65b9\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3); /*\u8bbe\u7f6e\u7528\u6de1\u7eff\u8272\u5b9e\u4f53\u586b\u5145*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*\u753b\u77e9\u5f62*/
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='%')
{
num1=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
act=6; /*\u505a\u8ba1\u7b97\u6a21\u8fd0\u7b97\u4e58\u65b9\u6807\u5fd7\u503c*/
setfillstyle(SOLID_FILL,color+3); /*\u8bbe\u7f6e\u7528\u6de1\u7eff\u8272\u5b9e\u4f53\u586b\u5145*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*\u753b\u77e9\u5f62*/
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='=')
{
num2=atof(str2); /*\u5c06\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u6570\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570*/
switch(act) /*\u6839\u636e\u8fd0\u7b97\u7b26\u53f7\u8ba1\u7b97*/
{
case 1:result=num1+num2;break; /*\u505a\u52a0\u6cd5*/
case 2:result=num1-num2;break; /*\u505a\u51cf\u6cd5*/
case 3:result=num1*num2;break; /*\u505a\u4e58\u6cd5*/
case 4:result=num1/num2;break; /*\u505a\u9664\u6cd5*/
case 5:result=pow(num1,num2);break; /*\u505ax\u7684y\u6b21\u65b9*/
case 6:result=fmod(num1,num2);break; /*\u505a\u6a21\u8fd0\u7b97*/
}
setfillstyle(SOLID_FILL,color+3); /*\u8bbe\u7f6e\u7528\u6de1\u7eff\u8272\u5b9e\u4f53\u586b\u5145*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*\u8986\u76d6\u7ed3\u679c\u533a*/
sprintf(temp,"%f",result); /*\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230temp\u4e2d*/
outtextxy(5*width,height,temp); /*\u663e\u793a\u7ed3\u679c*/
}
if(c=='c')
{
num1=0; /*\u5c06\u4e24\u4e2a\u64cd\u4f5c\u6570\u590d\u4f4d0\uff0c\u7b26\u53f7\u6807\u5fd7\u4e3a1*/
num2=0;
flag=1;
strcpy(str2,""); /*\u5c06str2\u6e05\u7a7a*/
setfillstyle(SOLID_FILL,color+3); /*\u8bbe\u7f6e\u7528\u6de1\u7eff\u8272\u5b9e\u4f53\u586b\u5145*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*\u8986\u76d6\u7ed3\u679c\u533a*/
outtextxy(5*width,height,"0."); /*\u663e\u793a\u5b57\u7b26\u4e32*/
}
if(c=='Q')exit(0); /*\u5982\u679c\u9009\u62e9\u4e86q\u56de\u8f66\uff0c\u7ed3\u675f\u8ba1\u7b97\u7a0b\u5e8f*/
}
putimage(x,y,rar,XOR_PUT); /*\u5728\u9000\u51fa\u4e4b\u524d\u6d88\u53bb\u5149\u6807\u7bad\u5934*/
return; /*\u8fd4\u56de*/
}
/*\u7a97\u53e3\u51fd\u6570*/
void mwindow( char *header )
{
int height;
cleardevice(); /* \u6e05\u9664\u56fe\u5f62\u5c4f\u5e55 */
setcolor( MaxColors - 1 ); /* \u8bbe\u7f6e\u5f53\u524d\u989c\u8272\u4e3a\u767d\u8272*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* \u8bbe\u7f6e\u89c6\u53e3\u5927\u5c0f */
height = textheight( "H" ); /* \u8bfb\u53d6\u57fa\u672c\u6587\u672c\u5927\u5c0f */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*\u8bbe\u7f6e\u6587\u672c\u6837\u5f0f*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*\u8bbe\u7f6e\u5b57\u7b26\u6392\u5217\u65b9\u5f0f*/
outtextxy( MaxX/4, 2, header ); /*\u8f93\u51fa\u6807\u9898*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*\u8bbe\u7f6e\u89c6\u53e3\u5927\u5c0f*/
drawboder(); /*\u753b\u8fb9\u6846*/
}
void drawboder(void) /*\u753b\u8fb9\u6846*/
{
struct viewporttype vp; /*\u5b9a\u4e49\u89c6\u53e3\u7c7b\u578b\u53d8\u91cf*/
setcolor( MaxColors - 1 ); /*\u8bbe\u7f6e\u5f53\u524d\u989c\u8272\u4e3a\u767d\u8272 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*\u8bbe\u7f6e\u753b\u7ebf\u65b9\u5f0f*/
getviewsettings( &vp );/*\u5c06\u5f53\u524d\u89c6\u53e3\u4fe1\u606f\u88c5\u5165vp\u6240\u6307\u7684\u7ed3\u6784\u4e2d*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*\u753b\u77e9\u5f62\u8fb9\u6846*/
}
/*\u8bbe\u8ba1\u9f20\u6807\u56fe\u5f62\u51fd\u6570*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*\u5b9a\u4e49\u591a\u8fb9\u5f62\u5750\u6807*/
setfillstyle(SOLID_FILL,2); /*\u8bbe\u7f6e\u586b\u5145\u6a21\u5f0f*/
fillpoly(8,raw); /*\u753b\u51fa\u4e00\u5149\u6807\u7bad\u5934*/
size=imagesize(4,4,16,16); /*\u6d4b\u8bd5\u56fe\u8c61\u5927\u5c0f*/
rar=malloc(size); /*\u5206\u914d\u5185\u5b58\u533a\u57df*/
getimage(4,4,16,16,rar); /*\u5b58\u653e\u5149\u6807\u7bad\u5934\u56fe\u8c61*/
putimage(4,4,rar,XOR_PUT); /*\u6d88\u53bb\u5149\u6807\u7bad\u5934\u56fe\u8c61*/
return 0;
}
/*\u6309\u952e\u51fd\u6570*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*\u7b49\u5f85\u952e\u76d8\u8f93\u5165*/
key=bioskey(0); /*\u952e\u76d8\u8f93\u5165*/
key=key&0xff? key&0xff:key>>8; /*\u53ea\u53d6\u7279\u6b8a\u952e\u7684\u626b\u63cf\u503c\uff0c\u5176\u4f59\u4e3a0*/
return(key); /*\u8fd4\u56de\u952e\u503c*/
}

#include
#include

typedef struct Node
{
int data;
struct Node *next;
}Node;//\u94fe\u8868\u7ed3\u70b9

typedef struct LinkList//\u5b9a\u4e49\u94fe\u8868\u6570\u636e\u7c7b\u578b
{
Node *head;//\u8868\u5934\u7ed3\u70b9
void PrintMform()//\u8f93\u51fa\u591a\u9879\u5f0f
{
Node *p=head->next;
int exp = head->data-1;
while(p!=NULL)
{
if(exp==head->data-1)
printf("%dX^%d",p->data,exp);
else
{
if(p->data>0)
printf("+%dX^%d",p->data,exp);
if(p->data==0)
printf("+0");
if(p->data<
0)
printf("%dX^%d",p->data,exp);
}
exp--;
p=p->next;
}
printf("\n");
}
void CreateByInput(int length)//\u901a\u8fc7\u8f93\u5165\u5143\u7d20\u5efa\u7acb\u94fe\u8868
{
head=(Node *)malloc(sizeof(Node));
head->data=0;//\u8868\u5934\u8282\u70b9\u5b58\u50a8\u94fe\u8868\u771f\u5b9e\u957f\u5ea6
head->next=NULL;
int i,temp;
Node *p,*cur = head;
for(i=1;i<=length;i++)
{
printf("structing LinkList,Please input the value:\n");
scanf("%d",&temp);
p = (Node *)malloc(sizeof(Node));
p->data=temp;
p->next=cur->next;
cur->next=p;
cur = cur->next;
head->data++;

}
}
}LinkList;

void main()
{
LinkList L1,L2;
int length;//\u5c31\u662f\u591a\u9879\u5f0f\u7684\u9879\u6570
printf("Please input the first LinkList's length:\n");
scanf("%d",&length);
printf("begin to struct the first LinkList\n");//\u5f00\u59cb\u6784\u9020\u7b2c\u4e00\u4e2a\u591a\u9879\u5f0f
L1.CreateByInput(length);
printf("begin to struct the second LinkList\n");//\u5f00\u59cb\u6784\u9020\u7b2c\u4e8c\u4e2a\u591a\u9879\u5f0f
L2.CreateByInput(length);
printf("the first duoxiangshi is:\n");
L1.PrintMform();//\u8f93\u51fa\u7b2c\u4e00\u4e2a\u591a\u9879\u5f0f
printf("the second duoxiangshi is:\n");
L2.PrintMform();//\u8f93\u51fa\u7b2c\u4e8c\u4e2a\u591a\u9879\u5f0f
Node *p = L1.head->next;/////////////////\u4ece\u8fd9\u91cc\u5f00\u59cb
Node *q = L2.head->next;//\u662f\u8ba1\u7b97\u591a\u9879\u5f0fL1-L2,\u7ed3\u679c\u5b58\u5165L1
while(p!=NULL)
{
p->data-=q->data;
p=p->next;
q=q->next;
}/////////////////////////////////////\u5230\u8fd9\u91cc\u7ed3\u675f
printf("the substract is:\n");
L1.PrintMform();
system("pause");
}

  1. 一元多项式简单的计算器的功能:
    1)输入并建立多项式;
    2)输出多项式;
    3)两个多项式相加,输出和多项式;
    4)两个多项式相减,输出差多项式。

  2. 例程

    #include <dos.h> /*DOS接口函数*/
    #include <math.h> /*数学函数的定义*/
    #include <conio.h> /*屏幕操作函数*/
    #include <stdio.h> /*I/O函数*/
    #include <stdlib.h> /*库函数*/
    #include <stdarg.h> /*变量长度参数表*/
    #include <graphics.h> /*图形函数*/
    #include <string.h> /*字符串函数*/
    #include <ctype.h> /*字符操作函数*/
    #define UP 0x48 /*光标上移键*/
    #define DOWN 0x50 /*光标下移键*/
    #define LEFT 0x4b /*光标左移键*/
    #define RIGHT 0x4d /*光标右移键*/
    #define ENTER 0x0d /*回车键*/
    void *rar; /*全局变量,保存光标图象*/
    struct palettetype palette; /*使用调色板信息*/
    int GraphDriver; /* 图形设备驱动*/
    int GraphMode; /* 图形模式值*/
    int ErrorCode; /* 错误代码*/
    int MaxColors; /* 可用颜色的最大数值*/
    int MaxX, MaxY; /* 屏幕的最大分辨率*/
    double AspectRatio; /* 屏幕的像素比*/
    void drawboder(void); /*画边框函数*/
    void initialize(void); /*初始化函数*/
    void computer(void); /*计算器计算函数*/
    void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/
    void mwindow(char *header); /*窗口函数*/
    int specialkey(void) ; /*获取特殊键函数*/
    int arrow(); /*设置箭头光标函数*/
    /*主函数*/
    int main()
    {
    initialize();/* 设置系统进入图形模式 */
    computer(); /*运行计算器 */
    closegraph();/*系统关闭图形模式返回文本模式*/
    return(0); /*结束程序*/
    }
    /* 设置系统进入图形模式 */
    void initialize(void)
    {
    int xasp, yasp; /* 用于读x和y方向纵横比*/
    GraphDriver = DETECT; /* 自动检测显示器*/
    initgraph( &GraphDriver, &GraphMode, "" );
    /*初始化图形系统*/
    ErrorCode = graphresult(); /*读初始化结果*/
    if( ErrorCode != grOk ) /*如果初始化时出现错误*/
    {
    printf("Graphics System Error: %s
    ",
    grapherrormsg( ErrorCode ) ); /*显示错误代码*/
    exit( 1 ); /*退出*/
    }
    getpalette( &palette ); /* 读面板信息*/
    MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/
    MaxX = getmaxx(); /* 读屏幕尺寸 */
    MaxY = getmaxy(); /* 读屏幕尺寸 */
    getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/
    AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/
    }
    /*计算器函数*/
    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; /*操作数和计算结果变量*/
    char cnum[5],str2[20]={""},c,temp[20]={""};
    char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */
    mwindow( "Calculator" ); /* 显示主窗口 */
    color = 7; /*设置灰颜色值*/
    getviewsettings( &vp ); /* 读取当前窗口的大小*/
    width=(vp.right+1)/10; /* 设置按钮宽度 */
    height=(vp.bottom-10)/10 ; /*设置按钮高度 */
    x = width /2; /*设置x的坐标值*/
    y = height/2; /*设置y的坐标值*/
    setfillstyle(SOLID_FILL, color+3);
    bar( x+width*2, y, x+7*width, y+height );
    /*画一个二维矩形条显示运算数和结果*/
    setcolor( color+3 ); /*设置淡绿颜色边框线*/
    rectangle( x+width*2, y, x+7*width, y+height );
    /*画一个矩形边框线*/
    setcolor(RED); /*设置颜色为红色*/
    outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/
    x =2*width-width/2; /*设置x的坐标值*/
    y =2*height+height/2; /*设置y的坐标值*/
    for( j=0 ; j<4 ; ++j ) /*画按钮*/
    {
    for( i=0 ; i<5 ; ++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) ; /*移动列坐标*/
    }
    y +=(height/2)*3; /* 移动行坐标*/
    x =2*width-width/2; /*复位列坐标*/
    }
    x0=2*width;
    y0=3*height;
    x=x0;
    y=y0;
    gotoxy(x,y); /*移动光标到x,y位置*/
    arrow(); /*显示光标*/
    putimage(x,y,rar,XOR_PUT);
    m=0;
    n=0;
    strcpy(str2,""); /*设置str2为空串*/
    while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/
    {
    while((v=specialkey())!=ENTER) /*当压下键不是回车时*/
    {
    putimage(x,y,rar,XOR_PUT); /*显示光标图象*/
    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); /*在新的位置显示光标箭头*/
    }
    c=str1[n*5+m]; /*将字符保存到变量c中*/
    if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/
    {
    if(flag==-1) /*如果标志为-1,表明为负数*/
    {
    strcpy(str2,"-"); /*将负号连接到字符串中*/
    flag=1;
    } /*将标志值恢复为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); /*将第一个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=1; /*做计算加法标志值*/
    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为空,说明是负号,而不是减号*/
    flag=-1; /*设置负数标志*/
    else
    {
    num1=atof(str2); /*将第二个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=2; /*做计算减法标志值*/
    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); /*将第二个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=3; /*做计算乘法标志值*/
    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); /*将第二个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=4; /*做计算除法标志值*/
    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); /*将第二个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=5; /*做计算乘方标志值*/
    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); /*将第二个操作数转换为浮点数*/
    strcpy(str2,""); /*将str2清空*/
    act=6; /*做计算模运算乘方标志值*/
    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=='=')
    {
    num2=atof(str2); /*将第二个操作数转换为浮点数*/
    switch(act) /*根据运算符号计算*/
    {
    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); /*设置用淡绿色实体填充*/
    bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/
    sprintf(temp,"%f",result); /*将结果保存到temp中*/
    outtextxy(5*width,height,temp); /*显示结果*/
    }
    if(c=='c')
    {
    num1=0; /*将两个操作数复位0,符号标志为1*/
    num2=0;
    flag=1;
    strcpy(str2,""); /*将str2清空*/
    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=='Q')exit(0); /*如果选择了q回车,结束计算程序*/
    }
    putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/
    return; /*返回*/
    }
    /*窗口函数*/
    void mwindow( char *header )
    {
    int height;
    cleardevice(); /* 清除图形屏幕 */
    setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*/
    setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 设置视口大小 */
    height = textheight( "H" ); /* 读取基本文本大小 */
    settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/
    settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/
    outtextxy( MaxX/4, 2, header ); /*输出标题*/
    setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/
    drawboder(); /*画边框*/
    }
    void drawboder(void) /*画边框*/
    {
    struct viewporttype vp; /*定义视口类型变量*/
    setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */
    setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/
    getviewsettings( &vp );/*将当前视口信息装入vp所指的结构中*/
    rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*画矩形边框*/
    }
    /*设计鼠标图形函数*/
    int arrow()
    {
    int size;
    int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定义多边形坐标*/
    setfillstyle(SOLID_FILL,2); /*设置填充模式*/
    fillpoly(8,raw); /*画出一光标箭头*/
    size=imagesize(4,4,16,16); /*测试图象大小*/
    rar=malloc(size); /*分配内存区域*/
    getimage(4,4,16,16,rar); /*存放光标箭头图象*/
    putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/
    return 0;
    }
    /*按键函数*/
    int specialkey(void)
    {
    int key;
    while(bioskey(1)==0); /*等待键盘输入*/
    key=bioskey(0); /*键盘输入*/
    key=key&0xff? key&0xff:key>>8; /*只取特殊键的扫描值,其余为0*/
    return(key); /*返回键值*/
    }


编了两天,希望楼主打赏点财富值

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct polylink
{
char ch; /*变量名*/
float coef; /* 系数域*/
int expn; /* 指数域*/
struct polylink *next; /* 链域*/
};
/*初始化链表*/
polylink *initpoly()
{
polylink *p;
p = (polylink *)malloc(sizeof(polylink));
if(p == NULL)
{
printf("memory error!");
exit(0);
}
p->next = NULL;
return p;
}
/*销毁链表*/
void destroy_link(polylink *p)
{
polylink *q;
while(p != NULL)
{
q = p;
p = p->next;
free(q);
}
}
/*将多项式存入链表*/
polylink * creat_ploy(polylink *p)
{
char c[1];
int i = 0, n=0;
polylink *p1;
p = initpoly();
printf("input variable's name 输入变量名:\n");
scanf("%s", c);
printf("input the polynomial's number 输入多项式项数:\n");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
p1 = (polylink *)malloc(sizeof(polylink));
if(p1 == NULL)/*分配不成功时*/
{
printf("memory error!");
exit(0);
}
printf("input the %dst's coefficient 输入第%d项的系数:\n", i+1, i+1);
scanf("%f", &p1->coef);
printf("input the variable's index 输入变量的指数:\n");
scanf("%d", &p1->expn);
p1->ch = c[0];
p1->next = p->next;
p->next = p1;
}
return p;
}
/*删除节点函数*/
polylink *delnode(polylink * h , polylink * maxp) /*删除一个节点,返回剩下的链表首地址*/
{
polylink *t;
t = h;
while(t->next != maxp )
t=t->next; /*找到maxp 的前节点t */
t->next = maxp->next ; /*删除maxp,maxp后面的接到t后面*/
maxp->next = NULL;
return h; /*链首依然是h ,返回 */
}
/*求表长函数*/
void link_lenth(polylink *p, int &a)/* &是引用*/
{
while(p->next != NULL)
{
p = p->next;
a++;
}
}

/*排序*/
polylink *sort(polylink *h)
{
int min; /*保存指数最小的值*/
polylink *t=NULL, *minp=NULL, *head=NULL;
if(h ->next == NULL)
{
printf("don't exist number");
exit(0);
}
while(h->next !=NULL) /*只要当前链中不为空就循环 */
{
t= h->next; // t :临时指针
min=t->expn; minp=t; /*把当前t中的值作为最小*/
while (t->next !=NULL) /*只要t后面还有节点就循环*/
{
t=t->next ; /*t往后移动一个*/
if (t->expn < min) /*如果t中的值大于maxn,则记下其值和位置*/
{
min = t->expn;
minp=t;
}
} /*找出当前头开始在链中最大节点 maxp*/
h = delnode(h, minp); /*删除minp节点,返回剩下的链表*/
minp->next=head; /*将每次的最小节点接在头结点之前*/
head = minp; /*head 重新回到该链的开头*/
}
h->next = head; /*将head 接在头节点之后*/
return h;
}
/*对一元多项式求导*/
polylink *poly_derivation(polylink *p)
{
polylink *n, *q;
q= p;
if(p->next == NULL)
{
printf("polynomial don't exist!\n");
}
while(p->next != NULL)
{
p = p->next;
if(p->expn == 0 || p->coef == 0) /*指数为零或者系数为零就删除*/
{
q = delnode(q, p);
}
}
n = q; /*保存删除无用节点的链表之后的头结点, 用于返回*/
if(q->next == NULL)
{
printf("polynomial don't exist!\n");
}
while(q->next != NULL)
{
q = q->next;
q->coef = q->coef * q->expn; /*系数乘指数*/
q->expn = q->expn - 1; /*指数减一*/
}
return n;
}
/*显示多项式*/
void display_poly(polylink *p)
{
int a;
p = sort(p); /*排序*/
while(p->next != NULL )
{
p = p->next;
a = 1;
if(p->coef < 0) a = 0;
a ? printf(" +"): printf(" ");
printf("%.1f%c^%d", p->coef, p->ch, p->expn);
}
printf("\n");
}

/*查找函数*/
polylink *locate_link(polylink *p, int index)
{

p = p->next;
while(p != NULL && p->expn != index)
p = p->next;
if(p == NULL)
return NULL;
else
return p;
}
/*为了代码重用*/
void creat(polylink *&p1, polylink *&p2)
{
p1 = creat_ploy(p1); /*创建多项式*/
printf("another polynomial building:\n");
p2 = creat_ploy(p2); /*创建多项式*/
printf("first :\n");
display_poly(p1); /*显示创建的多项式*/
printf("the second:\n");
display_poly(p2); /*显示创建的多项式*/
}
/*多项式相加*/
polylink * poly_add(polylink *head1, polylink *head2, char a)
{
char ch[1];
polylink *p1, *p2, *p, *r;
p1 = head1;
p2 = head2;
if(a == '+') /*其他函数调用时不执行*/
{
system("cls");
creat(p1, p2); /*创建p1 p2*/
}
r = p1;
while(r->next != NULL)
{
p1 = r->next;
r = delnode(r, p1); /*删除p1*/
p = locate_link(p2, p1->expn); /*查找p2中是否有 系数等于p1->expn的节点,有就返回该点,否则返回NULL*/
if(p != NULL)
p->coef = p->coef + p1->coef;
else /*把节点p1插入到链表p2*/
{

p1->next = p2->next;
p2->next = p1;
}
}
if(a == '+') /*被减或者乘运算时不调用*/
{
printf("add result:\n");
display_poly(p2); /*显示*/
printf("whether to derivat:y/n\n");
scanf("%s", ch);
if( ch[0] == 'Y' || ch[0] == 'y')
{
p2 = poly_derivation(p2);
display_poly(p2);
destroy_link(p2); /*销毁链表*/
}
else
destroy_link(p2);/*销毁链表*/
getchar();/*让屏幕显示停在运算结果*/
return NULL;
}
return p2;
}
/*多项式相减*/
void poly_sub(polylink *p1, polylink *p2)
{
char ch[1];
polylink *n, *p;
system("cls"); /*清屏*/
creat(p1, p2);
p = p2;
while(p->next != NULL)
{
p = p->next;
p->coef = -1 * p->coef;
}
printf("\nsub 1st - 2st\n");
n = poly_add(p1, p2, '-'); /*调用加运算函数*/
display_poly(n);
printf("whether to derivat:y/n\n");
scanf("%s", ch);
if( ch[0] == 'Y' || ch[0] == 'y')
{
n = poly_derivation(n); /*求导*/
display_poly(n);
destroy_link(n);
}
else destroy_link(n);
getchar();
}
/*多项式相乘*/
polylink *multiply(polylink *p, float coef, int index)
{ /*传入系数和指数,分别与p的每个节点运算 最后返回该链*/
polylink *q, *n;
n = (polylink *)malloc(sizeof(polylink));
n->next = NULL; /*n作为头节点*/
while(p->next != NULL)
{
p = p->next;
q = (polylink *)malloc(sizeof(polylink));
q->ch = p->ch;
q->coef = coef * p->coef;
q->expn = index + p->expn;
q->next = n->next;
n->next = q;
}
return n;
}
/*多项式相乘所用,另外为了提高代码重用性*/
void run_mul(polylink *p1, polylink *p2, int num)
{
int i = 0;
char ch[1];
polylink **p, *q; /*二维指针存每次multiply()函数返回的链表*/
p = (polylink **)malloc( num *sizeof(polylink));
while(p1->next != NULL)
{
p1 = p1->next;
p[i++] = multiply(p2, p1->coef, p1->expn);
}
q = poly_add(p[0], p[1], '*');
for(i = 2; i < num; i++)
{
q = poly_add(q, p[i], '*');
}
printf("mul result:\n");
display_poly(q);
printf("whether to derivat:y/n\n");
scanf("%s", ch);
if( ch[0] == 'Y' || ch[0] == 'y')
{
q = poly_derivation(q);
display_poly(q);
destroy_link(q);
}
else destroy_link(q);
getchar();
}
void poly_mul(polylink *p1, polylink *p2)
{
int num1 =0 , num2 = 0; /*用于存放p1 ,p2的表长*/
system("cls");
creat(p1, p2);
link_lenth(p1, num1);
link_lenth(p2, num2);
if(num2 >= num1) /*根据链表*/
{
run_mul(p1, p2, num1);
}
else
{
run_mul(p2, p1, num2);
}
}
/*选择函数*/
int menu_select()
{
int n;
printf("press enter key to enter the menu......"); /*按任一键进入主菜单*/
getchar(); /*从键盘读取一个字符,但不显示于屏幕*/
system("cls"); /*清屏*/
printf("\n\n\n\n\n\n");
printf("\t\t\tIt just for a variables\n\n");
printf("\t*************************************MENU***********************\n\n");
printf("\t\t\t1. 多项式相加 add operation\n\n");
printf("\t\t\t2. 多项式相减 sub operation\n\n"); /*显示*/
printf("\t\t\t3. 多项式相乘 mul operation\n\n"); /*寻找*/
printf("\t\t\t4. 退出 Quit\n\n"); /*删除*/
printf("\t****************************************************************\n\n");
do{
printf("\n\t输入你的选择Enter your choice(1~4):");
scanf("%d",&n);
}while(n < 1 || n > 4); /*如果选择项不在1~4之间则重输*/
return n; /*返回选择项,主函数根据该数调用相应的函数*/
}
void main()
{
polylink *head1=NULL, *head2=NULL, *head3=NULL; /*head3的作用不是很大*/

for(;;) /*循环无限次*/
{
switch( menu_select() )
{
case 1: head3 = poly_add(head1, head2, '+'); break;
case 2: poly_sub(head1, head2); break;
case 3: poly_mul(head1, head2); break;
case 4: exit(0); /*如菜单返回值为4则程序结束*/
}
}
}

  • 鏁版嵁缁撴瀯璇剧▼璁捐(C璇█鐗) 涓鍏绋鐤澶氶」寮鐨勮繍绠
    绛旓細鈶 杈撳嚭澶氶」寮锛岃緭鍑哄舰寮忎负鏁存暟搴忓垪锛歯锛宑1锛宔1锛宑2锛宔2鈥︹n锛宔n锛屽叾涓璶鏄椤瑰紡鐨勯」鏁帮紝ci锛宔i鍒嗗埆涓虹i椤圭殑绯绘暟鍜屾寚鏁般傚簭鍒楁寜鎸囨暟闄嶅簭鎺掑垪锛涒懚 澶氶」寮廇鍜孊鐩稿姞锛屽缓绔嬪椤瑰紡A+B锛岃緭鍑虹浉鍔犵殑澶氶」寮忥紱鈶 澶氶」寮廇鍜孊鐩稿噺锛屽缓绔嬪椤瑰紡A-B锛岃緭鍑虹浉鍑忕殑澶氶」寮忥紱鈶 璁捐涓涓彍鍗曪紝涓婅堪鎿嶄綔瑕佹眰鐨...
  • C璇█鐢ㄩ摼琛瀹炵幇涓鍏冨椤瑰紡鐨勭浉鍔
    绛旓細C璇█浠g爜锛歩nclude "stdio.h"#include "malloc.h"/* 閾捐〃缁撶偣缁撴瀯 */typedef struct LNode{ double coef; /* 绯绘暟 */ int exp; /* 鎸囨暟 */ struct LNode *next;}LNode;/* 鍒濆鍖栭摼琛 */LNode *Init(){ LNode *head = (LNode*)malloc(sizeof(LNode)); hea...
  • 涓鍏绋鐤澶氶」寮璁$畻鍣 C璇█缂栧啓
    绛旓細datastruct.h typedef struct list { int c; //澶氶」寮鐨勯」鏁 int e; //澶氶」寮忕殑鎸囨暟 struct list *next; //涓嬩竴缁撶偣 };typedef struct list *LinkList;typedef struct list Node;涓嬮潰鏄嚎鎬ц〃鐨勬搷浣滅浉鍏冲嚱鏁板0鏄庯紝瀵瑰簲鏂囦欢ListOper.h://File: ListOper.h ifndef DATASTRUCT define DATA...
  • 璁捐涓涓涓鍏冨椤瑰紡绠鍗鐨勮绠楀櫒(鏁版嵁缁撴瀯C璇█鐗)鎬
    绛旓細Polyn CreatePolyn(Polyn head,int m){//寤虹珛涓涓ご鎸囬拡涓篽ead銆侀」鏁颁负m鐨涓鍏冨椤瑰紡 int i;Polyn p;p=head=(Polyn)malloc(sizeof(struct Polynomial));head->next=NULL;for(i=0;i<m;i++){ p=(Polyn)malloc(sizeof(struct Polynomial));//寤虹珛鏂扮粨鐐逛互鎺ユ敹鏁版嵁 printf("璇疯緭鍏ョ%d椤圭殑...
  • 鏁版嵁缁撴瀯(C璇█)鐢ㄥ崟閾捐〃瀛樺偍涓鍏冨椤瑰紡,骞瀹炵幇涓や釜澶氶」寮忕殑鐩稿姞...
    绛旓細鏁版嵁缁撴瀯(c璇█)鐢ㄥ崟閾捐〃瀛樺偍涓鍏冨椤瑰紡,骞瀹炵幇涓や釜澶氶」寮忕殑鐩稿姞杩愮畻銆愭渶鏂般戦槄璇伙細1037娆 椤垫暟锛36椤 2016-03-21 涓炬姤 include<stdio.h> include<stdlib.h> include<malloc.h> typedef int ElemType;/*鍗曢」閾捐〃鐨勫0鏄*/ typedef struct PolynNode{ int coef; // 绯绘暟 int expn; // ...
  • 璇锋暀楂樻墜:鐢╟璇█瀹炵幇涓涓涓鍏冨椤瑰紡鐨勭浉鍔,娆℃暟鍜岀郴鏁板彲鐢辩敤鎴疯嚜宸辫緭...
    绛旓細浣跨敤椤哄簭琛紝鍏朵腑鏁扮粍鏀剧郴鏁帮紝鏁扮粍涓嬫爣鐢ㄦ寚鏁拌〃绀衡︹︿互涓嬫槸渚嬪瓙锛岃嚦浜庢暟缁勭殑鏁板间綘鑷繁鏀规垚鐢ㄦ埛杈撳叆 / 宸茬煡f(x)=8x^(6)+5x^(5)-10^(4)+32x^(2)-x+10;g(x)=7x^(5)+10x^(4)-20^x(3)-10^x(2)+x;姹俧(x)+g(x);/ include <stdio.h> define MAXLEN 100 typedef struct ...
  • C璇█闂,灏嗕竴涓涓鍏冨椤瑰紡鎸夋寚鏁颁粠灏忓埌澶х殑椤哄簭瀛樺叆鍗曢」閾捐〃涓(骞...
    绛旓細include <stdio.h>#include <stdlib.h>#include <math.h>typedef struct {double coef;int exp;}Term;typedef struct node {Term data;struct node *next;}Pnode,*Plink;void create(Plink *H);void charu(Plink H,Term x);void shuchu(Plink H);void InsertNode(Plink head,Term x) {Plink...
  • 璋佽兘甯垜鐢╟璇█甯垜鍐欎竴涓涓鍏冨椤瑰紡绋嬪簭銆
    绛旓細鎴戣寰楁暟鎹粨鏋勪功涓婃湁婧愪唬鐮佺殑鍟...杩樻槸甯綘鍐欎簡涓涓,鏁存暣涓涓皬鏃跺晩,绱鎴戜簡 include <stdio.h> include <stdlib.h> include <assert.h> typedef struct _List List;struct _List { int c;int e;List* next;};define OPER_PLUS 0 define OPER_MINUS 1 static List* list_oper(List*...
  • 鐢–璇█瀹炵幇鏁版嵁缁撴瀯鐨勯鐩:涓鍏冨椤瑰紡鐩稿姞
    绛旓細struct poly *jisuan(struct poly *head1,struct poly *head2) //澶氶」寮鐨勭浉鍔 { struct poly *p1,*p2,*r1,*r2;r1=head1;p1=head1->next;r2=p2=head2->next;while(p1&&p2){ if(p1->zhi==p2->zhi){ p1->xi=(p1->xi)+(p2->xi);p2=p2->next;free(r2);r2=p2;r1=p1;p1=...
  • C璇█ 涓鍏冨椤瑰紡鐨勮绠 鍏充簬x鐨勪竴鍏冨椤瑰紡鍑芥暟鍖栫畝鍣 缁欏畾涓涓叧浜巟...
    绛旓細杩欎綅鏄湪鍒汉鐨勫湴鏂筩opy鏉ョ殑锛岃繖鏄湴鍧锛氬垱寤洪摼琛ㄥごhead->next=NULL;scanf("%f%d",&x,&y);//瀹炵幇鐢ㄦ埛杈撳叆鐨勭涓涓」锛屽寘鎷叾鎸囨暟鍜岀郴鏁皐hile(x!=0)//褰撶敤鎴锋病鏈夎緭鍏ョ粨鏉熸爣蹇0鏃跺彲涓鐩磋緭鍏澶氶」寮鐨勯」锛屼笖杈撳叆涓涓垱寤轰竴涓粨鐐箋input=(PLOYList*)malloc(sizeof(PLOYList));//鍒涘缓鏂伴摼鑺俰nput-...
  • 扩展阅读:编程求1000以内的完数 ... 简单多项式求值c语言 ... 编程多项式相加 ... 多项式求和c语言编程 ... 一元多项式相加c语言 ... c语言两个一元多项式 ... c#编程软件叫什么 ... c语言基础知识入门 ... c语言递推法求多项式值和 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网