编程生成C#表的决定? [英] Programmatically generate decision table in C#?
问题描述
我这种情况,我需要让用户定义基于给定条件的数量决定。例如我的节目需要自动生成的矩阵如下面给出了有两个条件(IsMale和IsSmoker):
IsMale:否否否否
IsSmoker:YES NO YES NO
和所述deicsion由用户定义的,因此以下任一可以是有效的:
IsMale:否否否否
IsSmoker:YES NO YES NO
决定:五六五六
IsMale:否否否否
IsSmoker:YES NO YES NO
决定:F F F F
IsMale:否否否否
IsSmoker:YES NO YES NO
决定:T(T)T(T)
对于每个条件只能有两种状态,真和假。这样的组合的总数被计算为如下:
没有可能的状态(S)以没有的条件(C)电源 小号^ C =总没有的组合
4的可能性(2 ^ 2 = 4)
条件A T(T)F F
条件B五六五六
8的可能性(2 ^ 3 = 8),
条件A T(T)T(T)F F F F
条件B T(T)F F五六五六
条件(C T)F T F TŤF F
希望我已经解释了自己比原来的问题会好一点。
更新: 根据由<给定的答案href="http://stackoverflow.com/questions/1253454/programmatically-generate-decision-table-in-c/1253527#1253527">Guffa.下面是手工计算他的算法来产生不同的组合。
4的可能性(2 ^ 2 = 4)
指数= 0,(右移0)
二进制8 4 2 1值
原始0 0 0 1 1
&放大器; 1 0 0 0 1 1 T&
原始0 0 1 0 2
&放大器; 1 0 0 0 1 0,F
原始0 0 1 1 3
&放大器; 1 0 0 0 1 1 T&
原来0 1 0 0 4
&放大器; 1 0 0 0 1 0,F
指数= 1,(右移位1)
二进制8 4 2 1值
原始0 0 0 1 1
移动0 0 0 0 0
&放大器; 1 0 0 0 1 0,F
原始0 0 1 0 2
移位0 0 0 1 1
&放大器; 1 0 0 0 1 1 T&
原始0 0 1 1 3
移位0 0 0 1 1
&放大器; 1 0 0 0 1 1 T&
原来0 1 0 0 4
移0 0 1 0 2
&放大器; 1 0 0 0 1 0,F
的组合:
条件1:TFTF
条件2:FTTF
输出矩阵是相当简单:
INT条件= 3;
对于(INT C = 0; C&LT;条件; C ++){
Console.WriteLine(
条件{0} {1},
(焦)('A'+ C),
新的String(
Enumerable.Range(0,(1&其中;&所述;条件))
。选择(N =&gt;中的TF[(N&GT;&以及c)及1])
.ToArray()
)
);
}
所以,你想用它做什么呢?
I have this situation that I need to let users define decisions based on the number of given conditions. For example my program needs to automatically generate a matrix as below given that there are two conditions (IsMale and IsSmoker):
IsMale: YES YES NO NO
IsSmoker: YES NO YES NO
And the deicsion is defined by user, therefore any of the following can be valid:
IsMale: YES YES NO NO
IsSmoker: YES NO YES NO
Decision: T F T F
IsMale: YES YES NO NO
IsSmoker: YES NO YES NO
Decision: F F F F
IsMale: YES YES NO NO
IsSmoker: YES NO YES NO
Decision: T T T T
For each condition there can only be two states, True and False. So the total number of combinations are calculated as below:
no of possible states (S) to the power of no of conditions (C) S ^ C = total no of combinations
4 possibilities (2 ^ 2 = 4)
Condition A T T F F
Condition B T F T F
8 possibilities (2 ^ 3 = 8)
Condition A T T T T F F F F
Condition B T T F F T F T F
Condition C T F T F T T F F
Hope I have explained myself a bit better than the original question.
Updated: according to the answer given by Guffa. Below is the hand calculation of his algorithm to generate the different combinations.
4 possibilities (2^2=4)
index = 0, (right shift 0)
binary 8 4 2 1 Value
original 0 0 0 1 1
& 1 0 0 0 1 1 T
original 0 0 1 0 2
& 1 0 0 0 1 0 F
original 0 0 1 1 3
& 1 0 0 0 1 1 T
original 0 1 0 0 4
& 1 0 0 0 1 0 F
index = 1, (right shift 1)
binary 8 4 2 1 Value
original 0 0 0 1 1
shift 0 0 0 0 0
& 1 0 0 0 1 0 F
original 0 0 1 0 2
shift 0 0 0 1 1
& 1 0 0 0 1 1 T
original 0 0 1 1 3
shift 0 0 0 1 1
& 1 0 0 0 1 1 T
original 0 1 0 0 4
shift 0 0 1 0 2
& 1 0 0 0 1 0 F
combinations:
Condition 1: TFTF
Condition 2: FTTF
Outputting the matrix is rather trivial:
int conditions = 3;
for (int c = 0; c < conditions; c++) {
Console.WriteLine(
"Condition {0} : {1}",
(char)('A' + c),
new String(
Enumerable.Range(0, (1 << conditions))
.Select(n => "TF"[(n >> c) & 1])
.ToArray()
)
);
}
So, what do you want to do with it?
这篇关于编程生成C#表的决定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!