Initial git commit
This commit is contained in:
145
QuickLaunch/src/mod_calc/Calcmod.java
Normal file
145
QuickLaunch/src/mod_calc/Calcmod.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package mod_calc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import basics.BasicMod;
|
||||
|
||||
public class Calcmod extends BasicMod{
|
||||
ArrayList<Double> values;
|
||||
ArrayList<Character> operators;
|
||||
char current;
|
||||
String tmp;
|
||||
double output;
|
||||
double temp;
|
||||
public void init(){
|
||||
values = new ArrayList<Double>();
|
||||
operators = new ArrayList<Character>();
|
||||
current = ' ';
|
||||
tmp = "";
|
||||
System.out.println("Calculator installed");
|
||||
this.setModName("Calculator");
|
||||
this.setVersion(0.81);
|
||||
|
||||
}
|
||||
public void checkInput(String input) throws IOException {
|
||||
if(!input.isEmpty()){
|
||||
if(input.charAt(0)=='$'){
|
||||
for(int i = 1;i < input.length();i++){
|
||||
current = input.charAt(i);
|
||||
if(isAlternative(current)){
|
||||
current = changeAlternatives(current);
|
||||
System.out.println("alt");
|
||||
}
|
||||
if(isNumber(current)){
|
||||
tmp+= current;
|
||||
System.out.println("num");
|
||||
}
|
||||
else if(isOperator(current)){
|
||||
values.add(Double.parseDouble(tmp));
|
||||
tmp = "";
|
||||
operators.add(current);
|
||||
System.out.println("op");
|
||||
}
|
||||
}
|
||||
values.add(Double.parseDouble(tmp));
|
||||
}
|
||||
}
|
||||
solve();
|
||||
clear();
|
||||
|
||||
}
|
||||
|
||||
private boolean isNumber(char check){
|
||||
if((check == '1') ||
|
||||
(check == '2') ||
|
||||
(check == '3') ||
|
||||
(check == '4') ||
|
||||
(check == '5') ||
|
||||
(check == '6') ||
|
||||
(check == '7') ||
|
||||
(check == '8') ||
|
||||
(check == '9') ||
|
||||
(check == '0') ||
|
||||
(check == '.')){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isOperator(char check){
|
||||
if((check == '+') ||
|
||||
(check == '-') ||
|
||||
(check == '*') ||
|
||||
(check == '/')){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private boolean isAlternative(char check){
|
||||
if((check == ',') ||
|
||||
(check == 'x')){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private char changeAlternatives(char check){
|
||||
if(check == ','){
|
||||
return '.';
|
||||
}
|
||||
else if(check == 'x'){
|
||||
return '*';
|
||||
}
|
||||
else{
|
||||
return '?';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void solve(){
|
||||
if(values.size() == operators.size()+1){
|
||||
output = values.get(0);
|
||||
for(int i=0;i<operators.size();i++){
|
||||
temp = values.get(i+1);
|
||||
if(operators.get(i) == '+'){
|
||||
output += temp;
|
||||
}
|
||||
else if(operators.get(i) == '-'){
|
||||
output -= temp;
|
||||
}
|
||||
else if(operators.get(i) == '*'){
|
||||
output *= temp;
|
||||
}
|
||||
else if(operators.get(i) == '/'){
|
||||
output /= temp;
|
||||
}
|
||||
}
|
||||
System.out.println(output);
|
||||
|
||||
JOptionPane.showMessageDialog(null, output);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void clear(){
|
||||
values.clear();
|
||||
operators.clear();
|
||||
current = ' ';
|
||||
tmp = "";
|
||||
temp = 0;
|
||||
output = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user