当前位置:   article > 正文

四则运算_四则运算程序csdn

四则运算程序csdn

四则运算(10分)
题目内容:
你的程序要读入一个整数四则运算的式子,其中的整数均为非负整数且小于10000,其中的运算符只有加减乘除(+-/)四种。你的程序要计算这个式子,并输出结果。计算过程必须遵守优先级,即/要在+-之前先计算。当遇到除法时,按照Java语言的整数除法的方式来计算,即只留下整数的结果。

提示:一种可能的做法是,将整个式子作为字符串读入,然后找出其中最后一个+或-,在此位置将其截成两段,分别计算后再做+或-,以此类推。
另,用Integer.parseInt(s)可以从一个字符串得到整数。

输入格式:
输入时,所有的成分均没有有空格隔开,即每个运算符前后都没有空格。注意,单个数字也是可能的一种输入。

输出格式:
输出结果的整数。

输入样例:
2+3*4-6/3

输出样例:
12

import java.util.Scanner;

public class Main {
    public static int find(String ch,int p)//找到乘除的位置
    {
        p++;
            if(ch.indexOf('*',p)==-1)
            {   
                p=ch.indexOf('/',p);
                return p;
            }
            if(ch.indexOf('/',p)==-1)
            {
                p=ch.indexOf('*',p);
                return p;
            }
        if(ch.indexOf('*',p)!=-1 && ch.indexOf('/',p)!=-1)
        {
            if(ch.indexOf('*',p) > ch.indexOf('/',p) )
            {
                p=ch.indexOf('/',p);
            }
            if(ch.indexOf('*',p) < ch.indexOf('/',p))
            {
                p=ch.indexOf('*',p);
            }
        }
        else if(ch.indexOf('*',p)==-1 && ch.indexOf('/',p)==-1)
        {
            p=-1;
        }
        return p;

    }
    public static int md(String ch)
    {

        int a,b,n1=1;
        if(ch.charAt(0)=='-' || ch.charAt(0)=='+')
        {
            if(ch.charAt(0)=='-')
            {
                n1=-1;
            }
            ch=ch.substring(1);
        }
        String c;
        a=find(ch,0);
        b=find(ch,a);
        if(a!=-1)
        {   
            c=ch.substring(0,a);
            n1*=Integer.parseInt(c);
            while(a!=-1)
            {

                if(ch.charAt(a)=='*')
                {
                    if(b==-1)
                    {
                        n1=n1*Integer.parseInt(ch.substring(a+1));
                        break;
                    }
                    if(b!=-1)
                    {
                        n1=n1*Integer.parseInt(ch.substring(a+1, b));   
                    }
                }

                if(ch.charAt(a)=='/')
                {
                    if(b==-1)
                    {
                        n1=n1/Integer.parseInt(ch.substring(a+1));
                        break;
                    }
                    if(b!=-1)
                    {
                        n1=n1/Integer.parseInt(ch.substring(a+1, b));
                    }

                }
                a=b;
                b=find(ch,b);
            }   
        }
        else
        {
            c=ch.substring(0);
            n1*=Integer.parseInt(c);
        }
        return n1;
    }
    public static String divide(String str)
    {
        String ch=str;
        int a,b;
        a=str.lastIndexOf('+');
        b=str.lastIndexOf('-');
        if(a > b)//加更靠后
        {
            ch=str.substring(a);
        }
        else if(b > a)
        {
            ch=str.substring(b);
        }
        if(a==-1 && b==0)
        {
            int i;
            for(i=1; i<str.length() && str.charAt(i)!='+' && str.charAt(i)!='-';i++);
            ch=str.substring(0,i);  
        }
        if(a==-1 && b==-1)//
        {
            int i;
            for(i=0; i<str.length() && str.charAt(i)!='+' && str.charAt(i)!='-';i++);
            ch=str.substring(0,i);
        }

        return ch;
    }
    public static String part(String str)
    {
        String ch=null;
        int a,b;
        a=str.lastIndexOf('+');
        b=str.lastIndexOf('-');
        if(a > b)
        {
            ch=str.substring(0, a);
        }
        if(b > a)
        {
            ch=str.substring(0, b);
        }
        if(a==-1 && b==0)
        {
            ch=null;
        }
        if(a==-1 && b==-1)//
        {
            ch=null;
        }
        return ch;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
                String str,ch,c,num1,num2;
                int i,j,sum=0,n1,n2,a,b;
                str = in.nextLine();
                while(str!=null)
                {
                    ch=divide(str);
                    sum+=md(ch);
                    str=part(str);
                }
                System.out.println(sum);
    }

}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/913741
推荐阅读
相关标签
  

闽ICP备14008679号