【发布时间】: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 = 2x 或3y-y = 2y,所以我认为如果我将Crammer 的方程转换为字符串,我可以找到所有匹配项,如x + x 或y + 2y 或@987654326 @ 并从一个可以解决我最初的问题的解决方案开始,比如如果我找到一个 x * x 模式,我将通过 if 语句或其他东西告诉 pc 模式 x * x 是 x^2。
话虽如此,我想找出字符串中存在的特定序列的数量,例如X * y 或y + 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