将int和char分开并进行整数

时间:2018-08-24 05:16:01

标签: c#

大家好,我编写了一个简单的程序,将参数中的字符串分成数字,字母和运算符,但是我遇到了 23x + 3 = 8

foreach (char x in args[i]){

    if (char.IsNumber(x)){
        inter[i] = Convert.ToInt32(x);
            Console.WriteLine("{0} is a number ", x);
    }
    else if (char.IsLetter(x)){
        apha[i] = x;
        Console.WriteLine("{0} is a Letter ", x);
    }
    else if (char.IsSymbol(x)){
        symbol[i] = x;
        Console.WriteLine("{0} is a Symbol ", x);
    }

我发现输出要分为每个char 2和3,我希望将其作为整数23。有没有办法将两个数字推在一起?

3 个答案:

答案 0 :(得分:1)

要执行所需的操作,必须重写算法。我可能会像您一样枚举字符,并将它们累积在StringBuilder中,直到行尾或类型更改为止,例如数字更改为变量或符号。一旦发生这种情况,请对累积的数据进行处理,然后重新开始累积。

答案 1 :(得分:1)

您可以尝试下面的代码,它使用正则表达式,这使任务非常容易:

  string equation = "25x+20=120";
  if (string.IsNullOrEmpty(equation))
    throw new ArgumentException("No equation given!");
  //match all numbers
  var numbers = Regex.Matches(equation, @"\d+");
  //match all symbols
  var letters = Regex.Matches(equation, @"[a-zA-Z]");
  //take out all digits and letters, so only symbosl are left
  var symbols = Regex.Replace(equation, @"[0-9a-zA-Z]", "");
  //alternative:
  //var symbols = Regex.Matches(equation, @"[^0-9a-zA-Z]");
  foreach(Match number in numbers)
  {
    Console.WriteLine("{0} is a Number ", number.ToString());
  }
  foreach (Match letter in letters)
  {
    Console.WriteLine("{0} is a Letter ", letter.ToString());
  }
  //alternative
  //foreach (Match symbol in symbols)
  foreach (char symbol in symbols)
  {
    Console.WriteLine("{0} is a Symbol ", symbol.ToString());
  }

它产生以下输出:

25 is a Number 
20 is a Number 
120 is a Number 
x is a Letter 
+ is a Symbol 
= is a Symbol 

要按顺序列出所有部件,请尝试以下代码:

  string equation = "25x+20=120";
  if (string.IsNullOrEmpty(equation))
    throw new ArgumentException("No equation given!");
  //match all numbers
  var numbers = Regex.Matches(equation, @"\d+");
  //match all symbols
  var letters = Regex.Matches(equation, @"[a-zA-Z]");
  //take out all digits and letters, so only symbosl are left
  var symbols = Regex.Matches(equation, @"[^0-9a-zA-Z]");

  List<Tuple<int, string, string>> parts = new List<Tuple<int, string, string>>();

  foreach (Match number in numbers)
    parts.Add(new Tuple<int, string, string>(number.Index, number.ToString(), "Number"));
  foreach (Match letter in letters)
    parts.Add(new Tuple<int, string, string>(letter.Index, letter.ToString(), "Letter"));
  foreach (Match symbol in symbols)
    parts.Add(new Tuple<int, string, string>(symbol.Index, symbol.ToString(), "Symbol"));

  foreach(var part in parts.OrderBy(t => t.Item1))
    Console.WriteLine("{0} is a {1}", part.Item2, part.Item3);

答案 2 :(得分:1)

我会尝试以下

string equation = "25x+20=120";

// Guard against an empty input
if (String.IsNullOrWhiteSpace(equation))
    throw new ArgumentException("No equation given!");

// Regex split
// Split is being applied on the mathematical operations
var result = Regex.Split(equation, @"([*+-/=])");
foreach (var item in result)
{
    Console.WriteLine(item);
    // TODO : Further operations
}

这将生成输出

25x
+
20
=
120

该过程使用正则表达式。 ([*+-/=])将根据指定的数学运算进行拆分,()将确保将运算包括在结果拆分中,从而使您可以重建运算树。

请参阅System.Text.RegularExpressions上的文档,您可以在Regular Expression Language Reference上找到更深入的参考