我如何将此代码从C语言转换为C#? [英] How I can convert this code from C language to C#?
本文介绍了我如何将此代码从C语言转换为C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <stdlib.h>
#define strcasecmp _stricmp
struct BSTnode {
char word[128], meaning[256];
struct BSTnode *left, *right;
};
struct BSTnode *root = NULL;
struct BSTnode * createNode(char *word, char *meaning) {
struct BSTnode *newnode;
newnode = (struct BSTnode *)malloc(sizeof(struct BSTnode));
strcpy(newnode->word, word);
strcpy(newnode->meaning, meaning);
newnode->left = newnode->right = NULL;
return newnode;
}
void insert(char *word, char *meaning) {
struct BSTnode *parent = NULL, *current = NULL, *newnode = NULL;
int res = 0;
if (!root) {
root = createNode(word, meaning);
return;
}
for (current = root; current != NULL;
current = (res > 0) ? current->right : current->left) {
res = strcasecmp(word, current->word);
if (res == 0) {
printf("Duplicate entry!!\n");
return;
}
parent = current;
}
newnode = createNode(word, meaning);
res > 0 ? (parent->right = newnode) : (parent->left = newnode);
return;
}
void deleteNode(char *str) {
struct BSTnode *parent = NULL, *current = NULL, *temp = NULL;
int flag = 0, res = 0;
if (!root) {
printf("BST is not present!!\n");
return;
}
current = root;
while (1) {
res = strcasecmp(current->word, str);
if (res == 0)
break;
flag = res;
parent = current;
current = (res > 0) ? current->left : current->right;
if (current == NULL)
return;
}
if (current->right == NULL) {
if (current == root && current->left == NULL) {
free(current);
root = NULL;
return;
}
else if (current == root) {
root = current->left;
free(current);
return;
}
flag > 0 ? (parent->left = current->left) :
(parent->right = current->left);
}
else {
temp = current->right;
if (!temp->left) {
temp->left = current->left;
if (current == root) {
root = temp;
free(current);
return;
}
flag > 0 ? (parent->left = temp) :
(parent->right = temp);
}
else {
struct BSTnode *successor = NULL;
while (1) {
successor = temp->left;
if (!successor->left)
break;
temp = successor;
}
temp->left = successor->right;
successor->left = current->left;
successor->right = current->right;
if (current == root) {
root = successor;
free(current);
return;
}
(flag > 0) ? (parent->left = successor) :
(parent->right = successor);
}
}
free(current);
return;
}
void findElement(char *str) {
struct BSTnode *temp = NULL;
int flag = 0, res = 0;
if (root == NULL) {
printf("Binary Search Tree is out of station!!\n");
return;
}
temp = root;
while (temp) {
if ((res = strcasecmp(temp->word, str)) == 0) {
printf("Word : %s", str);
printf("Meaning: %s", temp->meaning);
flag = 1;
break;
}
temp = (res > 0) ? temp->left : temp->right;
}
if (!flag)
printf("Search Element not found in Binary Search Tree\n");
return;
}
void inorderTraversal(struct BSTnode *myNode) {
if (myNode) {
inorderTraversal(myNode->left);
printf("Word : %s", myNode->word);
printf("Meaning : %s", myNode->meaning);
printf("\n");
inorderTraversal(myNode->right);
}
return;
}
int main() {
int ch;
char str[128], meaning[256];
while (1) {
printf("\n1. Insertion\t2. Deletion\n");
printf("3. Searching\t4. Traversal\n");
printf("5. Exit\nEnter ur choice:");
scanf_s("%d", &ch);
getchar();
switch (ch) {
case 1:
printf("Word to insert:");
fgets(str, 100, stdin);
printf("Meaning:");
fgets(meaning, 256, stdin);
insert(str, meaning);
break;
case 2:
printf("Enter the word to delete:");
fgets(str, 100, stdin);
deleteNode(str);
break;
case 3:
printf("Enter the search word:");
fgets(str, 100, stdin);
findElement(str);
break;
case 4:
inorderTraversal(root);
break;
case 5:
exit(0);
default:
printf("You have entered wrong option\n");
break;
}
}
return 0;
}
我的尝试:
此代码用于使用二进制树在C中进行字典实现我希望转换为c#
What I have tried:
this code for dictionary implementation in C using Binary Trees i want convert to c#
推荐答案
不要。将代码从C转换为C#将不会产生良好的C#代码 - C是基于指针的,而C#则不是。 C#也是基于类的,C不知道。
和C#使用.NET,其中包含大量已经为你处理所有繁琐的东西的集合 - 包括字典,列表和HashTables。
用C#从头开始编写 - 你会得到比你从C转换更好的结果!
Don't. Converting code from C to C# will not produce good C# code - C is all pointer based, and C# isn't. C# is also class based, which C has no knowledge of.
And C# uses .NET which includes a wealth of collections which already handle all "the fiddly stuff" for you - including Dictionaries, Lists, and HashTables.
Write it from scratch in C# - you will get a much better result than you possibly would converting from C!
正如 Griff 明智地建议的那样,不要这样做。使用可用类从头开始重写它。我想 SortedSet(T)Class(System.Collections.Generic) [ ^ ]可以满足要求。
As Griff wisely suggested, don't do that. Rewrite it from scratch using available classes. I guess the SortedSet(T) Class (System.Collections.Generic)[^] could fit the bill.
这篇关于我如何将此代码从C语言转换为C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文