⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap4.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
void pr_reverse(char *s) 
{ 
  register int t; 
 
  for(t=strlen(s)-1; t >= 0; t--) printf("%c", s[t]); 
}

listing 2
int find_substr(char *s1, char *s2) 
{ 
  register int t; 
  char *p, *p2; 
 
  for(t=0; s1[t]; t++) { 
    p = &s1[t]; 
    p2 = s2; 
    while(*p2 && *p2==*p) { 
      p++; 
      p2++; 
    } 
    if(!*p2) return t; /* substring was found */ 
  } 
  return -1; /* substring not found */ 
}

listing 3
x = power(y); 
if(max(x, y) > 100) printf("greater"); 
for(ch=getchar(); isdigit(ch); ) ... ;

listing 4
swap(x, y) = 100;   /* incorrect statement */

listing 5
#include <stdio.h> 
 
int mul(int a, int b); 
 
int main(void) 
{ 
  int x, y, z; 
 
  x = 10;   y = 20; 
  z = mul(x, y);            /* 1 */ 
  printf("%d", mul(x, y));  /* 2 */ 
  mul(x, y);                /* 3 */ 
 
  return 0; 
} 
 
int mul(int a, int b) 
{ 
  return a*b; 
}

listing 6
/* return 1 if c is part of string s; 0 otherwise */ 
int is_in(char *s, char c) 
{ 
  while(*s) 
    if(*s==c) return 1; 
    else s++; 
 
  return 0; 
}

listing 7
#include <stdio.h> 
 
int sqr(int x); 
 
int main(void) 
{ 
  int t=10; 
 
  printf("%d %d", sqr(t), t); 
  return 0; 
} 
 
int sqr(int x) 
{ 
  x = x*x; 
  return x; 
}

listing 8
void swap(int *x, int *y) 
{ 
  int temp; 
 
  temp = *x;  /* save the value at address x */ 
  *x = *y;    /* put y into x */ 
  *y = temp;  /* put x into y */ 
}

listing 9
#include <stdio.h> 
 
void swap(int *x, int *y); 
 
int main(void) 
{ 
  int x, y; 
 
  x = 10; 
  y = 20; 
  printf("x and y before swapping: %d %d\n", x, y); 
  swap(&x, &y); 
  printf("x and y after swapping: %d %d\n", x, y); 
 
  return 0; 
}

listing 10
#include <stdio.h> 
 
void display(int num[10]); 
 
int main(void)  /* print some numbers */ 
{ 
  int t[10], i; 
 
  for(i=0; i<10; ++i) t[i]=i; 
  display(t); 
  return 0; 
} 
 
void display(int num[10]) 
{ 
  int i; 
 
  for(i=0; i<10; i++) printf("%d ", num[i]); 
}

listing 11
void display(int num[]) 
{ 
  int i; 
 
  for(i=0; i<10; i++) printf("%d ", num[i]); 
}

listing 12
void display(int *num) 
{ 
  int i; 
 
  for(i=0; i<10; i++) printf("%d ", num[i]); 
}

listing 13
#include <stdio.h> 
 
void display(int num); 
 
int main(void) /* print some numbers */ 
{ 
  int t[10], i; 
 
  for(i=0; i<10; ++i) t[i] = i; 
  for(i=0; i<10; i++) display(t[i]); 
 
  return 0; 
} 
 
void display(int num) 
{ 
  printf("%d ", num); 
}

listing 14
#include <stdio.h> 
#include <ctype.h> 
 
void print_upper(char *str); 
 
int main(void)  /* print string as uppercase */ 
{ 
  char s[80]; 
 
  printf("Enter a string: ");  
  gets(s); 
  print_upper(s); 
  printf("\ns is now uppercase: %s", s);  
 
  return 0; 
} 
 
void print_upper(char *str) 
{ 
  register int t; 
 
  for(t=0; str[t]; ++t)  { 
    str[t] = toupper(str[t]); 
    putchar(str[t]); 
  } 
}

listing 15
#include <stdio.h> 
#include <ctype.h> 
 
void print_upper(char *str); 
 
int main(void)  /* print string as uppercase */ 
{ 
  char s[80]; 
 
  printf("Enter a string: ");  
  gets(s); 
  print_upper(s); 
  printf("\ns is unchanged: %s", s);  
 
  return 0; 
} 
 
void print_upper(char *str) 
{ 
  register int t; 
 
  for(t=0; str[t]; ++t) 
    putchar(toupper(str[t])); 
}

listing 16
/* A simple version of the gets() library function. */ 
 
char *xgets(char *s) 
{ 
  char ch, *p; 
  int t; 
 
  p = s; 
 
  for(t=0; t<80; ++t) { 
    ch = getchar(); 
    switch(ch) { 
      case '\n': 
        s[t] = '\0'; /* terminate the string */ 
        return p; 
      case '\b': 
        if(t>0) t--; 
        break; 
      default: 
        s[t] = ch; 
 
    } 
  } 
  s[79] = '\0'; 
  return p; 
}

listing 17
#include <stdio.h> 
 
int main(int argc, char *argv[]) 
{ 
  if(argc!=2) { 
    printf("You forgot to type your name\n"); 
    return 1; 
  } 
  printf("Hello %s", argv[1]); 
 
  return 0; 
}

listing 18
/* Countdown program. */ 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
 
int main(int argc, char *argv[]) 
{ 
  int disp, count; 
 
  if(argc<2) { 
    printf("You must enter the length of the count\n"); 
    printf("on the command line. Try again.\n"); 
    exit(1); 
  } 
 
  if(argc==3 && !strcmp(argv[2],"display")) disp = 1; 
  else disp = 0; 
 
  for(count=atoi(argv[1]); count; --count) 
    if(disp) printf("%d\n", count); 
 
  putchar('\a');  /* this will ring the bell */ 
  printf("Done"); 
  return 0; 
}

listing 19
#include <stdio.h> 
 
int main(int argc, char *argv[]) 
{ 
  int t, i; 
 
  for(t=0; t<argc; ++t) { 
    i = 0; 
    while(argv[t][i]) { 
      printf("%c", argv[t][i]); 
      ++i; 
    } 
    printf(" "); 
  } 
 
  return 0; 
}

listing 20
/* Link this program with WILDARGS.OBJ. */ 
 
#include <stdio.h> 
 
int main(int argc, char *argv[]) 
{ 
  register int i; 
 
  printf("%d files match specified name\n", argc-1); 
 
  printf("They are: "); 
 
  for(i=1; i<argc; i++) 
    printf("%s ", argv[i]); 
 
  return 0; 
}

listing 21
/* This program prints all the environmental 
   strings. 
*/ 
 
#include <stdio.h> 
 
int main(int argc, char *argv[], char *env[]) 
{ 
  int t; 
 
  for(t=0; env[t]; t++) 
    printf("%s\n", env[t]); 
 
  return 0; 
}

listing 22
/* This program searches the environmental 
   strings for the one that contains the 
   current PATH. 
*/ 
#include <stdio.h> 
#include <string.h> 
 
int main(int argc, char *argv[], char *env[]) 
{ 
  int t; 
 
  for(t=0; env[t]; t++) { 
    if(strstr(env[t], "PATH")) 
      printf("%s\n", env[t]); 
  } 
 
  return 0; 
}

listing 23
/* This program uses a function prototype to 
   enforce strong type checking. */ 
 
void sqr_it(int *i); /* prototype */ 
 
int main(void) 
{ 
  int x; 
 
  x = 10; 
  sqr_it(x);  /* type mismatch */ 
 
  return 0; 
} 
 
void sqr_it(int *i) 
{ 
  *i = *i * *i; 
}

listing 24
#include <stdio.h> 
 
/* This definition will also serve 
   as a prototype within this program. */ 
void f(int a, int b) 
{ 
  printf("%d ", a % b); 
} 
 
int main(void) 
{ 
  f(10,3); 
 
  return 0; 
}

listing 25
int f(); /* C++ prototype for a function with no parameters */

listing 26
float f(void);

listing 39
char *f(const char *str1, int count, int index) 
{ 
  /* ... */ 
}

listing 40
char *f(str1, count, index) 
char *str1; 
int count, index; 
{ 
  /* ... */ 
}

listing 27
int f(void) { 
  /* ... */ 
  return 0; 
}

listing 28
f(void) { /* return type int by default */ 
  /* ... */ 
  return 0; 
}

listing 29
/* Here, the return type defaults to int, and so do  
   the types of a and b. */ 
f(register a, register b) { 
  register c; /* c defaults to int, too */ 
 
  c = a + b; 
 
  printf("%d", c); 
 
  return c; 
}

listing 30
int func(int a, int b, ...);

listing 31
int func(...); /* illegal */

listing 37
char *match(char c, char *s) 
{ 
  while(c != *s && *s) s++; 
  return(s); 
}

listing 38
#include <stdio.h> 
 
char *match(char c, char *s); 
 
int main(void) 
{ 
  char s[80], *p, ch; 
 
  gets(s); 
  ch = getchar(); 
  p = match(ch, s); 
  if(*p)  /* there is a match */ 
    printf("%s ", p); 
  else 
    printf("No match found."); 
 
  return 0; 
}

listing 41
/* Compute the factorial of a number. */ 
int factr(int n)  /* recursive */ 
{ 
  int answer; 
 
  if(n==1) return(1); 
  answer = factr(n-1)*n; 
  return(answer); 
} 
 
/* Compute the factorial of a number. */ 
int fact(int n)    /* non-recursive */ 
{ 
  int t, answer; 
 
  answer = 1; 
  for(t=1; t<=n; t++) 
    answer=answer*(t); 
  return(answer); 
}

listing 42
#include <stdio.h> 
#include <string.h> 
 
void check(char *a, char *b, int (*cmp) (const char *, const char *)); 
 
int main(void) 
{ 
  char s1[80], s2[80]; 
  int  (*p)(const char*, const char*); 
 
  p = strcmp;  /* get address of strcmp() */ 
 
  gets(s1); 
  gets(s2); 
 
  check(s1, s2, p); 
  return 0; 
} 
 
void check(char *a, char *b, int (*cmp) (const char *, const char *)) 
{ 
  printf("Testing for equality.\n"); 
  if(!(*cmp) (a, b)) printf("Equal"); 
  else printf("Not equal"); 
}

listing 43
if(!(*cmp) (a, b)) printf("Equal");

listing 44
if(!cmp(a, b)) printf("Equal");

listing 45
check(s1, s2, strcmp);

listing 32
#include <stdio.h> 
#include <ctype.h> 
#include <string.h> 
#include <stdlib.h> 
 
void check(char *a, char *b, int (*cmp) (const char *, const char *)); 
int numcmp(const char *a, const char *b); 
 
int main(void) 
{ 
  char s1[80], s2[80]; 
  gets(s1); 
  gets(s2); 
 
  if(isalpha(*s1)) 
     check(s1, s2, strcmp); 
  else 
     check(s1, s2, numcmp); 
 
   return 0; 
} 
 
void check(char *a, char *b, int (*cmp) (const char *, const char *)) 
{ 
  printf("Testing for equality.\n"); 
  if(!(*cmp) (a, b)) printf("Equal"); 
  else printf("Not equal"); 
} 
 
int numcmp(const char *a, const char *b) 
{ 
  if(atoi(a)==atoi(b)) return 0; 
  else return 1; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -