Jump to content

Hi guys,

So I was programming a programm just for beginners, where I must make a Class called Complex, so I cann work with the the type complex will be better shown in the programm code. I have 1 thing where I need help. First off i get "Complex\obj\Debug\Complex.exe' does not contain a static 'Main' method suitable for an entry point if i try to solve that i will not get the numbers what i wanted to display so i get basicly the name Class.Class but no numbers here is the code hope you guys can help me out:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Complex{    class Program    {        public void Main()        {            string op;            double x=0,y=0,z=0,o=0;            Console.Write("Geben sie den Operator ein mit dem Sie arbeiten möchten(+, -, /, *): ");            op = Console.ReadLine();            switch (op)            {                case "+":   Console.Write("\nGeben sie die erste Zahl ein:");                            x = double.Parse(Console.ReadLine());                            Console.WriteLine(" + ");                            y = double.Parse(Console.ReadLine());                            Console.Write("\nGeben sie die zweite Zahl ein:");                            z = double.Parse(Console.ReadLine());                            Console.WriteLine(" + ");                            o = double.Parse(Console.ReadLine());                            break;                case "-":   Console.Write("\nGeben sie die erste Zahl ein:");                            x = double.Parse(Console.ReadLine());                            Console.WriteLine(" - ");                            y = double.Parse(Console.ReadLine());                            Console.Write("\nGeben sie die zweite Zahl ein:");                            z = double.Parse(Console.ReadLine());                            Console.WriteLine(" - ");                            o = double.Parse(Console.ReadLine());                            break;                case "/":   Console.Write("\nGeben sie die erste Zahl ein:");                            x = double.Parse(Console.ReadLine());                            Console.WriteLine(" / ");                            y = double.Parse(Console.ReadLine());                            Console.Write("\nGeben sie die zweite Zahl ein:");                            z = double.Parse(Console.ReadLine());                            Console.WriteLine(" / ");                            o = double.Parse(Console.ReadLine());                            break;                case "*":   Console.Write("\nGeben sie die erste Zahl ein:");                            x = double.Parse(Console.ReadLine());                            Console.WriteLine(" * ");                            y = double.Parse(Console.ReadLine());                            Console.Write("\nGeben sie die zweite Zahl ein:");                            z = double.Parse(Console.ReadLine());                            Console.WriteLine(" * ");                            o = double.Parse(Console.ReadLine());                            break;                default: Console.WriteLine("Geben Sie einen der oben stehenden operanten ein!");                            break;            }            var c1 = new Complex(x, y);            var c2 = new Complex(z, o);            if(op == "+")            {                var c3 = c1 + c2;                Console.WriteLine("{0} + {1} = {2}", c1, c2, c3);            }            else if(op == "-")            {                var c3 = c1 - c2;                Console.WriteLine("{0} - {1} = {2}", c1, c2, c3);            }            else if(op == "/")            {                var c3 = c1 / c2;                Console.WriteLine("{0} / {1} = {2}", c1, c2, c3);            }            else if(op == "*")            {                var c3 = c1 * c2;                Console.WriteLine("{0} * {1} = {2}", c1, c2, c3);            }            Console.ReadKey();        }    }    class Complex    {        public double Re = 0, Im = 0, W, R;        public Complex()        {        }        public Complex(double Re, double Im)        {            this.Re = Re;            this.Im = Im;        }        public void KarthToPolar (double Re, double Im)        {            this.W = Math.Atan2(Im,Re);            this.R = Math.Sqrt((Re*Re)+(Im*Im));        }        public void PolarToKarth (double Re,double ImW)        {            this.Re = R * Math.Cos(W);            this.Im = R * Math.Sin(W);        }        public static Complex operator + (Complex lhs, Complex rhs)        {            var add = new Complex();            add.Re = lhs.Re + rhs.Re;            add.Im = lhs.Im + lhs.Im;            return add;        }        public static Complex operator - (Complex lhs, Complex rhs)        {            var subb = new Complex();            subb.Re = lhs.Re - rhs.Re;            subb.Im = lhs.Im - rhs.Re;            return subb;        }        public static Complex operator / (Complex lhs, Complex rhs)        {            var div = new Complex();            div.R = lhs.R / rhs.R;            div.W = lhs.W - lhs.W;            return div;        }        public static Complex operator * (Complex lhs, Complex rhs)        {            var mul = new Complex();            mul.R = lhs.R * rhs.R;            mul.W = lhs.W + rhs.W;            return mul;        }        }}

 

 

 

Just as Remember because I forgot it to mension, Im sorry if the text is not English, because it is german if someone needs it to be changed just let me know and I do it.

 

Hope you can help. Nice greets Rocki

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/
Share on other sites

Link to post
Share on other sites

 

Your main method needs to be static

static void Main(){    ...

Thx for the help, but now I got this Problem:

http://imgur.com/hbKf4TY

What should I do im stuck D:

 

So basicly it is giving me the ClassName.ClassName out instead of the numbers after the calculation so what should i do?

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429024
Share on other sites

Link to post
Share on other sites

Thx for the help, but now I got this Problem:

http://imgur.com/hbKf4TY

What should I do im stuck D:

 

So basicly it is giving me the ClassName.ClassName out instead of the numbers after the calculation so what should i do?

 

You need to override the ToString() method for your Complex class.

public override string ToString(){    return ""; // Return how you want things displayed when Complex is used as a string}
Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429051
Share on other sites

Link to post
Share on other sites

 

You need to override the ToString() method for your Complex class.

public override string ToString(){    return ""; // Return how you want things displayed when Complex is used as a string}

If you don't mind could you explane that abit better I don't understand that really well and would like to understand it

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429082
Share on other sites

Link to post
Share on other sites

If you don't mind could you explane that abit better I don't understand that really well and would like to understand it

 

ToString Documentation. Basically, if you want to use an object as a string, it defaults to using the class name because it has no way of knowing what you want it to display. You tell it what you want by overriding the ToString method.

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429105
Share on other sites

Link to post
Share on other sites

ToString Documentation. Basically, if you want to use an object as a string, it defaults to using the class name because it has no way of knowing what you want it to display. You tell it what you want by overriding the ToString method.

So to return the value c3 I can make an object and reutrn that? But how can I seperate in the Object the 2 values i need for c3? Sry for the Questions Im trying to learn and I am annoying I know

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429121
Share on other sites

Link to post
Share on other sites

Looked into it again and tried this one but thats wrong so how can i do it better D:

        public static Complex operator * (Complex lhs, Complex rhs)        {            var mul = new Complex();            mul.R = lhs.R * rhs.R;            mul.W = lhs.W + rhs.W;            return mul;        }        public override string ToString()        {            return mul.R.ToString() + "*" + mul.W.ToString; // Return how you want things displayed when Complex is used as a string        }
Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429148
Share on other sites

Link to post
Share on other sites

 

I think what you want is this

public override string ToString(){    return string.Format("({0} + {1}i)", Re, Im); // I added brackets but you can take them out}

Then for example your output becomes

(3 + 6i) * (2 + 3i) = (0 + 0i)

Thank you sooo much, One question if you don't mind just for understanding and further stuff I will do why do you use string.Fromat? why Format and what does it stand for

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429209
Share on other sites

Link to post
Share on other sites

Thank you sooo much, One question if you don't mind just for understanding and further stuff I will do why do you use string.Fromat? why Format and what does it stand for

 

String Format documentation. Basically it's a way to avoid having to concatenate strings together like so

return "(" + Re + " + " + Im + "i)";

It's basically what you're doing inside Console.WriteLine but since we aren't using it in the ToString method, we can use string.Format to achieve the same thing.

 

Reasons to use string.Format

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429236
Share on other sites

Link to post
Share on other sites

String Format documentation. Basically it's a way to avoid having to concatenate strings together like so

return "(" + Re + " + " + Im + "i)";

It's basically what you're doing inside Console.WriteLine but since we aren't using it in the ToString method, we can use string.Format to achieve the same thing.

 

Reasons to use string.Format

Thx man really helpfull from you. BTW last questionr eally is there a way of Console.ReadLine() that doesnt jump into the next line but next to the Text from before?

 

 

And you get a thumps up from me for all the help

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429263
Share on other sites

Link to post
Share on other sites

Thx man really helpfull from you. BTW last questionr eally is there a way of Console.ReadLine() that doesnt jump into the next line but next to the Text from before?

 

 

And you get a thumps up from me for all the help

You can get everything on one line and parse it, for example with string.Split().

string op;double x = 0, y = 0, z = 0, o = 0;// Change this to ask for entire instruction instead of operatorConsole.Write("Geben sie den Operator ein mit dem Sie arbeiten möchten(+, -, /, *): ");// Now this will contain a full line, for example: 2 + 4op = Console.ReadLine();// Now split the linestring[] parts = op.Split(); // splits on space character by default            //parts[0] = 2//parts[1] = +//parts[2] = 4

Now this is incorrect/incomplete for your needs but it should get you started and show you how string.split works. Your inputs would be more like 2 + 4 + 2 + 5 or maybe (2 + 4) + (2 + 5) or maybe (2 + 4i) + (2 + 5i) which are a little more complicated to work with but you can do it.

 

Alternatively, just do this inside your switch cases. So ask for operator like you are doing. Then handle things how you need to in the switch case.

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429353
Share on other sites

Link to post
Share on other sites

Also, I just wanted to mention that your input will need to be formatted correctly for string.split to work. If you split on a space character then (2 + 4) + (2 + 5) is much different than (2 +4) +(2+ 5)

string example = "(2 + 4) + (2 + 5)";string[] parts = example.Split();//parts[0] = (2//parts[1] = +//parts[2] = 4)//parts[3] = +//parts[4] = (2//parts[5] = +//parts[6] = 5)string example = "(2 +4) +(2+ 5)";string[] parts = example.Split();//parts[0] = (2//parts[1] = +4)//parts[2] = +(2+//parts[3] = 5)

You can make it so that formatting doesn't matter but that would require something like regular expressions which I expect is above your level right now.

Link to comment
https://linustechtips.com/topic/250072-need-help-with-c/#findComment-3429420
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×