[Cpp Primer 5th] 练习题答案-Part 1

采用 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

不合法,需要把v1v2后的分号删掉。

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

输出50100的和:

#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

100之间的整数

#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;
}

全部相同:
在这里插入图片描述
全部不同:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜饼同学

帮助别人,就是帮助自己,共勉。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值