1、记忆当前输入的表达式,给出一个最后的运算结果。可以清楚部分或全部记忆的值。系统流程图 图 系统流程图主要算法 比较运算符优先级:程序里的涉及到的运算符有7个,分别是+,-,*,/,(,),#,它们之间的优先级关系用一个二维数组来存放,int cmp77= 2,2,1,1,1,2,2, 2,2,2,2,1,2,2, 1,1,1,1,1,3,0, 2,2,2,2,0,2,2, 1,1,1,1,1,0,3;其中3代表=,2代表,1代表,0代表不可比。char Compare(char ch1, char ch2) int i,m,n; char pri; int priority; for(i=0;
2、i7;i+) /找到相比较的两个运算符在比较矩阵里的相对位置 if(ch1=opsi) m=i; if (ch2=opsi) n=i; priority = cmpmn; switch(priority) case 1: pri= case 3:= case 0:$ printf(表达式错误!n); return pri;主要代码1.计算器界面设计利用VC绘图库函数设计一个相对友好的操作界面。关于VC绘图库的介绍在本书第二章。界面图像如下:图:计算器界面画计算机界面。 initgraph(500,300); / 这里和 TC 略有区别 bar(15, 15, 480, 70); setcolo
3、r(WHITE); rectangle(10,10,490,290); for(j=0;j2;j+) for(i=0;i+) if(i3) circle(55+65*i,110+65*j,20); else rectangle(55+65*i-20,110+65*j-20,55+65*i+20,110+65*j+20); i+) if(i4) circle(55+65*i,110+65*j,20); else rectangle(55+65*i-20,110+65*j-20,55+65*i+20,110+65*j+20); outtextxy(50,105,7 outtextxy(115,10
4、5,8 outtextxy(180,105,9 outtextxy(240,105,C outtextxy(310,105,+ outtextxy(375,105,( outtextxy(440,105,) outtextxy(50,170,4 outtextxy(115,170,5 outtextxy(180,170,6 outtextxy(240,170,. outtextxy(310,170,- outtextxy(370,170,sqrt outtextxy(440,170, outtextxy(50,235,3 outtextxy(115,235,2 outtextxy(180,23
5、5,1 outtextxy(245,235,0 outtextxy(310,235,* outtextxy(375,235,/ outtextxy(440,235,=2.按键响应操作当用户通过鼠标左键按下0,1,2,3,4,5,6,7,8,9,.,+,*,/,(,),=,这些键时,字符按点击顺序在输出文本框输出;当用户点击35&m.x90&m.y130) strcat(string,outtextxy(25,40,string);100&=140&165&=205&225&=265&130) setcolor(WHITE);strcpy(string,/C290&=330&355&=395&4
6、20&=460&155&195) strcat(string,setcolor(BLACK);195) /lg n=strlen(string); switch(n)/不超过5位 case 5:i=string0*10000+string1*1000+string2*100+string3*10+string4;break; case 4:i=string0*1000+string1*100+string2*10+string3; case 3:i=string0*100+string1*10+string2; case 2:i=string0*10+string1; case 1:i=str
7、ing1; itoa(sqrt(i),string,10); setcolor(WHITE); setcolor(BLACK); outtextxy(25,40,string);195) n=strlen(string);printf(n=%d.,n);stringn-1=0/top =-1;/初始化运算数栈void InitStacknOperand(SeqStackOperand *S)/判断栈S为空栈时返回值为真, 反之为假int IsEmpty(SeqStack *S) return(S-top=-1?TRUE:FALSE);int IsEmptynOperand(SeqStackOp
8、erand *S)/判断栈S为满栈时返回值为真, 反之为假int IsFull(SeqStack *S)top=Stack_Size-1?int IsFullOperand(SeqStackOperand *S)/运算符栈入栈函数int Push(SeqStack *S, char x) if (S-top=Stack_Size-1)Stack is full! return FALSE; else S-top+;elemS-top=x; return TRUE;/运算数栈 入栈函数int PushOperand(SeqStackOperand *S, int x)/运算符栈 出栈函数int
9、Pop(SeqStack *S, char *x)top=-1)运算符栈空! *x=S-top;top-;/运算数栈 出栈函数int PopOperand(SeqStackOperand *S, int *x)/运算符栈取栈顶元素函数char GetTop(SeqStack *S)top =-1)运算符栈为空! return (S-top);/运算数栈取栈顶元素函数int GetTopOperand(SeqStackOperand *S)/判断输入字符是否为运算符函数, 是返回TRUE, 不是返回FALSEint Isoperator(char ch) int i; for (i=0; if(
10、ch=opsi) return TRUE; return FALSE;/比较运算符优先级函数/运算函数int Execute(int a, char op, int b) int result; switch(op) case +: result=a+b;- result=a-b;* result=a*b;/ result=a/b; return result;/*读入一个简单算术表达式并计算其值. operator和operand分别为运算符栈和运算数栈, OPS为运算符集合*/int ExpEvaluation(char *str) int a,b,v,temp; char ch,op;
11、int i=0; SeqStack operatordata; SeqStackOperand operand; InitStack(&operatordata); InitStacknOperand(&operand); Push(&operatordata,# ch=*str+; while(ch! | GetTop(&operatordata)!) if(!Isoperator(ch) temp=ch-0 /将字符转换为十进制数 ch=*str+; i+; while(! temp=temp*10 + ch- /将逐个读入运算数的各位转化为十进制数 ch=*str+; i+; PushO
12、perand(&operand,temp); else switch(Compare(GetTop(&operatordata),ch) case Push(&operatordata,ch); break; Pop(&operatordata,&op); PopOperand(&operand,&b);a); v=Execute(a,op,b); /对a和b进行op运算 PushOperand(&operand,v); /end while v=GetTopOperand(& return v;小结知识点:VC绘图库鼠标按键响应操作结构体定义顺序栈顺序栈的基本操作:初始化栈、判断栈是否为空、取栈顶元素、入栈、出栈功能扩充实现取对数、正弦、余弦等一些函数功能。