【问题标题】:How should i search through a string for a sequence of characters such as "x*y"?我应该如何在字符串中搜索诸如“x * y”之类的字符序列?
【发布时间】:2017-08-07 21:18:43
【问题描述】:

我正在尝试使用数学克莱默定理制作一个行列式计算器,如您所见,我将该定理转换为代码convertedString = Convert.ToString (x * y1 * 1 + x1 * y2 * 1 + x2 * x * y - (1 * y1 * x2 + 1 * y2 * y + 1 * y * x1));一切都很好,直到我需要计算 2 个未知数,我不知道如何在代码中“告诉”计算机x + x = 2x3y-y = 2y,所以我认为如果我将Crammer 的方程转换为字符串,我可以找到所有匹配项,如x + xy + 2y 或@987654326 @ 并从一个可以解决我最初的问题的解决方案开始,比如如果我找到一个 x * x 模式,我将通过 if 语句或其他东西告诉 pc 模式 x * xx^2。 话虽如此,我想找出字符串中存在的特定序列的数量,例如X * yy + x,我确实尝试了一些foreach 循环和for 循环,但我无法得到它工作,我不知道接下来应该如何解决这个问题,请帮忙。

这是我的代码:

using System;
using InputMath;

namespace MathWizard
{
     class Determinants
    {
        //Determinant of a first point and a second graphical point on the xoy axis.
         public static void BasicDeterminant()
        {
            float x;
            float y;
            float x1 = Input.x1;
            float y1 = Input.y1;
            float x2 = Input.x2;
            float y2 = Input.y2;
            float result;
            string convertedString;
            string pointsValue;
            string[] point;

            Console.WriteLine("Please introduce the 2 graphical points ( A and B) \n in the order x1 y1 x2 y2, separated by a space ");

            pointsValue = Console.ReadLine();
            point = pointsValue.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


            x1 = Convert.ToInt32(point[0]);
            y1 = Convert.ToInt32(point[1]);
            x2 = Convert.ToInt32(point[2]);
            y2 = Convert.ToInt32(point[3]);


            //The Cramer's Rule for solving a 2 points determinant ( P1(x1,y1) and P2(x2,y2)
            convertedString = Convert.ToString (x * y1 * 1 + x1 * y2 * 1 + x2 * x * y - (1 * y1 * x2 + 1 * y2 * y + 1 * y * x1));

        }
    }
}

【问题讨论】:

  • 您将x1, x2, y1, y2 声明为floats,然后使用Convert.ToInt16 从输入中获取它们。哪个是正确的类型?
  • 这段代码不包含任何关于查找字符串的内容。你想完成什么?
  • 您是否复制/粘贴了别人的代码然后尝试修改它?这没有道理。 'x' 和 'y' 总是 '1',而且你也有一些硬编码的 '1',它们都用于乘法..??

标签: c# string for-loop foreach


【解决方案1】:

您还可以使用以下regex 来查找每个运算符不区分大小写的 x*y:

string pattern = /(x(\*|\+|\-|\/|\^)y)/gi;

然后你可以做一些string.Contains(pattern); 来检查。让我知道这是否有帮助。

编辑:更新模式

更新了模式,使其也允许使用诸如y9X10 之类的变量。任意单个字符(x 或 y)后跟任意数量的数字。

string pattern =/(x\d*(\*|\+|\-|\/|\^)y\d*)/gi; // could match X1*y9

这不考虑空格,因此您可以使用一些.replace() 来消除空格或使用.split(/\s/) 并在使用此模式之前获取一个没有空格的字符串数组。

【讨论】:

  • 您能否在我的 cod 中展示您的解决方案的实现,我不明白我应该如何实现它...谢谢!
  • 好吧,我仍然感到困惑。我不确定为什么您的变量以floats 开头,然后转换为int,然后您将其全部转换为toString()。例如,当我第一次阅读此内容时,它只要求查找任何序列x*Y。现在您希望您的代码添加未知数吗? @Noobie
【解决方案2】:
 bool found = false;
 int xyCount = 0;

 if(convertedString.Contains("X*Y")){
     found = true;
      xyCount++;
      //makes a substring without the first case of the "X*Y"
      string s = convertedString.SubString(convertedString.IndexOf("X*Y"))
       if(s.Contains("X*Y")){
         xyCount++;
     }

这可能不是最好的方法,但你可能可以用更好的方法来做这样的事情

【讨论】:

  • 我可以使用这个解决方案从字符串中找到每个模式吗?即使是一样的?谢谢你的回答 :)。
  • 这只会找到一次,要多次找到它,您将不得不使用 .indexof。我会编辑我的答案
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多