forked from matemaynard99/Project_2A
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_2A.java
More file actions
159 lines (142 loc) · 5.38 KB
/
Copy pathProject_2A.java
File metadata and controls
159 lines (142 loc) · 5.38 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Written by
//Dalton Vining
//Samuel Maynard
//Shane Callaway
package Project_2A;
import java.util.*;
public class Project_2A {
public int evaluatEquation(String s) {
Stack<Integer> nums = new Stack<Integer>();
Stack<String> operators = new Stack<String>();
char[] tokenArray = s.toCharArray();
/**The idea of this program is to combine the infixToPostfix and
* postfix evaluating algorithims we discussed in class
* as that should be more efficient than simply performing each of those equations
* seperately.
*/
//use for loop to iterate through whole equation
for (int i = 0; i<tokenArray.length; i++) {
//simple line to ignore spaces
if (tokenArray[i] == ' ') {
continue;
}//end if
//check if current character is a number
if (isNumber(tokenArray[i])) {
/**converted char to string so it can become integer
* unsure how to change char to int directly
*/
//added while loop to catch multidigit numbers
while(isNumber(tokenArray[i])){
String newNum = "";
newNum = newNum + tokenArray[i];
i++;
//add to stack of numbers as an integer
nums.push(Integer.parseInt(newNum));
}//end while
/** i has to be decrimented, since it is incremented in the while loop
* that way the main for loop doesnt skip any tokens in the equation
*/
i--;
}//end if
//check if open parenthesis
else if(tokenArray[i] == '(') {
String newOp = "";
newOp = newOp + tokenArray[i];
operators.push(newOp);
}//end open parenthesis branch
//check for closing parenthesis
else if(tokenArray[i] == ')') {
while(operators.peek() != ")") {
/**Performsoperation in the parenthesis in order
* until it reaches the open parenthesis
* pushes the outcome into the numbers stack after each operation
*/
Integer right = nums.pop();
Integer left = nums.pop();
nums.push(equation(left, right, operators.pop()));
}//end while
}//end branch for closing parenthesis
else if(isOperator(tokenArray[i])) {
/**while loop to find multipart operators
* and convert them to a string
*/
string newOp = "";
while(isOperator(tokenArray[i])) {
newOp = newOp + tokenArray[i];
i++;
}//end while loop for op building
/** i has to be decrimented, since it is incremented in the while loop
* that way the main for loop doesnt skip any tokens in the equation
*/
i--;
//while loop until you reach open parenthesis
/**use while loop to check if current operator
* has the same or higher precendence to one
* on top of the operators stack
* if so performs the operator on top of stack
*
* added !.operators.isEmpty(), as empty stack causes error for comparison
*/
while(!operators.isEmpty() && (precedence(operators.peek()) > precedence(tokenArray[i]))) {
Integer right = nums.pop();
Integer left = nums.pop();
nums.push(equation(left, right, operators.pop()));
}//end while loop
//once token is either first token or highest precedence adds to stack
operators.push(tokenArray[i]);
}//end else if branch for operator
}//end for
/** once the while expression is either added to stacks or is
* put through needed operations solves remaining operations in
* the operators stack, until empty
*/
while (!operators.isEmpty()) {
Integer right = nums.pop();
Integer left = nums.pop();
nums.push(equation(left, right, operators.pop()));
return nums.pop();
}
}//end evaluate method
/*
*Precedence method. This returns a value based on the operator
*This value represents the order of operations on the expression so the proper result can be calculated
*Takes in the string operater and compares it using operator.equals to find the proper int value of precedence
*@Returns: Int representing precendence
*/
public int precedence(String operator) {
if (operator.equals("^")) { return 7; }
if (operator.equals("*") || operator.equals("/")) { return 6; }
if (operator.equals("+") || operator.equals("-")) { return 5; }
if (operator.equals(">") || operator.equals(">=")) { return 4; }
if (operator.equals("<") || operator.equals("<=")) { return 4; }
if (operator.equals("==") || operator.equals("!=")) { return 3; }
if (operator.equals("&&")) { return 2; }
if (operator.equals("||")) { return 1; }
throw new IllegalArgumentException(String.format("Operator %s is not supported.", operator)) } // Time complexity: O(n)
}//End Precedence method
public int equation(Integer left, Integer right, String s) {
/**
* FIX ME
*/
}
//method to check if character is a number
//simple method to see if char is between 1-9 and thus a number
public boolean isNumber(char c) {
if(c >= '0' && c <='9')
return true;
else
return false;
}//end is number check
/*
*Method to check if String s is equal to any of the allowed operators for this project.
*Returns: True if String s is equal to one of the operators, otherwise returns false bvecause String s isnt an operator
*/
public boolean isOperator(String s) {
if (s.equals("^" || "*" || "/" || "+" || "-" || ">" || ">=" || "<" || "<=" || "==" || "!=" || "&&" || "||"){
return true;
}else {return false;}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}