3.1节
练习3.1:
使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习
#ifdef 1
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
int sum = 0;
int i = 50;
while (i <= 100) {
sum += i;
i++;
}
cout << sum << endl;
return 0;
}
#endif
#ifdef 2
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
int n = 10;
while (n >= 0) {
cout << n << endl;
n--;
}
return 0;
}
#endif
#ifdef 3
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(void)
{
int i, j;
cout << "input two value" << endl;
cin >> i >> j;
int bigger, smaller;
bigger = (i > j ? i : j);
smaller = (i > j ? j : i);
while ( smaller <= bigger ) {
cout << smaller << endl;
smaller++;
}
return 0;
}
#endif
3.2.2 节练习
练习3.2:
编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(void)
{
string str;
// while (getline(cin, str))
// cout << str << endl;
while (cin >> str)
cout << str << endl;
return 0;
}
练习3.3:
请说明string类的输入运算符和getline函数分别是如何处理空白字符的。
空白字符包含换行符,空格符,制表符等。
cin从第一个非空白字符开始,将标准输入流的字符传递给指定字符串,并且以空字符为结束。
getline从标准输入流读取用户输入的内容,以换行符作为结束(换行符也被读入流),并将流写入str(抛弃换行符)。
练习3.4
编写一段程序读入两个字符串,## 3.1节
练习3.1:
使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习
#ifdef 1
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
int sum = 0;
int i = 50;
while (i <= 100) {
sum += i;
i++;
}
cout << sum << endl;
return 0;
}
#endif
#ifdef 2
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
int n = 10;
while (n >= 0) {
cout << n << endl;
n--;
}
return 0;
}
#endif
#ifdef 3
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(void)
{
int i, j;
cout << "input two value" << endl;
cin >> i >> j;
int bigger, smaller;
bigger = (i > j ? i : j);
smaller = (i > j ? j : i);
while ( smaller <= bigger ) {
cout << smaller << endl;
smaller++;
}
return 0;
}
#endif
3.2.2 节练习
练习3.2:
编写一段程序从标准输入中一次读入一整行,然后修改该程序使其一次读入一个词。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(void)
{
string str;
// while (getline(cin, str))
// cout << str << endl;
while (cin >> str)
cout << str << endl;
return 0;
}
练习3.3:
请说明string类的输入运算符和getline函数分别是如何处理空白字符的。
空白字符包含换行符,空格符,制表符等。
cin从第一个非空白字符开始,将标准输入流的字符传递给指定字符串,并且以空字符为结束。
getline从标准输入流读取用户输入的内容,以换行符作为结束(换行符也被读入流),并将流写入str(抛弃换行符)。
练习3.4
编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不相等,输出较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(void)
{
string str1, str2;
cin >> str1 >> str2;
string max;
if (str1 != str2) {
max = (str1 > str2) ? str1 : str2;
cout << max << endl;
}
if (str1.size() != str2.size()) {
max = (str1.size() > str2.size()) ? str1 : str2;
cout << max << endl;
}
return 0;
}
练习3.5
编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(void)
{
string str1, str2, str3;
cin >> str1 >> str2 >> str3;
string cat = str1 + " " + str2 + " " + str3;
cout << cat << endl;
return 0;
}
3.2.3 节练习
练习3.6:
编写一段程序,使用范围for语句将字符串内的所有字符用X代替。
string str = "hello world";
cout << str << endl;
for (auto &c : str) {
c = 'X';
}
练习3.7:
就上一题完成的程序而言,如果将循环控制变量的类型设为char 将发生什么?先估计一下结果,然后实际编程验证。
将循环变量改为char类型,字符串的值不会改变。