简单计算器 / 括号匹配

1.简单计算器

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

样例输入

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出

12178.21

步骤:

  • 1.中缀表达式转后缀表达式
  • 2.计算后缀表达式
#include<cstdio>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

struct node{
   double num;  //操作数
   char op; //操作符
   bool flag;//true代表操作数,flase代表操作符
};

string str;
stack<node> s;//操作符栈
queue<node> q; //后缀表达式序列
map<char,int> op;//操作符优先级映射

//将中缀表达式转换为后缀表达式
void change(){
    double num;
    node  temp;
    for(int i=0;i<str.length();){
       //如果是数字
       if(str[i]>='0'&&str[i]<='9'){  
        temp.flag=true;  //标记为数字
        temp.num=str[i]-'0'; //记录这个操作数的第一个位数
        i++;
        while(str[i]>='0'&&str[i]<='9'&&i<str.length()){
            temp.num=temp.num*10+(str[i]-'0');  //更新这个操作数
            i++;
        }
        q.push(temp);        //将数字压入队列
       }else{         //如果是操作符
          temp.flag=false;  //标记操作符
    //只要操作符栈的栈顶元素比操作符优先级高
    //就把操作符站栈顶元素弹出到后缀表达式队列中
          while(!s.empty()&&op[str[i]]<=op[s.top().op]){
            q.push(s.top());
            s.pop();
          }
          temp.op=str[i];
          s.push(temp);
        i++;
       }
    }
    //如果操作数中还有操作符,就把他弹出到后缀表达附中 
    while(!s.empty()){
         q.push(s.top());
         s.pop();
    }
}


//计算后缀表达式

double Cal(){
    double temp1,temp2;
    node cui,temp;

    while(!q.empty()){
        cui=q.front();  //记录队首元素
        q.pop();

        if(cui.flag==true){  //操作数直接压入栈
            s.push(cui);
        }else{     
            //如果是操作符则将栈顶两个元素弹出,进行对应的运算后将结果压入栈中
            temp2=s.top().num; 
            s.pop();
            temp1=s.top().num;
            s.pop();
            if(cui.op=='+')
                temp.num=temp1+temp2;
            else if(cui.op=='-')
                temp.num=temp1-temp2;
            else if(cui.op=='*')
                temp.num=temp1*temp2;
            else{
                temp.num=temp1/temp2;
            }
            temp.flag=true;
            s.push(temp);
        }
    }
   return s.top().num;  //栈顶元素就是后缀表达式的值
}


int main(){
    op['+']=op['-']=1;  //设定操作数的优先级
    op['*']=op['/']=2;

    while(getline(cin,str),str!="0"){
        //把表达式中的空格去掉
        for(string::iterator it =str.end();it!=str.begin();it--){
            if(*it == ' '){
                str.erase(it);
            }
        }
    
    //初始化栈
        while(!s.empty()){
            s.pop();
        }
        //将中缀表达式转化为后缀表达式
        change();
        //计算后缀表达式
        printf("%.2f\n",Cal());
    }
return 0;

}




2.括号匹配

题目描述

请写一个程序,判断给定表达式中的括号是否匹配,表达式中的合法括号为”(“, “)”, “[", "]“, “{“, ”}”,这三个括号可以按照任意的次序嵌套使用。

输入

有多个表达式,输入数据的第一行是表达式的数目,每个表达式占一行。

输出

对每个表达式,若其中的括号是匹配的,则输出”yes”,否则输出”no”。

样例输入

4
[(d+f)*{}]
[(2+3))
()}
[4(6]7)9

样例输出

yes
no
no
no

#include<cstdio>
#include<stack>
#include<iostream>
#include<string>
using namespace std;
const int ma=100;
stack <char>s;

int main(){
    int n;
    scanf("%d",&n);


    while(n--){

        string str;
        cin>>str;

        while(!s.empty()){
            s.pop();
        }
        bool flag=true;

        for(int i=0;i<str.length();i++){
            if(str[i]=='('||str[i]=='{'||str[i]=='['){
                s.push(str[i]);
            }
            else if(str[i]==')'){
               if(!s.empty() && s.top()=='(')
				{
					s.pop();//匹配则弹出
				}
				else
				{
					flag=false;
					break;
				}
           }
           else if(str[i]=='}'){
             if(!s.empty() && s.top()=='{'){
					s.pop();//匹配则弹出
            }
            else{
                flag=false;
                break;
            }
           }
            else if(str[i]==']'){
                if(!s.empty()&&s.top()=='['){
                    s.pop();
                }else{
                   flag=false;
                   break;
                }

           }
        }
        if(flag){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章