采用 unix 环境(Mac/linux)
1.1 节练习
练习1.1
int main() {
return 0;
}
$ vim prog1.cc
# 把前面的代码放入 prog1.cc 文件中
$ CC prog1.cc
# 编译后可以发现同目录下多了一个 1.out 文件
$ ./1.out
$ echo $? # 输出返回值
0
练习 1.2
int main() {
return -1;
}
重新编译执行
$ CC prog1.cc
$ ./1.out
$ echo $?
255
1.2 节练习
练习1.3
#include<iostream>
int main() {
std::cout << "Hello, World" << std::endl;
return 0;
}
这里用cc
编译会报错,具体原因懒得看了,改用g++
:
g++ -o a.out prog1.cc
练习1.4
#include<iostream>
int main() {
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The multiply of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl;
return 0;
}
练习 1.5
#include<iostream>
int main() {
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The multiply of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout << std::endl;
return 0;
}
练习 1.6
不合法,需要把v1
、v2
后的分号删掉。
1.3节练习
练习1.7
例如:
/*
* comment pairs /* */ cannot nest.
* ''cannot nest'' is considered source code,
* as is the rest of the program
*/
int main()
{
return 0;
}
编译结果:
练习1.8
1、2、4都可以正常运行,第三个的第一个双引号被注释掉了,会出问题。
#include <iostream>
int main() {
std::cout << "/*";
std::cout << "*/";
// std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
// 前后都被注释掉了 /* "*/ " /* " /*" */
return 0;
}
1.4.1节练习
练习1.9
输出50
到100
的和:
#include <iostream>
int main() {
int i = 50, sum = 0;
while (i <= 100) {
sum += i;
i++;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
练习1.10
10
到0
之间的整数
#include <iostream>
int main() {
int i = 10;
while (i >= 0) {
std::cout << i << " ";
i--;
}
std::cout << std::endl;
return 0;
}
练习1.11
#include <iostream>
int main() {
std::cout << "输入两个整数: ";
int i, j;
std::cin >> i >> j;
if (i < j) { // 保证 i 为较大值
i = i ^ j;
j = i ^ j;
i = i ^ j;
}
while (j <= i) {
std::cout << j++ << " ";
}
std::cout << std::endl;
return 0;
}
1.4.2节练习
练习1.12
从 -100
加到100
,所以结果为0
。
练习1.13
- 练习1.9
#include <iostream>
int main() {
int sum = 0;
for (int i = 50; i <= 100; ++i) {
sum += i;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
- 练习1.10
#include <iostream>
int main() {
for (int i = 10; i >= 0; i--) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
- 练习1.11
#include <iostream>
int main() {
std::cout << "输入两个整数: ";
int i, j;
std::cin >> i >> j;
if (i < j) { // 保证 i 为较大值
i = i ^ j;
j = i ^ j;
i = i ^ j;
}
for (int k = j; k <= i; ++k) {
std::cout << k << " ";
}
std::cout << std::endl;
return 0;
}
练习1.14
There are three loops in C: for, while, and do-while. What’s the difference between them?
练习1.15
Syntax Errors:
int main(){
std::cout << "Hello World!" << std::endl // semicolon missed
return 0;
}
Type errors:
int main(){
char s = "Hello World!"; // Here char should be std::string
std::cout << s << endl;
return 0;
}
Declaration errors:
int main(){
int k = 0;
std::cout << K << std::endl; // use of undeclared identifier 'K'
return 0;
}
1.4.3节练习
练习1.16
#include <iostream>
int main() {
int sum = 0, v;
while (std::cin >> v) {
sum += v;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
1.4.4节练习
练习1.17
#include <iostream>
int main() {
int curVal = 0, val = 0;
if (std::cin >> curVal) {
int cnt = 1;
while (std::cin >> val) {
if (val == curVal)
++cnt;
else {
std::cout << curVal << " occurs "
<< cnt << " times" << std::endl;
curVal = val;
cnt = 1;
}
}
std::cout << curVal << " occurs "
<< cnt << " times" << std::endl;
}
return 0;
}
全部相同:
全部不同: