0% found this document useful (0 votes)
25 views5 pages

Nsinteger I 0

The document shows the step-by-step working of evaluating a mathematical expression by breaking it down into an array of operands and operators and then applying the operations in order of precedence. It includes methods to parse strings to numbers, apply multiplication/division first before addition/subtraction, and shift elements in the array after applying each operation.

Uploaded by

Stacy Weber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Nsinteger I 0

The document shows the step-by-step working of evaluating a mathematical expression by breaking it down into an array of operands and operators and then applying the operations in order of precedence. It includes methods to parse strings to numbers, apply multiplication/division first before addition/subtraction, and shift elements in the array after applying each operation.

Uploaded by

Stacy Weber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

36 + 10 + 6 - 40 - 2 + 9 * 3

36 + 16 - 42
+ 27
52 - 15
37
"36 + 10 - 40 + 2 * 5 + 10 / 2"
["36", "+", "10", "-", "40", "+", "2", "*", "5", "+",
"10", "/", "2"]
[36, "+", 10, "-", 40, "+", 2, "*", 5, "+", 10, "/", 2]
[36, "+", 10, "-", 40, "+", 10, "+", 10, "/", 2]
[36, "+", 10, "-", 40, "+", 10, "+", 5]
[46, "-", 40, "+", 10, "+", 5]
[6, "+", 10, "+", 5]
[16, "+", 5]
[21]
- (NSNumber) solveEquationFromString: (NSString)
stringEquation;
NSArray *newArray = array by taking out
whitespace stringEquation; (["2", "+", "10", "*",
"4"])
NSInteger i = 0;
- (NSArray) turnStringNumbersInArrayToDoubles:
(NSArray)array {
try {
for(i, i < newArray.length, i++) {

if (newArray[i] !typeof double) {


parseStringToDouble(newArray[i]);
}
}
} catch (notAbleToParseIntoDouble e) {
i++;
[newArray
turnStringNumbersInArrayToDoubles];
}
return array;
}
(newArray now equals [2, "+", 10, "*", 4])
- (NSNumber/double)solveArrayOfStringEquation:
(NSArray/NSMutableArray)array
{
for (int i = 0; i < array.length; i++) {
if ([array[i] equalToString:"*"] || [array[i]
equalToString:"/"]) {
if([array[i] equalToString:"*"]) {
double num = parseToDouble(array[i 1]) * parseToDouble(array[i + 1]);
array[i - 1] = num; // probably change
to array[i + 1] = num;
array[i] = NULL;
array[i + 1] = NULL;
shift array[i + 2] and more down; //
write method shift (if array[i + 2] isOutOfBounds
array.length == 1)
}
if([array[i] equalToString:"/"]) {

double num = parseToDouble(array[i 1]) / parseToDouble(array[i + 1]);


array[i - 1] = num; // probably change
to array[i + 1] = num;
array[i] = NULL;
array[i + 1] = NULL;
shift array[i + 2] and more down; //
write method shift (if array[i + 2] isOutOfBounds
array.length == 1)
}
}
}
for (int = 0; i < array.length; i++) {
if ([array[i] equalToString:"+"] || [array[i]
equalToString:"-"]) {
if([array[i] equalToString:"+"]) {
double num = parseToDouble(array[i 1]) + parseToDouble(array[i + 1]);
array[i - 1] = num; // probably change
to array[i + 1] = num;
array[i] = NULL;
array[i + 1] = NULL;
shift array[i + 2] and more down; //
write method shift (if array[i + 2] isOutOfBounds
array.length == 1)
}
if([array[i] equalToString:"-"]) {
double num = parseToDouble(array[i 1]) - parseToDouble(array[i + 1]);
array[i - 1] = num; // probably change

to array[i + 1] = num;
array[i] = NULL;
array[i + 1] = NULL;
shift
array[i + 2] and more down; // write method shift
(if array[i + 2] isOutOfBounds array.length == 1)
}
}
}
if (array.length == 1) {
return [array[0] parseToDouble];
}
}
- (NSArray/NSMutableArray)shiftArray:
(NSArray/mutable)array downTwoFromIndex:
(int)index
{
// if (array.length > 3) {
NSArray array1 = [[NSArray alloc]
initWithAmountOfSpace: (array.length - 2)];
for (int i = index, int j = index - 2; i <
array.length; i++, j++) {
array1[j] = array[i];
}
if (array1[0] == NULL) {
for(int i = 0; i < index - 2; i++) {
array1[i] = array[i];
}
}
// } else {

// array = [[NSArray alloc] initWithElements:


array[0], nil];
// }
return array1;
}
array
array
array1
array
array1

3,
n,
5,
n,
1

+, 2, -, 4
n, 5, -, 4
-, 4
n, 1

array
array
array1
array1
array
array1
array1
array
array1
array
array1
array
array1

3, +, 2, *, 4, -, 3, /, 2, -, 1
3, +, n, n, 8, -, 3, /, 2, -, 1
n, n, 8, -, 3, /, 2, -, 1
3, +, 8, -, 3, /, 2, -, 1
3, +, 8, -, n, n, 1.5, -, 1
n, n, n, n, 1.5, -, 1
3, +, 8, -, 1.5, -, 1
n, n, 11, -, 1.5, -, 1
11, -, 1.5, -, 1
n, n, 9.5, -, 1
9.5, -, 1
n, n, 8.5
8.5

array 3, +, 2, *, 4, -, 3, /, (, 2, -, 1, )

You might also like