none
convert c++ code to csharp(c#) RRS feed

  • Question

  • bool maze(char a[1000][1000],int i,int j,int m,int n,int tari,int tarj,int r[][1000],int hor,int ver)
    {
    //cout<<hor<<""<<ver<<" ";
    if(i==tari&&j==tarj)
    {
    r[i][j]=1;
    for(int k=0;k<m;k++)
    {
    for(int l=0;l<n;l++)
    {
    cout<<r[k][l]<<" ";
    }
    cout<<endl;
    }
    cout<<endl;
    return true;
    }
    r[i][j]=1;
    if(a[i][j+1]!='X'&&j+1<n&&ver==1)
    {
    bool rightsebaatbangayi=maze(a,i,j+1,m,n,tari,tarj,r,hor,ver);
    if(rightsebaatbangayi)
    {
    return true;
    }
    }
    if(a[i][j-1]!='X'&&j-1>n&&ver==0)
    {
    bool leftsebaatbangayi=maze(a,i,j-1,m,n,tari,tarj,r,hor,ver);
    if(leftsebaatbangayi13wz)
    {
    return true;
    }
    }
    if(a[i+1][j]!='X'&&i+1<m&&hor==1)
    {
    bool neechesebaatbangayi=maze(a,i+1,j,m,n,tari,tarj,r,hor,ver);
    if(neechesebaatbangayi)
    {
    return true;
    }
    }
    if(a[i-1][j]!='X'&&i-1>0&&hor==0)
    {
    bool uparsebaatbangayi=maze(a,i-1,j,m,n,tari,tarj,r,hor,ver);
    if(uparsebaatbangayi)
    {
    return true;
    }
    }
    r[i][j]=0;
    return false;
    }
    int main() {
    int n=6,m=7,tari,tarj,curi,curj;
    cin>>curi>>curj;
    cin>>tari>>tarj;
    char maze1[1000][1000];
    bool ans;
    for(int i=0; i<n;i++)
    {
    for(int j=0;j<m;j++)
    {
    cin>>maze1[i][j];
    }
    }
    int sol[1000][1000]={0};
    //case 1
    //if any one is in top or bottom lane
    if (tari==5||tari==0||curi==0||curi==5)
    {
    if(curi<tari)
    {//neeche
    if(curj<tarj)
    {
    //right
    ans=maze(maze1,curi,curj,n,m,tari,tarj,sol,1,1);
    }
    else
    {
    //left
    ans=maze(maze1,curi,curj,n,m,tari,tarj,sol,1,0);
    }
    }
    if(curi>tari)
    {//upar
    if(curj<tarj)
    {
    //right
    ans=maze(maze1,curi,curj,n,m,tari,tarj,sol,0,1);
    }
    else
    {
    //left
    ans=maze(maze1,curi,curj,n,m,tari,tarj,sol,0,0);
    }
    }
    }
    // ans=maze(maze1,curi,curj,n,m,tari,tarj,sol);
    if(!ans)
    {
    cout<<"-1";
    }
    }
    Saturday, March 2, 2019 4:19 AM

All replies

  • There are 2 main issues for converting this particular C++ sample to C#: 1. console input/output and 2. C++ rectangular arrays.  The following is what our converter produces (with the help of 2 helper classes), but you may have to make a couple of manual adjustments:

    using System;
    
    private bool maze(string[] a, int i, int j, int m, int n, int tari, int tarj, int[][] r, int hor, int ver)
    {
    	//cout<<hor<<""<<ver<<" ";
    	if (i == tari && j == tarj)
    	{
    		r[i][j] = 1;
    		for (int k = 0;k < m;k++)
    		{
    			for (int l = 0;l < n;l++)
    			{
    				Console.Write(r[k][l]);
    				Console.Write(" ");
    			}
    			Console.Write("\n");
    		}
    		Console.Write("\n");
    		return true;
    	}
    
    	r[i][j] = 1;
    	if (a[i][j + 1] != 'X' && j + 1 < n && ver == 1)
    	{
    		bool rightsebaatbangayi = maze(a, i, j + 1, m, n, tari, tarj, r, hor, ver);
    		if (rightsebaatbangayi)
    		{
    			return true;
    		}
    	}
    
    	if (a[i][j - 1] != 'X' && j - 1 > n && ver == 0)
    	{
    		bool leftsebaatbangayi = maze(a, i, j - 1, m, n, tari, tarj, r, hor, ver);
    		if (leftsebaatbangayi13wz)
    		{
    			return true;
    		}
    	}
    
    	if (a[i + 1][j] != 'X' && i + 1 < m && hor == 1)
    	{
    		bool neechesebaatbangayi = maze(a, i + 1, j, m, n, tari, tarj, r, hor, ver);
    		if (neechesebaatbangayi)
    		{
    			return true;
    		}
    	}
    
    	if (a[i - 1][j] != 'X' && i - 1 > 0 && hor == 0)
    	{
    		bool uparsebaatbangayi = maze(a, i - 1, j, m, n, tari, tarj, r, hor, ver);
    		if (uparsebaatbangayi)
    		{
    			return true;
    		}
    	}
    
    	r[i][j] = 0;
    	return false;
    }
    
    static int Main()
    {
    	int n = 6;
    	int m = 7;
    	int tari;
    	int tarj;
    	int curi;
    	int curj;
    	curi = int.Parse(ConsoleInput.ReadToWhiteSpace(true));
    	curj = int.Parse(ConsoleInput.ReadToWhiteSpace(true));
    	tari = int.Parse(ConsoleInput.ReadToWhiteSpace(true));
    	tarj = int.Parse(ConsoleInput.ReadToWhiteSpace(true));
    
    	char[][] maze1 = RectangularArrays.RectangularCharArray(1000, 1000);
    	bool ans;
    	for (int i = 0; i < n;i++)
    	{
    		for (int j = 0;j < m;j++)
    		{
    			maze1[i][j] = char.Parse(ConsoleInput.ReadToWhiteSpace(true));
    		}
    	}
    
    	int[][] sol = new int[1000][1000];
    	//case 1
    	//if any one is in top or bottom lane
    	if (tari == 5 || tari == 0 || curi == 0 || curi == 5)
    	{
    		if (curi < tari)
    		{ //neeche
    			if (curj < tarj)
    			{
    				//right
    				ans = maze(maze1, curi, curj, n, m, tari, tarj, sol, 1, 1);
    			}
    			else
    			{
    				//left
    				ans = maze(maze1, curi, curj, n, m, tari, tarj, sol, 1, 0);
    			}
    		}
    
    		if (curi > tari)
    		{ //upar
    			if (curj < tarj)
    			{
    				//right
    				ans = maze(maze1, curi, curj, n, m, tari, tarj, sol, 0, 1);
    			}
    			else
    			{
    				//left
    				ans = maze(maze1, curi, curj, n, m, tari, tarj, sol, 0, 0);
    			}
    		}
    	}
    
    	// ans=maze(maze1,curi,curj,n,m,tari,tarj,sol);
    	if (!ans)
    	{
    		Console.Write("-1");
    	}
    }
    
    
    //----------------------------------------------------------------------------------------
    //	Copyright © 2006 - 2019 Tangible Software Solutions, Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class provides the ability to convert basic C++ 'cin' and C 'scanf' behavior.
    //----------------------------------------------------------------------------------------
    internal static class ConsoleInput
    {
    	private static bool goodLastRead = false;
    	public static bool LastReadWasGood
    	{
    		get
    		{
    			return goodLastRead;
    		}
    	}
    
    	public static string ReadToWhiteSpace(bool skipLeadingWhiteSpace)
    	{
    		string input = "";
    
    		char nextChar;
    		while (char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			//accumulate leading white space if skipLeadingWhiteSpace is false:
    			if (!skipLeadingWhiteSpace)
    				input += nextChar;
    		}
    		//the first non white space character:
    		input += nextChar;
    
    		//accumulate characters until white space is reached:
    		while (!char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			input += nextChar;
    		}
    
    		goodLastRead = input.Length > 0;
    		return input;
    	}
    
    	public static string ScanfRead(string unwantedSequence = null, int maxFieldLength = -1)
    	{
    		string input = "";
    
    		char nextChar;
    		if (unwantedSequence != null)
    		{
    			nextChar = '\0';
    			for (int charIndex = 0; charIndex < unwantedSequence.Length; charIndex++)
    			{
    				if (char.IsWhiteSpace(unwantedSequence[charIndex]))
    				{
    					//ignore all subsequent white space:
    					while (char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    					{
    					}
    				}
    				else
    				{
    					//ensure each character matches the expected character in the sequence:
    					nextChar = (char)System.Console.Read();
    					if (nextChar != unwantedSequence[charIndex])
    						return null;
    				}
    			}
    
    			input = nextChar.ToString();
    			if (maxFieldLength == 1)
    				return input;
    		}
    
    		while (!char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			input += nextChar;
    			if (maxFieldLength == input.Length)
    				return input;
    		}
    
    		return input;
    	}
    }
    
    //----------------------------------------------------------------------------------------
    //	Copyright © 2006 - 2019 Tangible Software Solutions, Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class includes methods to convert C++ rectangular arrays (jagged arrays
    //	with inner arrays of the same length).
    //----------------------------------------------------------------------------------------
    internal static class RectangularArrays
    {
        public static char[][] RectangularCharArray(int size1, int size2)
        {
            char[][] newArray = new char[size1][];
            for (int array1 = 0; array1 < size1; array1++)
            {
                newArray[array1] = new char[size2];
            }
    
            return newArray;
        }
    }
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, March 2, 2019 3:09 PM