简易计算器

发布于:2022-12-14 ⋅ 阅读:(424) ⋅ 点赞:(0)

//学校c++第一节实验课作业,难点在于小数点后面的数据提取

#include <iostream>

#include <string>

#include <stack>

#include <math.h>

using namespace std;

int main()

{

    //存放操作符

    stack<char>oper_reserve;

    string s;

    int flag=0;

    cout<<"请输入计算式:";

    cin>>s;

    for(int i=0; i<s.size(); i++)

    {

        switch(s[i])

        {

        case '(':

            cout<<"操作符:左小括号"<<endl;

            oper_reserve.push(s[i]);

            break;

        case '[':

            cout<<"操作符:左中括号"<<endl;

            oper_reserve.push(s[i]);

            break;

        case'{':

            cout<<"操作符:左大括号"<<endl;

            oper_reserve.push(s[i]);

            break;

        case '+':

            cout<<"操作符:加号"<<endl;

            break;

        case '-':

            cout<<"操作符:减号"<<endl;

            break;

        case '*':

            cout<<"操作符:乘号"<<endl;

            break;

        case '/':

            cout<<"操作符:除号"<<endl;

            break;

        case ')':

            if(oper_reserve.empty())

                cout<<"操作符:右小括号匹配错误"<<endl;

            else

            {

                if (oper_reserve.top()=!'(')

                {

                    cout<<"操作符:右小括号 匹配错误"<<endl;

                    oper_reserve.pop();

                }

                else

                {

                    cout<<"操作符:右小括号"<<endl;

                    oper_reserve.pop();

                }

            }

            break;

        case ']':

            if(oper_reserve.empty())

                cout<<"操作符:右中括号 匹配错误"<<endl;

            else

            {

                if (oper_reserve.top()=!'[')

                {

                    cout<<"操作符:右中括号 匹配错误"<<endl;

                    oper_reserve.pop();

                }

                else

                {

                    cout<<"操作符:右中括号"<<endl;

                    oper_reserve.pop();

                }

            }

            break;

        case'}':

            if(oper_reserve.empty())

                cout<<"操作符:右大括号 匹配错误"<<endl;

            else

            {

                if (oper_reserve.top()=!'{')

                {

                    cout<<"操作符:右大括号 匹配错误"<<endl;

                    oper_reserve.pop();

                }

                else

                {

                    cout<<"操作符:右大括号"<<endl;

                    oper_reserve.pop();

                }

            }

            break;

        }

       if((s[i]<'9'&&s[i]>'0')||s[i]=='.')   //若扫描到数字

        {

            float cur_num=0;

            float decimal=0;

            int j;

            j=i;

            while(j<s.size()&&(s[j]<'9'&&s[j]>'0')||s[j]=='.')

            {

               if(s[j]=='.')

                {   flag=j;   //记录小数点位置

                    int k=j;

                   while(k<s.size()&&s[k+1]<'9'&&s[k+1]>'0')

                    {

                            double index=k+1-flag;         //index代表小数点后x位

                        decimal=(s[k+1]-'0')*pow(0.1,index)+decimal;        //循环求小数部分

                        k++;

                        i=k;

                        j=j+index+1;     //赋值后s[j]指向数字后面一位符号,如12.3+ 此时s[j]是+

                    }

                }

                else

                {

                cur_num=cur_num*10+(s[j]-'0');

                j++;

                i=j-1;

                }

            }

           float sum=cur_num+decimal;

            cout<<"操作数:"<<sum<<endl;

        }

    }

    return 0;

}

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到