• 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;
    		}
    	}
    }
    

    程序测试

  • 相关阅读:
    LINUX
    DOCKER
    计算牛的数目
    计算a^3=b^3+c^3+d^3
    用递归计算C(m,n)
    A Mathematical Curiosity
    Who is lier?
    我的第一篇 实习报告
    R.java
    天气系统
  • 原文地址:https://www.cnblogs.com/lty12345678911/p/8850031.html
Copyright © 2020-2023  润新知