zoukankan      html  css  js  c++  java
  • 简单计算器

    简单计算器

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 34   Accepted Submission(s) : 11

    Font: Times New Roman | Verdana | Georgia

    Font Size:

    Problem Description

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

    Input

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

    Output

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

    Sample Input

    1 + 2
    4 + 2 * 5 - 7 / 11
    0
    

    Sample Output

    3.00
    13.36
    

    Source

    浙大计算机研究生复试上机考试-2006年
    数据从左到右输入
    运算也应从做到右;
    运算符从左到右入栈;
    要想运算从左到右
    将数据和运算符  移动到另外两个栈里面;
    数据输入 遇到 *  /  先处理数据
    #include<iostream>
    #include<stack>
    using namespace std;
    int main()
    {
        char word[205];
        while(gets(word))
        {
            if(strlen(word)==1&&word[0]=='0')
                break;
            
            int num;
            num=0;
            stack<double> shu;
            stack<char> fuhao;
            int nowshu=0;//记录数值
            for(num=0;word[num]!='
    ';)
            {
                nowshu=0;
                if(word[num]>='0'&&word[num]<='9')
                {
                    while(word[num]>='0'&&word[num]<='9')
                    {
                        nowshu=nowshu*10+word[num++]-'0';
                        
                    }
                    shu.push(nowshu);
                    continue;
                    
                }
                else    if(word[num]==' ')
                {
                    num++;
                    continue;
                    
                }
                else    if(word[num]=='+')//加法
                {
                    fuhao.push(word[num]);
                    num++;                 //符号进栈
                    continue;
                    
                }
                else    if(word[num]=='-')//减法
                {
                    fuhao.push(word[num]);
                    num++;                //符号进栈
                    continue;
                }
                else    if(word[num]=='*')//乘法
                {
                    double number;
                    num++;
                    while(word[num]==' ')
                    {
                        num++;
                    }
                    
                    while('0'<=word[num]&&word[num]<='9')//取下一个数
                    {
                        nowshu=nowshu*10+word[num++]-'0';
                        
                    }
                    number=nowshu*shu.top();
                    shu.pop();
                    shu.push(number);
                    
                    continue;
                }
                else    if(word[num]=='/')//除法
                {
                    double number3;
                    num++;
                    while(word[num]==' ')
                    {
                        num++;
                    }
                    while('0'<=word[num]&&word[num]<='9')//取下一个数
                    {
                        nowshu=nowshu*10+word[num++]-'0';
                        
                    }
                    number3=shu.top()/nowshu;
                    shu.pop();
                    shu.push(number3);
                    
                    continue;
                }
                else break;
                
                
            }//除法 乘法处理完
            stack<double>shu2;//数据逆置
            stack<char>fuhao2;//运算从左到右 
            while(!shu.empty())
            {
                shu2.push(shu.top());
                shu.pop();
            }
            while(!fuhao.empty())
            {
                fuhao2.push(fuhao.top());
                fuhao.pop();
            
            }
            while(!shu2.empty()&&!fuhao2.empty())//数据重来 运算从左到右
            {
                double number2;
                char fuhao3;
                number2=shu2.top();
                shu2.pop();
                fuhao3=fuhao2.top();
                if(fuhao3=='+')
                {
                    number2=number2+shu2.top();
                    shu2.pop();
                    shu2.push(number2);
                    fuhao2.pop();
                    
                    
                    
                }
                if(fuhao3=='-')
                {
                    number2=number2-shu2.top();
                    shu2.pop();
                    shu2.push(number2);
                    fuhao2.pop();
                    
                    
                }
                
            }//所有符号处理完
            printf("%.2lf
    ",shu2.top());
            
        }
        
        return 0;
    }
    /*
    1 + 2
    5
    4 + 2 * 5 - 7 / 11
    3
    0 + 5
    1 - 2 * 3 * 4 + 5 * 6
    1 * 2 * 3 + 5 + 6 - 7 * 8 + 9 / 10
    0 + 0 * 0
    1 + 5 * 0
    0 + 5
    0
    
      */
     
  • 相关阅读:
    Android 实现书籍翻页效果番外篇之光影效果
    ViewPager + Fragment 替换 TabActivity
    蓝绿简约可重复使用的简约Tab选项卡
    黑色漂亮的DIV+CSS导航菜单代码
    JavaScript+Css打造三种简洁的Tab网页选项卡
    来自中国站长站的导航菜单代码【强烈推荐】
    蓝紫色背景的漂亮CSS菜单代码
    仿Vista风格按钮菜单代码(纯CSS打造)
    仿Vista金属感导航菜单代码
    精致纯CSS打造绿色漂亮导航栏
  • 原文地址:https://www.cnblogs.com/2013lzm/p/3262590.html
Copyright © 2011-2022 走看看