question

mdenizaltun-2225 avatar image
0 Votes"
mdenizaltun-2225 asked WayneAKing-0228 answered

Convert C++ to C#

Hi All,

I need a help to convert below C++ code to C#.

include <stdio.h>

include <stdlib.h>

int top = 10;
struct node
{
char ch;
struct node *next;
struct node *prev;
} *stack[11];
typedef struct node node;


void push(node *str)
{
if (top <= 0)
printf("Stack is Full ");
else
{
stack[top] = str;
top--;
}
}


node *pop()
{
node *exp;
if (top >= 10)
printf("Stack is Empty ");
else
exp = stack[++top];
return exp;
}
void convert(char exp[])
{
node op1, op2;
node temp;
int i;
for (i=0;exp[i]!='\0';i++)
if (exp[i] >= 'a'&& exp[i] <= 'z'|| exp[i] >= 'A' && exp[i] <= 'Z')
{
temp = (node
)malloc(sizeof(node));
temp->ch = exp[i];
temp->next = NULL;
temp->prev = NULL;
push(temp);
}
else if (exp[i] == '+' || exp[i] == '-' || exp[i] == '
' || exp[i] == '/' ||
exp[i] == '^')
{
op1 = pop();
op2 = pop();
temp = (node
)malloc(sizeof(node));
temp->ch = exp[i];
temp->next = op1;
temp->prev = op2;
push(temp);
}
}


void display(node *temp)
{
if (temp != NULL)
{
display(temp->prev);
printf("%c", temp->ch);
display(temp->next);
}
}


void main()
{
char exp[50];

printf("Enter the postfix expression :");
scanf("%s", exp);
convert(exp);
printf("\nThe Equivalant Infix expression is:");
display(pop());
printf("\n\n");

}

c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Before you try to convert this C++ (it looks much more like C than C++) code to another language, you ought to correct the errors in the code.

Function pop has undefined behavior if the stack is empty.

Function convert:
temp, op1, and op2 must be pointer types.
malloc returns a pointer to void which cannot be cast to a value of type node.
The first if would be easier to understand and more accurate if you used the standard function isalpha.
There is an apparent typo in the second if, a missing *.


1 Vote 1 ·
GuidoFranzke avatar image
0 Votes"
GuidoFranzke answered

Hello,
I know C++, but I can't programme in C# code (although I can read it).
I think you should ask in the C# forum. Normally speaking, C# programmers can read C++ . So they can help you. I suppose you should add the tag for the C# forum dotnet-csharp.html to your question.
Regards, Guido


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

Whenever I see a request such as this my suspicions
often get aroused that the asker is:

(1) Trying to complete an assignment in the target
language, and has done a web search for code to
plagiarize. Finding code that looks promising,
but in another language, the questioner then
tries to get someone to convert it for free.

(2) Or the asker wants to copy the functionality
of someone else's code so they can claim it as their
own.

(3) Of course, it's also possible that the C/C++ code
posted was actually written by the asker, and they
are just trying to migrate it to C# as part of a
general shift to that language.

Now none of the above may apply in this case, and
if not can be ignored as generalizations with
no personal association. If the mud doesn't stick,
then perhaps this is for personal use or benefit,
which then leads me to query if the asker is
willing to pay for a code conversion?

While waiting for someone to convert it for you for
free (someone usually will eventually, if only to
show off their own skills), you might try this
converter - it has a feature-limited free version
you can try:

Tangible Software Solutions
Source Code Converters
https://www.tangiblesoftwaresolutions.com/index.html

  • Wayne


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.