《TheC Programming Language》答案(第六章)

《TheC Programming Language》答案(第六章)

我的新站

P1

/**
 * 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 114
char buf[BUFFSIZE];  // buffer for ungetch
int bufp = 0;        // next free posotion in buf
int getch(void){
   
  return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c){
   
  if (bufp >= BUFFSIZE)
    printf("ungetch: too many characters\n");
  else buf[bufp++] = c;
}
int acceptable(char c){
   
  return  ((c == '_') || (c == '"') || (c == '#') || (c == '/') || isalnum(c));
}
int getword(char *word, int lim){
   
  int c, getch(void);
  void ungetch(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];
}
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;
}
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;
}

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;
}
int ptinrect(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;
}
int main()
{
   
    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);
    return 0;
}

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 */
int i_strcmp(const char *s, const char *t){
   
  int diff = 0;
  char cs = 0;
  char ct = 0;
  while(diff == 0 && *s != '\0' && *t != '\0'){
   
      cs = tolower((unsigned char)*s);
      ct = tolower((unsigned char)*t);
      if(cs < ct){
   
   diff = -1;
 }
      else if(cs > ct){
   
   diff = 1;
 }
      ++s;
      ++t;
    }
  if(diff == 0 && *s != *t){
   
      /* the shorter string comes lexicographically sooner */
      if(*s == '\0'){
   
   diff =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值