K & R's masterpiece: Summary of p117 statistical keywords by Applets

Source: Internet
Author: User

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

# Define maxword 10
# Define nkeys (sizeof keytab/sizeof keytab [0])

Struct key
{
Char * word;
Int count;
} Keytab [] = {
{"Auto", 0 },
{"Break", 0 },
{"Case", 0 },
{"Char", 0 },
{"Const", 0 },
{"Continue", 0 },
{"Defalut", 0 },
{"Unsigned", 0 },
{"Void", 0 },
{"Volatile", 0 },
{"While", 0}

};

Int getword (char *, INT );
Int binsearch (char *, struct key *, INT );
Int getcounter (void );
Void ungetch1 (int c );

Int main (void)
{

Int N;
Char word [maxword];

While (getword (word, maxword )! = EOF)
If (isalpha (word [0])
If (n = binsearch (word, keytab, nkeys)> = 0)
Keytab [N]. Count ++;
For (n = 0; n <nkeys; n ++)
If (keytab [N]. Count> 0)
Printf ("% 4D % s \ n", keytab [N]. Count, keytab [N]. Word );
Return 0;

 
}

// Binsearch function: search for words from tab [0] To tab [n-1]
Int binsearch (char * word, struct key tab [], int N)
{
Int cond;
Int low, high, mid;

Low = 0;
High = n-1;
While (low <= high)
{
Mid = (low + high)/2;
If (cond = strcmp (word, Tab [Mid]. Word) <0)
High = mid-1;
Else if (cond> 0)
Low = Mid + 1;
Else
Return mid;
}
Return-1;
}

// Getword function: reads the next word or character from the input.
Int getword (char * word, int Lim)
{
Int C, getcounter (void );
Void ungetch1 (INT );
Char * w = word;

While (isspace (C = getspace ()))
;
If (C! = EOF)
* W ++ = C;
If (! Isalpha (c ))
{
* W = '\ 0 ';
Return C;
}
For (; -- Lim> 0; W ++)
If (! Isalnum (* w = getnum ()))
{
// Printf ("in the getword ()");
Ungetch1 (* w );
Break;
}
* W = '\ 0 ';
Return word [0];

}
# Define bufsize 100
Char Buf [bufsize];
Int bufp = 0;
Int getask( void)
{
Return (bufp> 0 )? Buf [-- bufp]: getchar ();
}

Void ungetch1 (int c)
{
If (bufp> = bufsize)
Printf ("ungetch: Too character characters \ n ");
Else
Buf [bufp ++] = C;

}

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.