📄 calculator.cpp
字号:
// Calculator.cpp: implementation of the CCalculator class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RepComb.h"
#include "Calculator.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
double CCalculator::sub(double a,double b)
{
return a-b;
}
double CCalculator::product(double a,double b)
{
return a*b;
}
double CCalculator::divide(double a,double b)
{
if(b==0)return 0;
return a/b;
}
char CCalculator::m_cOperatorTable[N]={'+','-','*','/','\0'};
char CCalculator::m_nPriorityL[N][N]={
{-1,-1,1,1,-1},
{-1,-1,1,1,-1},
{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1}
};
char CCalculator::m_nPriorityR[N][N]={
{1,1,1,1,-1},
{-1,1,1,1,-1},
{-1,-1,-1,1,-1},
{-1,-1,-1,1,-1},
{-1,-1,-1,-1,-1}
};
CCalculator::CCalculator()
{
m_pfOperation[0]=this->add;
m_pfOperation[1]=this->sub;
m_pfOperation[2]=this->product;
m_pfOperation[3]=this->divide;
}
CCalculator::~CCalculator()
{
}
double CCalculator::compute(double a, char op, double b)
{
int n;
for(n=0;n<N;n++)
if(m_cOperatorTable[n]==op)
break;
if(n>N-1)
{
m_nStatus=NOOPERATOR;
return -1;
}
m_nStatus=OK;
return (this->*m_pfOperation[n])(a,b);
}
double CCalculator::add(double a,double b)
{
return a+b;
}
//当c1出现在c2的右边时调用此函数比较
int CCalculator::compareR(char c1, char c2)
{
int n1,n2;
for(n1=0;n1<N;n1++)
if(m_cOperatorTable[n1]==c1)
break;
if(n1>N-1) return -2;
for(n2=0;n2<N;n2++)
if(m_cOperatorTable[n2]==c2)
break;
if(n2>N-1) return -2;
return m_nPriorityR[n1][n2];
}
//当c1出现在c2左边时调用此函数比较
int CCalculator::compareL(char c1, char c2)
{
int n1,n2;
for(n1=0;n1<N;n1++)
if(m_cOperatorTable[n1]==c1)
break;
if(n1>N-1) return -2;
for(n2=0;n2<N;n2++)
if(m_cOperatorTable[n2]==c2)
break;
if(n2>N-1) return -2;
return m_nPriorityL[n1][n2];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -