程序员人生 网站导航

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

栏目:php教程时间:2015-08-24 07:57:33

4则运算
描写:  
请实现以下接口
    /* 功能:4则运算
     * 输入:strExpression:字符串格式的算术表达式,如: "3+2*{1+2*[⑷/(8⑹)+7]}"
         * 返回:算术表达式的计算结果
     */
    public static int calculate(String strExpression)
    {
        /* 请实现*/
        return 0;
    }
束缚:
pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。
pucExpression算术表达式的有效性由调用者保证;
 

 
知识点:  栈
题目来源:  内部整理
练习阶段:  中级
运行时间限制: 10Sec
内存限制: 128MByte
输入: 
输入1个算术表达式
 
输出: 
得到计算结果
 
样例输入: 
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 特比注意:负数需要补0为0-正数。 */ 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; }


 

 

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐