基本计算器

来源:互联网 发布:董承非 知乎 编辑:程序博客网 时间:2024/05/29 09:40

输入表达式字符串,以“=”表示结束, 计算并输出表达式值。 操作数可以是正负整数或实数,操作符有“+”“-”“*”“/”“^”(乘方)和“sin( )”(正弦)、“cos( )”(余弦)、“log( )”(对数)、“ln( )”(自然对数)等函数。


int transfer(char c)

{

    if(c=='+'||c=='-')

        return1;

    if(c=='*'||c=='/')

        return2;

    if(c=='^')

        return3;

    if(c=='s'||c=='c'||c=='g'||c=='n')

        return4;

    if(c=='(')

        return5;

    return0;

}

void push_in(stack<char> &op_s,vector<string> &res,char c)

{

    if(c=='('||op_s.empty())

    {

        op_s.push(c);

        return;

    }

    if(c==')')

    {

        while(op_s.top()!='(')

        {

            string k;

            k+=op_s.top();

            res.push_back(k);

            op_s.pop();

        }

        op_s.pop();

        return;

    }

    if(transfer(c)>transfer(op_s.top())||op_s.top()=='(')

    {

        op_s.push(c);

        return;

    }

    if(transfer(c)<=transfer(op_s.top()))

    {

        while(transfer(c)<=transfer(op_s.top())&&transfer(op_s.top())!=5)

        {

            string k;

            k+=op_s.top();

            res.push_back(k);

            op_s.pop();

            if(op_s.empty())

                break;

        }

        op_s.push(c);

        return ;

    }

}


void show(stack<char> s)

{

    cout<<"now stack is: ";

    while(!s.empty())

    {

        cout<<s.top()<<' ';

        s.pop();

    }

    cout<<endl;

}


string SetString(string str)

{

    string s;

    if(str[0]=='-')

        s+='0';

    for(int i=0;i<=str.size()-1;i++)

    {

            

        if(i<str.size()-1&&str[i]=='('&&str[i+1]=='-')

        {

            s+='(';

            s+='0';

        }

        else

            s+=str[i];

    }

    return s;

}

vector<string> calculate(string str)

{

    str=SetString(str);

    stack<char> operator_s;

    vector<string> res;

    int i=-1;

    while(str[++i]!='=')

    {

        if(str[i]>='0'&&str[i]<='9')

        {

            string num; num+=str[i];

            i++;

            while(str[i]=='.'||(str[i]>='0'&&str[i]<='9'))

            {

                num+=str[i];

                i++;

            }

            res.push_back(num);

            i--;

            continue;

        }

        else

        {

        if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^'||str[i]=='('||str[i]==')')

           {

               //show(operator_s);

               push_in(operator_s, res, str[i]);

               continue;

           }

            if(str[i]=='s'||str[i]=='c')

            {

                push_in(operator_s, res, str[i]);

                i=i+2;

                continue;

            }

            if(str[i]=='n'||str[i]=='g')

            {

                push_in(operator_s, res, str[i]);

                continue;

            }

        }

        

    }

    while(!operator_s.empty())

    {

        string k;

        k+=operator_s.top();

        res.push_back(k);

        operator_s.pop();

    }

    return res;

}




double result(vector<string> &a)

{

    stack<string> s;

    for(auto n:a)

    {

        if(n[0]>='0'&&n[0]<='9')

            s.push(n);

        else

        {

            if(n[0]=='+')

            {

                string s1=s.top(); s.pop();

                string s2=s.top(); s.pop();

                double c1 =atof(s1.c_str());

                double c2 =atof(s2.c_str());

                double l=c1+c2;

                s.push(to_string(l));

                

            }

            elseif(n[0]=='-')

            {

                string s1=s.top(); s.pop();

                string s2=s.top(); s.pop();

                double c1 =atof(s1.c_str());

                double c2 =atof(s2.c_str());

                double l=c2-c1;

                s.push(to_string(l));

            }

            elseif(n[0]=='*')

            {

                string s1=s.top(); s.pop();

                string s2=s.top(); s.pop();

                double c1 =atof(s1.c_str());

                double c2 =atof(s2.c_str());

                double l=c1*c2;

                s.push(to_string(l));

            }

            elseif(n[0]=='/')

            {

                string s1=s.top(); s.pop();

                string s2=s.top(); s.pop();

                double c1 =atof(s1.c_str());

                double c2 =atof(s2.c_str());

                double l=c2/c1;

                s.push(to_string(l));

            }

            elseif(n[0]=='^')

            {

                string s1=s.top(); s.pop();

                string s2=s.top(); s.pop();

                double c1 =atof(s1.c_str());

                double c2 =atof(s2.c_str());

                double l=pow(c2,c1);

                //cout<<"pow"<<l<<endl;

                s.push(to_string(l));

            }

            elseif(n[0]=='s')

            {

                string s1=s.top(); s.pop();

                double c1=atof(s1.c_str());

                double l=sin(c1);

                s.push(to_string(l));

            }

            elseif(n[0]=='c')

            {

                string s1=s.top(); s.pop();

                double c1=atof(s1.c_str());

                double l=cos(c1);

                s.push(to_string(l));

            }

            elseif(n[0]=='g')

            {

                string s1=s.top(); s.pop();

                double c1=atof(s1.c_str());

                double l=log2(c1);

                s.push(to_string(l));

            }

            elseif(n[0]=='n')

            {

                string s1=s.top(); s.pop();

                double c1=atof(s1.c_str());

                double l=log(c1);

                s.push(to_string(l));

            }

        }

    }

    returnatof(s.top().c_str());

}

int main()

{

    cout<<"请输入表达式"<<endl;

    string s;

    cin>>s;

    vector<string> a=calculate(s);

    for(int i=0;i<=a.size()-1;i++)

        cout<<a[i]<<' ';

    cout<<endl;

    cout<<result(a);

}






原创粉丝点击