-4

I made a n-variable linear equation solver for a semester homework. It works fine (I already took some help from the site) only one feature is left which is loading the variables from the '.dat' file and copying them in the dynamic textboxes in the right order.

The jagged array is holding the variables for example, if the I have '2x+y=5', '3x-y=4' in my array, it should be changed to {2,1,5} and {3,-1,4}, the "ismeretlenek" represents the number of the variables.

How can I fill these textboxes from the '.dat' file?

ismeretlen = (int)numericUpDown1.Value;

TB = new TextBox[ismeretlen][];

for(int i = 0; i < ismeretlen; i++)
    TB[i] = new TextBox[ismeretlen + 1];


int height = 20;
int width = 40;
int curX = 10;
int curY = 10;

for(int i = 0; i < ismeretlen; i++)
{
    for(int j = 0; j < ismeretlen + 1; j++)
    {
        TextBox txtbox = new TextBox();
        txtbox = new TextBox();
        txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        txtbox.Left = curX;
        txtbox.Top = curY;
        txtbox.Width = width;
        txtbox.Height = height;
        txtbox.Font = new Font(txtbox.Font.FontFamily, 14);
        txtbox.BackColor = Color.Azure;
        txtbox.TextAlign = HorizontalAlignment.Center;
        txtbox.Text = "0";

        TB[i][j] = txtbox;
        this.panel1.Controls.Add(TB[i][j]);

        curX += width + 15;
    }

    curX = 10;
    curY = curY + height + 20;
    }

}

private void Save_Click(object sender, EventArgs e)
{
    for (int a = 0; a < ismeretlen; a++)
        segéd[a] = new double[ismeretlen + 1];

    for (int a = 0; a < ismeretlen; a++)
    {
        for (int b = 0; b < ismeretlen + 1; b++)
        {
            segéd[a][b] = double.Parse(TB[a][b].Text);

        }
    }

    string file = ismeretlen+"ismeretlen.dat";
    FileStream fs = new FileStream(file, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    for (int r = 0; r < ismeretlen; r++)
        for (int t = 0; t < ismeretlen + 1; t++)
           bw.Write(segéd[r][t]);

    bw.Close();
    fs.Close();

}

private void Load_Click(object sender, EventArgs e)
{

}

}

Subz6
  • 7
  • 1
  • 1
    Start by trying to solve the problem yourself, and seeing what problems you have. From there, after doing research for existing information on how to solve those problems, you need to accurately describe both what you're doing (without including lots of information irrelevant to the problem at hand) and what specific problem you're having with that solution. – Servy May 21 '15 at 20:23
  • Look, if the file has text, shove a Memo onto the form and load it up from the '.dat' file. When that's working, you have visible lines of equations you can easily index, parse out and load into your textboxes. Do you have to use dynamically-built boxes? I would have gone with a load of invisible design-time boxes and made them visible as required as data is added. – Martin James May 21 '15 at 20:48
  • Ohwait - does C# even have a Memo component? Mebbe I'm mixing up my languages.. – Martin James May 21 '15 at 20:51
  • Meh - only 'RichTextBox':( It has a Lines' property, but no 'LoadFromFile' method:( – Martin James May 21 '15 at 21:04
  • So i can load the file but I dont know how can I cut in pieces and write its components to an jagged array with x rows and x+1 columns. After that it is just a simple parse like i did here: "segéd[a][b] = double.Parse(TB[a][b].Text" just its inverse?! – Subz6 May 21 '15 at 21:45

1 Answers1

-1
        /* example:
         * 2x+y=5
         * 3x-y=4
         */

        var outofFile = new List<string> { "2x+y=5", "3x-y=4" };

        var terms = outofFile.Select(x => x.Split(new[] { '+', '-', '=' }));

        // gets all the numbers to a List<List<double>
        var numbers = terms.Select(equalation => equalation.Aggregate(new List<double>(), (List<double> listPerLine, string term) => // term is something like '2x' or '4'
        {
            double number;
            if (!double.TryParse(term, out number)) // if no variable inside just a number you can convert it and return it
            {
                // cut the variable away eg. 2x -> 2
                var numberstring = term.Substring(0, term.Length - 1);
                if (numberstring.Length == 0)
                    number = 1; // eg. if there is only y -> '' -> 1
                else
                    number = double.Parse(numberstring); // eg 2x -> 2 
            }
            listPerLine.Add(number);
            return listPerLine;
        })).ToList();

        /* now you can access the values like this
         *  (indizes zero based)
         * numbers[equalation indizes][inner coeffizient indizes]
         * 
         * check out the values
         * 
         * that way you can assign them to the textboxes.
         */

        var _0_0 = numbers[0][0];
        var _0_1 = numbers[0][1];
        var _0_2 = numbers[0][2];
        var _1_0 = numbers[1][0];
        var _1_1 = numbers[1][1];
        var _1_2 = numbers[1][2];

This shall give you an idea of how to do this. Simply debug it and understand it :) then you can continue assigning it to the textboxes..

Sure this may be improved, but anyway im going to sleep now

LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • Thank you! In the .dat string you can already only find numbers. With your example there is 2,1,5,3,-1,4 in the dat file. I think I'll need the lenght of the .dat file square root it, round it and adding +1 to it will show how many numbers should the first array contain. Thank you for your help.. – Subz6 May 21 '15 at 21:55
  • You just need to load the dat file to a string list and then executing it.. numbers.count() will count the equalations and numbers[i].count() will show how many numbers a equalation contains.. – LuckyLikey May 21 '15 at 22:14