C # getting started with classic Chapter4 process control,

Source: Internet
Author: User
Tags bitwise operators

C # getting started with classic Chapter4 process control,

4.1 boolean logic

Boolean comparison operator

=! = <> <=> =

Boolean operators for processing boolean values

! & | ^ (Exclusive or)

Conditional boolean operator

& | Better performance than & and | for example, you only need to judge that the previous Boolean value is false, and the overall value is false. You do not need to calculate the following Boolean value.

1. boolean value assignment operator

& =| = ^ =

2. bitwise operators

& | ^ ~

Displacement operator >><

Shift assignment operator >=<=

3. Operator priority (updated)

++, -- (Used as the prefix); (), +,-(one dollar ),!,~

*,/, %

+ ,-

<,>

<,>, <=,> =

= ,! =

&

^

|

&&

|

=, * =,/=, % =, + =,-=, >>=, <<=, & =, ^ =, | = Value assignment operator

++, -- (Used as a suffix)

4.2 goto statement

Goto <labelName>

4.3 Branch

1. Ternary Operators

? :

2. if statement

If () {} else {}

3. switch statement

Switch ()

{

Case val1 :......; Break;

Case val2 :......; Break;

......

Default: If no matching val value exists and default exists, run the code in default.

}

Declare constants: Specify the variable type and keyword const, and assign values to them at the same time.

4. Loop: repeated statement execution

1. do Loop

Do

{Execute the command to determine the value in the while (). If the value is true, execute the command again. If the value is false, exit the loop.

} While ();

2. while Loop

While () {} first judge the value in while (), true to start execution

3. for Loop

For (int I = 0; I <4, I ++ ){}

4. Cyclic interruption

Break: Terminate the cycle immediately

Continue: Terminate the current loop immediately and enter the next loop.

Goto: jump out of the loop to the specified tag location

Return: jump out of the loop and the function that contains the loop

5. Infinite Loop

While (true) {} exited using break and so on

Examples of the Mandelbrot set (C # is used for the sample code given in the book #)

Class Program {// each position in the Mandelbrot image corresponds to a plural value in formula N = x + y * I. In fact, the number part is x, the imaginary part is y, and I is the square root of-1. The x and y coordinates of each position in the image correspond to the x and y coordinates of the imaginary number. // each position in the image is represented by the parameter N, it is the square root of x * x + y * y. If the value is greater than or equal to 2, the position value corresponding to this number is 0. If the value of parameter N is less than 2, change the value of N to N * N-N (N = (x * x-y * y-x) + (2 * x * y-y) * I) and test the new N value again. If the value is greater than or equal to 2, the position value corresponding to this number is 1. This process continues until we assign a value to the position in the image, or the number of iterations exceeds the specified number ." Static void Main (string [] args) {// the real number of N and the virtual number of double realCoord, imagCoord; // stores the Temporary Information in the calculation process double realTemp, imagTemp, realTemp2, arg; // record the number of iterations int iterations before the N (arg) parameter is equal to or greater than 2; // select an appropriate boundary value to display the main part of the manelbrot image, to enlarge the image, you can enlarge these boundary values. For (imagCoord = 1.2; imagCoord> =-1.2; imagCoord-= 0.05) {// two for loops process a vertex in the image and specify a value for N. For (realCoord =-0.6; realCoord <= 1.77; realCoord ++ = 0.03) {// initialization variable iterations = 0; realTemp = realCoord; imagTemp = imagCoord; arg = (realCoord * realCoord) + (imagCoord * imagCoord); // 2 is the square root of 4. Therefore, only the value of x ^ 2 + y ^ 2 is calculated, and the while LOOP executes iteration, while (arg <4) & (iterations <40) {// real part realTemp2 = (realTemp * realTemp)-(imagTemp * imagTemp)-realCoord; // N * The Imaginary Part Of The N-N imagTemp = (2 * realTemp * imagTemp) -ImagCoord; realTemp = realTemp2; arg = (realTemp * realTemp) + (imagTemp * imagTemp); // the value of the current vertex is stored in iterations + = 1 ;} // select the character switch (iterations % 4) {case 0: Console. write (". "); break; case 1: Console. write ("o"); break; case 2: Console. write ("O"); break; case 3: Console. write ("@"); break ;}// end a row after the inner loop ends, so the line break is output. Console. Write ("\ n") ;}console. ReadKey ();}}

The demo result is:

Chapter exercises require the user to enter the border of the image and display the selected image section. The characters output by the current code should be placed exactly on the line of the console application. Consider how to make each selected image occupy exactly

To maximize the visible area.

Class Program {// each position in the Mandelbrot image corresponds to a plural value in formula N = x + y * I. In fact, the number part is x, the imaginary part is y, and I is the square root of-1. The x and y coordinates of each position in the image correspond to the x and y coordinates of the imaginary number. // each position in the image is represented by the parameter N, it is the square root of x * x + y * y. If the value is greater than or equal to 2, the position value corresponding to this number is 0. If the value of parameter N is less than 2, change the value of N to N * N-N (N = (x * x-y * y-x) + (2 * x * y-y) * I) and test the new N value again. If the value is greater than or equal to 2, the position value corresponding to this number is 1. This process continues until we assign a value to the position in the image, or the number of iterations exceeds the specified number ." Static void Main (string [] args) {// the real number and the imaginary part of N are double realCoord, imagCoord; double realMax = 1.77; double realMin =-0.6; double imagMax =-1.2; double imagMin = 1.2; double realStep; double imagStep; // stores the Temporary Information During calculation. double realTemp, imagTemp, realTemp2, arg; // records the information in parameter N (arg) number of iterations equal to or greater than 2 int iterations; // select an appropriate boundary value to display the main part of the Mandelbrot image. If you want to enlarge this image, you can zoom in (actually decrease) these boundary values. While (true) {// set the span to ensure that each selected image occupies exactly the same size space to maximize the visible area. RealStep = (realMax-realMin)/79; imagStep = (imagMax-imagMin)/48; for (imagCoord = imagMin; imagCoord> = imagMax; imagCoord + = imagStep) {// two for loops process one point in the image and specify a value for N. For (realCoord = realMin; realCoord <= realMax; realCoord + = realStep) {// initialization variable iterations = 0; realTemp = realCoord; imagTemp = imagCoord; arg = (realCoord * realCoord) + (imagCoord * imagCoord); // 2 is the square root of 4, so only the value of x ^ 2 + y ^ 2 is calculated, while loop executes iteration, while (arg <4) & (iterations <40) {// realTemp2 = (realTemp * realTemp)-(imagTemp * imagTemp)-realCoord; // N * The Imaginary Part Of The N-N imagTemp = (2 * realTemp * ImagTemp)-imagCoord; realTemp = realTemp2; arg = (realTemp * realTemp) + (imagTemp * imagTemp); // the value of the current vertex is stored in iterations + = 1 ;} // select the character switch (iterations % 4) {case 0: Console. write (". "); break; case 1: Console. write ("o"); break; case 2: Console. write ("O"); break; case 3: Console. write ("@"); break ;}// end a row after the inner loop ends, so the line break is output. Console. write ("\ n");} // current boundary value Console. writeLine ("Current limits:"); Console. writeLine ("realCoord: from {0} to {1}", realMin, realMax); Console. writeLine ("imagCoord: from {0} to {1} \ n", imagMin, imagMax); // enter the new boundary value Console. writeLine ("Enter new limits:"); // real number Console. writeLine ("realCoord: from:"); realMin = Convert. toDouble (Console. readLine (); Console. writeLine ("realCoord: to:"); realMax = Convert. toDouble (Console. readLine (); // virtual number Console. writeLine ("imagCoord: from:"); imagMin = Convert. toDouble (Console. readLine (); Console. writeLine ("imagCoord: to:"); imagMax = Convert. toDouble (Console. readLine ());}}}

Original boundary (-0.6, 1.2) (1.77,-1.2)

Current boundary (-0.6, 1.2)

It is equivalent to enlarging a part of the original image: Probably this part? At present, I can only understand this degree.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.