Accelerated C++ 第1章答案

1-0:编译 运行并测试本章中的程序

#include <iostream>
using namespace std;
int main()
{
	cout << "请输入你的名字" << endl;
	string name;
	cin >> name;
	cout << "hello:" << name << endl;
}
#include <iostream>
using namespace std;
int main()
{
	cout << "please input your name";
	string name;
	cin >> name;
	const string greeting = "hello," + name + "!";
	string spaces(greeting.size(), ' ');  //和问候语大小一致 不过全用空字符代替了
	string second = "*" + spaces + "*";  //在问候语两端加上 *  
	string first(second.size(), '*');  //和second大小一致  不过全用 * 代替
	cout << first << endl;
	cout << second << endl;
	cout << "*" << greeting << "*"<< endl;
	cout << second << endl;
	cout << first << endl;
	return 0;
}

1-1 以下的定义有效吗?理由是什么?

第一行 有效 可以把字符串直接量赋值给字符串变量

第二行 无效  不可以用 + 号连接两个字符串直接量

1-2 以下定义是否有效?

第一行有效  可以把字符串直接量赋值给字符串变量

第二行无效 不可以用 + 号连接两个字符串直接量

1-3 以下程序是否有效?

有效 作用 输出字符串  a string \n another string

1-4 

输出字符串  如果更改 效果不变

1-5

无效 最后一行输出 x 不在作用范围内 编译报错  修改如下

#include <iostream>
using namespace std;
int main()
{
    {{
      string s = "a string";
      string x = s + ",really";
      cout << s << endl;
      cout << x << endl;
    }}
    return 0;
}

1-6  运行结果

what is your name? Samuel

Hello, Samuel

And what is yours?Beckett

Hello, Beckett ; nice to meet you!

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值