I'm trying to create a program which can multiplicate complex numbers.
For example.

(1+2j)*(3-4j)= 11+2j

What would be the best way to split up the equation (1+2j)*(3-4j), so i can persom the calculations? I tried using the split method but I just got stuck, since i couldn't make it see wheither the number was positive or negative.

Any suggestions?

Well this really depends - will it always accept input in EXACTLY this form: (constant+/-constant*j)*(constant+/-constant*j) ?

If this is the case, you won't need to get too fancy. If you want it to parse ANY equation that is a different story.

It will only accept input in that form, yes.

The real part can also be +/- , so the input form is (+/-constant+/-constant*j)*(+/-constant+/-constant*j).

Well, the simplest way I can think of:

-split the string in half (delete the brackets since they will always be there) so that there are 2 strings, one with the left bracket contents, one with the right. you might also choose to get rid of the j if it will always be in the same spot
-split each string based on +/- symbols, if theres a count of 1 assume no negative sign on the first constant and only use this symbol to represent the second constant. If there are 2, use the first index to set the symbol of the first constant, use the second for the second constant.
-you now have the 2 strings split and the values of the 4 constants!

If things get any more complicated than this, it might be a good idea to check out regular expressions (although these come with a bit of a learning curve)

i prefer regular expresssions(Regex) to split the equation. It's the easier way,and easy to learn too.

If i use Regex.Split(string, @"\D+") it still makes all the numbers positive, how can I get the regex to find negative numbers?

You don't split the equation, taken it's value from equation.
use this

ResultString = Regex.Match(string, "(\\D?\\d+[a-z]?)").Groups[1].Value;

Send you feedback, so that i can give you any other approach

That only found the first number in the equation, how do I make it find the second one, etc?

That only found the first number in the equation, how do I make it find the second one, etc?

Splitting the string (directly or by regex) is a little tricky, and the code quickly gets tedious and ugly.

Below is an attempt at a regular expression that gets the sign right. I'm giving this to you on the condition that you only use it if you understand it, i.e., you should know what everything in that regex is and what it does.

using System;
using System.Text.RegularExpressions;

class Program
    public static void Main(string[] args)
        RegexTest("(1+2j)*(3-4j)= 11+2j");
    static readonly Regex regex = new Regex(
        RegexOptions.ExplicitCapture |
    static void RegexTest(string equation)
        Match m = regex.Match(equation);
            double ar = Double.Parse(m.Groups["a_real"].Value);
            double ai = Double.Parse(m.Groups["a_imag"].Value);
            double br = Double.Parse(m.Groups["b_real"].Value);
            double bi = Double.Parse(m.Groups["b_imag"].Value);
            Console.WriteLine("a.real = {0}", ar);
            Console.WriteLine("a.imag = {0}", ai);
            Console.WriteLine("b.real = {0}", br);
            Console.WriteLine("b.imag = {0}", bi);
            Console.WriteLine("Couldn't match '{0}'", equation);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.