This repository was archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFormulaCell.java
More file actions
136 lines (133 loc) · 4.82 KB
/
Copy pathFormulaCell.java
File metadata and controls
136 lines (133 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//By Ryan Chan
//AP Computer Science 2016
public class FormulaCell extends Cell{//Cell of type Formula
public Cell[][] table;
boolean error = false;
public FormulaCell(String input,Cell[][] table){
type="formula";
content=input;
this.table=table;
}
public FormulaCell(FormulaCell old){
this(old.toString(),old.table);
}
public double getNumber(){
return Double.parseDouble(evaluate());
}
public String getText(){
return evaluate();
}
public String toString(){
return content;
}
public String evaluate() { //Determines order of operation and executes operations accordingly
String input = content.substring(2,content.length()-2);
if (input.contains("sum")){
return Double.toString(sumAvg(input,"sum"));
} else if (input.contains("avg")){
return Double.toString(sumAvg(input,"avg"));
}else{
int operatorPos = 0;
int startPos = 0;
int endPos = 0;
String excerpt, firstHalf, secondHalf, combined = null;
double num1,num2;
while (input.indexOf(" ") != -1) { //Loops until there is only one term in the string, which will be the final answer
if ((input.indexOf("*") != -1)||(input.indexOf("/ ") != -1)) { //Searches for multiplication and division first
if ((input.indexOf("*")!=-1)&&input.indexOf("/ ")!=-1){ //Finds first instance of multiplication or division, if present
operatorPos = Math.min(input.indexOf("*"),input.indexOf("/ "));
} else if ((input.indexOf("*")!=-1)&&input.indexOf("/ ")==-1){
operatorPos = input.indexOf("*");
} else if ((input.indexOf("*")==-1)&&input.indexOf("/ ")!=-1){
operatorPos=input.indexOf("/ ");
}
} else if ((input.indexOf("+") != -1)||(input.indexOf("- ") != -1)) { //Searches for addition and subtraction if no multiplication or division remain
if ((input.indexOf("+")!=-1)&&input.indexOf("- ")!=-1){ //Finds first instance of addition or subtration, if present
operatorPos = Math.min(input.indexOf("+"),input.indexOf("- "));
} else if ((input.indexOf("+")!=-1)&&input.indexOf("- ")==-1){
operatorPos = input.indexOf("+");
} else if ((input.indexOf("+")==-1)&&input.indexOf("- ")!=-1){
operatorPos=input.indexOf("- ");
}
}
if (input.lastIndexOf(" ", operatorPos - 2) != -1) { //Checks if the number before the operator is the first in the string and records starting position
startPos = input.lastIndexOf(" ", operatorPos - 2) + 1;
} else {
startPos = 0;
}
if (input.indexOf(" ", operatorPos + 2) != -1) { //Checks if the number after the operator is the last number and records end position
endPos = input.indexOf(" ", operatorPos + 2);
} else {
endPos = input.length();
}
firstHalf = input.substring(0, startPos); //First half of string taken up until the starting point for the operation
secondHalf = input.substring(endPos); //Second half of string taken from ending point of the operation to string ending
excerpt = input.substring(startPos, endPos); //Operation string is taken between starting point and ending point
num1=getNumFromString(excerpt.substring(0, excerpt.indexOf(" ")));
num2=getNumFromString(excerpt.substring(excerpt.lastIndexOf(" ")+1));
String operation = input.substring(operatorPos,operatorPos+1);
if (error==true){
return "Error";
}
if (operation.equals("+")){
combined= Double.toString(num1+num2);
} else if (operation.equals("-")) {
combined = Double.toString(num1 - num2);
} else if (operation.equals("*")) {
combined = Double.toString(num1 * num2);
} else if (operation.equals("/")) {
combined = Double.toString(num1 / num2);
} else {
return "Error";
}
input = firstHalf + combined + secondHalf;
}
return input;
}
}
private double getNumFromString(String input){
if ((input.charAt(0)>='A')&&(input.charAt(0)<='Z')){
char collum=input.charAt(0);
String row=input.substring(1);
int x = collum-'A';
int y = Integer.parseInt(row)-1;
if (table[y][x].type.equals("double")||table[y][x].type.equals("formula")){
return(table[y][x].getNumber());
} else {
System.out.println("aaa");
error=true;
return 0;
}
}
else {
return Double.parseDouble(input);
}
}
private double sumAvg(String input, String mode){
double total = 0;
double count = 0;
char collum=input.charAt(4);
String row=input.substring(5,input.indexOf(" ",5));
int x1 = collum-'A';
int y1 = Integer.parseInt(row)-1;
collum=input.charAt(input.indexOf(" - ")+3);
row=input.substring(input.indexOf(" - ")+4);
int x2 = collum-'A';
int y2 = Integer.parseInt(row)-1;
for (int i = y1;i<=y2;i++){
for (int j = x1;j<=x2;j++){
if (table[i][j].type.equals("double")||table[i][j].type.equals("formula")){
total+=table[i][j].getNumber();
count++;
}
}
}
if (mode.equals("avg")){
return total/count;
} else if (mode.equals("sum")){
return total;
} else {
return 999999;
}
}
}