华为面试题：四则运算 C语言源码

4则运算

/* 功能：4则运算
* 输入：strExpression：字符串格式的算术表达式，如: "3+2*{1+2*[⑷/(8⑹)+7]}"
* 返回：算术表达式的计算结果
*/
public static int calculate(String strExpression)
{
/* 请实现*/
return 0;
}

pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’， ‘)’,‘[’, ‘]’,‘{’ ,‘}’。
pucExpression算术表达式的有效性由调用者保证;

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⑴]=='{'))
{
//可以不使用括号
// 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

*/
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;
}

1. 本站所有资源来源于用户上传和网络，如有侵权请邮件联系站长！
2. 分享目的仅供大家学习和交流，您必须在下载后24小时内删除！
3. 不得使用于非法商业用途，不得违反国家法律。否则后果自负！
4. 本站提供的源码、模板、插件等等其他资源，都不包含技术服务请大家谅解！
5. 如有链接无法下载、失效或广告，请联系管理员处理！
6. 本站资源售价只是赞助，收取费用仅维持本站的日常运营所需！
7. 如遇到加密压缩包，请使用WINRAR解压,如遇到无法解压的请联系管理员！