C语言实现“计算器”
支持 加减乘除 混合运算。具体设计见代码。
#include <assert.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////
typedef char bool;
#define true 1
#define false 0
typedef struct {
const char *szExp;
int nPos;
} Context;
typedef struct{
int nVal;
Context tCon;
} Env;
typedef bool (*ExpectOperation)(Context *pCon);
typedef int (*GetCalcValue)(Env *pEnv);
int MixCalc(Env *pEnv);
///////////////////////////////////////////////////////////////////////////////
int GetValue(Env *pEnv)
{
int nVal = 0;
const char *szExp = pEnv->tCon.szExp;
int nPos = pEnv->tCon.nPos;
if (*(szExp+nPos) == '(')
{
pEnv->tCon.nPos ++;
nVal = MixCalc(pEnv);
pEnv->tCon.nPos ++;
}
else
{
while ((*(szExp+nPos) >= '0')
&& (*(szExp+nPos) <= '9'))
{
nVal = nVal*10 + (*(szExp+nPos) - '0');
nPos ++;
}
pEnv->tCon.nPos = nPos;
}
return nVal;
}
bool IsAddSubOperation(Context *pCon)
{
return (pCon->szExp[pCon->nPos] == '+') || (pCon->szExp[pCon->nPos] == '-');
}
bool IsMulDivOperation(Context *pCon)
{
return (pCon->szExp[pCon->nPos] == '*') || (pCon->szExp[pCon->nPos] == '/');
}
char GetOperation(Context *pCon)
{
char cOperation = '\0';
char cCurrChar = pCon->szExp[pCon->nPos];
if ((cCurrChar == '+')
|| (cCurrChar == '-')
|| (cCurrChar == '*')
|| (cCurrChar == '/'))
{
cOperation = cCurrChar;
pCon->nPos ++;
}
return cOperation;
}
int ApplyOperation(int nVal1, int nVal2, char cOperation)
{
switch (cOperation)
{
case '+':
return nVal1 + nVal2;
case '-':
return nVal1 - nVal2;
case '*':
return nVal1 * nVal2;
case '/':
return nVal1 / nVal2;
}
return 0;
}
int Calc(Env *pEnv, ExpectOperation fExpOperation, GetCalcValue fGetCalcValue)
{
int nVal1 = fGetCalcValue(pEnv);
int nVal2 = 0;
char cOperation;
while (fExpOperation(&pEnv->tCon))
{
cOperation = GetOperation(&pEnv->tCon);
nVal2 = fGetCalcValue(pEnv);
nVal1 = ApplyOperation(nVal1, nVal2, cOperation);
}
pEnv->nVal = nVal1;
return nVal1;
}
int Mul_Div(Env *pEnv)
{
return Calc(pEnv, IsMulDivOperation, GetValue);
}
int MixCalc(Env *pEnv)
{
return Calc(pEnv, IsAddSubOperation, Mul_Div);
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
Env env1 = {0, "1+2", 0};
Env env2 = {0, "1+2-3+4+5", 0};
Env env3 = {0, "1*2", 0};
Env env4 = {0, "1*2*3*4/2", 0};
Env env5 = {0, "1*2+2*4", 0};
Env env6 = {0, "1*(2+2)*4+3", 0};
MixCalc(&env1);
assert(env1.nVal == 3);
assert(env1.tCon.nPos == 3);
MixCalc(&env2);
assert(env2.nVal == 9);
assert(env2.tCon.nPos == 9);
Mul_Div(&env3);
assert(env3.nVal == 2);
assert(env3.tCon.nPos == 3);
Mul_Div(&env4);
assert(env4.nVal == 12);
assert(env4.tCon.nPos == 9);
MixCalc(&env5);
assert(env5.nVal == 10);
assert(env5.tCon.nPos == 7);
MixCalc(&env6);
assert(env6.nVal == 19);
assert(env6.tCon.nPos == 11);
printf("Try Calculate. \n");
return 0;
}
posted on 2017-04-28 22:19 Waaaaaall-E 阅读(1357) 评论(0) 编辑 收藏 举报