Standford University
C++抽象编程 斯坦福大学中级编程课
http://web.stanford.edu/class/cs106b/
第八章 迭代策略 (Recursive Strategies)
Psychologically, however, the important thing is to avoid asking that question altogether.
例题,移动环,每次移动一个,大的不能在小的上面。
#include <iostream>
using namespace std;
void moveTower(int n, char start, char finish, char tmp);
void moveSingleDisk(char start, char finish);
int main() {
int n = 5;
moveTower(n, 'A', 'B', 'C');
return 0;
}
void moveTower(int n, char start, char finish, char tmp) {
if (n == 1) {
moveSingleDisk(start, finish);
} else {
moveTower(n - 1, start, tmp, finish);
moveSingleDisk(start, finish);
moveTower(n - 1, tmp, finish, start);
}
}
void moveSingleDisk(char start, char finish) {
cout << start << " -> " << finish << endl;
}
1. Overivew of C++
C++概述
1.1 Your first C++ program
讲述了hello world的输出,照着书上的代码敲。
1.2 The history of C++
主要讲面向对象的发展历史,C++主要在C的基础上发展而来。
source file -> object file (compiler) -> executable file (linker)
1.3 The structure of C++ program
program comments
Library inclusions
Function prototype
Main program
Function comment
Function definition
1.4 Variables
局部变量和全局变量
1.5 数据类型
1.6 表达式
1.7 语句
总结
这部分主要讲述C++基础,内容从各小节的标题可以看出来,以后不再复习此类基础。
复习题
1. source file
2. /* */ //
3. <> system library, "" user library
4. const double CENTIMETERS_PER_INCH = 2.54;
5. main, return
6. next line
7. name 名字,type 类型,value,数值,scope,作用域
8. a b c e f h i j k l
9. field operation
10. short <= int <= long
11. 美国字符数值表
12. true false
13. #include <iostream> double x;
14. cout << "i " << + i << " d " << d
<< " c " << c << " s " << s;
15. int 5, int 3, double 3.8
double 18, int 4, int 2
16. 正负值,减法操作
17. 去掉小数部分
18. type()
19. 4, 2, 42, 42
20. =
21. ++x先加1再使用,x++先使用再加1
22. 字母缩减
23. if(statement) {}
switch(statement) { case: break; default: ;}
while(statement) {}
for(init, test, s) {}
24. switch只能是整形变量,每个case后有个break防止进入下一个case,
末尾可以有default,在没有任何case匹配的情况下执行。
25. 特殊的记号
26. for (int i = 0; i <= 100; i++)
for (int i = 0; i < 100; i+=7)
for (int i = 100; i >= 0; i-=2)
/* Chapter 1 exercises */
#include <iostream>
#include "console.h"
#include <math.h>
using namespace std;
int digitReverse(int n);
bool isPrime(int n);
void hailstoneSequence(int n);
int main() {
/* exercise 1
// reads in a temperature in degrees Celsius and
// displays the corresponding temperature in degrees Fahrenheit.
cout << "Enter a temperature in degrees Celsius: ";
double celsius = 0;
cin >> celsius;
double fahrenheit = celsius * 9.0 / 5 + 32;
cout << "Corresponding temperature in degrees Fahrenheit: " << fahrenheit << endl;
return 0;
*/
/* exercise 2
// convert a distance in meters to the corresponding English distance in feet and inches.
// 1 inch = 0.0254 meters
// 1 foot = 12 inch
cout << "Enter a distance in meters: ";
double meters = 0;
cin >> meters;
int inches = int (meters / 0.0254);
int feet = inches / 12;
inches = inches % 12;
cout << "Distance in feet and inches: " << feet << " feet, " << inches << " inches" << endl;
return 0;
*/
/* exercise 3
// compute the sum of the numbers between 1 and 100.
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
cout << "the sum of the numbers between 1 and 100: " << sum << endl;
return 0;
*/
/* exercise 4
// read in a positive integer N and then calculates and displays the
// sum of the first N odd integers.
cout << "Enter a positive integer: ";
int n, sum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
sum += 2 * i + 1;
}
cout << "the sum of the first N odd integers: " << sum << endl;
return 0;
*/
/* exercise 5 & 6
cout << "This program finds the largeset integer in a list." << endl;
cout << "Enter 0 to signal the end of the list." << endl;
int largest = 0;
int larger = 0;
int sentinel = 0;
int number;
while (true) {
cout << "? ";
cin >> number;
if (number > largest) {
larger = largest;
largest = number;
}
if (number == sentinel) break;
}
cout << "The largest value was " << largest << ".\n";
cout << "The second largest value was " << larger << ".\n";
return 0;
*/
/* exercise 7
const int SENTINEL = -1;
cout << "This program adds a list of numbers." << endl;
cout << "Use " << SENTINEL << " to signal the end." << endl;
int total = 0;
int count = 0;
while (true) {
int value;
cout << " ? ";
cin >> value;
if (value == SENTINEL) break;
count++;
total += value;
}
cout << "The average grade of student is: " << total / count << endl;
return 0;
*/
/* exercise 8
cout << "This program reverses the digits in an integer." << endl;
cout << "Enter a positive integer: ";
int number;
cin >> number;
cout << "The reversed integer is " << digitReverse(number) << endl;
return 0;
*/
/* exercise 9
cout << "This program factors a number." << endl;
cout << "Enter number to be factored: ";
int number;
cin >> number;
int max = number;
for (int i = 2; i <= max; i++) {
while (number % i == 0) {
cout << i;
number = number / i;
if (number != 1) cout << " * ";
}
}
return 0;
*/
/* exercise 10
cout << "Enter a number: ";
int number;
cin >> number;
hailstoneSequence(15);
return 0;
*/
/*
double sum =0;
bool flag = true;
for ( int i = 0; i < 10000; i++) {
if (flag) {
sum += 1.0/(2*i + 1);
flag = false;
} else {
sum -= 1.0/(2*i + 1);
flag = true;
}
}
cout << "PI = " << sum * 4 << endl;
*/
double sum = 0;
for (int i = 0; i < 10000; i++) {
sum += (1.0/10000) * sqrt(1.0 - 1.0 / 10000 * 1.0 / 10000 * i * i);
}
cout << "PI = " << sum * 4 << endl;
return 0;
}
bool isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
int digitReverse(int n) {
int reverse = 0;
while (n > 0) {
reverse *= 10;
reverse += n % 10;
n /= 10;
}
return reverse;
}
bool isOdd(int n) {
if (n % 2 == 1) return true;
return false;
}
void hailstoneSequence(int n) {
while (n != 1) {
if (isOdd(n)) {
cout << n << " is odd, so I multiply by 3 and add 1 to get ";
n = n * 3 + 1;
cout << n << endl;
} else {
cout << n << " is even, so I divide it by 2 to get ";
n = n / 2;
cout << n << endl;
}
}
}
/*
* output
*
* exercise 1
* Enter a temperature in degrees Celsius: 23
* Corresponding temperature in degrees Fahrenheit: 73.4
*
* exercise 2
* Enter a distance in meters: 52
* Distance in feet and inches: 170 feet, 7 inches
*
* exercise 3
* the sum of the numbers between 1 and 100: 5050
*
* exercise 4
* Enter a positive integer: 4
* the sum of the first N odd integers: 16
*
* exercise 5
* This program finds the largeset integer in a list.
* Enter 0 to signal the end of the list.
* ? 17
* ? 42
* ? 11
* ? 19
* ? 35
* ? 0
* The largest value was 42.
*
* exercise 6
* This program finds the largeset integer in a list.
* Enter 0 to signal the end of the list.
* ? 223
* ? 251
* ? 317
* ? 636
* ? 766
* ? 607
* ? 607
* ? 0
* The largest value was 766.
* The second largest value was 636.
*
* exercise 7
* This program adds a list of numbers.
* Use -1 to signal the end.
* ? 1
* ? 2
* ? 3
* ? -1
* The average grade of student is: 2
*
* exercise 8
* This program reverses the digits in an integer.
* Enter a positive integer: 123456789
* The reversed integer is 987654321
*
* exercise 9
* This program factors a number.
* Enter number to be factored: 60
* 2 * 2 * 3 * 5
*
* exercise 10
* Enter a number: 15
* 15 is odd, so I multiply by 3 and add 1 to get 46
* 46 is even, so I divide it by 2 to get 23
* 23 is odd, so I multiply by 3 and add 1 to get 70
* 70 is even, so I divide it by 2 to get 35
* 35 is odd, so I multiply by 3 and add 1 to get 106
* 106 is even, so I divide it by 2 to get 53
* 53 is odd, so I multiply by 3 and add 1 to get 160
* 160 is even, so I divide it by 2 to get 80
* 80 is even, so I divide it by 2 to get 40
* 40 is even, so I divide it by 2 to get 20
* 20 is even, so I divide it by 2 to get 10
* 10 is even, so I divide it by 2 to get 5
* 5 is odd, so I multiply by 3 and add 1 to get 16
* 16 is even, so I divide it by 2 to get 8
* 8 is even, so I divide it by 2 to get 4
* 4 is even, so I divide it by 2 to get 2
* 2 is even, so I divide it by 2 to get 1
*
* exercise 11
* PI = 3.14149
*
* exercise 12
* PI = 3.14179
*/
第二章 函数与库
2.1 The idea of a function
2.2 Libraries
2.3 Defining functions in C++
2.4 The mechanics of function calls
2.5 Reference parameters
2.6 Interfaces and implementations
2.7 Principles of interface design
2.8 Designing a random number library
2.9 Introduction to the Standard libraries
主要介绍函数与库的使用,基本原理都已经掌握。
复习题
1. 函数是执行某些操作的模块,程序是运行在机器上的实例。
2. 调用函数,函数参数,返回值。
3. fallse
4. double sqrt(double d);
5. yes
6. bool function
7. 函数名相同,参数类型或者个数不相同。
8. 声明中等于默认值。
9. false
10. 调用函数时的保存参数的堆栈。
11. 函数括号内的参数。
12. 只在本函数内可以使用。
13. 调用时不进行copy,直接使用原来的值进行操作。
14. &
15. 用户,实现,接口
16. #ifndef _mylib_h
#define _mylib_h
#endif
17. 声明和实现中都需要export
18. 函数调用的基础。
19. 用户不用改自己的程序。
20. 生成随机数。
21. 整形数据的最大值。
22. normalize, scale, translate, convert.
23. randomInteger(1, 100);
24. -5 ~ 5
25. no
26. true
27. 设置初始值。
28. 每次初始值不变。
29. 生成随机整形,浮点型数据,以及可以设置初始值
combinatorics.h
#ifndef COMBINATORICS_H
#define COMBINATORICS_H
int combinations(int n, int k);
int permutations(int n, int k);
#endif // COMBINATORICS_H
combinatorics.cpp
#include "combinatorics.h"
int fact(int n) {
int result = 1;
for (int i = 1; i <=n; i++) {
result *= i;
}
return result;
}
int combinations(int n, int k)
{
return fact(n) / (fact(k) * fact(n - k));
}
int permutations(int n, int k)
{
int multi = 1;
for (int i = n; i > k; i--) {
multi *= i;
}
return multi;
}
#include <iostream>
#include "combinatorics.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Test combinatorics: " << endl;
cout << combinations(5, 2) << endl;
cout << permutations(5, 2) << endl;
return 0;
}
/*
* output
* Test combinatorics:
* 10
* 60
*/
#ifndef CALENDAR_H
#define CALENDAR_H
#include <string>
enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
};
int daysInMonth(Month month, int year);
bool isLeapYear(int year);
std::string monthToString(Month month);
#endif // CALENDAR_H
#include "calendar.h"
#include <string>
#include <iostream>
int daysInMonth(Month month, int year)
{
switch (month) {
case 0:
return 31;
case 1:
if (isLeapYear(year))
return 29;
else
return 28;
case 2:
return 31;
case 3:
return 30;
case 4:
return 31;
case 5:
return 30;
case 6:
return 31;
case 7:
return 31;
case 8:
return 30;
case 9:
return 31;
case 10:
return 30;
case 11:
return 31;
default:
std::cout << "invalid month.";
break;
}
}
bool isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
std::string monthToString(Month month)
{
switch (month) {
case 0:
return "JANUARY";
case 1:
return "FEBRUARY";
case 2:
return "MARCH";
case 3:
return "APRIL";
case 4:
return "MAY";
case 5:
return "JUNE";
case 6:
return "JULY";
case 7:
return "AUGUST";
case 8:
return "SEPTEMBER";
case 9:
return "OCTOBER";
case 10:
return "NOVEMBER";
case 11:
return "DECEMBER";
default:
std::cout << "invalid month";
break;
}
}
#include <iostream>
#include "calendar.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Enter a year: ";
int year;
cin >> year;
for (int i = 0; i < 12; i++) {
cout << monthToString(Month(i)) << "has " << daysInMonth(Month(i), year) << " days." << endl;
}
}
/*
* output
* Enter a year: 2014
* JANUARYhas 31 days.
* FEBRUARYhas 28 days.
* MARCHhas 31 days.
* APRILhas 30 days.
* MAYhas 31 days.
* JUNEhas 30 days.
* JULYhas 31 days.
* AUGUSThas 31 days.
* SEPTEMBERhas 30 days.
* OCTOBERhas 31 days.
* NOVEMBERhas 30 days.
* DECEMBERhas 31 days.
*/
#include <iostream>
#include <math.h>
#include <iomanip>
#include <error.h>
#include <cstdlib>
#include "console.h"
#include "random.h"
#include "gwindow.h"
using namespace std;
double celsiusToFahrenheit(double c);
void meterToInchAndFoot(double meter, int &foot, int &inch);
int roundToNearestInt(double x);
double windChill(double t, double v);
bool isPerfect(int number);
bool isPrime(int number);
double sqrtN(double x);
void findEaster(int year, string & month, int & day);
// int permutations(int n, int k);
/* Main program */
int main() {
/* exercise 1
// reads in a temperature in degrees Celsius and
// displays the corresponding temperature in degrees Fahrenheit.
cout << "Enter a temperature in degrees Celsius: ";
double celsius = 0;
cin >> celsius;
double fahrenheit = celsiusToFahrenheit(celsius);
cout << "Corresponding temperature in degrees Fahrenheit: " << fahrenheit << endl;
return 0;
*/
/*
// convert a distance in meters to the corresponding English distance in feet and inches.
// 1 inch = 0.0254 meters
// 1 foot = 12 inch
cout << "Enter a distance in meters: ";
double meters = 0;
cin >> meters;
int foot, inch;
meterToInchAndFoot(meters, foot, inch);
cout << "Distance in feet and inches: " << foot << " feet, " << inch << " inches" << endl;
return 0;
*/
/* exercise 3
cout << "Enter a float number: ";
double number;
cin >> number;
cout << "The nearest integer number is " << roundToNearestInt(number) << endl;
return 0;
*/
/* exercise 4
for (double v = 5; v <= 60; v += 5 ) {
for (double t = 40; t >= -45; t -= 5) {
cout << setw(4) << roundToNearestInt(windChill(t, v));
}
cout << endl;
}
*/
/* exercise 5
for (int i = 1; i <= 9999; i ++) {
if (isPerfect(i)) cout << i << endl;
}
*/
/* exercise 6
for (int i = 0; i < 100; i++) {
if (isPrime(i)) cout << i << " ";
}
cout << endl;
*/
/* exercise 7
cout << "Enter a number: ";
double number;
cin >> number;
cout << "The square root is " << sqrtN(number) << endl;
cout << "The standard library square root is " << sqrt(number) << endl;
return 0;
*/
/* exercise 8
cout << "Enter a year(1700-1899): ";
int year;
cin >> year;
string month;
int day;
findEaster(year, month, day);
cout << "The date of Easter: " << month << " " << day << endl;
return 0;
*/
/* exercise 9
cout << "Choose k values from a set of n elements p(n, k): ";
int n, k;
cin >> n >> k;
cout << "permutations: " << permutations(n, k);
return 0;
*/
/*
* Finsh exercise 10 and exercise 11 in other files.
*/
/* exercise 12
cout << "Enter a number you want to trials: ";
int number;
cin >> number;
double sum = 0;
for (int i = 0; i < number; i++) {
sum += randomReal(0, 1);
}
cout << "Average fater a specified number: " << sum /number;
return 0;
*/
/*
* exercise 13
* a sample that contains 10000 atoms of radioactive material
*/
/*
int year = 0;
int total = 10000;
cout << "There are 10000 atoms initially." << endl;
while (total != 0) {
int left = 0;
for (int i = 0; i < total; i++) {
if (randomChance(0.5)) left++;
}
total = left;
year++;
cout << "There are " << total << " atoms at the end of year " << year << "." << endl;
}
*/
/* exercise 14
int count = 0;
for (int i = 0; i < 10000; i++) {
double a = randomReal(-1, 1);
double b = randomReal(-1, 1);
if (sqrt(a*a + b*b) < 1) count++;
}
cout << "fall within the circle: " << count/10000.0;
*/
/* exercise 15
int number = 0;
while (true) {
number++;
if (randomChance(0.5)) {
cout << "heads" << endl;
number++;
if (randomChance(0.5)) {
cout << "heads" << endl;
number++;
if (randomChance(0.5)) {
cout << "heads" << endl;
cout << "It took " << number << " filps to get 3 consecutive heads." << endl;
return 0;
} cout << "tails " << endl;
} else
cout << "tails" << endl;
}
else
cout << "tails" << endl;
}
*/
/* exercise 16
GWindow gw(400, 200);
double width = gw.getWidth();
double height = gw.getHeight();
gw.setColor("CYAN");
gw.fillRect(0, 0, width, height);
gw.setColor("RED");
gw.fillOval(-width/8, height/8-10, width*1.25, height*1.25);
gw.setColor("ORANGE");
gw.fillOval(-width/8, height/8*2-10, width*1.25, height*1.25);
gw.setColor("YELLOW");
gw.fillOval(-width/8, height/8*3-10, width*1.25, height*1.25);
gw.setColor("GREEN");
gw.fillOval(-width/8, height/8*4-10, width*1.25, height*1.25);
gw.setColor("BLUE");
gw.fillOval(-width/8, height/8*5-10, width*1.25, height*1.25);
gw.setColor("MAGENTA");
gw.fillOval(-width/8, height/8*6-10, width*1.25, height*1.25);
gw.setColor("CYAN");
gw.fillOval(-width/8, height/8*7-10, width*1.25, height*1.25);
*/
/* exercise 17
GWindow gw(240, 240);
double width = gw.getWidth();
double height = gw.getHeight();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i+j)%2 != 0) {
gw.setColor("CYAN");
gw.fillRect(j*width/8, i*height/8, width/8, height/8);
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
if ((i+j)%2 != 0) {
gw.setColor("RED");
gw.fillOval(j*width/8 + width/80, i*height/8 + height/80, width/10, height/10);
}
}
}
for (int i = 5; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i+j)%2 != 0) {
gw.setColor("BLACK");
gw.fillOval(j*width/8 + width/80, i*height/8 + height/80, width/10, height/10);
}
}
}
return 0;
*/
GWindow gw(300, 300);
gw.setTitle("Taoist philosophy");
double width = gw.getWidth();
double height = gw.getHeight();
gw.setColor("BLACK");
gw.fillOval(15, 15, width -30, height - 30);
gw.setColor("White");
gw.fillRect(0, 0, width/2, height);
gw.setColor("BLACK");
gw.drawOval(15, 15, width - 30, height -30);
// big circle
gw.setColor("WHITE");
gw.fillOval(7.5+width/4, 15, (width-30)/2, (height-30)/2);
gw.setColor("BLACK");
gw.fillOval(7.5+width/4, height/2, (width-30)/2, (height-30)/2);
// small circle
gw.setColor("BLACK");
gw.fillOval(width/2 - 15, height/4 -15, 15, 15);
gw.setColor("WHITE");
gw.fillOval(width/2 - 15, height/4*3 - 15, 15, 15);
return 0;
}
/*
int permutations(int n, int k) {
int multi = 1;
for (int i = n; i > k; i--) {
multi *= i;
}
return multi;
}
*/
void findEaster(int year, string & month, int & day) {
int a = (year / 19 % 2 == 0) ? 0 : year % 19;
int b = (year / 4 % 2 == 0) ? 0 : year % 4;
int c = (year / 7 % 2 == 0) ? 0 : year % 7;
int d = (19 * a + 23) % 30;
int e;
if (year >= 1700 & year <= 1799) {
e = (2 * b + 4 * c + 6 * d + 3) % 7;
}
if (year >= 1800 & year <= 1899) {
e = (2 * b + 4 * c + 6 * d + 4) % 7;
}
if (d + e > 9) {
month = "April";
day = d + e - 9;
} else {
month = "March";
day = 22 + d + e;
}
}
double sqrtN(double x) {
double k = x/2;
while (fabs(k*k - x) > 1e-9) {
k = 0.5*(k+x/k);
}
return k;
}
bool isPrime(int number) {
for (int i = 2; i < number; i++) {
if (number % i == 0) return false;
}
return true;
}
bool isPerfect(int number) {
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i;
}
}
if (sum == number) return true;
return false;
}
double windChill(double t, double v) {
if (v == 0) return t;
if (t > 40) error("undefined");
return 35.74 + 0.6215 * t - 35.75 * pow(v, 0.16) + 0.4275 * t * pow(v, 0.16);
}
double celsiusToFahrenheit(double celsius) {
return celsius * 9.0 / 5 + 32;
}
void meterToInchAndFoot(double meter, int &foot, int &inch) {
inch = int (meter / 0.0254);
foot = inch / 12;
inch = inch % 12;
}
int roundToNearestInt(double x) {
if ( x > 0)
return (int)(x + 0.5);
else
return (int)(x - 0.5);
}
/*
* output
*
* exercise 1
*Enter a temperature in degrees Celsius: 32
*Corresponding temperature in degrees Fahrenheit: 89.6
*
* exercise 2
* Enter a distance in meters: 32
* Distance in feet and inches: 104 feet, 11 inches
*
* exercise 3
* Enter a float number: 0.5
* The nearest integer number is 1
* Enter a float number: -0.4
* The nearest integer number is 0
*
* exercise 4
* 36 31 25 19 13 7 1 -5 -11 -16 -22 -28 -34 -40 -46 -52 -57 -63
* 34 27 21 15 9 3 -4 -10 -16 -22 -28 -35 -41 -47 -53 -59 -66 -72
* 32 25 19 13 6 0 -7 -13 -19 -26 -32 -39 -45 -51 -58 -64 -71 -77
* 30 24 17 11 4 -2 -9 -15 -22 -29 -35 -42 -48 -55 -61 -68 -74 -81
* 29 23 16 9 3 -4 -11 -17 -24 -31 -37 -44 -51 -58 -64 -71 -78 -84
* 28 22 15 8 1 -5 -12 -19 -26 -33 -39 -46 -53 -60 -67 -73 -80 -87
* 28 21 14 7 0 -7 -14 -21 -27 -34 -41 -48 -55 -62 -69 -76 -82 -89
* 27 20 13 6 -1 -8 -15 -22 -29 -36 -43 -50 -57 -64 -71 -78 -84 -91
* 26 19 12 5 -2 -9 -16 -23 -30 -37 -44 -51 -58 -65 -72 -79 -86 -93
* 26 19 12 4 -3 -10 -17 -24 -31 -38 -45 -52 -60 -67 -74 -81 -88 -95
* 25 18 11 4 -3 -11 -18 -25 -32 -39 -46 -54 -61 -68 -75 -82 -89 -97
* 25 17 10 3 -4 -11 -19 -26 -33 -40 -48 -55 -62 -69 -76 -84 -91 -98
*
* exercise 5
* 6
* 28
* 496
* 8128
*
* exercise 6
* 0 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*
* exercise 7
* Enter a number: 2
* The square root is 1.41421
* The standard library square root is 1.41421
*
* exercise 8
* Enter a year(1700-1899): 1755
* The date of Easter: April 15
*
* exercise 9
* Choose k values from a set of n elements p(n, k): 3 2
* permutations: 3
*
*
* exercise 12
* Enter a number you want to trials: 3
* Average fater a specified number: 0.619592
*
* exercise 13
* There are 10000 atoms initially.
* There are 4961 atoms at the end of year 1.
* There are 2418 atoms at the end of year 2.
* There are 1201 atoms at the end of year 3.
* There are 594 atoms at the end of year 4.
* There are 281 atoms at the end of year 5.
* There are 134 atoms at the end of year 6.
* There are 60 atoms at the end of year 7.
* There are 32 atoms at the end of year 8.
* There are 10 atoms at the end of year 9.
* There are 6 atoms at the end of year 10.
* There are 2 atoms at the end of year 11.
* There are 1 atoms at the end of year 12.
* There are 0 atoms at the end of year 13.
*
* exercise 14
* fall within the circle: 0.783
*
* exercise 15
* tails
* tails
* heads
* tails
* heads
* tails
* heads
* tails
* heads
* heads
* heads
* It took 11 filps to get 3 consecutive heads.
*
* exercise 16
* graphics display, done!
*
* exercise 17
* graphics display, done!
*
* exercise 18
* graphics display, done!
*
*/
第三章 String类
3.1 Using strings as abstract values
3.2 String operations
3.3 The <cctype> library
3.4 Modifying the contents of a string
3.5 The legacy of C-style strings
3.6 Writing string applictions
3.7 The strlib.h library
本章主要介绍String类,介绍类中常用的方法。
练习题
1. 字符表示一个字母,字符串表示一串字符2. false
3. getline(cin, string);
4. 方法属于类中的方法,自由函数可以随意调用。
5. false, str.length();
6. s1
7. 两个字符串连接。
8. 每个字母出现的先后顺序。
9. string.at(i), string[i] 函数与字符重载
10