Info

此问题已关闭。 请重新打开它进行编辑或回答。

Need help with procedural placement of parentheses.

3 次查看(过去 30 天)
I have been working on some code for the procedural placement of parentheses and have run into a snag with the following code:
function D = parComboD(amount, item)
%Positions of parentheses according to the equation's array
C = parComboC(amount,item);
D = zeros(amount - 2,2);
p = 1;
x = 1;
y = 1;
pos = 1;
n = 0;
while p <= amount - 2
if C(y,x) >= 2
D(p,1) = pos;
D(p,2) = pos + 4*(C(y,x) - 1);
p = p + 1;
if C(y,x) >= 3
x = x + 1;
pos = pos + 1;
n = n + 1;
if y == 2
y = y - 1;
end
elseif y == 1
y = y + 1;
if ~(C(y,x) == 1)
pos = pos + 6;
n = n + 6;
end
else
C(:,x) = [];
x = x - 1;
pos = pos + 4*C(1,x) - 2 - n;
n = 0;
end
elseif y == 1
y = y + 1;
pos = pos + 2;
n = n + 2;
else
C(:,x) = [];
x = x - 1;
pos = pos + 4*C(1,x) - 2 - n;
n = 0;
end
end
D = D';
end
parComboC outputs the sizes of the parenthetical groups on the basis of the 'amount' of numbers and the 'item' in a list on how to order the parentheses.
parComboD is supposed to tell me where the open (top) and close (bottom) parentheses would be if the equation were a string.
Here are two examples:
parComboC(7,60) =
3 2 1 2
4 1 3 1
parComboD(7,60) =
1 2 11 14 15
9 6 23 22 19
parComboC(8,50) =
1 2 3 1
7 5 2 2
parComboD(8,50) =
3 4 10 11 14 11
27 8 26 19 18 15
Notice how the second example for parComboD gives off a repeated value. Here is what the output should be:
parComboD(8,50) =
3 4 10 11 14 21
27 8 26 19 18 25
Part A just tells the number of parenthetical combinations that can be achieved with the given 'amount' of numbers.
Part C is the spacing for the parentheses reading column by column.
parComboC(7,60) =
3 2 1 2
4 1 3 1
In the above example, the 3 & 4 would represent a group of 3 followed by a group of 4. The second column tells what is inside of the group of 3 as it must be broken down further. In this case it is broken into a group of 2 and the 1. Now that the group of 3 is sorted the third column represents the internal parts of the group of 4. The pattern continues like this.
Part B pretty much outputs commands for part C to follow. It does this by first taking the parenthetical 'item' from the input and systematically subtracts portions of parCombo(amount) until that would result in a negative number. This is done in the following form:
item = item - parComboA(j)*parComboA(7-j)
For instance, parComboB(7,60) does the following:
60 - parComboA(1)*parComboA(7-1) = 16
16 - parComboA(2)*parComboA(7-2) = 4
4 - parComboA(3)*parComboA(7-3) = -6
The last value of 'j' is saved as well as the value of 'amount' - 'j'. Both of these values are then sent through part A and the results are used in tangent to make an array of size:
(parComboA(j)*parComboA(amount - j)) x 2
Which in the case of parComboB(7,60) looks like:
1 1
2 1
1 2
2 2
1 3
2 3
1 4
2 4
1 5
2 5
The next step is to take the last non-negative 'item' value (in this example '4'), select that row from the array above, place the values of 'j' and 'amount' - 'j' above them and then transpose the rows and columns of the new array.
parComboB(7,60) =
3 2
4 2
The best way to read this output is: the second kind of a group of 3 and then the second kind of a group of 4.
Part D attempts to show the places where the open (top) and close (bottom) parentheses are supposed to be placed. It tries to do this in the same manner that I described part C being read while leaving room for single digit numbers and operation symbols.
parComboD(7,60) =
1 2 11 14 15
9 6 23 22 19
So the above could be made into a char array that looks like the following:
'((6+6)-6)*(6-((6-6)+6))'
Note the number and the operations are random and are only there as placeholders to show the locations of the parentheses.
  4 个评论
Stephen23
Stephen23 2018-7-30
@Brent King: your code has bug in it. You will have to track it down and fix it.
Note that your code includes almost no comments, explanations, references. Note that your code does not have meaningful variable names. Note that you have not explained anything about the algorithm. Note that the code includes recursive functions, three files, and more than one hundred lines of code. Note that this means all we have is some broken code and no idea what it should be doing.
If you want help with this then please help us by explaining how the algorithm works, or what the exact problem is, what steps you have taken... etc. The more you help us the more likely it is that people will understand the problem and figure out a solution for you.
Brent King
Brent King 2018-8-5
Finally fixed the explanations for each part in what they do and how they do it. I hope that clears a few things up.

回答(0 个)

此问题已关闭。

标签

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by