64,282
社区成员
发帖
与我相关
我的任务
分享
double CountMath(vector<string> vOp)
{
int nMid = vOp.size()/2;
double dLeft = 0.0;
double dRight = 0.0;
double dLast = 0.0;
const char *pOp = NULL;
dLeft = atof(vOp.at(0).c_str());
for (int i=0 ;i<nMid-1 ;i++) //1 等于号不算
{
dRight = atof(vOp.at(i+1).c_str());
pOp = vOp.at(nMid+i).c_str();
if (0 == strcmp(pOp ,"+"))
dLast = dLeft + dRight;
if (0 == strcmp(pOp ,"-"))
dLast = dLeft - dRight;
if (0 == strcmp(pOp ,"*"))
dLast = dLeft * dRight;
if (0 == strcmp(pOp ,"/"))
dLast = dLeft / dRight;
dLeft = dLast; // 结果赋值给左值
}
return dLast;
}
vector<string> GetOp(char *pInput)
{
vector<string> vOp;
const int MAX_NUM_LEN = 32;
char acTmp[MAX_NUM_LEN] = {0};
while (true)
{
memset(acTmp ,0 ,MAX_NUM_LEN);
sscanf(pInput ,"%s" ,acTmp);
vOp.push_back(acTmp);
pInput += strlen(acTmp);
if (0 == *pInput)
break;
pInput += 1; //1 :space
}
return vOp;
}
void main()
{
//char Input[] = "3 4 5 + + =";
char Input[] = "3 4 5 + * =";
vector<string> vOp = GetOp(Input);
printf("Count = %lf\n" ,CountMath(vOp));
}
double CountMath(vector<string> vOp)
{
int nMid = vOp.size()/2;
double dLeft = 0.0;
double dRight = 0.0;
double dLast = 0.0;
const char *pOp = NULL;
dLeft = atof(vOp.at(0).c_str());
for (int i=0 ;i<nMid-1 ;i++) //1 等于号不算
{
dRight = atof(vOp.at(i+1).c_str());
pOp = vOp.at(nMid+i).c_str();
if (0 == strcmp(pOp ,"+"))
dLast = dLeft + dRight;
if (0 == strcmp(pOp ,"-"))
dLast = dLeft - dRight;
if (0 == strcmp(pOp ,"*"))
dLast = dLeft * dRight;
if (0 == strcmp(pOp ,"/"))
dLast = dLeft / dRight;
dLeft = dLast; // 结果赋值给左值
}
return dLast;
}
vector<string> GetOp(char *pInput)
{
vector<string> vOp;
while (true)
{
char acTmp[8] = {0};
int nRet = sscanf(pInput ,"%s" ,acTmp);
vOp.push_back(acTmp);
pInput += nRet;
if (0 == *pInput)
break;
pInput += 1; //1 :space
}
return vOp;
}
void main()
{
//char Input[] = "3 4 5 + + =";
char Input[] = "3 4 5 + * =";
vector<string> vOp = GetOp(Input);
printf("Count = %lf\n" ,CountMath(vOp));
}