#include "arithmetic.h"
#include <iostream>
#include "global.h"
using namespace std;
//-----------------------------input the infix expression-----------------------------
void Api_arithmetic::getInfix()
{
char ch;
cin.get(ch);
int i=0;
while(ch!='\n')
{
infix[i]=ch;
cin.get(ch);
i++;
}
ptr_infix=i;
}
//-------------------------------------constructor---------------
Api_arithmetic::Api_arithmetic()
{
infix=new char[MAX_SIZE];
postfix=new char[MAX_SIZE];
ptr_infix=0;
ptr_postfix=0;
}
//-----------------------------------implement the function of the setColor-----------------------
/*void Api_arithmetic::setColor(WORD Color)
{
SetConsoleTextAttribute(hOut,Color);
}
//----------------------------------implement the function of resetColor-------------------------
void Api_arithmetic::colorReset()
{
SetConsoleTextAttribute(hOut,OriginalColor);
}*/
//--------------------------------show the infix expression with color green----------------------
void Api_arithmetic::showInfix()
{
int i;
setColor(GREEN);
for(i=0;i<ptr_infix;i++)
cout<<infix[i];
cout<<endl;
colorReset();
}
void Api_arithmetic::showPostfix()
{
int i;
setColor(RED);
convert2Postfix();
for(i=0;i<ptr_postfix;i++)
cout<<postfix[i];
cout<<endl;
colorReset();
}
//------------------------------convert----------------------------------------------------------------------------
//convert infix expression into postfix
//-----------------------------------------------------------------------------------------------------------------
void Api_arithmetic::convert2Postfix()
{
char temp;//src
char dst;//dst
int priority;//to get the value of the function of precedence
int ptr_temp=0;
for(ptr_temp=0;ptr_temp<ptr_infix;ptr_temp++)
{
temp=infix[ptr_temp];
//<<temp<<endl;
if(temp>='0'&&temp<='9')
{
postfix[ptr_postfix]=temp;
ptr_postfix++;
}//end if
else if(temp=='+'||temp=='-'||temp=='*'||temp=='/')//compare the priority
{
if(!operators.isEmpty())
{
dst=operators.getLastItem();//get the top element of the stack
while(dst!='('&&!operators.isEmpty())//out loop while meet charcter '(' and pop out the character whose priority higher than src
{
priority=precedence(temp,dst);
if(priority<=0)
{
operators.pop(postfix[ptr_postfix]);
//cout<<"^"<<postfix[ptr_postfix];
ptr_postfix++;
if(!operators.isEmpty())
dst=operators.getLastItem();
}//end if
else
break;//end else
}//end while
}//end if
operators.push(temp);
}//end else if
else if(temp=='(')
{
operators.push(temp);
}//end else if
else if(temp==')')
{
operators.pop(dst);
while(dst!='(')
{
postfix[ptr_postfix]=dst;
ptr_postfix++;
operators.pop(dst);
}//end while
}//end else if
else if(temp=='=')
{
while(!operators.isEmpty())
{
operators.pop(postfix[ptr_postfix]);
//cout<<"!"<<postfix[ptr_postfix];
ptr_postfix++;
}//end while
}//end else if
else
{
cout<<"input error"<<endl;
}//end else
}//end for
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//This function is to compare the priority of the operators;
//if the priority of the src higher than dst, return 1; if equal,return 0; else return -1;
////////////////////////////////////////////////////////////////////////////////////////////////
int Api_arithmetic::precedence(char src,char dst)
{
if(src=='*'||src=='/')
{
if(dst=='+'||dst=='-')
return 1;
else
return 0;
}//end if
else
{
if(dst=='+'||dst=='-')
return 0;
else
return -1;
}
}
Api_arithmetic::~Api_arithmetic()
{
delete[] infix;
delete[] postfix;
}
/*void Api_arithmetic::precedence(char src,char dst,int& pri)
{
if(src=='*'||src=='/')
{
if(dst=='+'||dst=='-')
pri=1;
else
pri=0;
}//end if
else
{
if(dst=='+'||dst=='-')
pri=0;
else
pri=-1;
}
}*/
没有合适的资源?快使用搜索试试~ 我知道了~
四则混合运算的算法
共28个文件
h:5个
cpp:4个
obj:4个
温馨提示
此算法用于四则运算,没有对异常进行处理。 输入形式请如下: A+(B+C)*D= ((B+C)*D+(A+F)/E)+G/H+W= 记得输入等号,本人没有考虑回车的情况,请自行修改。 文件arithmeic主要用于将中缀表达式转为后缀表达式。 cal_arithmetic从arithmetic派生用于计算后缀表达式。
资源推荐
资源详情
资源评论
格式:pdf 资源大小:166.4KB 页数:14
收起资源包目录
arithmetic.rar (28个子文件)
arithmetic
Debug
arithmetic.exe 46KB
arithmetic.ilk 483KB
arithmetic.pdb 715KB
arithmetic.suo 18KB
arithmetic
cal_arithmetic.cpp 3KB
arithmetic.h 911B
listType.h 2KB
arithmetic.vcproj.MSTARSEMI.mac.zhu.user 1KB
Debug
vc90.pdb 260KB
vc90.idb 603KB
arithmetic.exe.embed.manifest.res 728B
BuildLog.htm 8KB
mt.dep 69B
arithmetic.exe.embed.manifest 663B
arithmetic.exe.intermediate.manifest 621B
arithmethic.obj 70KB
main.obj 43KB
cal_arithmetic.obj 72KB
global.obj 22KB
arithmetic.vcproj 4KB
stackType.h 2KB
main.cpp 678B
global.h 785B
arithmethic.cpp 4KB
cal_arithmetic.h 505B
global.cpp 450B
arithmetic.ncb 14.61MB
arithmetic.sln 896B共 28 条
- 1
资源评论
u0107988042013-05-25算法详细,可读性强,帮助很大
qq84468887612013-11-03代码写得很详细,很好
qiouyanbin2014-07-23代码写的不错,解决了问题
头文字P2013-05-03很好,很详细
he1109he2012-10-10算法代码很详细,谢谢
judymac
- 粉丝: 0
上传资源 快速赚钱
我的内容管理
展开
我的资源
快来上传第一个资源
我的收益 登录查看自己的收益
我的积分
登录查看自己的积分
我的C币
登录后查看C币余额
我的收藏
我的下载
下载帮助
前往需求广场,查看用户热搜最新资源
- 互联网上的文学风景-我国网络文学现状调查与走势分析.docx
- 农产品电子商务发展现状附趋势.doc
- 基于大数据的图书馆个性化图书荐购策略.docx
- 白银市化工循环产业链项目管理.doc
- (源码)基于Keras框架的手写数字识别系统.zip
- 多传感器障碍物检测系统的软件方案设计书.doc
- 城市有线电视网络设计规范.ppt
- IPv4IPv6隧道技术的通信分析研究87760.doc
- 软考网络工程师复习(知识点汇总).docx
- 接口与通信习题参考答案.doc
- 28个你必须知道的HTML5的新特性技巧以及技术.doc
- 沈阳理工大学-面向对象程序设计20121017.doc
- 计算机电子信息技术及工程管理模式研究.docx
- (源码)基于Linux系统的远程视频监控系统.zip
- 《基因工程的基本操作程序》(新人教版必修).ppt
- 11计算机科学与技术专业大学设计.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功