Question

For the following program I'm suppose to create a class WComplex that perform operations on complex numbers. I did that and it is working just fine. In addition, I have to Create a Windows Forms Application of a simple calculator with 3 text boxes (value1,value2,result), 4 operation buttons (+,-,*,/). This Uses the WComplex class (by adding it as a new existing item).

The problem that I'm having with the code below, for instance I'm trying to pass the number in the first text box (value 1) to num1 (so i carry out operations) as the following: WComplex num1 = new WComplex(textBox1.Text); and I'm getting an error that WComplex takes two arguments (real,imaginary). But how can I read the value of form (a+bi) from the text box (value 1) and split it into 2 arguments or values? Also, what piece of code do I have to add to each specific button click that makes it carry the specified operation ? Ask for any clarification on the comments below.

**[update] - So I have figured out how to pass the two values using split and separator, see the button1_click (addition) below, but I'm still getting a debug error

Using WComplexClass;

namespace WindowsFormsApplication1
{
    public partial class Calculator : Form
    {

    public Calculator()
    {
        InitializeComponent();

    }

    // ---------------------------------------------------------------------------
   // ---------------------------------------------------------------------------

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //text box (value 1)
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        //text box (value 2)
    } 

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        //text box (result)

    }

    private double button1_Click(object sender, EventArgs e)
    {
        //addition

         string t = textBox1.Text;
        string[] split = t.Split('+');

        double num1 = Double.Parse(split[0]);
        double num2 = Double.Parse(split[1]);

        string t2 = textBox1.Text;
        string[] split2 = t2.Split('+');

        double num3 = Double.Parse(split2[0]);
        double num4 = Double.Parse(split2[1]);

        WComplex num5 = new WComplex(num1,num2);
        WComplex num6 = new WComplex(num3,num4);

        WComplex sum = num5 + num6;
    } 

    private void button2_Click(object sender, EventArgs e)
    {
        //subtraction


    }

    private void button3_Click(object sender, EventArgs e)
    {
        //multiplication


    } 

    private void button4_Click(object sender, EventArgs e)
    {
        //division

    }

    private void Calculator_Load(object sender, EventArgs e)
    {

    }

}
}
Was it helpful?

Solution

You pretty much answered your own question. If the user enters something like 'a+bi' in the TextBox then you can use String.Split to split it on the "+". You should also be validating and parsing the data so that you can pass int values to your constructor and not string values, which could be any text at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top