好吧,你会看到问题是,我正在翻译一个c#java代码,它通过huffman方法加密一个文本文件,在java中,原来的程序运行得很完美,但这里的问题是,我需要c#。
我试着把它翻译成c#但是它在setprefixcode和buildtree方法中产生了4个错误,我什么都不懂,我不知道我翻译错了什么,或者我需要实现其他东西。
我真的需要知道如何正确地翻译它
救命啊!!!!!!
namespace Huffman_v2
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
String documento;
private static IDictionary<char, string> charPrefixHashMap = new Dictionary<char, string> ();
internal static HuffmanNode root;
private void btnCifrar_Click(object sender, EventArgs e)
{
string txtText = documento;
IDictionary<char, int> freq = new Dictionary<char, int> ();
for (int i = 0; i < txtText.Length; i++)
{
if (!freq.ContainsKey(txtText[i]))
{
freq[txtText[i]] = 0;
}
freq[txtText[i]] = freq[txtText[i]] + 1;
}
txtFrecuencia.Text = freq.ToString();
root = buildTree(freq);
setPrefixCodes(root, new StringBuilder());
StringBuilder s = new StringBuilder();
for (int i = 0; i < txtText.Length; i++)
{
char c = txtText[i];
s.Append(charPrefixHashMap[c]);
}
string xs = decode(s.ToString());
txtPrefijos.Text = charPrefixHashMap.ToString();
txtCifrado.Text = xs.ToString();
}
private static void setPrefixCodes(HuffmanNode node, StringBuilder prefix)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
charPrefixHashMap[node.data] = prefix.ToString();
}
else
{
prefix.Append('0');
setPrefixCodes(node.left, prefix);
prefix.deleteCharAt(prefix.Length - 1); //error en .deleteChatAt
prefix.Append('1');
setPrefixCodes(node.right, prefix);
prefix.deleteCharAt(prefix.Length - 1); //error en .deleteChatAt
}
}
}
private static string decode(string s)
{
StringBuilder stringBuilder = new StringBuilder();
HuffmanNode temp = root;
for (int i = 0; i < s.Length; i++)
{
int j = int.Parse(s[i].ToString());
if (j == 0)
{
temp = temp.left;
if (temp.left == null && temp.right == null)
{
stringBuilder.Append(temp.data);
temp = root;
}
}
if (j == 1)
{
temp = temp.right;
if (temp.left == null && temp.right == null)
{
stringBuilder.Append(temp.data);
temp = root;
}
}
}
return stringBuilder.ToString();
}
private static HuffmanNode buildTree(IDictionary < char, int > freq)
{
PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<HuffmanNode>(); // esta linea tiene el error en PriorityQueue<HuffmanNode>
ISet<char> keySet = (ISet<char>) freq.Keys;
foreach(char ? c in keySet) {
HuffmanNode huffmanNode = new HuffmanNode();
huffmanNode.data = (char) c;
huffmanNode.frequency = freq.get(c); //aqui en .get
huffmanNode.left = null;
huffmanNode.right = null;
priorityQueue.offer(huffmanNode);
}
Debug.Assert(priorityQueue.size() > 0);
while (priorityQueue.size() > 1)
{
HuffmanNode x = priorityQueue.peek();
priorityQueue.poll();
HuffmanNode y = priorityQueue.peek();
priorityQueue.poll();
HuffmanNode sum = new HuffmanNode();
sum.frequency = x.frequency + y.frequency;
sum.data = '-';
sum.left = x;
sum.right = y;
root = sum;
priorityQueue.offer(sum);
}
return priorityQueue.poll();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!