zoukankan      html  css  js  c++  java
  • NYOJ

    #include<iostream>
    #include<string>
    #include<stack>
    #include<cstdio>
    using namespace std;
    stack<double>sn;
    stack<char>sc;
    	double num, des, dou;
    	bool prt, flag;
    	int t, len;
    	string str;
    bool isNum(char c)
    {
    	if(c >= '0' && c <= '9')
    		return true;
    	if(c== '.')
    	{
    		flag = true;
    		return true;
    	}
    	return false;
    }
    
    bool isOp(char c)
    {
    	if(c == '+' || c == '-' || c == '*' || c == '/')
    		return true;
    	return false;
    }
    
    void init()
    {
    	while(sn.size())	sn.pop();
    	while(sc.size())	sc.pop();
    	str.clear();
    	len = num = des = 0;
    	dou = 1;
    	prt = false;
    	flag = false;
    }
    
    void calm()
    {
    	if(sc.size() && isOp(sc.top()))
    	{
    		double ans;
    		double y = sn.top();
    		sn.pop();
    		double x = sn.top();
    		sn.pop();
    		char op = sc.top();
    		sc.pop();
    		switch(op)
    		{
    			case '+':	ans = x + y;	break;
    			case '-':	ans = x - y;	break;
    			case '*':	ans = x * y;	break;
    			case '/':	ans = x / y;	break;
    		}		
    //		cout<<op<<" "<<ans<<endl;
    		sn.push(ans);
    	}
    
    }
    
    int main()
    {
    //	freopen("in.in","r",stdin); 
    	cin>>t;
    	while(t--)
    	{
    		init();
    		cin>>str;
    		int len = str.length();
    		
    		for(int i = 0; i < len; i++)
    		{
    			if(isNum(str[i]))
    			{
    				if(str[i] == '.')	continue;
    				if(!flag)	num = num*10 + str[i] - '0';
    				else{
    					des = des*10 + str[i] - '0';
    					dou *= 10;
    				}
    			}
    			else
    			{
    				num = num + des/dou;
    				if(str[i] == '+' || str[i] == '-')
    				{	
    					if(!prt)	sn.push(num);
    					while(sc.size() && sc.top() != '(')calm();
    					sc.push(str[i]);
    					prt = false;
    				}
    				else if(str[i] == '*' || str[i] == '/')
    				{
    					if(!prt)	sn.push(num);
    					if(sc.size())
    						if(sc.top()=='*' || sc.top()=='/')
    							calm();
    					sc.push(str[i]);
    					prt = false;
    				}
    				else if(str[i] == '(')
    				{
    					sc.push(str[i]);
    				}
    				else if(str[i] == ')')
    				{
    					if(!prt)	sn.push(num);
    					while(sc.top() != '(')	calm();
    						
    					sc.pop();
    					prt = true;
    				}
    				else
    				{
    					if(!prt)	sn.push(num);
    					while(sn.size() > 1)	calm();				
    						printf("%0.2lf
    ",sn.top());
    					sn.pop();
    					break;
    				}
    				num = 0; 
    				flag = false;
    				dou = 1;
    				des = 0;
    			}
    		}		
    	}
    	
    
    	return 0;
    }        



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    关于C语言中类型的理解,有符号无符号的理解以及浮点数的理解
    关于集中注意力,情绪管理,记忆的总结整体
    关于链表逆置的问题
    git中reset和checkout的总结整理
    git中关于分支和stash的理解
    SVN和git的区别
    shell命令之find的用法
    (转载)获取服务器响应时间
    (转载)Spring定时任务的几种实现
    (转载)spring单例和多例详解。如何在单例中调用多例对象
  • 原文地址:https://www.cnblogs.com/you-well-day-fine/p/4671604.html
Copyright © 2011-2022 走看看