0

我有一个关于 C# 的作业,要计算一些东西,保存在数据库(或文本文件)中并稍后查找。这是我的代码。我试过了,但它似乎不起作用。谁能帮我检查一下?太感谢了。

private void button1_Click(object sender, EventArgs e)
{
    int l0 = 210;
    int l1 = 440;
    int l2 = 330;
    int l3 = 435;
    DataTable workspace = new DataTable();
    workspace.Columns.Add("Xd", typeof(double));
    workspace.Columns.Add("Yd", typeof(double));
    workspace.Columns.Add("Zd", typeof(double));
    workspace.Columns.Add("Th1", typeof(double));
    workspace.Columns.Add("Th2", typeof(double));
    workspace.Columns.Add("Th3", typeof(double));
    workspace.Columns.Add("Th4", typeof(double));
    workspace.Columns.Add("Th5", typeof(double));
    workspace.Columns.Add("Th6", typeof(double));
    workspace.Columns.Add("Th7", typeof(double));

    int th1 = 0, th2 = 0, th3 = -90, th4 = -45, th5 = -130, th6 = -130, th7 = -45;
    while (th1 <= 180)
    {
        double t1 = th1 * Math.PI / 180;
        while (th2 <= 180)
        {
            double t2 = th1 * Math.PI / 180;
            while (th3 <= 90)
            {
                double t3 = th3 * Math.PI / 180;
                while (th4 <= 45)
                {
                    double t4 = th4 * Math.PI / 180;
                    while (th5 <= 130)
                    {
                        double t5 = th5 * Math.PI / 180;
                        while (th6 <= 130)
                        {
                            double t6 = th6 * Math.PI / 180;
                            while (th7 <= 45)
                            {
                                double t7 = th7 * Math.PI / 180;
                                double Xd = l3 * (Math.Cos(t6) * (Math.Sin(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) + Math.Cos(t1) * Math.Cos(t4) * Math.Sin(t2)) + Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) - Math.Cos(t1) * Math.Sin(t2) * Math.Sin(t4)) + Math.Sin(t5) * (Math.Cos(t3) * Math.Sin(t1) - Math.Cos(t1) * Math.Cos(t2) * Math.Sin(t3)))) + l0 * Math.Cos(t1) + l2 * (Math.Sin(t4) * (Math.Sin(t1) * Math.Sin(t3) + Math.Cos(t1) * Math.Cos(t2) * Math.Cos(t3)) + Math.Cos(t1) * Math.Cos(t4) * Math.Sin(t2)) + l1 * Math.Cos(t1) * Math.Sin(t2);
                                double Yd = l0 * Math.Sin(t1) - l3 * (Math.Cos(t6) * (Math.Sin(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) - Math.Cos(t4) * Math.Sin(t1) * Math.Sin(t2)) + Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) + Math.Sin(t1) * Math.Sin(t2) * Math.Sin(t4)) + Math.Sin(t5) * (Math.Cos(t1) * Math.Cos(t3) + Math.Cos(t2) * Math.Sin(t1) * Math.Sin(t3)))) - l2 * (Math.Sin(t4) * (Math.Cos(t1) * Math.Sin(t3) - Math.Cos(t2) * Math.Cos(t3) * Math.Sin(t1)) - Math.Cos(t4) * Math.Sin(t1) * Math.Sin(t2)) + l1 * Math.Sin(t1) * Math.Sin(t2);
                                double Zd = l3 * (Math.Sin(t6) * (Math.Cos(t5) * (Math.Cos(t2) * Math.Sin(t4) + Math.Cos(t3) * Math.Cos(t4) * Math.Sin(t2)) - Math.Sin(t2) * Math.Sin(t3) * Math.Sin(t5)) - Math.Cos(t6) * (Math.Cos(t2) * Math.Cos(t4) - Math.Cos(t3) * Math.Sin(t2) * Math.Sin(t4))) - l2 * (Math.Cos(t2) * Math.Cos(t4) - Math.Cos(t3) * Math.Sin(t2) * Math.Sin(t4)) - l1 * Math.Cos(t2);

                                textBoxX.Text = Xd.ToString();
                                textBoxY.Text = Yd.ToString();
                                textBoxZ.Text = Zd.ToString();
                                workspace.Rows.Add(Xd, Yd, Zd, t1, t2, t3, t4, t5, t6, t7);
                                th7 = th7 + 5;
                            }
                            th6 = th6 + 5;
                        }
                        th5 = th5 + 5;
                    }
                    th4 = th4 + 5;
                }
                th3 = th3 + 5;
            }
            th2 = th2 + 5;
        }
        th1 = th1 + 5;
    }

}

当我如下运行应用程序时,它有一个问题:XML 文档中存在错误 (12504158, 75)。我做错什么了吗?错误是什么意思?再次感谢你。

        //Reading saved data
        var dtDeserializer = new XmlSerializer(typeof(DataTable));
        var data = (DataTable)dtDeserializer.Deserialize(File.Open("out.xml", FileMode.Open));

        DataRow[] result = data.Select("Xd= '100' AND Yd = '1000' AND Zd = '0'");
        foreach (DataRow row in result)
        {
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7} {8}, {9}", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]);
        }

        Console.ReadLine();
4

1 回答 1

0

只需序列化/反序列化您的数据表,这里有一些示例代码:

        //Saving the DataTable
        workspace.TableName = "SomeName"; //A name is required in order to save the datable
        var dtSerializer = new XmlSerializer(typeof(DataTable));
        dtSerializer.Serialize(File.Create("out.xml"), workspace);

        //Reading saved data
        var dtDeserializer = new XmlSerializer(typeof(DataTable));
        var data = (DataTable)dtDeserializer.Deserialize(File.Open("out.xml", FileMode.Open));
于 2013-02-21T14:39:03.580 回答