lol
This commit is contained in:
commit
613bef2496
108 changed files with 8397 additions and 0 deletions
110
src/zutil/math/Tick.java
Normal file
110
src/zutil/math/Tick.java
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package zutil.math;
|
||||
|
||||
public class Tick {
|
||||
|
||||
/**
|
||||
* TEST
|
||||
*/
|
||||
public static void main(String[] args){
|
||||
String temp = "a";
|
||||
while(true){
|
||||
temp = tick(temp,3);
|
||||
System.out.println(temp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ticks a given string(increments the string with one)
|
||||
*
|
||||
* @param ts The string to tick
|
||||
* @param maxChar The maximum number of characters in the string
|
||||
* @return The ticked string
|
||||
*/
|
||||
public static String tick(String ts, int maxChar){
|
||||
StringBuffer ret = new StringBuffer(ts.trim());
|
||||
int index = ret.length()-1;
|
||||
|
||||
if(ret.length() < maxChar){
|
||||
ret.append('a');
|
||||
}
|
||||
else{
|
||||
while(index >= 0){
|
||||
char c = increment(ret.charAt(index));
|
||||
if(c != 0){
|
||||
if(index == 0 && ret.length() < maxChar) ret.append('a');
|
||||
if(index == 0) ret = new StringBuffer(""+c);
|
||||
else ret.setCharAt(index,c);
|
||||
break;
|
||||
}
|
||||
else{
|
||||
//ret.setCharAt(index,'a');
|
||||
ret.deleteCharAt(index);
|
||||
index--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the char with one after the swedish alfabet
|
||||
*
|
||||
* @param c The char to increment
|
||||
* @return The incremented char in lowercase 0 if it reached the end
|
||||
*/
|
||||
public static char increment(char c){
|
||||
switch(Character.toLowerCase(c)){
|
||||
case 'z': return 'å';
|
||||
case 'å': return 'ä';
|
||||
case 'ä': return 'ö';
|
||||
}
|
||||
c = (char)(Character.toLowerCase(c) + 1);
|
||||
if(isAlfa(c)){
|
||||
return c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the char is a valid character in
|
||||
* the Swedish alfabet
|
||||
*
|
||||
* @param c The char to check
|
||||
* @return True if the char is a valid letter
|
||||
*/
|
||||
public static boolean isAlfa(char c){
|
||||
switch(Character.toLowerCase(c)){
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
case 'd':
|
||||
case 'e':
|
||||
case 'f':
|
||||
case 'g':
|
||||
case 'h':
|
||||
case 'i':
|
||||
case 'j':
|
||||
case 'k':
|
||||
case 'l':
|
||||
case 'm':
|
||||
case 'n':
|
||||
case 'o':
|
||||
case 'p':
|
||||
case 'q':
|
||||
case 'r':
|
||||
case 's':
|
||||
case 't':
|
||||
case 'u':
|
||||
case 'v':
|
||||
case 'w':
|
||||
case 'x':
|
||||
case 'y':
|
||||
case 'z':
|
||||
case 'å':
|
||||
case 'ä':
|
||||
case 'ö': return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/zutil/math/ZMath.java
Normal file
16
src/zutil/math/ZMath.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package zutil.math;
|
||||
|
||||
/**
|
||||
* Very Simple math functions
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class ZMath {
|
||||
|
||||
/**
|
||||
* Calculates the percentige the value has
|
||||
*/
|
||||
public static double percent(int min, int max, int value){
|
||||
return ((double)(value-min)/(max-min))*100;
|
||||
}
|
||||
}
|
||||
257
src/zutil/math/parser/MathParser.java
Normal file
257
src/zutil/math/parser/MathParser.java
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
package zutil.math.parser;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This class parses a string with math
|
||||
* and solves it
|
||||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class MathParser {
|
||||
|
||||
public static MathNode parse(String functionString){
|
||||
StringBuffer functionStringBuffer = new StringBuffer(functionString+(char)0);
|
||||
MathNode node = new MathNode();
|
||||
|
||||
parse(functionStringBuffer, new StringBuffer(), null, node);
|
||||
|
||||
System.out.println("----------------------------------------------------------------------");
|
||||
System.out.println(node+" = "+node.exec());
|
||||
System.out.println("----------------------------------------------------------------------");
|
||||
return node;
|
||||
}
|
||||
|
||||
private static void parse(StringBuffer functionString, StringBuffer temp, MathOperation previus, MathNode rootNode){
|
||||
if(functionString.length() <= 0){
|
||||
return;
|
||||
}
|
||||
char c = functionString.charAt(0);
|
||||
functionString.deleteCharAt(0);
|
||||
System.out.println("char: "+c);
|
||||
MathOperation current = null;
|
||||
|
||||
if(!Character.isWhitespace(c)){
|
||||
if(isNumber(c)){
|
||||
temp.append(c);
|
||||
}
|
||||
else{
|
||||
Math container = new MathNumber();
|
||||
if(temp.length() > 0){
|
||||
System.out.println("("+Double.parseDouble(temp.toString())+")");
|
||||
((MathNumber)container).num = Double.parseDouble(temp.toString());
|
||||
temp.delete(0, temp.length());
|
||||
}
|
||||
|
||||
if(rootNode.math == null){
|
||||
System.out.println("Initializing rootNode");
|
||||
previus = getOperation(c);
|
||||
System.out.println("operation: "+previus.getClass().getName());
|
||||
previus.math1 = container;
|
||||
rootNode.math = previus;
|
||||
}
|
||||
else{
|
||||
if(c == '('){
|
||||
MathNode parantes = new MathNode();
|
||||
MathOperation previusParantes = previus;
|
||||
parse(functionString, temp, previus, parantes);
|
||||
previusParantes.math2 = parantes;
|
||||
System.out.println(parantes);
|
||||
container = parantes;
|
||||
|
||||
// get the next operation
|
||||
c = functionString.charAt(0);
|
||||
functionString.deleteCharAt(0);
|
||||
System.out.println("char: "+c);
|
||||
}
|
||||
|
||||
current = getOperation(c);
|
||||
System.out.println("operation: "+current.getClass().getName());
|
||||
current.math1 = container;
|
||||
previus.math2 = current;
|
||||
|
||||
if(c == ')'){
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(current != null) parse(functionString, temp, current, rootNode);
|
||||
else parse(functionString, temp, previus, rootNode);
|
||||
return;
|
||||
}
|
||||
|
||||
private static boolean isNumber(char c){
|
||||
if(Character.isDigit(c)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static MathOperation getOperation(char c){
|
||||
switch(c){
|
||||
case '+': return new MathAddition();
|
||||
case '-': return new MathSubtraction();
|
||||
case '*': return new MathMultiplication();
|
||||
case '/': return new MathDivision();
|
||||
case '%': return new MathModulus();
|
||||
case '^': return new MathPow();
|
||||
case ')':
|
||||
case (char)0: return new EmptyMath();
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
||||
while(true){
|
||||
System.out.print(">>Math: ");
|
||||
parse(in.readLine());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Math{
|
||||
public abstract double exec();
|
||||
|
||||
public abstract String toString();
|
||||
}
|
||||
|
||||
class MathNode extends Math{
|
||||
Math math;
|
||||
|
||||
public double exec() {
|
||||
return math.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "( "+math.toString()+" )";
|
||||
}
|
||||
}
|
||||
|
||||
class MathNumber extends Math{
|
||||
double num;
|
||||
|
||||
public double exec() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ""+num;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class MathOperation extends Math{
|
||||
Math math1;
|
||||
Math math2;
|
||||
int priority;
|
||||
|
||||
public abstract double exec();
|
||||
}
|
||||
|
||||
class MathAddition extends MathOperation{
|
||||
public MathAddition(){
|
||||
priority = 1;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
return math1.exec() + math2.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+" + "+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class MathSubtraction extends MathOperation{
|
||||
public MathSubtraction(){
|
||||
priority = 1;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
return math1.exec() - math2.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+" - "+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class MathMultiplication extends MathOperation{
|
||||
public MathMultiplication(){
|
||||
priority = 2;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
return math1.exec() * math2.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+" * "+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class MathDivision extends MathOperation{
|
||||
public MathDivision(){
|
||||
priority = 2;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
return math1.exec() / math2.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+" / "+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class MathModulus extends MathOperation{
|
||||
public MathModulus(){
|
||||
priority = 2;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
return math1.exec() % math2.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+" % "+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class MathPow extends MathOperation{
|
||||
public MathPow(){
|
||||
priority = 3;
|
||||
}
|
||||
|
||||
public double exec() {
|
||||
double ret = 1;
|
||||
double tmp1 = math1.exec();
|
||||
double tmp2 = math2.exec();
|
||||
for(int i=0; i<tmp2 ;i++){
|
||||
ret *= tmp1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString()+"^"+math2.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class EmptyMath extends MathOperation{
|
||||
public double exec() {
|
||||
return math1.exec();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return math1.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue