将c ++代码转换为csharp(c#) [英] convert c++ code to csharp(c#)

查看:221
本文介绍了将c ++代码转换为csharp(c#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


bool 迷宫 char a [ 1000 ] [ 1000 ], int
i int j int m int n int
tari int tarj int r [] [ 1000 ], int
hor int ver
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)

推荐答案

将此特定C ++示例转换为C#有两个主要问题:1。控制台输入/输出和2. C ++矩形数组。 以下是我们的转换器产生的(在2个辅助类的帮助下),但您可能需要进行几个手动调整
a:

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


这篇关于将c ++代码转换为csharp(c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆