Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
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


C++
#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);
}
Posted
Updated 16-Aug-11 11:22am
v2
Comments
[no name] 16-Aug-11 16:43pm    
Looks like the eval method is just set up to parse the string and run math ops. I would suggest re-writing from scratch since pointers are not necessary to do this in C#.

But maybe that is not why you want it converted.
pham.quocduy 17-Aug-11 11:53am    
I really do not understand for example "char *str", "int isdigit(char ch)" or "free (temp)" how it should be in C#

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 like removemultsign should be rewritten as:

C#
string removemultsign(string str)
{
    return str.Replace("*", string.Empty);
}


And insert function can be written that way:
C#
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...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Aug-11 23:29pm    
You're right. There is no "translation" from C++ to C#. My 5.
--SA
pham.quocduy 17-Aug-11 12:15pm    
I do not understand IF condition at method Try
void Try(char *str, int k, int sum)
{
if (*(str+k))...

Thanks for the explanation
Philippe Mori 17-Aug-11 12:45pm    
if (str[k] != '\0')... would be equivalent
pham.quocduy 18-Aug-11 6:58am    
All of a sudden appears this line.
...free (temp);...
It is necessary for the program?
Philippe Mori 18-Aug-11 7:50am    
See Solution 3. You need to learn C# basics.
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:

C#
// 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.
 
Share this answer
 
Comments
[no name] 16-Aug-11 16:41pm    
I think he wants the logic part converted...
TRK3 16-Aug-11 16:47pm    
Really? I guess I forgot the <SARCASM> </SARCASM> tags.
Legor 17-Aug-11 6:05am    
Maybe Collin was sarcastic too ;)
pham.quocduy 17-Aug-11 12:16pm    
Thanks for the idea :)
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".
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900