将代码从C ++转换为C# [英] convert code from c++ to c#

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

问题描述

请有人帮助我将此代码从c ++转换为c#

Please someone help me to convert this code from c++ to c#

#include <stdafx.h>

推荐答案

C#等效项是(通过我们的转换器添加了'ConsoleInput'帮助器类):

using System;

static int Main()
{
	int number, a, b, c;

	Console.Write("write a three-digit number:");
	Console.Write("\n");

	chislo = ConsoleInput.ReadToWhiteSpace(true);

	a = chislo / 100;

	b = (chislo / 10) % 10;

	c = chislo % 10;

	Console.Write("the number in reverse is :");
	Console.Write(c);
	Console.Write(b);
	Console.Write(a);
	Console.Write("\n");

	Console.ReadKey();

	return 0;
}

internal static class ConsoleInput
{
	private static bool goodLastRead = false;
	internal static bool LastReadWasGood
	{
		get
		{
			return goodLastRead;
		}
	}

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

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


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

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