c#-创建一个数的质数分解的字符串(带指数)
作者:互联网
因此,我有一个程序可以创建用户输入数字的主要因素的二叉树并将其显示在treeView控件中:
现在,我想创建一个类似于消息框中显示的字符串,但要使用指数(“ 256 = 2 ^ 8”,“ 1234567890 = 2 X 3 ^ 2 X 5 X 3607 X 3803”)
我当前的代码如下:
private void LabelDisplayCondensed(FactorTreeNode currentNode)
{
string result = Convert.ToString(root.Key) + " = "
+ Convert.ToString(currentNode.Left.Key);
FactorTreeNode prevNode = currentNode;
int exponent = 1;
while (currentNode.Right != null)
{
prevNode = currentNode;
currentNode = currentNode.Right;
if (currentNode.Left.Key == prevNode.Left.Key)
{
exponent += 1;
}
else
{
exponent = 1;
}
if ((exponent != 1) && (currentNode.Left.Key != prevNode.Left.Key))
{
result += " ^ " + exponent + " X " + currentNode.Left.Key;
}
}
MessageBox.Show(result);
}
这是我最新的,绝望的尝试.用树的根调用该函数.我意识到这段代码是完全有缺陷的.我碰到的当前墙是currentNode到达树中最右边的孩子,评估其.Left.Key的关键
if (currentNode.Left.Key == prevNode.Left.Key)
并崩溃,因为.Left为空.
实际上我早些时候离我很近.我认为在500评估为“ 500 = 2 ^ 2 X 5 ^ 2 ^ 2”的时候(而不是理想的500 = 2 ^ 2 X 5 ^ 3)
这是我的FactorTreeNode的代码:
class FactorTreeNode
{
// constants
// variables
private long data; // this is our only data, so also key
private FactorTreeNode leftPointer;
private FactorTreeNode rightPointer;
// these pointers point to the
// left and right children respectively
private FactorTreeNode parentPainter;
// pointer to the parent of the node
// constructors
public FactorTreeNode(long newValue)
{
Key = newValue;
Left = null;
Right = null;
Parent = null;
}
// Properties
public long Key
{
get
{
return data;
}
set
{
data = value;
}
}
public FactorTreeNode Left
{
get
{
return leftPointer;
}
set
{
leftPointer = value;
}
}
public FactorTreeNode Right
{
get
{
return rightPointer;
}
set
{
rightPointer = value;
}
}
public FactorTreeNode Parent
{
get
{
return parentPainter;
}
set
{
parentPainter = value;
}
}
}
我整天都在咀嚼.感谢您的帮助.
解决方法:
假设您的FactorTreeNodeclass是这样的:
class FactorTreeNode
{
public FactorTreeNode(long key) { this.Key = key; }
public FactorTreeNode Left { get; set; }
public FactorTreeNode Right { get; set; }
public long Key { get; private set; }
}
然后这将起作用:
private void LabelDisplayCondensed(FactorTreeNode root)
{
string result = Convert.ToString(root.Key) + " =";
long prevFactor = 0;
int exponent = 0;
bool firstFactor = true;
FactorTreeNode currentNode = root;
while (currentNode != null)
{
long nextFactor = currentNode.Left == null ?
currentNode.Key :
currentNode.Left.Key;
if (nextFactor != prevFactor && prevFactor != 0)
{
result += WriteFactor(prevFactor, exponent, ref firstFactor);
exponent = 1;
}
else
{
exponent++;
}
prevFactor = nextFactor;
currentNode = currentNode.Right;
}
result += WriteFactor(prevFactor, exponent, ref firstFactor);
MessageBox.Show(result);
}
private string WriteFactor(long factor, int exponent, ref bool firstFactor)
{
string result = firstFactor ? " " : " X ";
firstFactor = false;
if (exponent == 1)
{
result += factor.ToString();
}
else
{
result += factor.ToString() + " ^ " + exponent.ToString();
}
return result;
}
显然,这不包括对树是否有效的检查.
您可能还希望使用StringBuilder实际构建字符串,而不是执行所有这些附加操作.
标签:prime-factoring,binary-tree,c 来源: https://codeday.me/bug/20191122/2059022.html