• 四则运算需求分析和功能实现--杨宇杰


    四则运算需求分析和功能实现

    一.基本要求

    1.编写小学四则运算测试系统,要求完成两位数以内(包括两位数)的加,减,乘,除四则运算。下述所有四则运算表达式均需随机生成。使用参数能够控制生成题目的数量。

    (例如:12+6+7=3*4*6=?)

    2.能根据用户的输入来选择运算种类(例如:加法运算、减法运算....),让程序能接受用户输入答案,并判定对错。 最后给出总共 对/错 的数量。程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。例如,23 + 45 = 45 + 23 = 是重复的题目,6 × 8 = 8 × 6 = 也是重复的题目。

      

    二.扩展要求

    1.扩展用户的运算种类,除了整数,还要支持真分数四则运算

    (例如:1/3+3/5+2/7=?

    2.进一步扩展功能,实现混合四则运算,即扩展表达式类型。

    例如:25 - 3 * 4 - 2 / 2 + 89 =

            1/2 + 1/3 - 1/4 =

            5 - 4 * 3 +28=

    注:(1)判别各运算符在表达式中计算的优先级

       2)括号的生成位置

       3)分数与整数的混合运算,可以考虑将整数转化为分数形式,即分母为1

    下面是个人编写的真分数运算实现代码,供参考:

    	public static String caculate(String s){
    		String ss = s;
    		String end = null;
    		
    		int a1 = ss.charAt(0)-'0';
    		int a2 = ss.charAt(2)-'0';
    		int b1 = ss.charAt(4)-'0';
    		int b2 = ss.charAt(6)-'0';
    		int c = 1;
    
    		if(ss.charAt(3)=='-')
    			c = -1;
    		int e1 = a1 * b2 + c * a2 * b1;
    		int e2 = a2 * b2;
    		int e11 = Math.abs(e1);
    		int e22 = Math.abs(e2);
    		if(e1 == 0)
    	      end = 0+"";
    		else
    			if(e11 % e22 == 0){
    				end = e1/e2 +"";
    			}else if(e22 % e11 == 0){
    				if(e2/e1<0)
    					end = "-" + 1 + "/" +Math.abs(e2/e1);
    				else
    				end = 1 + "/" +e2/e1;
    			}else if(c == 1){
    				end = e11 + "/" + e22;
    				}else 
    					end = "-" + e11 + "/" + e22;				
    		return end;
    	}
    

      生成真分数表达式核心代码:

    public class caculator {
    	
    	 //生成真分數表達式
    	public static String zhen(){
    		int y = (int) (Math.random()*2)+1;//随机生成运算符的数量
    		int[] w = new int[10];
    		String str =null;
    		if(y==1){
    			char ch = "+-".charAt((int) (Math.random()*2));
    			for(int i=0;i<(2*y+2);i++){
    				w[i]= (int) (Math.random()*9)+1;
    			}
    			str = w[0] + "/" + w[1] + ch + w[2] + "/" + w[3] + "=";
    			return str;
    		}
    		if(y==2){
    			for(int i=0;i<2*y+2;i++){
    				w[i] = (int) (Math.random()*9)+1;
    			}
    			char ch1 = "+-".charAt((int) (Math.random()*2));
    			char ch2 = "+-".charAt((int) (Math.random()*2));
    			str = w[0] + "/" + w[1] + ch1 + w[2] + "/" + w[3] + ch2 + w[4] + "/" + w[5] + "=";
    			return str;
    		}
    		if(y==3){
    			
    		}
    		return str;
    	}
    
    	//生成整数表达式
    	public static String zhengshu(){
    		int y = (int) (Math.random()*1)+2;//随机生成运算符的数量
    		int[] in = new int[10];
    		String str =null;
    		char ch1=0,ch2=0,ch3=0,ch4=0;
    		if(y==2){
    			for(int i=0;i<y+1;i++){
    				in[i] = (int) (Math.random()*30)+1;
    			}
    			int num = (int) (Math.random()*2);
    			if(num==0){//左边加括号
    				 ch1 = "+-".charAt((int) (Math.random()*2));
    				 ch2 = "*/".charAt((int) (Math.random()*2));
    			str = "(" + in[0] + ch1 + in[1] + ")" + ch2 + in[2] +"=";
    			}else{
    				 ch1 = "+-".charAt((int) (Math.random()*2));
    				 ch2 = "*/".charAt((int) (Math.random()*2));
    			str = in[0] + ch2 + "(" + in[1] + ch1 + in[2] + ")" +"=";
    			}
    			return str;
    		}
    		return str;
    	}
    

      

  • 相关阅读:
    重构的体会——类属性优先移动
    jQuery实现无限循环滚动公告
    jquery菜单左右翻屏效果
    44种IE css bug实例测试总结
    IE6不支持position:fixed的解决方法
    DedeCMS会员排行调用代码,实现连接到会员空间
    程序员们 不要想一辈子靠技术混饭吃
    Load JSON data with jQuery, PHP and MySQL
    mysql 实现行号的方法——如何获取当前记录所在行号
    jQuery精仿手机上的翻牌效果菜单
  • 原文地址:https://www.cnblogs.com/zlp2016218061/p/5941500.html
Copyright © 2020-2023  润新知