Select the sort---while (scanf ("%d", &n)! = EOF) and GetChar () introduction and their existing problems,

Source: Internet
Author: User
Tags number sign types of functions truncated

Basic idea:

the direct selection sort of n records files can be ordered by N-1 Direct selection order to get an orderly result:① Initial state: Unordered area is R[1..N], ordered area is empty. ② 1th Trip Sortin the unordered area R[1..N], select the smallest record of the keyword R[k], and the 1th record of the unordered zone r[1] exchange, so that r[1..1] and R[2..N] respectively to increase the number of records added a new ordered area and the number of records reduced by 1 new unordered area. ...③ Order of the first I tripat the beginning of the sequence I, the current ordered and unordered regions are r[1..i-1] and R (I.) respectively. N). The sequencing selects the smallest record of the keyword from the current unordered area R[k], swapping it with the 1th record R in the unordered zone, so that r[1..i] and R change to a new unordered area with a new ordered area and a number of records with a decrease of 1 respectively. Code Implementation:
#include <stdio.h> #include <stdlib.h>int *select_sort (int a[],int n) {int i;    cyclic factor int J;  cyclic factor int k;    the subscript int temp;for (i=0;i<n;++i) {k = i) that holds the maximum value for Each loop;    Assuming the first number is the largest, so the index of the first number of each cycle is assigned to KFOR (J=I+1;J<N;++J)//and the following values in turn, the maximum value of the subscript to K{if (A[j] > A[k]) {k = j;}} if (k! = i)//the subscript of       the maximum value of each loop is not itself (if it does not have to be exchanged) {temp = A[i];a[i] = a[k];a[k] = temp;}} return A;} void my_printf (int a[],int n) {int i;for (i=0;i<n;i++) {printf ("%d\n", A[i]);}} int main () {System ("mode con cols=100 lines=100"); System ("Color 0A"); int a[] = {8,5,7,3,9,12,6};int n = sizeof (a)/sizeof (a [0]); Select_sort (a,n); my_printf (a,n); System ("pause"); return 0;}


What do I do if I dynamically enter the number to sort?

 #include <stdio.h># Include<stdlib.h>void my_swap (int a[],int i,int j) {int tmp;tmp = A[i];a[i] = a[j];a[j] = tmp;}    int *quick_sort (int a[],int n) {int i;  cyclic factor int J;    cyclic factor int k;    Save the subscript for (i=0;i<n;++i) {k = i) of the maximum value per loop. Assuming the first number is the largest, so the index of the first number of each cycle is assigned to KFOR (J=I+1;J<N;++J)//and the following values in turn, the maximum value of the subscript to K{if (A[j] > A[k]) {k = j;}} if (k! = i)//the subscript of the maximum value of each loop is not itself (if it does not have to be exchanged) {My_swap (a,k,i);}} return A;} void my_printf (int a[],int n) {int i;for (i=0;i<n;i++) {printf ("%d\n", A[i]);}} int main () {System ("mode con cols=100 lines=100"); System ("Color 0A"); int n;int i;int *array;printf ("How many nums does you WA NT to sort: "); (scanf ("%d ", &n)! = EOF)//When input is CTRL + Z when the end loop {array = (int *) malloc (sizeof (int) *n);//Dynamic Open space printf (" Please input%d num:\n ", N), for (i=0;i<n;i++) {scanf ("%d ", &array[i]),//Initialize array}}quick_sort (array,n);p rintf (" the Sorted num:\n "); my_printf (array,n); System (" pause "); return 0;} 

while (scanf ("%d", &n)! = EOF) describes and issues that exist:

To tell the truth, never seen this kind of writing before, did not notice scanf also has the return value, EOF also did not see. Baidu, know that EOF is-1 (that is, # define EOF (-1));


SCANF returns the number of successful scans. such as scanf ("%d%d", &a, &b), if a, B are entered successfully returned 2, a successful return 1, did not successfully return 0, error returned-1.


This code means that the input CTRL + Z terminates the loop (this is ctrl+d under Windows, in Unix). If you enter the character a, and there is no function in the loop body that reads characters like GetChar, the loop will die because a will remain in the input buffer.




If you want to terminate the!=eof in the case of an input error, it is OK to remove the line, that is, if the number of successful inputs is 0, roll out the loop.



In the process of Baidu see there is while (~scanf ("%d", &n)!=eof) This writing, can not find a specific explanation. Later found "~" explanation is to take the reverse, that is, 0 change to 0. Try this way to exit the loop when the error is entered, meaning that the value of ~0 is-1. Think of a bit also pass: The computer is in the complement of the number, 02 in the eight-digit complement of 0000 0000, "Take the reverse" after 1111 1111, corresponding to 1 of the complement. Here the inverse of the quotation marks, because the real arithmetic of the inverse, the number sign bit is constant, that is 0000 0000 of the inverse code is 0111 1111, and the complement of 0111 1111 is the number 1.


Similar code as follows, often met with:


int C; Note c is defined as shaping because the GetChar () function returns a value of while ((c = GetChar ())! = EOF) {Putchar (c);}
The return value types of functions such as getchar are of type int, and when these functions read an error or read the file, EOF is returned. EOF is a macro, and the standard specifies that its value must be a negative constant of type int. Usually the compiler will define EOF as the -1.getchar return int assigned to char C when truncated, and the EOF comparison will be promoted to int, the following error may occur:

    1. Some legitimate characters are "truncated" and then equal to 1, causing the program to break in the process of copying.
    2. The preceding C cannot be taken as EOF, causing the program to produce a dead loop.
another situation is that the compiler does not implement the C language specification, although it may produce the first said "truncation" and the second can not find "EOF" case, and the compiler also assigns the value to C, but the compiler inside the GetChar () The returned value is compared with EOF, which results in the correct result.

The          standard input is not the same as the file, the length of the input cannot be known beforehand, and a character must be entered manually, indicating that EOF is reached. Linux, at the beginning of a new line, press ctrl-d, which represents EOF (if you press ctrl-d in the middle of a row , the output "standard input" buffer , so you must press two times ctrl-d); Ctrl-z represents EOF. (Incidentally, by pressing Ctrl-z in Linux, the process is interrupted, suspended in the background, the FG command can be re-cut back to the foreground, and the process is terminated by pressing CTRL-C.) So, what if you really want to enter ctrl-d? At this point you must press CTRL-V, then you can enter the ctrl-d, the system will not think this is the EOF signal. Ctrl-v means to interpret the next input by "literal meaning", if you want to enter Ctrl-v by "literal meaning", enter it two times in a row. EOF means end of file!!!! You press Ctrl+d and try.




Select the sort---while (scanf ("%d", &n)! = EOF) and GetChar () introduction and their existing problems,

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.