Dear friends,

do you know how to convert the following codes from C++ into C# ?

Thanx for your helps

Duy BK

#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);
}

Recommended Answers

All 10 Replies

How about you post YOUR attempt, then we suggest where you might have gone wrong.

As opposed to expecting us to do all the work for you.

How about you post YOUR attempt, then we suggest where you might have gone wrong.

As opposed to expecting us to do all the work for you.

Also i got a job, here is the task:

Enter the + / - in the middle of the digits: 123456789 (in that order) to form an expression value = n.
For a positive integer n is given.

Examples:
n = 12 answer is: 123 + 45 - 67 - 89
n = 1 answer is: 1+2+3-4-5-6-7+8+9
n = 122 answer is: 12 +34 -5 -6 +78 +9

requirement - all numbers 1-9 must be used

I found a solution, but sadly in C++. I used different "converter C++ code to C#" but i got just incorrect results.

Also i need your helps guys !

Sorry for my bad English, im a czech...

> I found a solution, but sadly in C++
Yeah, there's a lot of "finding" going on.
http://www.daniweb.com/software-development/cpp/threads/374401/1611050

People who go around "finding" code don't seem to be that interested in learning anything. Only in (ab)using the generosity of online forums just to get some free coding done.

Try http://www.vworker.com/ if you're not interested in actually learning, but just interested in getting something done in exchange for money.

> I found a solution, but sadly in C++
Yeah, there's a lot of "finding" going on.
http://www.daniweb.com/software-development/cpp/threads/374401/1611050

People who go around "finding" code don't seem to be that interested in learning anything. Only in (ab)using the generosity of online forums just to get some free coding done.

Try http://www.vworker.com/ if you're not interested in actually learning, but just interested in getting something done in exchange for money.

Certainly you're right. But how can you learn something when you know nothing about it? You have to look for, ask someone who knows.

I sent this problem to a forum in our community. 1 user recommended me to look at the code to understand the problem. Unfortunately, I am a beginner C # programmer, and these codes are in C++. Before i can understand this problem, I must first know what the program actually does, and then apply in my own program.

I know a lot of those people who just copied the code from others, without knowing what the codes mean. But I really want to learn something, and I need help from others. I'm not the one who already knows everything from birth

So google "C# tutorials" and do some reading for a while.

I mean, it wouldn't take long to figure out how to write say "hello world", or how to declare/call a function, or write a for loop, or perform a bit of string handling.

So google "C# tutorials" and do some reading for a while.

I mean, it wouldn't take long to figure out how to write say "hello world", or how to declare/call a function, or write a for loop, or perform a bit of string handling.

Now I really dont know what you mean? :)

Dear friends,

do you know how to convert the following codes from C++ into C# ?

Thanx for your helps

Duy BK

#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);
}

Anyone can help pls ? :(

Now I really dont know what you mean? :)

Here is the help you seek

Now do the same with c++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.