计算字符串运算的结果 如 (10.5*((1+4/2)-5+(9+1)))/2.0

某公司的笔试题,做出来还是花了点时间 用比较笨的方法

/*************************************************************************
    > File Name: numstr_calculate.cpp
    > Author: chunquanL
    > Created Time: 2017-03-22
 ************************************************************************/

#include
#include
#include
using namespace std;


//计算字符串运算的结果
//(10.5*((1+4/2)-5+(9+1)))/2.0

int isnum(char ch)
{
    if(ch=='.')
        return 1;
    return ('0'<=ch && '9'>=ch)?1:0;
}
double myatof(const char *str,int *len)
{
    double result=0;
    double base =0.1;
    int length=0;
    int i=0;
    for(int j=0;isnum(str[j]);j++)
        length++;
    *len = length;
    while(i'.')
    {
        result*=10;
        result += str[i]-'0';
        i++;
    }
    if(i'.')
    {
        i++;
    }
    while(i'0')*base;
        i++;
        base*=0.1;
    }

    for(int i=0;ireturn result;
}



double getResult_normal(vector<double> &num,vector<char> &sign,int start,int end)
{
    //首先处理乘除法

    int num_of_handle1=0; //乘除运算次数
    int num_of_handle2=0; //加减运算次数

    double result;
    for(int i=start;i<=end;i++)
    {
        if(sign[i]=='*' || sign[i]=='/')
            num_of_handle1++;
        if(sign[i]=='+' || sign[i]=='-')
            num_of_handle2++;
    }
    while(num_of_handle1)
    {
        int i;
        for(i=start;i<=end;i++)
        {
            if(sign[i]=='*' || sign[i]=='/')
                break;
        }
        int before = i;
        int next = i;
        double res=0;
        while(sign[--before]!='n');
        while(sign[++next]!='n');
        if(sign[i] == '*')
            res = num[before]*num[next];
        else if(sign[i]=='/')
            res = num[before]/num[next];
        sign[before]=' ';
        sign[next] = ' ';
        sign[i] = 'n';
        num[i] = res;
        num_of_handle1--;

    }
    while(num_of_handle2)
    {
        int i;
        for(i=start;i<=end;i++)
        {
            if(sign[i]=='+' || sign[i]=='-')
                break;
        }
        int before = i;
        int next = i;
        double res=0;
        while(sign[--before]!='n');
        while(sign[++next]!='n');
        if(sign[i] == '+')
            res = num[before]+num[next];
        else if(sign[i]=='-')
            res = num[before]-num[next];
        sign[before]=' ';
        sign[next] = ' ';
        sign[i] = 'n';
        num[i] = res;
        result = res;
        num_of_handle2--;
    }
    return result;
}

double getResult(char *expr,int length)
{
    if(!expr)
        return 0;

    vector<double> num;
    vector<char> sign;

    for(int i=0;iif(isnum(expr[i]))
        {
            int len=0;
            double res = myatof(&expr[i],&len);
            i+=len;
            num.push_back(res);
            sign.push_back('n');
        }
        else
        {
            sign.push_back(expr[i]);
            i++;
            num.push_back(0.0);
        }
    }
/*
    for(int i=0;i
    //先解决括号
    int num_of_kuohao = 0;
    for(int i=0;iif(sign[i]=='(')
            num_of_kuohao++;
    }
    if(num_of_kuohao)
    {
        while(num_of_kuohao)
        {
            //找到最后面那个括号的位置
            int before=0;
            int cnt=0;
            for(before=0;beforeif(sign[before]=='(')
                    ++cnt;
                if(cnt==num_of_kuohao)
                    break;
            }
            //找到后面的第一个括号
            int next = before;
            for(;nextif(sign[next]==')')
                    break;
            }
            getResult_normal(num,sign,before+1,next-1);
            sign[before] = ' ';
            sign[next] =' ';
            num_of_kuohao--;
        }
    }
    getResult_normal(num,sign,0,length-1);
//  cout<
    double res=0;
    for(int i=0;iif(sign[i]=='n')
            res=num[i];
    }
    return res;
}

int main(void)
{
/*  char a[]="0.12121";
    int len;
    cout<

    char str[] = "((1.0+2.3)*2)+3/2*(6-5.9)";
    for(int i=0;i<strlen(str);i++)
        cout<cout<cout<strlen(str))<return 0;
}


你可能感兴趣的:(面试&笔试)