四则运算的实现
2011-05-04 10:43
676 查看
继续温习数据结构...
实现用到了两个栈:操作数栈与操作符栈。
主要过程是将中序表达式转换为后续表达式,然后按顺序进行运算。
简单过程:1+8-7(中序) ---->1 8 7 - + (后序)--->1 1 + (运算8-7)---> 2 (运算1+1)
源码:
实现用到了两个栈:操作数栈与操作符栈。
主要过程是将中序表达式转换为后续表达式,然后按顺序进行运算。
简单过程:1+8-7(中序) ---->1 8 7 - + (后序)--->1 1 + (运算8-7)---> 2 (运算1+1)
源码:
#include "stack.h" //利用到了前一篇文章实现的栈 #define SIZE 256 #define PSIZE 512 char postOrder[SIZE]; Stack<char> s(SIZE); Stack<int> res(SIZE); int prior(char ch) //自定义优先级 { if(ch=='+'||ch=='-')return 2; else if(ch=='*'||ch=='/')return 3; else if(ch=='('||ch==')')return 4; else if(ch=='=')return 1; else return 0; } int comp(int i,int j,char c) //运算 { switch(c) { case '+': return i+j; case '-': return i-j; case '*': return i*j; case '/': return i/j; default: return -1; } } int compute() { for(int i=0;i!=strlen(postOrder);) { if (postOrder[i]==' '||postOrder[i]==NULL||postOrder[i]=='=') { ++i; continue; } if (postOrder[i]>='0'&&postOrder[i]<='9'/*&&!res.isFull()*/) { int val=0; //计算操作数的大小 while(postOrder[i]>='0'&&postOrder[i]<='9') { val=10*val+(postOrder[i]-'0'); i++; } res.push(val); continue; } if(res.top>=1) { int p=res.pop(); //取出离第一个操作符最近的两个操作数 int q=res.pop(); int s1=comp(q,p,postOrder[i]); //进行四则运算 res.push(s1); ++i; } } return res.pop(); } void change(char *prestr) { int i=0,j=0; while(i<SIZE) { if (prestr[i]==' '||prestr[i]==NULL) {i++;continue;} if(prestr[i]>='0'&&prestr[i]<='9') //将字符串转换为表示后续表达式的字符数组 { while(prestr[i]>='0'&&prestr[i]<='9') { //如果当前字符为数字,则继续 postOrder[j++]=prestr[i++]; } postOrder[j++]=' '; } if(prestr[i]==')') //如果为右括号,则进行出栈操作,直到遇到左括号为止 { int flag = 0; while(!s.isEmpty()) { if(s.data[s.top]=='(') { flag=1; ++i; s.pop(); break; } postOrder[j++]=s.pop(); postOrder[j++]=' '; } if(s.isEmpty()&&flag==0) { cout<<"Error:Do you miss a '('?"<<endl; exit(0); } } if(prestr[i]!=')'&&!s.isFull()&&(s.isEmpty() //如果当前符号优先级大于栈顶符号优先级,则入栈 ||prior(prestr[i])>prior(s.data[s.top]))) { s.push(prestr[i++]); } else if(prestr[i]!=')') { while(!s.isEmpty()&&prior(prestr[i])<=prior(s.data[s.top]) &&s.data[s.top]!='(') //如果当前符号优先级比栈顶的小,则出栈,直到栈顶符号优先级 { //小于等于当前符号优先级,然后入栈 postOrder[j++]=s.pop(); //记录出栈的元素 postOrder[j++]=' '; } s.push(prestr[i++]); } } while(!s.isEmpty()) //遍历完字符串后,将还未出栈的元素全部出栈,记录在后续表达式的后面 { if(s.data[s.top]=='(') { cout<<"Error:Do you miss a ')'?"<<endl; exit(0); } postOrder[j++]=s.pop(); postOrder[j++]=' '; } } int main() { char preStr[PSIZE]; char ch; int preIndex=0; memset(preStr,NULL,sizeof(preStr)); for(int i=0;i<PSIZE;++i) { ch=getchar(); if (ch==' '||ch==NULL)continue; if(ch=='/n')break; preStr[preIndex++]=ch; if(ch=='=')break; } if(strlen(preStr)>SIZE) { cout<<"Exception:/nThe size of expression is beyond default value." <<"/nThe length of a string cannot be larger than "<<SIZE<<"./n"; exit(0); } change(preStr); for(int i=0;i<strlen(postOrder);++i) { cout<<postOrder[i]; } cout<<endl; for(int i=0;i!=strlen(preStr);++i) { cout<<preStr[i]; } cout<<endl<<compute()<<endl; return 0; }
相关文章推荐
- week2-结对编程【网页实现四则运算】
- java小练习(实现分数的四则运算)
- 栈实现简单的四则运算表达式
- 带括号的四则混合运算的算符优先算法java实现
- 支持多位数,括号,四则运算,的计算器算法c++实现
- 字符串四则运算表达式的求解(中缀表达式实现,模拟笔算的方法)
- 如何处理加括号的四则混合运算表达式——基于二叉树的实现(Eclipse平台 Java版)
- C语言实现四则混合运算
- 基于51单片机设计简单计算器,实现6位有效四则运算的程序设计(汇编调用C)
- 实现一个Add函数,让两个数相加,但是不能使用+、-、*、/等四则运算符。
- 作业:实现0-10的随机四则运算
- 分别用C++和JavaScript 实现四则运算表达式求值
- java小练习(实现分数的四则运算)
- 用栈实现四则运算计算器 implement a calculator using stack
- java 四则运算 栈的实现
- java实现四则运算,难点主要在理解加减乘除优先级以及使用递归
- 实现复数的加减乘除四则运算的C#类(已测成功)
- 利用位运算实现四则运算 -- 华为面试题
- 位运算实现四则运算
- 【实践】js实现简易的四则运算计算器