华为计算机如何计算sina,一道华为的“简单四则运算”题
*expStr:?表達(dá)式字符串;
【輸出】 無
【返回】 計(jì)算結(jié)果
int calcucate(int len, char* expStr)
{
//算法思路:先乘除,后加減
assert(expStr != NULL);
len = strlen(expStr);//防止函數(shù)調(diào)用者粗心傳錯(cuò)參數(shù)
int result = 0;
char* temp = new char[len];
char* temp2 = temp;
memset(temp,0,len);
//先做乘除運(yùn)算
for (int i=0; i
{
if (*(expStr+i) != '*' &&
*(expStr+i) != '/')
{
*temp = *(expStr+i);
temp++;
}
else
{
if (*(expStr+i) == '*')
{
int a = (*(expStr+i-1) - 48);
int b = (*(expStr+i+1) - 48);
*temp= (*--temp - 48) * (*(expStr+i+1) - 48) + 48;
temp++;
i += 1;
}
else
if (*(expStr+i) == '/')
{
*temp = (*--temp - 48) / (*(expStr+i+1) - 48) + 48;
temp++;
i += 1;
}
}
}
//后做加減運(yùn)算
for (int i=0; i
{
if (*(temp2+i)<='9'
&& * (temp2+i)>='0'
)
{
result += *(temp2+i) - 48;
}
else
{
if (*(temp2+i) == '-')
{
result -= *(temp2+i+1) - 48;
i += 1;
}
else
{
result += *(temp2+i+1) - 48;
i += 1;
}
}
}
temp -= strlen(temp2);
delete []temp; //new 出來的內(nèi)存,用完后請(qǐng)釋放,且不要忘了執(zhí)行上一語句,否則運(yùn)行報(bào)錯(cuò)。
return result;
}
轉(zhuǎn)自:http://topic.csdn.net/u/20111025/23/18936b47-d9b0-48db-a49a-97c2be52fad5.html
總結(jié)
以上是生活随笔為你收集整理的华为计算机如何计算sina,一道华为的“简单四则运算”题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结构分析的计算机方法有哪些,第6篇 桥梁
- 下一篇: 志愿填报显示服务器错误,高分落榜案例:志