服务器关闭通知:服务器将于近日关闭并不再会打开,请将重要数据及时保存在本地

Commit 902a6ef6 authored by CPUmaker's avatar CPUmaker
Browse files

Initial project

parents
image: local-registry.inner.buaaoo.top/image-dev/java:13
stages:
- build
- test
before_script:
- java -version
- javac -version
- mvn -v
mvn_build:
stage: build
script:
- echo "Build Project"
- mvn compile
mvn_test:
stage: test
dependencies:
- mvn_build
script:
- echo "Run JUnit4"
- mvn test
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA">
<option name="configuration">
<map>
<entry key="checkstyle-version" value="8.30" />
<entry key="copy-libs" value="false" />
<entry key="location-0" value="BUNDLED:(bundled):Sun Checks" />
<entry key="location-1" value="BUNDLED:(bundled):Google Checks" />
<entry key="scan-before-checkin" value="false" />
<entry key="scanscope" value="JavaOnly" />
<entry key="suppress-errors" value="false" />
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="CI_CD_Test" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
</component>
</module>
\ No newline at end of file
<?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>
<groupId>org.example</groupId>
<artifactId>CI_CD_Test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>CI_CD_Test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package org.example;
import java.util.Scanner;
public class App
{
public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
PolyProcessor processor = new PolyProcessor();
processor.processExpress(line);
processor.printDerivative();
}
}
package org.example;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class Poly {
private final HashMap<BigInteger, BigInteger> map;
public Poly() {
map = new HashMap<>();
}
public void addNewTerm(BigInteger exp, BigInteger ratio) {
if (map.containsKey(exp)) {
BigInteger oldRatio = map.get(exp);
map.replace(exp, oldRatio.add(ratio));
} else {
map.put(exp, ratio);
}
}
public StringBuilder calcDerivative() {
if (map.isEmpty()) {
return new StringBuilder("0");
}
StringBuilder der = new StringBuilder();
for (Map.Entry<BigInteger,BigInteger> entry : map.entrySet()) {
BigInteger exp = entry.getKey();
BigInteger ratio = entry.getValue();
ratio = ratio.multiply(exp);
exp = exp.add(new BigInteger("-1"));
if (ratio.equals(BigInteger.ZERO)) {
continue;
}
if (exp.equals(BigInteger.ZERO)) {
if (ratio.compareTo(BigInteger.ZERO) > 0) {
StringBuilder temp = new StringBuilder()
.append("+").append(ratio.toString());
der.insert(0, temp);
} else {
der.append(ratio.toString());
}
} else if (exp.equals(BigInteger.ONE)) {
if (ratio.compareTo(BigInteger.ZERO) > 0) {
StringBuilder temp = new StringBuilder()
.append("+").append(ratio.toString())
.append("*x");
der.insert(0, temp);
} else {
der.append(ratio.toString()).append("*x");
}
} else if (ratio.equals(BigInteger.ONE)) {
der.append("+x**").append(exp.toString());
} else if (ratio.equals(new BigInteger("-1"))) {
der.append("-x**").append(exp.toString());
} else if (ratio.compareTo(BigInteger.ZERO) > 0) {
StringBuilder temp = new StringBuilder()
.append("+").append(ratio.toString())
.append("*x**").append(exp.toString());
der.insert(0, temp);
} else {
der.append(ratio.toString()).append("*x**").append(exp.toString());
}
}
return der;
}
}
package org.example;
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PolyProcessor {
private final Poly poly;
private final String regex;
public PolyProcessor() {
poly = new Poly();
String digit = "([+\\-]?\\d+)";
String exp = String.format("([*]{2}\\s*%s)", digit);
String power = String.format("(x(\\s*%s)?)", exp);
String termX = String.format("(((%s\\s*[*]\\s*)|([+\\-]\\s*))?%s)", digit, power);
String term = String.format("(%s|%s)", termX, digit);
regex = String.format("\\s*([+\\-]\\s*)?%s\\s*", term);
}
public void processExpress(String line) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(line);
while (m.find()) {
if (m.group().contains("x")) {
BigInteger ratio = ratioProcess(m.group(6), m.group(7));
BigInteger exp = expProcess(m.group(11));
String termSign = m.group(1);
if (termSign != null && termSign.length() >= 1 && termSign.charAt(0) == '-') {
ratio = ratio.negate();
}
poly.addNewTerm(exp, ratio);
}
}
}
public void printDerivative() {
StringBuilder der = poly.calcDerivative();
if (der.length() == 0) {
der.append("0");
}
if (der.charAt(0) == '+') {
der.deleteCharAt(0);
}
System.out.println(der.toString());
}
private BigInteger ratioProcess(String data1, String data2) {
BigInteger ratio;
if (data1 != null) {
if (data1.length() == 0) {
ratio = new BigInteger("1");
} else {
ratio = new BigInteger(data1);
}
} else if (data2 != null) {
if (data2.contains("-")) {
ratio = new BigInteger("-1");
} else {
ratio = new BigInteger("1");
}
} else {
ratio = new BigInteger("1");
}
return ratio;
}
private BigInteger expProcess(String data) {
BigInteger exp;
if (data == null || data.length() == 0) {
exp = new BigInteger("1");
} else {
exp = new BigInteger(data);
}
return exp;
}
}
package org.example;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.*;
/**
* Unit test for simple App.
*/
public class AppTest {
private static PolyProcessor processor = null;
@BeforeClass
public static void setUp() {
processor = new PolyProcessor();
}
@Test
public void testConst() {
// String str = "100\n";
// ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
// System.setIn(bis);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos));
processor.processExpress("100");
processor.printDerivative();
assertEquals("0\n", bos.toString());
}
@Test
public void testWithX() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos));
processor.processExpress("100 * x");
processor.printDerivative();
assertEquals("100\n", bos.toString());
}
@Test
public void shouldAnswerWithTrue() {
assertTrue(true);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment