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

📄 chap9.lst

📁 一个类c++语言解释器
💻 LST
字号:
int i, j; // global vars  
char ch; 
 
int main() 
{ 
  int i, j; // local vars  
 
  // Call a "standard library' function. 
  cout << "Mini C++ Demo Program.\n\n"; 
 
  // Call a programmer-defined function. 
  print_alpha(); 
 
  cout << "\n"; 
 
  // Demonstrate do and for loops. 
  cout << "Use loops.\n"; 
  do { 
    cout << "Enter a number (0 to quit): "; 
    cin >> i; 
 
    // Demonstrate the if 
    if(i < 0 ) { 
      cout << "Numbers must be positive, try again.\n"; 
    } 
    else { 
      for(j = 0; j <= i; ++j) { 
        cout << j << " summed is "; 
        cout << sum(j) << "\n"; 
      } 
    } 
  } while(i != 0); 
 
  cout << "\n"; 
 
  // Demonstrate the break in a loop. 
  cout << "Break from a loop.\n"; 
  for(i=0; i < 100; i++) { 
    cout << i << "\n"; 
    if(i == 5) { 
      cout << "Breaking out of loop.\n"; 
      break; 
    } 
  } 
 
  cout << "\n"; 
 
  // Demonstrate the switch 
  cout << "Use a switch.\n"; 
  for(i=0; i < 6; i++) { 
    switch(i) { 
      case 1: // can stack cases 
      case 0: 
        cout << "1 or 0\n"; 
        break; 
      case 2: 
        cout << "two\n"; 
        break; 
      case 3: 
        cout << "three\n"; 
        break; 
      case 4: 
        cout << "four\n"; 
        cout << "4 * 4 is "<< 4*4 << "\n"; 
//        break; // this break is optional 
      // no case for 5 
    } 
  } 
  cout << "\n"; 
 
  cout << "Use a library function to generate "  
       << "10 random integers.\n"; 
  for(i=0; i < 10; i++) { 
    cout << rand() << " "; 
  } 
 
  cout << "\n"; 
  cout << "Done!\n"; 
 
  return 0; 
} 
 
// Sum the values between 0 and num. 
// This function takes a parameter. 
int sum(int num) 
{ 
  int running_sum; 
 
  running_sum = 0; 
 
  while(num) { 
    running_sum = running_sum + num; 
    num--; 
  } 
  return running_sum; 
} 
 
// Print the alphabet. 
int print_alpha() 
{ 
  cout << "This is the alphabet:\n"; 
 
  for(ch = 'A'; ch<='Z'; ch++) { 
    putchar(ch); 
  } 
  cout << "\n"; 
 
  return 0; 
}

listing 6
// Nested loop example. 
int main() 
{ 
  int i, j, k; 
 
  for(i = 0; i < 5; i = i + 1) { 
    for(j = 0; j < 3; j = j + 1) { 
      for(k = 3; k ; k = k - 1) { 
        cout << i <<", "; 
        cout << j << ", "; 
        cout << k << "\n"; 
      } 
    } 
  } 
 
  cout << "done"; 
 
  return 0; 
}

listing 7
// Assigments as operations. 
int main() 
{ 
  int a, b; 
 
  a = b = 5; 
 
  cout << a << " " << b << "\n"; 
 
  while(a=a-1) { 
    cout << a << " ";  
    do { 
      cout << b << " "; 
    } while((b=b-1) > -5); 
    cout << "\n"; 
  } 
 
  return 0; 
}

listing 8
// This program demonstrates a recursive function. 
 
// A recursive function that returns the 
// factorial of i. 
int factr(int i) 
{ 
  if(i<2) { 
    return 1; 
  } 
  else { 
     return i * factr(i-1); 
  } 
} 
 
int main() 
{ 
  cout << "Factorial of 4 is: "; 
  cout << factr(4) << "\n"; 
 
  cout << "Factorial of 6 is: "; 
  cout << factr(6) << "\n"; 
 
  return 0; 
}

listing 9
// A more rigorous example of function arguments.  
 
int f1(int a, int b) 
{ 
  int count; 
 
  cout << "Args for f1 are "; 
  cout << a << " " << b << "\n"; 
 
  count = a; 
  do { 
    cout << count << " "; 
  } while(count=count-1); 
 
  cout << a << " " << b 
       << " " << a*b << "\n"; 
 
  return a*b; 
} 
 
int f2(int a, int x, int y) 
{ 
  cout << "Args for f2 are "; 
  cout << a << " " << x << " " 
       << y << "\n"; 
  cout << x / a << " "; 
  cout << y*x << "\n"; 
 
  return 0; 
} 
 
int main() 
{ 
  f2(10, f1(10, 20), 99); 
 
  return 0; 
}

listing 10
// Exercise the loop statements. 
int main() 
{ 
  int a; 
  char ch; 
 
  // The while. 
  cout << "Enter a number: "; 
  cin >> a; 
  while(a) { 
    cout << a*a << " "; 
    --a; 
  } 
  cout << "\n"; 
 
  // The do-while. 
  cout << "\nEnter characters, 'q' to quit.\n"; 
  do { 
    // Use two "standard library" functions. 
    ch = getchar(); 
    putchar(ch); 
  } while(ch != 'q'); 
  cout << "\n\n"; 
 
  // the for. 
  for(a=0; a<10; ++a) { 
     cout << a << " "; 
  } 
 
  cout << "\n\nDone!\n"; 
 
  return 0; 
}

listing 11
// Demonstrate nested scopes. 
 
int x; // global x 
 
int main() 
{ 
  int i; 
 
  i = 4; 
 
  x = 99; // global x is 99 
 
  if(i == 4) { 
    int x; // local x 
    int num; // local to if statement 
 
    x = i * 2; 
    cout << "Outer local x before loop: " 
         << x << "\n"; 
 
    while(x--) { 
      int x; // another local x 
 
      x = 18; 
      cout << "Inner local x: " << x << "\n"; 
    }  
 
    cout << "Outer local x after loop: " 
         << x << "\n"; 
  } 
 
  // Can't refer to num here because it is local 
  // to the preceding if block. 
//  num = 10;  
 
  cout << "Global x: " << x << "\n"; 
}

⌨️ 快捷键说明

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