2015-02-09 199 views
-2

我目前正在编写一个处理笛卡尔积的程序。我已经计算出输入两个数字并获得每个输入数字的第一部分。但我需要的是两个数字的最终产品。 换句话说,我正在寻找我的产品是这个样子:笛卡尔积

{ (1,1), (1,2), (1,3), ... } 

这里是我用在我需要帮助的最后一部分,现在的代码。

private void btnCal_Click(object sender, EventArgs e) 
{ 
    int iN, iM, i, j; 
    string strOut1, strOut2, strOut; 
    bool bN, bM; 

    bN = int.TryParse(txtN.Text, out iN); 
    bM = int.TryParse(txtM.Text, out iM); 

    if (bN && bM && iM > 0 && iM > 0) 
    { 
     strOut1 = "{1"; 

     for (i = 2; i <= iM; i++) 
      strOut1 += "," + i; 
     txtFirst.Text = strOut1 + "}"; 

     strOut2 = "{1"; 

     for (j = 2; j <= iN; j++) 
      strOut2 += "," + j; 
     txtSecond.Text = strOut2 + "}"; 
    } 

    //HERE IS WHERE THE PRODUCT CODE WILL BE AT 

    if (bN && bM && iM > 0 && iM > 0) 
    { 
     for (i = 2; i <= iM; i++) 
      for (j = 2; j <= iN; j++) 

     strOut = ("strOut1"); 

     txtProduct.Text = strOut + " }"; 
    } 
    else 
     txtProduct.Text = "Please enter valid number."; 
} 
+0

您所要求的VB代码,但有些是在C#中 - 不知道你在找什么。 – 2015-02-09 03:40:34

+0

你到底需要什么帮助?另外,我认为if(bN && bM && iM> 0 && iM> 0)这两行不符合你希望他们做的事情。 (提示:你不能分组检查这样的多个值。) – 2015-02-09 03:55:31

+0

我正在寻找当我在我的文本框1和文本框2中放置一个数字,并且一旦我点击了我的按钮,它将产生{(1,1 ),(1,2),(1,3)....} – dmbfan42 2015-02-09 04:16:38

回答

1

使用LINQ,任何事情都是可能的:

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnCal_Click(object sender, EventArgs e) 
     { 
      var tuples = from m in Enumerable.Range(1, int.Parse(txtM.Text)) 
         from n in Enumerable.Range(1, int.Parse(txtN.Text)) 
         select Tuple.Create(m, n); 
      txtProduct.Text = "{" + String.Join(",", tuples) + "}"; 
     } 
    } 
} 
+0

谢谢,但即时通讯使用visual studio,它必须在C# – dmbfan42 2015-02-09 04:38:01

+0

这是C#。 LINQ被添加到C#年前。 https://msdn.microsoft.com/en-us/library/bb397926.aspx – 2015-02-09 04:45:54

+0

抱歉新编程游戏!谢谢 – dmbfan42 2015-02-09 04:50:12