20165101刘天野 2017-2018-2 《Java程序设计》 结对编程练习_四则运算(第一周)

20165101刘天野 2017-2018-2 《Java程序设计》 结对编程练习_四则运算


需求分析

第一周完成内容

  • 四则运算(带括号)
  • 随机出题,系统给出正确答案
  • 简单的边界检测

    第二周待解决问题

  • 进行真分数运算
  • 能出复杂的算式
  • 对算式的正确性进行检测
  • 用户可以做题
  • 计算正确率
  • 进行详细的边界测试

实现代码

Main类
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Random rand = new Random();
        int a,b;
        char [] ch = {'+','-','*','/'};
        for(int i=0;i<60;i++){
            Calculator calculator = new Calculator();
        //  Scanner read = new Scanner(System.in);
            a = rand.nextInt(100);
            b = rand.nextInt(100);
            calculator.a = a+""+ch[rand.nextInt(ch.length)]+ b+"";
        //  calculator.a=read.nextLine();
            calculator.Getstack();
            System.out.print(calculator.a+"=");
            System.out.println(calculator.manage());
        }
    }
}
DataStack类
public class DataStack {
    int n;
    double [] a;
    int top = -1;
    public DataStack(int m) {
        this.n=m;
    }
    void getStack() {
        a = new double[n];
    }
    void push(double data) {
        a[++top]=data;
    }
    double pop() {
        return a[top--];
    }
}
CharStack类
public class CharStack {
    int n;
    char [] a;
    int top = -1;
    public CharStack(int m) {
        this.n=m;
    }
    void getStack() {
        a = new char[n];
    }
    void push(char data) {
        a[++top]=data;
    }
    char pop() {
        return a[top--];
    }   
}
Calculator类
import javax.swing.JApplet;

public class Calculator {
    String a = new String();
    DataStack datastack;
    CharStack charstack;
    void Getstack(){
        datastack = new DataStack(a.length());
        datastack.getStack();
        charstack = new CharStack(a.length());
        charstack.getStack();
    }
    double manage() {
        int i=0;
        while(true) {
            if(i>=a.length())
                break;
            char c = a.charAt(i);
            if(c>='0' && c<='9') {
                String temp = String.valueOf(c);
                String s = new String();
                s = s+temp;
                i++;
                if(i>=a.length()) {
                    datastack.push(Double.parseDouble(s));
                    break;
                }
                c = a.charAt(i);
                while(c>='0' && c<='9') {
                    temp = String.valueOf(c);
                    s = s+temp;
                    i++;
                    if(i>=a.length())
                        break;
                    c = a.charAt(i);
                }
                datastack.push(Double.parseDouble(s));
                continue;
            }
            else if(c=='*' || c=='/') {
                while(true) {
                    if(charstack.top==-1) {
                        charstack.push(c);
                        break;
                    }
                    if(charstack.a[charstack.top]=='+' || charstack.a[charstack.top]=='-') {
                        charstack.push(c);
                        break;
                    }
                    else if((charstack.a[charstack.top]=='*' || charstack.a[charstack.top]=='/') && (datastack.top>=1)) {
                        calculate(charstack.a[charstack.top]);
                        continue;
                    }
                    else {
                        charstack.push(c);
                        break;
                    }
                }
                
            }
            else if(c=='+' || c=='-') {
                while(true) {
                    if(charstack.top==-1) {
                        charstack.push(c);
                        break;
                    }                       
                    else if((charstack.a[charstack.top]=='+' || charstack.a[charstack.top]=='-' || charstack.a[charstack.top]=='*' || charstack.a[charstack.top]=='/') && (datastack.top>=1)) {
                        calculate(charstack.a[charstack.top]);
                        continue;
                    }
                    else {
                        charstack.push(c);
                        break;
                    }
                }
                
            }
            else if(c=='(') {
                charstack.push(c);
            }
            else if(c==')') {
                while(true) {
                    if(charstack.a[charstack.top]=='(') {
                        charstack.pop();
                        break;
                    }
                    else {
                        calculate(charstack.a[charstack.top]);
                    }
                }
            }
            i++;
        }
        while(true) {
            if(datastack.top>=1)
                calculate(charstack.a[charstack.top]);
            else
                break;
        }
        return datastack.pop();
    }
    void calculate(char c) {
        double a,b;
        charstack.pop();
        b = datastack.pop();
        a = datastack.pop();
        switch(c) {
            case '+':
                datastack.push(a+b);
                break;
            case '-':
                datastack.push(a-b);
                break;
            case '*':
                datastack.push(a*b);
                break;
            case '/':
                if(b==0) {
                    System.out.println("除数不能为0");
                    System.exit(0);
                }
                datastack.push(a/b);
                break;
        }
    }
}

程序测试

1043510-20180416012430152-668435438.png

转载于:https://www.cnblogs.com/lty12345678911/p/8850031.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值