给班里同学布置的一道题目……虽然做的人并不算多。

布置一个课外作业:各位根据自己的能力去完成不同的级别:
LV1:完成一个计算器,跟老师的题目要求一样;
LV2:计算器的输入只有一个字符串的输入,如123312+3212回车得到结果
LV3:完成同级别两个以上的数字的运算,如123123+432432+543254回车得到结果
LV4:完成不同预算级别的数字运算,如123123+43243*432-43243/4324回车得到结果
隐藏级别:包括一些其他的运算符,如(4321341+432432)*43243-543/(432432-543)

目前我自己也只做到了LV4,看之后有没有机会去改进这些东西了。

NCnum.java

package caculator;
public class NCnum {
    public char f[];
    public double n[];
}

NewCaculator.java

package caculator;

import java.util.Scanner;

public class NewCaculator {
    public double iNum1, iNum2, oNum;
    public final String[] arithmetic = { "+", "-", "*", "/" };
    public final char[] num = { '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '0', '.' };
    public final char[] arith = { '+', '-', '*', '/' };
    private String str = "";
    private String ari = "", arip = "";

    // LV2
    public void solve() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您打算计算的方法,仅支持两个数的运算:");
        String str = input.nextLine();
        for (int i = 0; i < arithmetic.length; i++) {
            if (str.contains(arithmetic[i])) {
                iNum1 = Double.parseDouble(str.substring(0, str
                        .indexOf(arithmetic[i])));
                iNum2 = Double.parseDouble(str.substring(str
                        .indexOf(arithmetic[i]) + 1, str.length()));
                switch (i) {
                case 0:
                    oNum = iNum1 + iNum2;
                    break;
                case 1:
                    oNum = iNum1 - iNum2;
                    break;
                case 2:
                    oNum = iNum1 * iNum2;
                    break;
                case 3:
                    oNum = iNum1 / iNum2;
                    break;
                }
                System.out.println(iNum1 + arithmetic[i] + iNum2 + "=" + oNum);
                break;
            }
            if (i == arithmetic.length - 1) {
                System.out.println("运算符输入有误");
            }
        }
    }

    // 是否为数字
    private boolean isnum(char c) {
        boolean flag = true;
        for (int j = 0; j < num.length; j++) {
            if (num[j] == c) {
                break;
            }
            if (j == num.length - 1) {
                flag = false;
            }
        }
        return flag;
    }

    // 运算
    private double pcompute(double n1, double n2, char c) {
        double x = 0;
        switch (c) {
        case '+':
            x = n1 + n2;
            break;
        case '-':
            x = n1 - n2;
            break;
        case '*':
            x = n1 * n2;
            break;
        case '/':
            x = n1 / n2;
            break;
        default:
            System.out.println("方法使用错误");
            break;
        }
        return x;
    }

    // 运算字符串解析成NCnum类的对象
    private NCnum analyze() {
        NCnum nc = new NCnum();
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您打算计算的方法,仅支持多个数的运算(数1+运算符1+数2+运算符2+数3+……回车):");
        this.str = input.nextLine();
        for (int i = 0; i < str.length(); i++) {
            if (!this.isnum(str.charAt(i))) {
                this.ari += str.charAt(i) + ":";// String.valueOf(str.charAt(i));
                this.arip += i + ":";
            }
        }
        String[] A = this.ari.split(":");// 运算符号数组
        String[] B = this.arip.split(":");// 运算符号位置数组
        int[] p = new int[B.length];
        for (int i = 0; i < B.length; i++) {
            p[i] = Integer.parseInt(B[i]);
        }
        nc.f = new char[A.length];// 符号数组
        for (int i = 0; i < nc.f.length; i++) {
            nc.f[i] = A[i].charAt(0);
        }
        nc.n = new double[A.length + 1];// 运算数值数组
        for (int i = 0; i < nc.n.length; i++) {
            if (i < nc.n.length - 1 && i > 0) {
                nc.n[i] = Double.parseDouble(str.substring(p[i - 1] + 1, p[i]));
            } else if (i == 0) {
                nc.n[i] = Double.parseDouble(str.substring(0, p[i]));
            } else if (i == nc.n.length - 1) {
                nc.n[i] = Double.parseDouble(str.substring(p[i - 1] + 1, str
                        .length()));
            }
        }
        return nc;
    }

    // 迁移数组内的double数据数组
    private void movedarr(double x[], int index) {
        for (int i = index; i < x.length - 1; i++) {
            x[i] = x[i + 1];
        }
        x[x.length - 1] = 0;
    }

    // 迁移数组内的double数据数组
    private void movecarr(char x[], int index) {
        for (int i = index; i < x.length - 1; i++) {
            x[i] = x[i + 1];
        }
        x[x.length - 1] = ' ';
    }

    // 规则
    private double opRools(NCnum nc) {
        double sum = 0;
        for (int i = 0; i < nc.f.length; i++) {
            if (nc.f[i] == '*' || nc.f[i] == '/') {
                nc.n[i] = pcompute(nc.n[i], nc.n[i + 1], nc.f[i]);
                movecarr(nc.f, i);
                movedarr(nc.n, i + 1);
                i = -1;
            }
        }
        for (int i = 0; i < nc.f.length; i++) {
            if (nc.f[i] == '+' || nc.f[i] == '-') {
                nc.n[i] = pcompute(nc.n[i], nc.n[i + 1], nc.f[i]);
                movecarr(nc.f, i);
                movedarr(nc.n, i + 1);
                i = -1;
            }
        }
        sum = nc.n[0];
        return sum;
    }

    public void susolve() {
        // 测试题目:342121+543254-543254*543245/543254+543245/543254
        System.out.println(opRools(this.analyze()));

    }

    public static void main(String[] args) {
        NewCaculator nc = new NewCaculator();
        nc.susolve();
    }
}

见笑见笑!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值