c语言多项式相加

Python024

c语言多项式相加,第1张

我这有一个实现加减乘除的多项式程序,自己写的,另外输入形式为:-2x^3 +5x^2+3x+4 即可。

其中百度的现实问题,有一个©A和©B的 应该是&COPYA, &COPYB

去掉中间空格

支持整数多项式加减乘除。

#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

typedef struct _POLYNODE{

int coef//系数

int exp//指数

struct _POLYNODE *next

}polynode,*polyptr

void createPoly(polynode **P, char ch[])//建立多项式链表

void polyAdd(polynode *A,polynode *B)//多项式加

void polyMinus(polynode *A,polynode *B)//减

void polyMulti(polynode *A,polynode *B)//乘

void polyDiv(polynode *A,polynode *B)//除

void order(polynode **P)//排序

void display(polynode *P)//展示多项式

void destroy(polynode **P)//销毁多项式

void menu()//命令菜单

int isPut(char ch[])

//菜单

void menu(){

printf("1.输入多项式.\n"

"2.多项式相加.\n"

"3.多项式相减.\n"

"4.多项式相乘.\n"

"5.多项式相除.\n"

"6.显示多项式.\n"

"7.销毁多项式.\n"

"8.退出.\n")

}

//判断菜单选择

int IsChoice(int choice){

if(0 <choice &&9 >choice)

return 1

else

return 0

}

int isPut(char ch[]){

int i,j = 1

for(i = 0ch[i] != '\0'i++){

{if(0 == j &&'^' == ch[i])

return 0

if('^' == ch[i] &&1 == j)

j = 0

if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) &&0 == j)

j = 1

}

if('.' != ch[i] &&'x' != ch[i] &&'X' != ch[i] &&'^' != ch[i] &&'+' != ch[i] &&'-' != ch[i] &&'*' != ch[i] &&'/' != ch[i] &&!isdigit(ch[i]))

return 0

else{

if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0] || '.' == ch[0])

return 0

if('\0' == ch[i+1] &&'+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == ch[0])

return 0

// 上面是判断字符串首尾是否合格 下面是中间部分

if(0 != i &&ch[i+1] != '\0' ){

if(('X' == ch[i] || 'x' == ch[i]) &&!isdigit(ch[i-1]) &&'+' != ch[i-1] &&'-' != ch[i-1] &&'*' != ch[i-1] &&'/' != ch[i-1] &&'.' != ch[i-1])

return 0

if(('X' == ch[i] || 'x' == ch[i]) &&'^' != ch[i+1] &&'+' != ch[i+1] &&'-' != ch[i+1] &&'*' != ch[i+1] &&'/' != ch[i+1])

return 0

if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == ch[i]) &&!isdigit(ch[i-1]) &&'X' != ch[i-1] &&'x' != ch[i-1] &&!isdigit(ch[i+1]) &&'X' != ch[i+1] &&'x' != ch[i+1])

return 0

if('^' == ch[i] &&'X' != ch[i-1] &&'x' != ch[i-1])

return 0

if('^' == ch[i] &&!isdigit(ch[i+1]))

return 0

if('.' == ch[i] &&!isdigit(ch[i+1]) &&!isdigit(ch[i-1]))

return 0

}

}

}

return 1

}

void createPoly(polynode **P, char ch[]){

char *t = ch

int i = 0, j = 1

int iscoef = 1,isminus = 1

polyptr Q,L

if('-' == ch[0]){

isminus = -1

*t++

}

while('\0' != *t){

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = 1

Q->exp = 0

Q->next = NULL//申请节点,初始化参数为1.

if(-1 == isminus){

Q->coef *= isminus

isminus = 1

}

while('+' != *t &&'-' != *t &&'*' != *t &&'/' != *t &&'\0' != *t){

if('x' != *t &&'X' != *t){

while(isdigit(*t)){

i =((int)*t - 48) + i*10

t++

j *= i

}//抽取数字

if(1 == iscoef &&0 != i){

Q->coef *= i

}

if(0 == iscoef){

Q->exp += i

iscoef = 1

}

//如果i=0,则

}

else{

iscoef = 0

t++

if('^' == *t)

t++

else

Q->exp = 1

}

i = 0

}//while 遍历到加减乘除,则退出循环,到下一新的节点

iscoef = 1

if('\0' != *t){

if('-' == *t)

isminus = -1

t++

}

if(0 == j){

Q->coef = 0

j = 1

}

printf("系数:%d,指数:%d\n",Q->coef,Q->exp)

if(NULL == *P){

*P = Q

}

else{

L->next = Q

}

L = Q

}//while遍历整个字符串

}

void polyAdd(polynode *A,polynode *B){

polyptr P = A, Q,L

polyptr COPYA = NULL,COPYB = NULL

if(NULL == A || NULL == B){

printf("多项式未被建立,请先输入多项表达式.\n")

return

}

while(NULL != P){//复制A

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYA)

COPYA = Q

else

L->next = Q

L = Q

P = P->next

}

P = B

while(NULL != P){//复制B

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYB)

COPYB = Q

else

L->next = Q

L = Q

P = P->next

}

L->next = COPYA//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.

order(©B)

order(©B)

printf("相加结果为:")

display(COPYB)

destroy(©B)

}

void polyMinus(polynode *A,polynode *B){//相减和相加差不多

polyptr P = A, Q,L

polyptr COPYA = NULL,COPYB = NULL

if(NULL == A || NULL == B){

printf("多项式未被建立,请先输入多项表达式.\n")

return

}

while(NULL != P){//复制A

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYA)

COPYA = Q

else

L->next = Q

L = Q

P = P->next

}

P = B

while(NULL != P){//复制B

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = -(P->coef)

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYB)

COPYB = Q

else

L->next = Q

L = Q

P = P->next

}

L->next = COPYA//把COPYA,COPYB两个多项式连接起来,整理一下就OK了.

order(©B)

order(©B)

printf("相减结果为:")

display(COPYB)

destroy(©B)

}

void polyMulti(polynode *A,polynode *B){

polyptr R = A, P = B, Q, L = NULL, T

if(NULL == A || NULL == B){

printf("多项式未被建立,请先输入多项表达式.\n")

return

}

if(0 == A->coef || 0 == B->coef){

printf("多项式乘积为:0\n")

return

}

while(NULL != R){

while(NULL != P){

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef * R->coef

Q->exp = P->exp + R->exp

Q->next = NULL

if(NULL == L)

L = Q

else

T->next = Q

T = Q

P = P->next

}

P = B

R = R->next

}

order(&L)

order(&L)

printf("多项式乘积为:\n")

display(L)

destroy(&L)

}

void polyDiv(polynode *A,polynode *B){//多项式除法

polyptr P = A, Q,L,R,T,C,D

polyptr COPYA = NULL,COPYB = NULL

if(NULL == A || NULL == B){

printf("多项式未被建立,请先输入多项表达式.\n")

return

}

if(A->coef == 0){

printf("0.\n")

return

}

if(B->coef == 0){

printf("除数为0,错误!\n")

return

}

if(A->coef <B->coef){

printf("商:0,余数为:")

display(A)

return

}

while(NULL != P){//复制A

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYA)

COPYA = Q

else

L->next = Q

L = Q

P = P->next

}

P = B

while(NULL != P){//复制B

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = -(P->coef)

Q->exp = P->exp

Q->next = NULL

if(NULL == COPYB)

COPYB = Q

else

L->next = Q

L = Q

P = P->next

}

C = P = Q = L = R = T = NULL

//------------------开始计算

while(COPYA->exp >= COPYB->exp){

D = COPYA

while(NULL != D->next)

D = D->next

R = COPYB

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = (-COPYA->coef) / R->coef

Q->exp = COPYA->exp - R->exp

Q->next = NULL

if(NULL == L)

L = Q

else

P->next = Q

P = Q

while(NULL != R){

Q = (polyptr)malloc(sizeof(polynode))

Q->coef = P->coef * R->coef

Q->exp = P->exp + R->exp

Q->next = NULL

if(NULL == T)

T = Q

else

C->next = Q

C = Q

R = R->next

}

D->next = T

order(©A)

order(©A)

T = NULL

C = NULL

}

order(&L)

order(©A)

printf("A除以B,商:")

display(L)

printf("余数:")

display(COPYA)

destroy(&L)

destroy(©A)

destroy(©B)

}

void display(polynode *P){

//考虑情况有系数为1,指数为1,0,一般数系数为系数不为1,指数为1,0,一般数

//系数为负数,指数为1,0,一般数,主要考虑中间符号问题.

if(NULL == P){

printf("多项式为空.\n")

return

}

if(1 == P->coef){

if(0 == P->exp)

printf("1")

else if(1 == P->exp) printf("x")

else printf("x^%d",P->exp)

}

else{

if(-1 == P->coef){

if(0 == P->exp)

printf("-1")

else if(1 == P->exp) printf("-x")

else printf("-x^%d",P->exp)

}

else if(0 == P->exp)

printf("%d",P->coef)

else if(1 == P->exp) printf("%dx",P->coef)

else

printf("%dx^%d",P->coef,P->exp)

}

P = P->next

while(NULL != P){

if(0 <P->coef){

if(1 == P->coef){

if(0 == P->exp)

printf("+1")

else if(1 == P->exp) printf("+x")

else printf("+x^%d",P->exp)

}

else{

if(0 == P->exp)

printf("+%d",P->coef)

else if(1 == P->exp) printf("+%dx",P->coef)

else

printf("+%dx^%d",P->coef,P->exp)

}

}

else{

if(-1 == P->coef){

if(0 == P->exp)

printf("-1")

else if(1 == P->exp) printf("-x")

else printf("-x^%d",P->exp)

}

else{

if(0 == P->exp)

printf("%d",P->coef)

else if(1 == P->exp) printf("%dx",P->coef)

else

printf("%dx^%d",P->coef,P->exp)

}

}

P = P->next

}

printf("\n")

}

void destroy(polynode **P){

polyptr Q = *P

if(NULL == *P)

return

while(*P != NULL){

Q = *P

*P = (*P)->next

delete Q

}

}

void order(polynode **P){

//首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的要合并.

polyptr prev,curr,OUT,INcurr//前一节点和当前节点

int temp

//出去第一节点系数为0的项

while(NULL != *P){

if(0 != (*P)->coef)

break

else{

if(NULL == (*P)->next)

return

curr = *P

(*P) = (*P)->next

delete curr

}

}

if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不需要整理,退出函数

return

//冒泡排序

OUT = INcurr = *P

while(NULL != OUT->next){//外循环

while(NULL != INcurr->next){//内循环

prev = INcurr

INcurr = INcurr->next

if(prev->exp <INcurr->exp){

temp = prev->coef

prev->coef = INcurr->coef

INcurr->coef = temp//交换系数

temp = prev->exp

prev->exp = INcurr->exp

INcurr->exp = temp//交换指数

}

}

OUT = OUT->next

INcurr = *P

}

//去除0项

prev = curr = *P

curr = curr->next

while(NULL != curr){

if(0 == curr->coef){

prev->next = curr->next

delete curr

curr = prev->next

}

else{

prev = curr

curr = curr->next

}

}

//合并同类项

OUT = INcurr = *P

while(NULL != OUT->next){

while(NULL != INcurr->next){

prev = INcurr

INcurr = INcurr->next

if(INcurr->exp == OUT->exp){

OUT->coef += INcurr->coef

prev->next = INcurr->next

delete INcurr

INcurr = prev

}

}

INcurr = OUT = OUT->next

if(NULL == OUT)

return

}

}

void main(){

int choice

// int i

char ch[100]

polynode *polyA,*polyB

polyA = polyB = NULL

menu()

scanf("%d",&choice)

while(!IsChoice(choice)){

menu()

printf("输入错误,重新输入.\n")

scanf("%d",&choice)

}

while(8 != choice){

switch(choice){

case 1:

if(NULL != polyA || NULL != polyB){

destroy(&polyA)

destroy(&polyB)

printf("原多项式被销毁.\n")

}

printf("多项式输入格式:4x^3+7x^2+x+6--x不分大小写.\n输入多项式A:\n")

scanf("%s",&ch)

while(!isPut(ch)){

printf("输入错误!重新输.\n")

scanf("%s",&ch)

}

createPoly(&polyA,ch)//建立多项式A链表

printf("输入多项式B:\n")

scanf("%s",&ch)

while(!isPut(ch)){

printf("输入错误!重新输.\n")

scanf("%s",&ch)

}

createPoly(&polyB,ch)//建立多项式B链表

order(&polyB)

order(&polyA)//整理排序多项式,默认降幂

printf("建立多项式成功!多项式:\nA为:")

display(polyA)

printf("B为:")

display(polyB)

break

case 2:

polyAdd(polyA,polyB)

break

case 3:

polyMinus(polyA,polyB)

break

case 4:

polyMulti(polyA,polyB)

break

case 5:

printf("PS:系数只支持整数,计算除法存在误差\n如果除数所有项系数为1,能得到正确答案,或者某些情况系数刚好被整除.")

polyDiv(polyA,polyB)

break

case 6:

printf("------显示多项式------\nA :")

display(polyA)

printf("B :")

display(polyB)

break

case 7:

destroy(&polyA)

destroy(&polyB)

printf("此多项式已被清空.\n")

break

default:

return

}

choice = 0

menu()

scanf("%d",&choice)

while(!IsChoice(choice) || 0 == choice){

menu()

printf("输入错误,重新输入.\n")

scanf("%d",&choice)

}

}

}

一元多项式简单的计算器的功能:

1)输入并建立多项式;

2)输出多项式;

3)两个多项式相加,输出和多项式;

4)两个多项式相减,输出差多项式。

例程

#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\n",

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+num2break /*做加法*/

case 2:result=num1-num2break /*做减法*/

case 3:result=num1*num2break /*做乘法*/

case 4:result=num1/num2break /*做除法*/

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) /*返回键值*/

}