5.1(封装,测试)

程序源代码(简单Java版):

package arrays.myArray;

import java.util.Scanner;

public class SortObject {
 private static int intercePosition = 0; // 记录单个运算数据的长度
 private static int[] intercePositionIndex = null; // 记录“(”的下标
 private static int[] intercePositionEnd = null; // 记录“)”的下标

 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  do {
   System.out.println("请输入你要计算的字符串(注意:只能输入数字和加,减,乘除符号;输入完毕后,请直接回车):");
   String numberString = input.next().trim();
   // 判断输入的运算字符串是否符合规定
   if (ispassString(numberString) == false) {
    System.out.println("您输入的计算字符串有误,请正确输入!");
   } else {
    // 计算结果返回
    System.out.println(interceResult(numberString));
   }
  } while (true);
 }

 // 判断是否有带括号的运算字符串存在
 private static String interceResult(String str) {
  String result = str;
  char[] numberString = str.toCharArray(); // 1+2+(1*2+1-1*2+5)+2+(1+5+9+10-11)+1*5/2+3
  // 1+8-9+(1*8/2-5+(1+2+8))+4/5*8/3*2
  int IndexStart = 0; // 记录“(”的实际数量
  int EndStart = 0; // 记录“)”的实际数量
  for (int i = 0; i < numberString.length; i++) {
   if (‘(‘ == numberString[i]) {
    // 记录最后一个正括号的位置
    IndexStart = i;
   }
   if (‘)‘ == numberString[i]) {
    // 记录反括号的最初始下标的位置
    EndStart = i;

    // 截取最里面一个括号里的运算字符串
    result = result.substring(IndexStart + 1, EndStart);

    // 截取括号的运算字符串进行运算,生成新的运算字符串
    result = str.substring(0, IndexStart)
      + interceptOperation(result, ‘*‘, ‘/‘)
      + str.substring(EndStart + 1, str.length());

    // 回调执行,其它小括号的运算字符串
    return interceResult(result);
   }
   if (i == numberString.length - 1)
    if (EndStart == 0)
     break;
  }
  // 不存在括号了,再进行混合运算
  result = interceptOperation(str, ‘*‘, ‘/‘);
  return result;
 }

 // 不带括号的四则运算
 private static String interceptOperation(String operationNumber, char a,
   char b) {
  String mess = operationNumber;
  char[] stringOperation = mess.toCharArray();

  // 循环遍历运算字符串,并做相应的运算
  for (int i = 0; i < stringOperation.length; i++) {

   // 判断运算符所在的索引
   if (stringOperation[i] == a || stringOperation[i] == b) {
    if (i != 0) {
     // 运算符前的第一个数
     double num1 = interceptNumIndex(mess.substring(0, i));

     // 记录第一个数据的长度
     int frontPosition = intercePosition;

     // 运算符前的第二个数
     double num2 = interceptNumEnd(mess.substring(i + 1,
       stringOperation.length));

     // 记录第二个数据的长度
     int backPosition = intercePosition;

     // 算完乘除,将结果替换到原来运算的位置,得到新的运算字符串
     String IndexMess = mess.substring(0, i - frontPosition + 1);
     String IndexResult = "";

     // 判断是否运算到最后的结果了
     if (IndexMess.indexOf(‘+‘) == -1
       && IndexMess.indexOf(‘*‘) == -1
       && IndexMess.indexOf(‘/‘) == -1
       && IndexMess.lastIndexOf(‘-‘) == -1)
      IndexMess = "";
     if (IndexMess != "")
      IndexResult = IndexMess.lastIndexOf(‘-‘) == IndexMess
        .length() - 1 ? IndexMess.substring(0, i
        - frontPosition) : IndexMess;

     // 组装新的运算字符串
     mess = IndexResult// mess.substring(0,i-frontPosition+1)
       + reslutString("" + stringOperation[i], num1, num2)
       + mess.substring(i + backPosition + 1);
     // 0.111/1212/2/2/2/2/2/2/2
     if (mess.lastIndexOf(‘-‘) == 0 && mess.indexOf(‘+‘) == -1
       && mess.indexOf(‘*‘) == -1
       && mess.indexOf(‘/‘) == -1) {
      break;
     }
     // 回调,继续运算
     return interceptOperation(mess, a, b);// 1+7-5+89/3+4-6*8/2+4-6
    } else
     continue;
   }
   if (i == stringOperation.length - 1) {
    // 递归出口,判断是否还有运算字符串在
    if (mess.indexOf(‘+‘) != -1 || mess.indexOf(‘-‘) != -1)
     return interceptOperation(mess, ‘+‘, ‘-‘);
    break;
   }
  }
  return mess;
 }

 // 截取第二个数
 private static double interceptNumEnd(String str) {
  double a = 0;
  int InrerceIndex = 0;
  char[] stringOperation = str.toCharArray();
  boolean ispas = false; // 记录是否为负数
  for (int i = 0; i < stringOperation.length; i++) {
   switch (stringOperation[i]) {
   case ‘*‘:
   case ‘/‘:
   case ‘+‘:
   case ‘-‘:
    InrerceIndex = i;
    if (i != 0) // 判断该数是否为负数
     ispas = true;
    break;
   default:
    break;
   }
   if (ispas)
    break;
  }
  // 判断此数据是否在运算字符串的最后一位
  if (InrerceIndex == 0) {
   a = Double.parseDouble(str);
   intercePosition = str.length();
   if (ispas)
    intercePosition++;

  } else {
   a = Double.parseDouble(str.substring(0, InrerceIndex));
   // 记录数据的真实长度
   intercePosition = str.substring(0, InrerceIndex).length();
  }
  return a;
 }

 // 截取第一个数
 private static double interceptNumIndex(String str) {
  double a = 0; // 记录数据
  int InrerceIndex = 0; // 记录运算符的位置
  boolean temp = false; // 记录数据前运算符的状态
  char[] stringOperation = str.toCharArray();
  for (int i = stringOperation.length - 1; i >= 0; i--) {
   switch (stringOperation[i]) {
   case ‘*‘:
   case ‘/‘:

   case ‘+‘:
   case ‘-‘:
    InrerceIndex = i;
    temp = true;
    break;
   default:
    break;
   }
   if (temp)
    break;
  }
  // 判断此数据是否在运算字符串的第一位
  if (InrerceIndex == 0) {
   a = Double.parseDouble(str);
   intercePosition = str.length();
   // if(temp)
   // intercePosition++;
  } else {
   a = Double.parseDouble(str.substring(InrerceIndex, str.length()));
   // 记录数据的真实长度
   intercePosition = str.substring(InrerceIndex, str.length())
     .length();
  }
  return a;
 }

 // 计算结果
 private static double reslutString(String operation, double num1,
   double num2) {
  double sumResult = 0;
  if (operation.equals("*"))
   sumResult = num1 * num2;
  if (operation.equals("-"))
   sumResult = num1 - num2;
  if (operation.equals("/"))
   sumResult = num1 / num2;
  if (operation.equals("+"))
   sumResult = num1 + num2;
  return sumResult;
 }

 // 判断是否正确输入运算方式
 private static boolean ispassString(String messString) {
  boolean ispass = false;
  boolean operationIspass = true; // 记录被除数的状态
  int ai = 0; // 记录是否有运算符号的存在
  char[] IsString = messString.toCharArray();
  int num1 = 0;
  int num2 = 0;
  for (int i = 0; i < IsString.length; i++) {
   // 记录有几对小括号的存在
   if (‘(‘ == IsString[i])
    num1++;
   if (‘)‘ == IsString[i])
    num2++;

   // 判断除数是否为零
   if (‘/‘ == IsString[i] && IsString[i + 1] == ‘0‘)
    operationIspass = false;

   // 判断是否输入了运算符合
   if (IsString[i] == ‘+‘ || IsString[i] == ‘-‘ || IsString[i] == ‘*‘
     || IsString[i] == ‘/‘)
    ai++;

   if (i == IsString.length - 1)
    if (ai == 0)
     num2++;
  }
  if (operationIspass)
   if (num1 == num2)
    ispass = true;
  return ispass;
 }
}

封装代码:

package test.test;

public class Core {
    int sumResult=0;
    static double test1(String operation, double num1,double num2) {
              double sumResult = 0;
              if (operation.equals("*"))
               sumResult = num1 * num2;
              if (operation.equals("-"))
               sumResult = num1 - num2;
              if (operation.equals("/"))
               sumResult = num1 / num2;
              if (operation.equals("+"))
               sumResult = num1 + num2;
              return sumResult;
             }

}

测试代码:

package test.test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class CoreTest {

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void test() {
        Core core = new Core();
        double a = core.test1("+",1,3);
        assertEquals(4,a);
    }

}

对于这次实验的“测试方法”还在摸索之中。。。

时间: 2024-07-29 10:59:15

5.1(封装,测试)的相关文章

2015430 加法阶段一的封装测试

public class fdgfg { Vector<Expression> v; public fdgfg() { v = new Vector<Expression>(); } public void produceExpression(int bit,char operator) { Expression temp = new Expression(); temp.operator = operator; temp.a = (int)(Math.random() * Mat

第三次作业-封装测试感悟

1.角色分工 (1)方俊杰-201306114417:驾驶员 http://www.cnblogs.com/imfjj/ (2)余雅诗-201306114453:领航员 http://www.cnblogs.com/ys1101/ 2.作业内容 (1) 能把计算的功能封装起来,通过测试程序和API 接口测试其简单的加法功能 (2)扩展后,还要支持两个数的减法,乘法,除法 (3)支持小数点.负数 (4)具备界面 (5)进行了报错处理提示 (详情请看我的队友余雅诗的博客http://www.cnbl

C#中使用多款LevelDB.Net封装测试性能

一.使用http://www.nuget.org/packages/LevelDB.NET 测试 1.新建项目,并Nuget引入库: 2.写代码 using LevelDB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LevelDBDemo { class Program { static

【作业报告】作业5 四则运算 测试与封装 5.1

测试与封装 5.1 程序开发简介: [开发环境]:eclipse [开发人员]:Ives & 郑胜斌 [博客地址]:38郑胜斌 [开发时间]:2015-04-30 [版本]:5.1 [要求]: 封装 测试 封装: 概念 封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口.面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的对象,这些对象通过一个受保护的接口访问其他对象.封装是一种信息隐藏技术,在java中通过关键字private实现封装.什么是封装?封装把对象的所

四则运算,测试与封装。

测试与封装 5.1 程序开发简介: [开发环境]:eclipse [开发人员]:Ives & 郑胜斌 [博客地址]:http://www.cnblogs.com/IvesHe/ [开发时间]:2015-04-30 [版本]:5.1 [要求]: 封装 测试 [分工]: Ives:单元测试.界面.自定义异常. 郑胜斌:封装 Expression类. 封装: 概念 封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口.面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的对

Skyfree的毕业论文 《系统封装与部署的深入研究》

https://www.itsk.com/thread-197-1-4.html Skyfree 发表于 2007-9-13 07:25:40 关于封装与部署的深入研究 前言 从Windows95到现在的WindowsVista,Windows优秀的图形界面和可操作性,赢得了目前广泛的使用人群.虽然Windows各方面性能,特别是稳定性方面,依然有所不及Unix.Linux这些高稳定性的系统,但是它仍然不可否认的成为当前使用范围最广的操作系统.但是Windows发展了整整10于个年头,虽然Win

[求助] win7 x64 封装 出现 Administrator.xxxxx 的问题

jacky_qu 发表于 2014-9-3 23:34:37 https://www.itsk.com/thread-334907-1-12.html 虚拟机vm10环境封装用msdn 原盘安装系统,安装完使用ctrl+shift+F3 跳过用户名设置进入封装模式,一切优化完成后,使用封装工具在封装时启用Administrator用户. 问题来了,,部署完成后 c:\users 目录下会有 Administrator.xxxxx 目录 而不是administrator 目录 请问这个需要怎么处理

【Android测试】UI自动化代码优化之路(临时发布)

关于UI自动化的抱怨 听过不少人这样讲 "UI自动化非常不稳定,需求一改,界面一遍,全部都费了".我相信做过的人可能也会有同感.既然这个问题一直都是存在的,那么为什么没有人仔细分析原因呢? 我的老板georgeliao举了这样一个例子:每当需求变化的时候,开发没有跳起来,反而是测试跳了起来.然后不断的抱怨,界面元素全都改了,我的自动化的用例全部都要废弃掉了.那么我们是否想过,为什么开发可以从容不破的应对产品不断变化的需求?而我们却不能呢? 业内不少人也都放弃了UI自动化,觉得接口测试才

将查询的结果封装成List&lt;Map&gt;与用回调函数实现数据的动态封装(44)

手工的开始QueryRunner类.实现数据封装: MapListHandler MapHandler BeanListHandler BeanHandler 第一步:基本的封装测试 写一个类,QueryRunner,实现一个方法query(sql)- List<map> package cn.itcast.dbutils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaD

selenium测试框架(java) 版本演化一

selenium的自动化测试代码应该如何组织?  如链接:https://code.google.com/p/selenium/wiki/PageObjects  这里提供了一种PageObject的设计思想,并且在百度内部给出了一个感觉比较实用的实现.其组织结构思想如下: Page 封装页面元素,以及页面应提供的服务. 尽量隐藏页面的细节,不要暴露出来. widget 封装Page中的通用的组件. 这里的理念是所有的WebElement都是控件. 通用的页面样式,如导航栏,列表,组合查询框,可