第0章
0-0 编译并运行Hello, world! 程序。
#include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }
0-1 下面的表达式是做什么的?
3+4
计算3+4,结果为7
0-2 编写一个程序,使它在运行时输出:
This (*) is a quote , and this (\) is a backlash.
#include <iostream> using namespace std; int main() { cout << "This (\*) is a quote , and this (\\) is a backlash" << endl; return 0; }
0-3 字符串直接量"\t"代表一个水平制表符;不同的C++实现以不同的形式显示制表符。在你的实现中实验一下,看它是怎样处理制表符的。
\t处理为4个空格
0-4 编写一个程序,运行时以Hello, world!程序作为这个程序输出。
#include <iostream> using namespace std; int main() { cout << "#include <iostream>\n" << "using namespace std;\n" << "int main()\n" << "{\n" << "\tcout << \"Hello, world!\" << endl;\n" << "\treturn 0;\n" << "}\";\n" << "return 0;\n"; return 0; }
0-5 下面的程序是一个有效的程序吗?说出理由。
#include <iostream> int main() std::cout << "Hello, world!" << std::endl;
这是一个无效程序,因为函数的函数体必须用花括号括起来,就算函数的函数体只有一条语句,也必须用花括号括住它。
0-6 下面的程序是一个有效的程序吗?说出理由。
#include <iostream> int main() {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}}
这是一个有效程序,一般来说,函数必须包含至少一条的return语句,而且函数的最后一定要有return语句,但main比较特殊,它可以没有返回语句,若果这样,编译器就会假设它返回0。
0-7 那下面的这个程序呢?
#include <iostream> int main() { /*这是一个注释,因为我们使用了/*和*/来作为它的定界符, 所以它占据了几行的范围*/ std::cout << "Does this work?" << std::endl; return 0; }
无效程序,注释有误,注释在前一个结束符*/就结束了,所以后面的内容都未能注释。
0-8 ······这个呢?
#include <iostream> int main() { //这是一个注释,它占据了几行的范围 //在这里,我们使用了//而不是/* //和*/来为注释定界 std::cout << "Does this work?" << std::endl; return 0; }
有效程序,单行注释使用后,后面的多行注释符号不在起作用。
0-9 最短的有效程序是什么?
int main() {}
0-10 重写Hello, workd! 程序,让程序中每一个允许出现空白符的地方都换行。
#include<iostream> #include<conio.h> int main() { std::cout << "hello, world!" << std::endl; return 0; }
第1章
1-0 编译、允许并测试本章中的程序。
#include "stdafx.h" #include<iostream> #include<string> int main() { std::cout <<"Pleause enter your first name: "; std::string name; std::cin >> name; std::cout << "Hello, " << name << "!" << std::endl; return 0; }
#include "stdafx.h" #include<iostream> #include<string> int main() { std::cout << "Please enter your first name: "; std::string name; std::cin >> name; const std::string greeting = "Hello," + name + "!"; const std::string spaces(greeting.size(),' '); const std::string second = "*" + spaces + "*"; const std::string first(second.size(),'*'); std::cout << std::endl; std::cout <<first << std::endl; std::cout <<second << std::endl; std::cout << "*" << greeting << "*" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0; }
1-1 以下的定义有效吗?理由是什么?
const std::string hello = "Hello"; const std::string message = hello + ", world" + "!";
a) 定义有效,可以将一个字符串直接量直接赋给一个字符串变量
b) 定义有效,可以用 + 将一个字符串和一个字符串直接量连接起来
1-2 以下的定义有效吗?理由是什么?
const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam;
a) 定义有效
b) 定义无效,”Hello” 和 “world”都是字符串直接量,用+操作符是不合法的,因为其中一个必须std::string对象才是合法的。
1-3 以下的定义有效吗?如果有效的话,它是做什么的?如果无效,为什么呢?
#include<iostream> #include<string> int main() { {const std::string s = "a string"; std::cout << s << std::endl;} {const std::string s = "another string"; std::cout << s << std::endl;} return 0; }
有效,s分别为每对花括号内的局部变量。程序输出了两个字符串
1-4 下面的这个程序又怎样呢?如果我们把倒数第三行的 }} 改成 };} 的话,会出现什么情况呢?
#include "stdafx.h" #include<iostream> #include<string> int main() { {const std::string s = "a string"; std::cout << s << std::endl; {const std::string s = "another string"; //std::cout << s << std::endl;}} std::cout << s << std::endl;} ;} return 0; }
有效。内部作用域嵌套在外部作用域,内部作用域的s隐藏了外部作用域的s,所以程序是有效的,改变之后仍然有效。
1-5 下面这个程序呢?如果有效,它是做什么的?如果无效,说出理由,然后把它改写成有效的程序。
#include "stdafx.h" #include<iostream> #include<string> int main() { {std::string s = "a string"; {std::string x = s+ ", really"; std::cout << s << std::endl;} std::cout << x << std::endl; // x is out of its scope here } return 0; }
无效,x是在内部作用域定义的,出了内部作用域后,x便是不可用的。去掉内部的花括号,程序就变为有效程序。
1-6 在下面的程序向你发出输入请求的时候,如果你输入了两个名字(例如,Samuel Beckett),它会怎么样处理呢?在运行程序之前先预测一下结果,然后上机测一下。
#include "stdafx.h" #include<iostream> #include<string> int main() { std::cout << "What is your name? "; std::string name; std::cin >> name; std::cout << "Hello, " << name << std::endl << "And what is yours? "; std::cin >> name; std::cout << "Hello, " << name << ";nice to meet you roo!" << std::endl; return 0; }
第2章
2-0 编译并运行我们在本章中介绍的程序。
#include "stdafx.h" #include<iostream> #include<string> using std::cin; using std::cout; using std::endl; using std::string; int main() { cout <<"please enter your first name:"; string name; cin >> name; const string greeting ="hello," + name +"!"; const int pad = 1; const int rows = pad*2 + 3; const string::size_type cols = greeting.size() + pad*2 + 2; cout << endl; for (int r = 0; r !=rows; r++) { string::size_type c = 0; while (c != cols) { if (r == pad+1 && c == pad+1) { cout << greeting; c += greeting.size(); }else { if (r == 0 || r == rows-1 || c == 0 || c == cols-1) cout<< "*"; else cout<< " "; ++c; } } cout << endl; } return 0; }
2-1 改写框架程序,输出跟框架没有间隔的问候语。
2-0中 const int pad = 1; 改为 const int pad = 0;
2-2 在我们的框架程序中,我们使用了一定数目的空格来把问候语和顶部以及底部边界分隔开来。现在,重新编写这个程序,在重写的程序中使用数量跟源程序不同的空格来把各边界和问候语分隔开。
把pad的值变成由用户输入即可:
const int pad = 1; 替换成 cout <<"please enter the pad:"; int pad; cin >> pad;
2-3 重写框架程序,让用户自己提供在框架和问候语之间的空格个数。
同上
2-4 在框架程序中的空白行是用来把边界和问候语分隔开的,程序每次一个字符地输出了大部分的空白行。改写这个程序,让它在单独的一条输出表达式中输出所有的空白行。
#include "stdafx.h" #include<iostream> #include<string> using std::cin; using std::cout; using std::endl; using std::string; int main() { cout <<"please enter your first name:"; string name; cin >> name; const string greeting ="hello," + name +"!"; const int pad = 1; const int rows = pad*2 + 3; const string::size_type cols = greeting.size() + pad*2 + 2; const string spaces = string(greeting.size() + pad * 2, ' '); cout << endl; for (int r = 0; r !=rows; r++) { string::size_type c = 0; while (c != cols) { if (r == pad+1 && c == pad+1) { cout << greeting; c += greeting.size(); }else { if (r == 0 || r == rows-1 || c == 0 || c == cols-1) { cout<< "*"; ++c; } else if (r == pad + 1) { cout<< " "; ++c; }else { cout<< spaces; c +=spaces.size(); } } } cout << endl; } return 0; }
2-5 编写一个程序,让它输出一系列的“*”字符,程序输出的这些字符将构成一个正方形,一个长方形和一个三角形。
#include "stdafx.h" #include<iostream> #include<string> using std::cin; using std::cout; using std::endl; using std::string; int main() { // this constant is the height of each figure const int height = 5; // these variables are used to track the row and // column count while drawing each figure int row; int col; // draw square row = 0; col = 0; // draw top line while (col < height) { cout <<'*'; ++col; } cout << endl; ++row; // draw middle lines while (row < height - 1) { col = 0; while (col < height) { ++col; if (col == 1) cout <<'*'; else { if (col == height) cout<<'*'; else cout<<' '; } } cout <<endl; ++row; } // draw bottom line col = 0; while (col < height) { cout <<'*'; ++col; } // new line after figure cout << endl; // blank line between figures cout << endl; // draw rectangle row = 0; col = 0; // draw top line while (col < height * 2) { cout <<'*'; ++col; } cout << endl; ++row; // draw middle lines while (row < height - 1) { col = 0; while (col <= height * 2) { ++col; if (col == 1) cout<<'*'; else { if (col == height * 2) cout<<'*'; else cout<<' '; } } cout <<endl; ++row; } // draw bottom line col = 0; while (col < height * 2) { cout <<'*'; ++col; } // new line after figure cout << endl; // blank line between figures cout << endl; // draw triangle row = 0; // draw rows above base while (row < height - 1) { col = 0; while (col < height + row) { ++col; if (col == height - row) cout<<'*'; else { if (col == height + row) cout<<'*'; else cout<<' '; } } cout <<endl; ++row; } // draw the base col = 0; while (col < height * 2 - 1) { cout <<'*'; ++col; } // new line after figure cout << endl; return 0; }
2-6 下面的代码是做什么的?
int i=0; while(i<10) { i+=1; std::cout << i << std::endl; }
代码运行输出1—10的数字,且每个数字占一行。
2-7 编写一个程序来一次输出从10~-5的整数
int i=10; while(i>-5) { std::cout << i << std::endl; i-=1; }
2-8 编写一个程序来计算区间[1,10)中的所有数值的乘积。
int result = 1; for (int i = 1; i< 10; i++) { result *= i; } cout << result<< endl;
2-9 编写一个程序,让用户输入两个数值并告知用户在这两个数值中哪一个较大。
#include "stdafx.h" #include<iostream> using std::cin; using std::cout; using std::endl; int main() { int m, n; cout <<"please enter two numbers:"; cin >> m; cin >> n; if (m == n) { cout << m<<" equals " << n; }else if (m > n) { cout << m<<" > " << n; }else { cout << m<<" < " << n; } return 0; }
2-10 在下面的程序中,对std::的每一次使用进行解释。
int main() { int k =0; while (k!=n){ using std::cout; cout << "*"; } std::cout << std::endl; return 0; }
第1个std::是一个using声明,只适用于最里面的{}内
第2个std::必须明确指出,表示cout的名称空间
第3个std::也要明确指出,表示输出一个新行
第3章
3-0 编译、运行并测试本章中的程序
// test3.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<ios> #include<iostream> #include<string> #include<iomanip> using std::cin; using std::cout; using std::endl; using std::setprecision; using std::string; using std::streamsize; int main() { //请求输入并读入学生的姓名 cout << "please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; //请求输入并读入期中和期末成绩 cout << "please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; cout <<"enter all your homework grades, followed by end-of-file: "; //到目前为止,读到的家庭作业成绩的个数及总和 int count =0; double sum =0; //把家庭作业成绩读到变量x中 double x; //不变式 //到目前为止,我们已经读到了count个家庭作业成绩,而且sum等于头count个成绩的总和 while (cin >> x) { ++count; sum += x; } //输出结果 streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0; }