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

📄 chap5.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
char p[10];

listing 2
#include <stdio.h> 
 
int main(void) 
{ 
  int x[10];  /* this reserves 10 integer elements */ 
  int t; 
 
  for(t=0; t<10; ++t) x[t] = t; 
 
  for(t=0; t<10; ++t) printf("%d ", x[t]); 
 
  return 0; 
}

listing 3
char a[7];

listing 4
int sample[10];

listing 5
int *p; 
int sample[10]; 
 
p = sample;

listing 6
int main(void) 
{ 
  int i[10]; 
 
  func1(i); 
  /*... */ 
  return 0; 
}

listing 7
void func1(int *a)  /* pointer */ 
{ 
  /*...*/ 
}

listing 9
void func1(int a[]) /* unsized array */ 
{ 
  /*...*/ 
}

listing 10
void func1(int a[32]) 
{ 
  /*...*/ 
}

listing 11
char s[11];

listing 12
#include <string.h> 
#include <stdio.h> 
 
int main(void) 
{ 
  char s1[80], s2[80]; 
 
  gets(s1); gets(s2); 
 
  printf("lengths: %d %d\n", strlen(s1), strlen(s2)); 
 
  if(!strcmp(s1, s2)) printf("The strings are equal\n"); 
 
  strcat(s1, s2); 
  printf("%s\n", s1); 
 
  strcpy(s1, "This is a test.\n"); 
  printf(s1); 
  if(strchr("hello", 'e')) printf("e is in hello\n"); 
  if(strstr("hi there", "hi")) printf("found hi"); 
 
  return 0; 
}

listing 13
int d[10][20];

listing 14
d[3][5]

listing 15
#include <stdio.h> 
 
int main(void) 
{ 
  int t,i, num[3][4]; 
 
  /* load numbers */ 
  for(t=0; t<3; ++t) 
    for(i=0; i<4; ++i) 
      num[t][i] = (t*4)+i+1; 
 
  /* display numbers */ 
  for(t=0; t<3; ++t) { 
    for(i=0; i<4; ++i) 
      printf("%d ", num[t][i]); 
    printf("\n"); 
  } 
 
  return 0; 
}

listing 16
void func1(int x[][10]) 
{ 
  /*...*/ 
}

listing 17
x[2][4]

listing 18
#include <conio.h> 
#include <ctype.h> 
#include <stdio.h> 
#include <stdlib.h> 
 
#define CLASSES  3 
#define GRADES  30 
int grade[CLASSES][GRADES]; 
 
void disp_grades(int g[][GRADES]), enter_grades(void); 
int get_grade(int num); 
 
int main(void)  /* class grades program */ 
{ 
  char ch; 
 
  for(;;) { 
    do { 
      printf("(E)nter grades\n"); 
      printf("(R)eport grades\n"); 
      printf("(Q)uit\n"); 
      ch = toupper(getche()); 
    } while(ch!='E' && ch!='R' && ch!='Q'); 
 
    switch(ch) { 
      case 'E': 
        enter_grades(); 
        break; 
      case 'R': 
        disp_grades(grade); 
        break; 
      case 'Q': 
        return 0; 
    } 
  } 
} 
 
/* Enter each student's grade. */ 
void enter_grades(void) 
{ 
  int t, i; 
 
  for(t=0; t<CLASSES; t++) { 
    printf("Class # %d:\n", t+1); 
    for(i=0; i<GRADES; ++i) 
      grade[t][i] = get_grade(i); 
  } 
} 
 
/* Actually input the grade. */ 
int get_grade(int num) 
{ 
  char s[80]; 
 
  printf("enter grade for student # %d:\n", num+1); 
  gets(s); 
  return(atoi(s)); 
} 
 
/* Display the class grades. */ 
void disp_grades(int g[][GRADES]) 
{ 
  int t, i; 
 
  for(t=0; t<CLASSES; ++t) { 
    printf("Class # %d:\n", t+1); 
    for(i=0; i<GRADES; ++i) 
      printf("grade for student #%d is %d\n",i+1, g[t][i]); 
  } 
}

listing 19
char str_array[30][80];

listing 20
gets(str_array[2]);

listing 21
gets(&str_array[2][0]);

listing 22
#include <stdio.h> 
 
#define MAX 100 
#define LEN 255 
 
char text[MAX][LEN]; 
 
/* A very simple text editor. */ 
int main(void) 
{ 
  register int t, i, j; 
 
  for(t=0; t<MAX; t++) { 
    printf("%d: ", t); 
    gets(text[t]); 
    if(!*text[t]) break; /* quit on blank line */ 
  } 
 
  /* this displays the text one character at a time */ 
  for(i=0; i<t; i++) { 
    for(j=0; text[i][j]; j++) putchar(text[i][j]); 
    putchar('\n'); 
  } 
 
  return 0; 
}

listing 23
  for(i=0; i<t; i++) 
    printf("%s\n", text[i]);

listing 24
int m[4][3][6][5];

listing 25
int func1(int d[][3][6][5]) 
{ 
  /*...*/}

listing 26
char p[10];

listing 27
p 
 
&p[0]

listing 28
p == &p[0]

listing 29
int *p, i[10]; 
p = i; 
p[5] = 100;  /* assign using index */ 
*(p+5) = 100; /* assign using pointer arithmetic */

listing 30
a 
&a[0][0]

listing 31
int num[10][10]; 
  /*...*/ 
void pr_row(int j) 
{ 
  int *p, t; 
 
  p = (int *) &num[j][0]; /* get address of first 
                             element in row j */ 
  for(t=0; t<10; ++t) printf("%d ", *(p+t)); 
}

listing 32
/* General */ 
void pr_row(int j, int row_dimension, int *p) 
{ 
  int t; 
 
  p = p + (j * row_dimension); 
  for(t=0; t<row_dimension; ++t) 
    printf("%d ", *(p+t)); 
}

listing 33
char *p; 
p = malloc(1000); /* get 1000 bytes */

listing 34
p = (char *) malloc(1000); /* get 1000 bytes */

listing 35
/* Print a string backwards using dynamic allocation. */ 
 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
 
int main(void) 
{ 
  char *s; 
  register int t; 
 
  s = (char *) malloc(80); 
 
  if(!s) { 
    printf("Memory request failed.\n"); 
    exit(1); 
  } 
 
  gets(s); 
  for(t=strlen(s)-1; t>=0; t--) putchar(s[t]); 
  free(s); 
 
  return 0; 
}

listing 36
int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

listing 37
char str[6] = "hello";

listing 38
char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};

listing 39
int sqrs[10][2] = { 
  1, 1, 
  2, 4, 
  3, 9, 
  4, 16, 
  5, 25, 
  6, 36, 
  7, 49, 
  8, 64, 
  9, 81, 
  10, 100 
};

listing 40
int sqrs[10][2] = { 
  {1, 1}, 
  {2, 4}, 
  {3, 9}, 
  {4, 16}, 
  {5, 25}, 
  {6, 36}, 
  {7, 49}, 
  {8, 64}, 
  {9, 81}, 
  {10, 100} 
};

listing 41
char e1[12] = "Read Error\n"; 
char e2[13] = "Write Error\n"; 
char e3[18] = "Cannot Open File\n";

listing 42
char e1[] = "Read Error\n"; 
char e2[] = "Write Error\n"; 
char e3[] = "Cannot Open File\n";

listing 43
printf("%s has length %d\n", e2, sizeof e2);

listing 44
int sqrs[][2] = { 
  1, 1, 
  2, 4, 
  3, 9, 
  4, 16, 
  5, 25, 
  6, 36, 
  7, 49, 
  8, 64, 
  9, 81, 
  10, 100 
};

listing 45
#include <stdio.h> 
#include <stdlib.h> 
 
/* A simple game of Tic-Tac-Toe. */ 
 
#define SPACE  ' ' 
 
char matrix[3][3] = {  /* the tic-tac-toe matrix */ 
  {SPACE, SPACE, SPACE}, 
  {SPACE, SPACE, SPACE}, 
  {SPACE, SPACE, SPACE} 
}; 
 
void get_computer_move(void), get_player_move(void); 
void disp_matrix(void); 
char check(void); 
 
int main(void) 
{ 
  char done; 
 
  printf("This is the game of Tic-Tac-Toe.\n"); 
  printf("You will be playing against the computer.\n"); 
 
  do { 
    disp_matrix();         /* display the game board */ 
    get_player_move();     /* get your move */ 
    done = check();        /* see if winner */ 
    if(done!=SPACE) break; /* winner! */ 
    get_computer_move();   /* get computer's move */ 
    done=check();          /* see if winner */ 
  } while(done==SPACE); 
  if(done=='X') printf("You won!\n"); 
  else printf("I won!!!!\n"); 
  disp_matrix(); /* show final positions */ 
 
  return 0; 
} 
 
/* Input the player's move. */ 
void get_player_move(void) 
{ 
  int x, y; 
 
  printf("Enter coordinates for your X.\n"); 
  printf("Row? "); 
  scanf("%d", &x); 
  printf("Column? "); 
  scanf("%d", &y); 
  x--; y--; 
  if(x<0 || y<0 || x>2 || y>2 || matrix[x][y]!=SPACE) { 
    printf("Invalid move, try again.\n"); 
    get_player_move(); 
  } 
  else matrix[x][y]='X'; 
} 
 
/* Get the computer's move */ 
void get_computer_move(void) 
{ 
  register int t; 
  char *p; 
 
  p = (char *) matrix; 
  for(t=0; *p!=SPACE && t<9; ++t) p++; 
  if(t==9)  { 
    printf("draw\n"); 
    exit(0); /* game over */ 
  } 
  else *p = 'O'; 
} 
 
/* Display the game board. */ 
void disp_matrix(void) 
{ 
  int t; 
 
  for(t=0; t<3; t++) { 
    printf(" %c | %c | %c ", matrix[t][0], 
      matrix[t][1], matrix [t][2]); 
    if(t!=2) printf("\n---|---|---\n"); 
  } 
   printf("\n"); 
} 
 
/* See if there is a winner. */ 
char check(void) 
{ 
  int t; 
  char *p; 
 
  for(t=0; t<3; t++) { /* check rows */ 
    p = &matrix[t][0]; 
    if(*p==*(p+1) && *(p+1)==*(p+2)) return *p; 
  } 
 
  for(t=0; t<3; t++) { /* check columns */ 
    p = &matrix[0][t]; 
    if(*p==*(p+3) && *(p+3)==*(p+6)) return *p; 
  } 
 
  /* test diagonals */ 
  if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) 
    return matrix[0][0]; 
 
  if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0]) 
    return matrix[0][2]; 
 
  return SPACE; 
}

⌨️ 快捷键说明

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