K & R masterpiece p103 function pointer: concatenate previous applets

Source: Internet
Author: User

# Include <stdio. h>
# Include <string. h>

# Define maxlines 5000
Char * lineptr [maxlines];

Int readlines (char * lineptr [], int nlines );
Void writelines (char * lineptr [], int nlines );

Void qsort1 (char * lineptr [], int left, int right, INT (* comp) (void *, void *));
Int numcmp (char *, char *);

Int main (INT argc, char * argv [])
{
Int nlines;
Int numeric = 0;

If (argc> 1 & strcmp (argv [1], "-n") = 0)
Numeric = 1;
If (nlines = readlines (lineptr, maxlines)> = 0)
{
Qsort1 (void *) lineptr, 0, nlines-1, (INT (*) (void *, void *) (numeric? Numcmp: strcmp ));
Writelines (lineptr, nlines );
Return 0;
} Else {
Printf ("input too big to sort! \ N ");
Return 1;
}
}

// Qsort1 function: sorts V [left]... V [right] in ascending order.
Void qsort1 (char * V [], int left, int right, INT (* comp) (void *, void *))
{
Int I, last;
Void swap (char * V [], int I, Int J );

If (left> = right)
Return;
Swap (v, left, (left + right)/2 );
Last = left;
For (I = left + 1; I <= right; ++ I)
If (* comp) (V [I], V [left]) <0)
Swap (v, ++ last, I );
Swap (v, left, last );
Qsort1 (v, left, last-1, comp );
Qsort1 (v, last + 1, right, comp );

}
Void swap (char * V [], int I, Int J)
{
Char * temp;
Temp = V [I];
V [I] = V [J];
V [J] = temp;
}

// Numcmp function: Compares strings 1 and 2 in numerical order.
# Include <stdlib. h>
Int numcmp (char * S1, char * S2)
{
Double V1, V2;
V1 = atof (S1 );
V2 = atof (S2 );
If (V1 <V2)
Return-1;
Else if (V1> V2)
Return 1;
Else
Return 0;
}

# Define maxlen 1000
Int getline1 (char *, INT );
Char * alloc (INT );

// Readlines function: Read the input line
Int readlines (char * lineptr [], int maxlines)
{
Int Len, nlines;
Char * P, line [maxlen];
Nlines = 0;
While (LEN = getline1 (line, maxlen)> 0)
If (nlines> = maxlines | (P = alloc (LEN) = NULL)
Return-1;
Else {
Line [len-1] = '\ 0 ';
Strcpy (p, line );
Lineptr [nlines ++] = P;
}
Return nlines;
}

// Write the output row using the writelines Function
Void writelines (char * lineptr [], int nlines)
{
Int I;
For (I = 0; I <nlines; I ++)
Printf ("% s \ n", lineptr [I]);
}

/*************************************** ***********************************
Dynamic Memory Allocation Function and release function
**************************************** ***********************************
*/

# Define allocsize 10000

Static char allocbuf [allocsize];
Static char * allocp = allocbuf;

Char * alloc (int n)
{
If (allocbuf + allocsize-allocp> = N ){
Allocp + = N;
Return allocp-N;
} Else
Return 0;
}

Void afree (char * P)
{
If (P> allocbuf & P <allocbuf + allocsize)
Allocp = P;
}

/*************************************** ***********************
The read-in function returns the actual number of reads.
**************************************** **********************
*/
Int getline1 (char * s, int Lim)
{
Int C;
Char * t = s;

While (-- Lim> 0 & (C = getchar ())! = EOF & C! = '\ N ')
* S ++ = C;
If (C = '\ n ')
* S ++ = C;
* S = '\ 0 ';
Return s-t;

}

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.