码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现“计算器”

时间:2017-04-28 23:40:03      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:switch   env   cal   while   printf   str   add   space   text   

 

支持 加减乘除 混合运算。具体设计见代码。 

 

#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;
}

 

C语言实现“计算器”

标签:switch   env   cal   while   printf   str   add   space   text   

原文地址:http://www.cnblogs.com/ant-wjf/p/6783524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!