转换C ++代码执行C# [英] Convert C++ code do C#
本文介绍了转换C ++代码执行C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你们中的任何人都可以帮助我将C ++中的这些代码转换为C#吗?
我尝试了其他程序(也在线)进行转换,但是它们都没有给我正确的结果...非常感谢
Can anybody of you help me translate those codes in C++ to C# ?
I have tried different programs (online too) to convert, but neither of them gave me the correct result... Thanks alot
#include <stdio.h>
#include <stdlib.h>
#define max 100
int isdigit(char ch) {return '0' <= ch && ch <= '9';}
int isoperator(char ch) {return ch == '+' ? 1: -1;}
int ismultsign(char ch) {return ch == '*';}
long eval(char *str)
{
long val = 0;
char sign = '+';
long temp = 0;
int k = 0;
char *ptr = str;
while (*ptr)
{
if (isdigit(*ptr))
temp = temp*10 + *ptr - '0';
else if(!ismultsign(*ptr))
{
val = val + isoperator(sign)*temp;
temp = 0;
sign = *ptr;
}
ptr ++;
}
return val + isoperator(sign)*temp;
}
char *insert(char *str)
{
char *temp = (char *) malloc(2*max);
char *ptr = str;
int k = 0;
while (*ptr)
{
k+=2;
temp[k-2] = *ptr;
temp[k-1] = '*';
ptr ++;
}
temp[k-1] = 0;
return temp;
}
char *removemultsign(char *str)
{
char *temp = (char *) malloc (max);
char *ptr = temp;
while (*str)
{
if(!ismultsign(*str))
{
*ptr = *str;
ptr++;
}
str++;
}
*ptr = 0;
return temp;
}
void Try(char *str, int k, int sum)
{
if (*(str+k))
for (int j = 0; j < 3; j++)
{
char ch = str[k];
if (j==1) str[k] = '-';
else if (j==2) str[k] = '+';
long num = eval(str);
if (num == sum)
{
char *temp = removemultsign(str);
printf("\n%s = %d\n",temp, sum);
free (temp);
}
Try(str,k+2,sum);
str[k] = ch;
}
}
void main()
{
char str[] = "123456789";
char *ptr = insert(str);
Try(ptr,1,12);
free (ptr);
}
推荐答案
实际上,代码本质上是C代码……尝试将其转换为C#并没有多大意义.
重写程序以使用诸如字符串之类的托管类型会更好.
像removemultsign
这样的函数应重写为:
In fact, the code is essentially C code... and it does not make much sense to try to convert as it to C#.
It will be much better to rewrite the program to uses managed types like strings.
A function likeremovemultsign
should be rewritten as:
string removemultsign(string str)
{
return str.Replace("*", string.Empty);
}
而insert
函数可以这样写:
And insert
function can be written that way:
string insert(string str)
{
var builder = new StringBuilder();
foreach (var ch in str)
{
builder.Append(ch);
builder.Append("*");
}
if (builder.Length > 0)
{
builder.Remove(builder.Length - 1, 1);
}
return builder.ToString();
}
其他方法留给读者练习...
Other methods are left as an exercise for the reader...
为什么不只是用C ++编译代码并运行它.
然后,您可以复制输出并将其粘贴到基本的C#hello world程序中:
Why don''t you just compile the code in C++ and run it.
Then you can copy the ouput and paste into your basic C# hello world program:
// Hello1.cs
public class Hello1
{
public static void Main()
{
System.Console.WriteLine("123-45-67-8+9 = 12");
System.Console.WriteLine("123-4-5-6-7-89 = 12");
System.Console.WriteLine("123-4-5-6-7-89 = 12");
System.Console.WriteLine("123+45-67-89 = 12");
System.Console.WriteLine("123+45-67-89 = 12");
System.Console.WriteLine("12-3-4-5+6+7+8-9 = 12");
System.Console.WriteLine("12-3-4+5-6+7-8+9 = 12");
System.Console.WriteLine("12-3+4-5-6-7+8+9 = 12");
System.Console.WriteLine("12+3-4+5+6+7-8-9 = 12");
System.Console.WriteLine("12+3+4-5+6-7+8-9 = 12");
System.Console.WriteLine("12+3+4+5-6-7-8+9 = 12");
System.Console.WriteLine("1-2+34-5-6+7-8-9 = 12");
System.Console.WriteLine("1+2+34+5-6-7-8-9 = 12");
}
}
这两个程序将具有相同的输出,因此就所有意图和目的而言,它们都是等效的.
The two programs will have identical output, so for all intents and purposes, they will be equivalent.
阅读有关C#基础的教程.我们已经为您提供了许多有关如何以C#方式进行操作的提示.
C#简介 [ C#编程指南 [ C#站教程 [ Char.IsNumber [字符串 [
Read a tutorial on the basic of C#. We already give you a lot of hint on how to do it the C# way.
Introduction to C#[^]
Once you will have fully read that tutorial, you will understand the langage much better. If it is not enough, you can read even more...
C# programming guide[^]
C# Station Tutorial[^]
Now some more specific links:
You can read Char.IsNumber[^]
And for free, if you uses managed string, you don''t have to worry about freeing memory as it is garbage collected.
char *str
is remplaced with the use of string[^]. Do the appropriate changes.
You might first convert C code to uses indexes instead of pointers as the conversion will then be easier. And that way, you would do it "one step at the time".
这篇关于转换C ++代码执行C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文