commit bin-2-dic service:

pull/58/head
sortinn 8 years ago
parent f293f72a63
commit 949b5a916c

@ -0,0 +1,29 @@
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/
### VS Code ###
.vscode/

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sortinn</groupId>
<artifactId>bin-to-dec</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bin-to-dec</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,13 @@
package com.sortinn.bintodec;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BinToDecApplication {
public static void main(String[] args) {
SpringApplication.run(BinToDecApplication.class, args);
}
}

@ -0,0 +1,32 @@
package com.sortinn.bintodec.controller;
import com.sortinn.bintodec.service.BinToDecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
/**
* @author tian.gao
* @create 2019.04.22 11:47 PM
**/
@Slf4j
@Controller
@RequestMapping("/cal")
public class BinToDecController {
@Resource
private BinToDecService binToDecService;
@PostMapping("/b2d/{binary}")
public ModelAndView b2d(@PathVariable("binary") String binary) {
Integer res = binToDecService.binToDecConvert(binary);
ModelAndView modelAndView = new ModelAndView("home-list");
modelAndView.addObject("res", res);
return modelAndView;
}
}

@ -0,0 +1,52 @@
package com.sortinn.bintodec.service;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author tian.gao
* @create 2019.04.22 11:47 PM
**/
@Slf4j
@Service
public class BinToDecService {
private static final int MAX_LENGTH = 8;
public Integer binToDecConvert(String binary) {
if (Strings.isEmpty(binary)) {
log.error("binary empty");
return null;
}
if (binary.length() > MAX_LENGTH) {
log.error("more than max length");
return null;
}
String regex = "^[01]+$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(binary);
Double res = 0.0;
if (matcher.find()) {
String[] binaryArr = binary.split("");
int length = binaryArr.length;
for (int i = 0; i < length; i++) {
res += Integer.parseInt(binaryArr[i]) * Math.pow(2, length - 1 - i);
}
} else {
log.error("input is not binary number");
return null;
}
return res.intValue();
}
public static void main(String[] args) {
System.out.println(new BinToDecService().binToDecConvert("11111110"));
}
}

@ -0,0 +1,114 @@
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Home</title>
</head>
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="/user/list">主页</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/user/addNewUser">新增</a>
</li>
</ul>
<div class="jumbotron">
<h1>十进制计算器</h1>
<p>可二进制转换转换为十进制</p>
</div>
<body>
<!--最外层容器-->
<div id="wrap" class="container">
<!--页面头部-->
<header>
</header>
<!-- 页面主体部分-->
<div id="main_content" th:include="::main_content">
<div th:fragment="main_content">
<table class="table">
<thead class="table-dark">
<tr>
<th scope="col">用户id</th>
<th>用户名</th>
<th>性别</th>
<th>年龄</th>
<th>备注</th>
<th>是否可用</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="userInfo:${content}">
<td th:text="${userInfo.getUserId()}"></td>
<td th:text="${userInfo.getUserName()}"></td>
<td th:text="${userInfo.getGender()}"></td>
<td th:text="${userInfo.getAge()}"></td>
<td th:text="${userInfo.getComment()}"></td>
<td th:text="${userInfo.isAvailable()}"></td>
<td>
<a th:href="@{'/user/update/'+${userInfo.getId()}}">编辑</a>
<a th:href="@{'/user/delete/'+${userInfo.getId()}}" onclick="alert('确定要删除用户吗?')">删除</a>
</td>
</tr>
</tbody>
</table>
<div style="position:absolute;left:50%;margin-left:-100px;">
<ul class="pagination">
<!-- 上一页 -->
<li class="page-item" data-th-classappend="*{firstPage} ? 'disabled' : ''">
<a th:href="@{'/user/list?pageNum=' + ${currentNumber - 1}}" class="page-link"
data-th-attr="pageIndex=${currentNumber} - 1"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<!-- 迭代生成页码 -->
<li class="page-item" data-th-each="i : ${#numbers.sequence(1, totalPages)}"
data-th-classappend="${(currentNumber + 1) eq i} ? 'active' : ''">
<a class="page-link" data-th-attr="pageIndex=${i} - 1"
th:href="@{'/user/list?pageNum=' + ${i - 1}}">
<span data-th-text="${i}"></span>
</a>
</li>
<!-- 下一页 -->
<li class="page-item" data-th-classappend="*{lastPage} ? 'disabled' : ''">
<a th:href="@{'/user/list?pageNum=' + ${currentNumber + 1}}" class="page-link"
data-th-attr="pageIndex=${currentNumber} + 1"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="/jquery.min.js"></script>
<script src="/jquery.page.js"></script>
</body>
</html>

@ -0,0 +1,29 @@
package com.sortinn.bintodec;
import com.sortinn.bintodec.service.BinToDecService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BinToDecApplicationTests {
@Resource
private BinToDecService binToDecService;
@Test
public void contextLoads() {
}
@Test
public void b2d_test() {
Integer res = binToDecService.binToDecConvert("11111111");
System.out.println(res);
}
}
Loading…
Cancel
Save