/**
* a better version of getword
* written by swy
**/#include<stdio.h>#include<ctype.h>#include<string.h>#define MAXWORD 114#define NKEYS (sizeof keytab /sizeof keytab[0])struct key {
char*word;int count;} keytab[]={
"auto",0,"break",0,"case",0,"char",0,"continue",0,/*...*/"unsigned",0,"void",0,"volatile",0,"while",0};#define BUFFSIZE 114char buf[BUFFSIZE];// buffer for ungetchint bufp =0;// next free posotion in bufintgetch(void){
return(bufp >0)? buf[--bufp]:getchar();}voidungetch(int c){
if(bufp >= BUFFSIZE)printf("ungetch: too many characters\n");else buf[bufp++]= c;}intacceptable(char c){
return((c =='_')||(c =='"')||(c =='#')||(c =='/')||isalnum(c));}intgetword(char*word,int lim){
int c,getch(void);voidungetch(int);char*w = word;while(isspace(c =getch()));if(c !=EOF)*w++= c;if(!acceptable(c)){
*w ='\0';return c;}for(;--lim >0; w++)if(!acceptable(*w =getch())){
ungetch(*w);break;}*w ='\0';return word[0];}intbinsearch(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;elseif(cond >0)
low = mid +1;elsereturn mid;}return-1;}intmain(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);return0;}
P2
#include<stdio.h>#define XMAX 114#define YMAX 114#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))struct point{
int x;int y;};struct rect{
struct point pt1;struct point pt2;};struct point makepoint(int x,int y){
struct point temp;
temp.x = x;
temp.y = y;return temp;}struct point addpoint(struct point p1,struct point p2){
p1.x += p2.x;
p1.y += p2.y;return p1;}intptinrect(struct point p,struct rect r){
return p.x >= r.pt1.x && p.x < r.pt2.x
&& p.y >= r.pt1.y && p.y < r.pt2.y;}struct rect canonrect(struct rect r){
struct rect temp;
temp.pt1.x =min(r.pt1.x, r.pt2.x);
temp.pt1.y =min(r.pt1.y, r.pt2.y);
temp.pt2.x =max(r.pt1.x, r.pt2.x);
temp.pt2.y =max(r.pt1.y, r.pt2.y);return temp;}intmain(){
struct rect screen;struct point middle;struct point origin;struct point *pp;
screen.pt1 =makepoint(0,0);
screen.pt2 =makepoint(XMAX, YMAX);
middle =makepoint((screen.pt1.x + screen.pt2.x)/2,(screen.pt1.y + screen.pt2.y)/2);
origin = screen.pt1;
pp =&origin;printf("origin is (%d,%d)\n",(*pp).x,(*pp).y);printf("origin is (%d,%d)\n", pp->x, pp->y);return0;}
P3
/*这是要我写轮子么*//*Write a cross-referencer that prints a list of all words in a document, and, for
each word, a list of the line numbers on which it occurs. Remove noise words like
"the", "and," and so on.*/#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>/* no such thing as strdup, so let's write one
*
* supplementary question: why did I call this function dupstr,
* rather than strdup?
*
*/char*dupstr(char*s){
char*p =NULL;if(s !=NULL){
p =malloc(strlen(s)+1);if(p){
strcpy(p, s);}}return p;}/* case-insensitive string comparison */inti_strcmp(constchar*s,constchar*t){
int diff =0;char cs =0;char ct =0;while(diff ==0&&*s !='\0'&&*t !='\0'){
cs =tolower((unsignedchar)*s);
ct =tolower((unsignedchar)*t);if(cs < ct){
diff =-1;}elseif(cs > ct){
diff =1;}++s;++t;}if(diff ==0&&*s !=*t){
/* the shorter string comes lexicographically sooner */if(*s =='\0'){
diff =