结对编程《四则运算》

1.结对编程题目是:面向小学生,随机生成30个100以内的四则运算,并且用户可以录入结果,检查用户录入的结果是否是正确的。

2.结对编程对象:晏蜀杰,博客地址:http://www.cnblogs.com/ysjwj320/

3双方贡献度: 1:2

4.结对编程照片:

5.源程序代码:

MyExpression.java

 

package com.yanshujie.calculator;

import java.util.Map;

public interface MyExpression {
	
	public void setCount(int count);
	
	public Map<String,Number> getExpressions();
}

  

ObtainExpression.java

 

package com.yanshujie.calculator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

public class ObtainExpression implements MyExpression{
	
	private int count = 100;
	private Number start;
	private Number end;
	private boolean isDecimal = true;
	private static final List<String> operators = Arrays.asList(new String[]{"+","-","*","/"});
	
	public ObtainExpression(@NonNull Number start, @NonNull Number end) throws IllegalArgumentException{
		setNumbers(start,end);
	}
	
	public ObtainExpression(@NonNull Number start, @NonNull Number end, int count) throws IllegalArgumentException{
		this(start,end);
		setCount(count);
	}
	
	@Override
	public void setCount(int count){
		if( !isDecimal ){
			int range = end.intValue() - start.intValue() + 1;
			if( this.count > range*(range-1)){
				throw new IllegalArgumentException("the count of expressions is bigger than the count of all expressions.");
			}
		}
		this.count = count;
	}
	
	@Override
	public Map<String,@NonNull Number> getExpressions(){
		Map<String,Number> map = new HashMap<String,Number>();
		if( !isDecimal ){
			int start = this.start.intValue();
			int end = this.end.intValue();
			Random random = new Random(System.currentTimeMillis());
			while(map.size() < this.count ){
				int leftNumber = random.nextInt(end-start+1)+start;
				int rightNumber = random.nextInt(end-start+1)+start;
				int oper = random.nextInt(operators.size());
				if( rightNumber == 0 && operators.get(oper).equals("/") ){
					continue;
				}
				StringBuilder builder = new StringBuilder();
				builder.append(leftNumber).append(operators.get(oper)).append(rightNumber).append("=");
				if( map.containsKey(builder.toString()) ){
					continue;
				}
				Number result = compute(leftNumber,rightNumber,operators.get(oper));
				map.put(builder.toString(), result);		
			}
		}else{
			// TODO to handle double numbers
		}
		
		return map;
	}
	
	private Number compute(int leftNumber, int rightNumber, String operator){
		Number result = null;
		switch(operator){
		case "+":
			result = leftNumber + rightNumber;
			break;
		case "-":
			result = leftNumber - rightNumber;
			break;
		case "*":
			result = leftNumber * rightNumber;
			break;
		case "/":
			result = (leftNumber*1.0) / rightNumber;
			break;
		default:
			break;
		}
		return result;
	}
	
	private void setNumbers(@NonNull Number start, @NonNull Number end){
		if( start instanceof Integer && end instanceof Integer){
			this.isDecimal = false;
		}
		if( start.doubleValue() > end.doubleValue() ){
			throw new IllegalArgumentException("the start number must be bigger than the end number");
		}
		this.start = start;
		this.end = end;
	}
}

  

Calculator.java

 

package com.yanshujie.calculator;

import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Calculator {
	private static final Scanner input = new Scanner(System.in);
	
	public static void main(String[] args) {
		System.out.print("please input the start number:");
		int start = getInt();
		System.out.print("please input the end number:");
		int end = getInt();
		System.out.print("please input the count of expressions:");
		int count = getUnsignedInt();
		MyExpression myexp = new ObtainExpression(start, end);
		myexp.setCount(count);
		Map<String,Number> result = myexp.getExpressions();
		Set<String> keys = result.keySet();
		System.out.print("to output the result,yes(y) or no(n)?:");
		String chosen = getChosen();
		System.out.println("the expressions are: ");
		for( String key : keys ){
			if( chosen.equals("y") || chosen.matches("Y")){
				System.out.println(key + result.get(key));
			}else{
				System.out.println(key);
			}
		}
		realse();
	}
	
	public static String getChosen(){
		String chosen = null;
		if( input.hasNext() ){
			chosen = input.next();
		}
		while(!chosen.matches("^[yYnN]$")){
			System.out.print("please input 'y' or 'n':");
			if( input.hasNext()){
				chosen = input.next();
			}
		}
		return chosen;
	}
	
	public static int getUnsignedInt(){
		int unsignedNum = getInt();
		while(unsignedNum <= 0){
			System.out.print("input error,please input an unsigned number:");
			unsignedNum = getInt();
		}
		return unsignedNum;
	}
	
	public static int getInt(){
		do{
			if( input.hasNextInt() ){
				return input.nextInt();
			}else{
				System.out.print("input error,please input the number:");
			}
		}while(true);
	}
	
	public static void realse(){
		if( input != null ){
			input.close();
		}
	}
}

  

posted @ 2016-04-10 23:47  MoMask  阅读(207)  评论(0编辑  收藏  举报