Jump to content

How would I do this? C#

werto165
            //Ask user for Complex Number + reads it             Console.WriteLine("Please Enter your Complex Number");            string imaginaryNo = Console.ReadLine();            //Converts number into char array            char[] imaginaryNoArray = imaginaryNo.ToArray();            //Goes through each char in array            for (int i = 0; i < imaginaryNo.Length; i++)            {                //finds the index of the imaginary part of the number                if (imaginaryNoArray[i] == 'i')                {                    //Only works for single digit imaginary numbers                     char Imaginary = imaginaryNoArray[(i - 1)];                    Console.WriteLine("The Imaginary Part of your number is {0}" ,Imaginary);                 }            }#region                 //Ignore this            //var c1 = new Complex(1,2);             //Console.WriteLine(c1.ToString()); #endregion        

I'm trying get an imaginary part of a number this example works only for single digit Imaginary numbers:

e.g. 

10 + 5i ---> result being = The Imaginary Part of your number is 5 

 

10+ 63i ---> result being = The Imaginary Part of your number is 3 (should be 63) 

 

How would I implement it to work for any number of digits? 

 

Also I can't seem to get Imaginary to work outside the if loop(just says doesn't exist within the current context.

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well since it will always end in "i" you can start at the i and then loop backwards through the input until you hit a non-number character.

Link to comment
Share on other sites

Link to post
Share on other sites

You're almost there. Instead of hard-coding the i - 1 to get the index of the number, call a function that walks backwards from the index of "i", gathering all the preceding integers into a string to print.

Link to comment
Share on other sites

Link to post
Share on other sites

~snip~

If you can wait till 5pm GMT+1 (is in 10hours) I can give you a code that will solve your propblem with comments if xou want

Link to comment
Share on other sites

Link to post
Share on other sites

You're almost there. Instead of hard-coding the i - 1 to get the index of the number, call a function that walks backwards from the index of "i", gathering all the preceding integers into a string to print.

Yeah, I finally did it, not exactly the most succinct code ever but, it's better than not working at all.

//empty string to hold the data             string imagString = "";            int imaginaryNoIndex = 1;             //Ask user for Complex Number + reads it             Console.WriteLine("Please Enter your Complex Number");            string imaginaryNo = Console.ReadLine();            //Converts number into char array            char[] imaginaryNoArray = imaginaryNo.ToArray();            //Goes through each char in array backwards            for (int i = imaginaryNo.Length- 1; i > 0; i--)            {                //finds the index of the imaginary part of the number                if (imaginaryNoArray[i] == 'i')                {                    imaginaryNoIndex = i-1;                     Console.WriteLine(imaginaryNoIndex);                }                //Goes through the array backwards until it no longer finds a # value.                while (char.IsNumber(imaginaryNoArray[imaginaryNoIndex]))                {                    imagString = imagString + imaginaryNoArray[imaginaryNoIndex];                    imaginaryNoIndex--;                    break;                }                            }                        char[] imagStringArray = imagString.ToCharArray();            //Reverse the char array.             Array.Reverse(imagStringArray);            //Make a new string from the inversed char array.             string stringReverse = new string(imagStringArray);             Console.WriteLine("Imaginary Number is {0}i", stringReverse);            Console.ReadLine();            } 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
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

×