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

📄 chap3.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
#include <stdio.h> 
 
/* Magic number program. */ 
int main(void) 
{ 
  int magic = 123;  /* magic number */ 
  int guess; 
 
  printf("Enter your guess: "); 
  scanf("%d", &guess); 
 
  if(guess == magic) printf("** Right **"); 
 
  return 0; 
}

listing 2
#include <stdio.h> 
 
/* Magic number program - improvement 1. */ 
int main(void) 
{ 
  int magic = 123;  /* magic number */ 
  int guess; 
 
  printf("Enter your guess: "); 
  scanf("%d",&guess); 
 
  if(guess == magic) printf("** Right **"); 
  else printf(".. Wrong .."); 
 
  return 0; 
}

listing 3
if(x) 
  if(y) printf("1"); 
  else printf("2");

listing 4
if(x) { 
  if(y) printf("1"); 
} 
else printf("2");

listing 5
#include <stdio.h> 
 
/* Magic number program - improvement 2. */ 
int main(void) 
{ 
  int magic = 123;  /* magic number */ 
  int guess; 
 
  printf("Enter your guess: "); 
  scanf("%d", &guess); 
 
  if(guess == magic) { 
    printf("** Right ** "); 
    printf("%d is the magic number", magic); 
  } 
  else { 
    printf(".. Wrong .. "); 
    if(guess > magic) printf("Too high"); 
    else printf("Too low"); 
  } 
 
  return 0; 
}

listing 6
#include <stdio.h> 
 
/* Magic number program - improvement 3. */ 
int main(void) 
{ 
  int magic = 123;  /* magic number */ 
  int guess; 
 
  printf("Enter your guess: "); 
  scanf("%d", &guess); 
 
  if(guess == magic) { 
    printf("** Right ** "); 
    printf("%d is the magic number", magic); 
  } 
  else if(guess > magic) 
    printf(".. Wrong .. Too High"); 
  else printf(".. Wrong .. Too low"); 
 
  return 0; 
}

listing 7
x = 10; 
y = x>9 ? 100 : 200;

listing 8
x = 10; 
if(x>9) y = 100; 
else y = 200;

listing 9
#include <stdio.h> 
 
int f1(int n), f2(void); 
 
int main(void) 
{ 
  int t; 
 
  printf("Enter a number: "); 
  scanf("%d", &t); 
  /* print proper message */ 
  t ? f1(t) + f2() : printf("zero entered."); 
 
  return 0; 
} 
 
int f1(int n) 
{ 
  printf("%d ",n); 
  return 0; 
} 
 
int f2(void) 
{ 
  printf("entered "); 
  return 0; 
}

listing 10
#include <stdio.h> 
 
/* Magic number program - improvement 4. */ 
int main(void) 
{ 
  int magic = 123;  /* magic number */ 
  int guess; 
 
  printf("Enter your guess: "); 
  scanf("%d", &guess); 
  if(guess == magic) { 
    printf("** Right ** "); 
    printf("%d is the magic number", magic); 
  } 
  else 
    guess > magic ? printf("High") : printf("Low"); 
 
  return 0; 
}

listing 11
void menu(void) 
{ 
  char ch; 
 
  printf("1. Check Spelling\n"); 
  printf("2. Correct Spelling Errors\n"); 
  printf("3. Display Spelling Errors\n"); 
  printf("Strike Any Other Key to Skip\n"); 
  printf("      Enter your choice: "); 
 
  ch = getche(); /* read the selection from the keyboard */ 
 
  switch(ch) { 
    case '1': 
      check_spelling(); 
      break; 
    case '2': 
      correct_errors(); 
      break; 
    case '3': 
      display_errors(); 
      break; 
    default : 
      printf("No option selected"); 
  } 
}

listing 12
void inp_handler(void) 
{ 
  int ch, flag; 
 
  ch = read_device(); /* read some sort of device */ 
  flag = -1; 
 
  switch(ch) { 
    case 1:  /* these cases have common statement */ 
    case 2:  /* sequences */ 
    case 3: 
      flag = 0; 
      break; 
    case 4: 
      flag = 1; 
    case 5: 
      error(flag); 
      break; 
    default: 
      process(ch); 
  } 
}

listing 13
flag = 0; 
break;

listing 17
switch(x) { 
  case 1: 
    switch(y) { 
      case 0: printf("Divide by zero error."); 
              break; 
      case 1: process(x,y); 
              break; 
    } 
    break; 
  case 2: 
   . 
   . 
   .

listing 18
#include <stdio.h> 
 
int main(void) 
{ 
  int x; 
 
  for(x=1; x <= 100; x++) printf("%d ", x); 
 
  return 0; 
}

listing 19
for(x=100; x != 65; x -= 5) { 
  z = x*x; 
  printf("The square of %d, %d", x, z); 
}

listing 20
x = 10; 
for(y=10; y != x; ++y) printf("%d", y); 
printf("%d", y); /* this is the only printf() 
                    statement that will execute */

listing 21
for(x=0, y=0; x+y < 10; ++x) { 
  scanf("%d", &y); 
    . 
    . 
    . 
}

listing 22
/* Copy s into r backwards. */ 
void reverse(char *s, char *r) 
{ 
  int i, j; 
 
  for(i=strlen(s)-1, j=0; i > =0; j++, i--) r[i] = s[j]; 
  r[j] = '\0'; /* append null terminator */ 
}

listing 23
void sign_on(void) 
{ 
  char str[20]; 
  int x; 
 
  for(x=0; x<3 && strcmp(str,"password"); ++x) { 
    printf("enter password please:"); 
    gets(str); 
  } 
  if(x==3) hang_up(); 
}

listing 24
#include <stdio.h> 
 
int readnum(void), prompt(void); 
int sqrnum(int num); 
 
int main(void) 
{ 
  int t; 
 
  for(prompt(); t=readnum(); prompt()) sqrnum(t); 
  return 0; 
} 
 
int prompt(void) 
{ 
  printf("Enter a number: "); 
  return 0; 
} 
 
int readnum(void) 
{ 
  int t; 
 
  scanf("%d", &t); 
  return t; 
} 
 
int sqrnum(int num) 
{ 
  printf("%d\n", num*num); 
  return 0; 
}

listing 25
for(x=0; x != 123; ) scanf("%d", &x);

listing 26
gets(s);  /* read a string into s */ 
if(*s) x = strlen(s); /* get the string's length */ 
else x = 10; 
 
for( ; x < 10; ) { 
  printf("%d", x); 
  ++x; 
}

listing 27
for(;;) printf("This loop will run forever.\n");

listing 28
ch = '\0'; 
 
for( ; ; ) { 
  ch = getchar();  /* get a character */ 
  if(ch == 'A') break;  /* exit the loop */ 
} 
 
printf("you typed an A");

listing 29
for( ; *str == ' '; str++) ;

listing 30
for(t=0; t < SOME_VALUE; t++) ;

listing 31
char wait_for_char(void) 
{ 
  char ch; 
 
  ch = '\0';  /* initialize ch */ 
  while(ch != 'A')  ch = getchar(); 
  return ch; 
}

listing 32
/* Add spaces to the end of a string. */ 
void pad(char *s, int length) 
{ 
  int l; 
  l = strlen(s);  /* find out how long it is */ 
 
  while(l < length) { 
    s[l] = ' ';   /* insert a space */ 
    l++; 
  } 
 
  s[l] = '\0';  /* strings need to be 
                   terminated in a null */ 
}

listing 33
void func1(void) 
{ 
  int working; 
 
  working = 1;   /* i.e., true */ 
 
  while(working) { 
    working = process1(); 
    if(working) 
      working = process2(); 
    if(working) 
      working = process3(); 
  } 
}

listing 34
while((ch=getchar()) != 'A') ;

listing 35
do { 
  scanf("%d", &num); 
} while(num > 100);

listing 36
void menu(void) 
{ 
  char ch; 
 
  printf("1. Check Spelling\n"); 
  printf("2. Correct Spelling Errors\n"); 
  printf("3. Display Spelling Errors\n"); 
  printf("      Enter your choice: "); 
 
  do { 
    ch = getche(); /* read the selection from the keyboard */ 
    switch(ch) { 
      case '1': 
        check_spelling(); 
        break; 
      case '2': 
        correct_errors(); 
        break; 
      case '3': 
        display_errors(); 
        break; 
    } 
  } while(ch!='1' && ch!='2' && ch!='3'); 
}

listing 37
#include <stdio.h> 
 
int main(void) 
{ 
  int t; 
 
  for(t=0; t<100; t++) { 
    printf("%d ", t); 
    if(t == 10) break; 
  } 
  return 0; 
}

listing 38
int look_up(char *name) 
{ 
  char tname[40]; 
  int loc; 
 
  loc = -1; 
  do { 
    loc = read_next_name(tname); 
    if(kbhit()) break; 
  } while(!strcmp(tname, name)); 
  return loc; 
}

listing 39
for(t=0; t<100; ++t) { 
  count = 1; 
  for(;;) { 
    printf("%d ", count); 
    count++; 
    if(count == 10) break; 
  } 
}

listing 40
#include <stdlib.h> 
 
int main(void) 
{ 
  if(!special_adaptor()) exit(1); 
  play(); 
  return 0; 
}

listing 41
void menu(void) 
{ 
  char ch; 
 
  printf("1. Check Spelling\n"); 
  printf("2. Correct Spelling Errors\n"); 
  printf("3. Display Spelling Errors\n"); 
  printf("4. Quit\n"); 
  printf("      Enter your choice: "); 
 
  do { 
    ch = getche(); /* read the selection from the keyboard */ 
 
    switch(ch) { 
      case '1': 
        check_spelling(); 
        break; 
      case '2': 
        correct_errors(); 
        break; 
      case '3': 
        display_errors(); 
        break; 
      case '4': 
        exit(0);  /* return to OS */ 
    } 
  } while(ch!='1' && ch!='2' && ch!='3'); 
}

listing 42
do { 
  scanf("%d", &x); 
  if(x < 0) continue; 
  printf("%d ", x); 
} while(x != 100);

listing 43
for(t=0; t<100; ++t) { 
  scanf("%d", &x); 
  if(x < 0) continue; 
  printf("%d ", x); 
}

listing 44
void code(void) 
{ 
  char done, ch; 
 
  done = 0; 
  while(!done) { 
    ch = getchar(); 
    if(ch=='.') { 
      done = 1; 
      continue; 
    } 
    putchar(ch+1);  /* shift the alphabet one position */ 
  } 
}

listing 45
x = 1; 
 
loop1: 
  x++; 
  if(x <= 100) goto loop1;

listing 46
for(...) { 
  for(...) { 
    while(...) { 
      if(...) goto stop; 
      . 
      . 
      . 
    } 
  } 
} 
stop: 
  printf("error in program\n");

⌨️ 快捷键说明

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