華為面試題:四則運(yùn)算 C語(yǔ)言源碼
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-08-24 07:57:33 閱讀次數(shù):3273次
4則運(yùn)算
描寫(xiě):
請(qǐng)實(shí)現(xiàn)以下接口
/* 功能:4則運(yùn)算
* 輸入:strExpression:字符串格式的算術(shù)表達(dá)式,如: "3+2*{1+2*[⑷/(8⑹)+7]}"
* 返回:算術(shù)表達(dá)式的計(jì)算結(jié)果
*/
public static int calculate(String strExpression)
{
/* 請(qǐng)實(shí)現(xiàn)*/
return 0;
}
束縛:
pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。
pucExpression算術(shù)表達(dá)式的有效性由調(diào)用者保證;
知識(shí)點(diǎn): 棧
題目來(lái)源: 內(nèi)部整理
練習(xí)階段: 中級(jí)
運(yùn)行時(shí)間限制: 10Sec
內(nèi)存限制: 128MByte
輸入:
輸入1個(gè)算術(shù)表達(dá)式
輸出:
得到計(jì)算結(jié)果
樣例輸入:
3+2*{1+2*[⑷/(8⑹)+7]}
樣例輸出:
25
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX_PATH 256
char stack_all[MAX_PATH] = {0};
char stack_symbol[MAX_PATH] = {0};
char stack_num[MAX_PATH] = {0};
int num_all=0;
int num_symbol =0;
int num_num = 0;
int cal(int a,int b,char op)
{
switch(op)
{
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
if (b==0)
{
return 0;
}
return a/b;
default:
return 0;
}
}
//有效字符是0⑼
int calculate(char *strExpression)
{
int length = strlen(strExpression);
for (int i=length⑴;i>=0;i--)
{
char t = strExpression[i];
if(t=='*' || t=='/' || t==')' || t==']' || t=='}')
{
stack_symbol[num_symbol] = t;
num_symbol++;
}
else if (t=='(')
{
while(true)
{
if (stack_symbol[num_symbol⑴]==')')
{
num_symbol--;
break;
}
stack_all[num_all]=stack_symbol[num_symbol⑴];
num_all++;
num_symbol--;
}
}
else if (t=='[')
{
while(true)
{
if (stack_symbol[num_symbol⑴]==']')
{
num_symbol--;
break;
}
stack_all[num_all]=stack_symbol[num_symbol⑴];
num_all++;
num_symbol--;
}
}
else if (t=='{')
{
while(true)
{
if (stack_symbol[num_symbol⑴]=='}')
{
num_symbol--;
break;
}
stack_all[num_all]=stack_symbol[num_symbol⑴];
num_all++;
num_symbol--;
}
}
else if(t=='+' || t=='-')
{
while((stack_symbol[num_symbol⑴]=='*' || stack_symbol[num_symbol⑴]=='/') && num_symbol!=0 )
{
stack_all[num_all]=stack_symbol[num_symbol⑴];
num_all++;
num_symbol--;
}
stack_symbol[num_symbol] = t;
num_symbol++;
}
else
{
stack_all[num_all] = t;
num_all++;
}
}
for (int i=num_symbol⑴;i>=0;i--)
{
stack_all[num_all]=stack_symbol[i];
num_all++;
}
for (int i=0;i<num_all;i++)
{
char t = stack_all[i];
if (t>='0'&&t<='9')
{
stack_num[num_num]=t-'0';
num_num++;
}
else
{
stack_num[num_num⑵] = cal(stack_num[num_num⑴],stack_num[num_num⑵],t);
num_num--;
}
}
return (int)stack_num[0];
}
void init(char *buffer,char *buffer2)
{
int length = strlen(buffer);
int k = 0;
for (int i=0;i<length;i++)
{
char t = buffer[i];
if (t=='+'||t=='-')
{
if(i>=1 && (buffer[i⑴]=='(' || buffer[i⑴]=='[' || buffer[i⑴]=='{'))
{
//可以不使用括號(hào)
// buffer2[k]='0';
// k++;
// buffer2[k]=buffer[i];
// k++;
buffer2[k]='(';
k++;
buffer2[k]='0';
k++;
buffer2[k]=buffer[i];
k++;
buffer2[k]=buffer[i+1];
i++;
k++;
buffer2[k]=')';
k++;
}
else
{
buffer2[k]=buffer[i];
k++;
}
}
else
{
buffer2[k]=buffer[i];
k++;
}
}
}
/*
2 3
4 6
8 10
特比注意:負(fù)數(shù)需要補(bǔ)0為0-正數(shù)。
*/
int main()
{
int m=0,n=0;
char buffer[MAX_PATH] = {0};
gets(buffer);
// char buffer[MAX_PATH] = "3+2*{1+2*[⑷/(8⑹)+7]}";
char buffer2[MAX_PATH] = {0};
init(buffer,buffer2);
// printf(buffer2);
// printf("
");
int ret = calculate(buffer2);
// printf(stack_all);
// printf("
");
printf("%d",ret);
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)