好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

简单四则运算计算器.doc

9页
  • 卖家[上传人]:壹****1
  • 文档编号:545812444
  • 上传时间:2022-08-17
  • 文档格式:DOC
  • 文档大小:59.02KB
  • / 9 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • include/* 就是需要引用这个图形库*/#include#include/* 控制台输入输出头文件,getch()语句需要*/#include#include#include#define TRUE 1#define FALSE 0#define Stack_Size 50/*存储空间初始分配量*/char ops[7]={'+','-','*','/','(',')','#'};int cmp[7][7]={ {2,2,1,1,1,2,2}, {2,2,1,1,1,2,2}, {2,2,2,2,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}}; typedef struct { char elem[Stack_Size]; int top; }SeqStack; /*运算数栈的定义*/ typedef struct { int elem[Stack_Size]; int top; }SeqStackOperand; /*初始化运算符栈*/ void InitStack(SeqStack *S) { S->top =-1; } /*初始化运算数栈*/ void InitStacknOperand(SeqStackOperand *S) { S->top =-1; } /*判断栈S为空栈时返回值为真,反之为假*/ int IsEmpty(SeqStack *S) { return(S->top==-1?TRUE:FALSE); } /*判断栈S为空栈时返回值为真,反之为假*/ int IsEmptynOperand(SeqStackOperand *S) { return(S->top==-1?TRUE:FALSE); } /*判断栈S为满栈时返回值为真,反之为假*/ int IsFull(SeqStack *S) { return(S->top==Stack_Size-1?TRUE:FALSE); } /*判断栈S为满栈时返回值为真,反之为假*/ int IsFullOperand(SeqStackOperand *S) { return(S->top==Stack_Size-1?TRUE:FALSE); } /*运算符栈入栈函数*/ int Push(SeqStack *S,char x) { if (S->top==Stack_Size-1) { printf("Stack is full!\n"); return FALSE; } else { S->top++; S->elem[S->top]=x; return TRUE; } } /*运算数栈入栈函数*/ int PushOperand(SeqStackOperand *S,int x) { if (S->top==Stack_Size-1) { printf("Stack is full!\n"); return FALSE; } else { S->top++; S->elem[S->top]=x; return TRUE; } } /*运算符栈出栈函数*/ int Pop(SeqStack *S,char *x) { if (S->top==-1) { printf("运算符栈空!\n"); return FALSE; } else { *x=S->elem[S->top]; S->top--; return TRUE; } } /*运算数栈出栈函数*/ int PopOperand(SeqStackOperand *S,int *x) { if (S->top==-1) { printf("运算符栈空!\n"); return FALSE; } else { *x=S->elem[S->top]; S->top--; return TRUE; } } /*运算符栈取栈顶元素函数*/ char GetTop(SeqStack *S) { if (S->top ==-1) { printf("运算符栈为空!\n"); return FALSE; } else { return (S->elem[S->top]); } } /*运算数栈取栈顶元素函数*/ int GetTopOperand(SeqStackOperand *S) { if (S->top ==-1) { printf("运算符栈为空!\n"); return FALSE; } else { return (S->elem[S->top]); } } /*判断输入字符是否为运算符函数,是返回TRUE,不是返回FALSE*/ int Isoperator(char ch) { int i; for (i=0;i<7;i++) { if(ch==ops[i]) return TRUE; } return FALSE; } char Compare(char ch1,char ch2) { int i,j,k; char pri; int priority; for(i=0;i<7;i++) { if (ch1=ops[i]) j=i; if (ch2=ops[i]) k=i; } priority=cmp[j][k]; switch(priority) { case 1: pri='>';break; case 2: pri='<';break; case 3: pri='=';break; case 4: pri='$'; printf("错误!\n"); break; } return pri; } int Execute(int num1,char op,int num2) /*进行单个算术运算*/ { int Rusult; switch(op) { case '+': Rusult=num1+num2; break; case '-': Rusult=num1-num2; break; case '*': Rusult=num1*num2; break; case '/': { if(num2==0) printf("\nError!\n"); else Rusult=num1/num2; } break; } return(Rusult); } int ExpEvaluation(char *str) { int a,b,v,temp; char ch,op; 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(!Isoperator(ch)) { temp=temp*10 + ch-'0'; /*将逐个读入运算数的各位转化为十进制数*/ ch=*str++; i++; } PushOperand(&operand,temp); } else { switch(Compare(GetTop(&operatordata),ch)) { case '<': Push(&operatordata,ch); ch=*str++; i++; break; case '=': Pop(&operatordata,&op); ch=*str++; i++; break; case '>': Pop(&operatordata,&op); PopOperand(&operand,&b); PopOperand(&operand,&a); v=Execute(a,op,b); /*对a和b进行op运算*/ PushOperand(&operand,v); break; } } }/*end while*/ v=GetTopOperand(&operand); return v; } void MainFace() { int i,j; initgraph(400,250); /*初始化*/// bar(10,10,390,240); setcolor(WHITE); /*图形边框颜色设置*/ rectangle(20,20,380,230); setcolor(LIGHTRED); bar(30,30,370,70); for(i=0;i<3;i++) for(j=0;j<7;j++) {//setcolor(BLACK); rectangle(30+50*j,80+50*i,70+50*j,120+50*i); circle(50+50*j,100+50*i,20); } setcolor(RED); outtextxy (46,90,"7"); outtextxy(96,90,"8"); outtextxy(146,90,"9"); outtextxy(196,90,"C"); outtextxy(246,90,"+"); outtextxy(296,90,"("); outtextxy(346,90,")"); outtextxy(46,140,"4"); outtextxy(96,140,"5"); outtextxy(146,140,"6"); outtextxy(196,140。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.