2019独角兽企业重金招聘Python工程师标准>>> 
该书中有一个好玩的打印
case 1:
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == 'n')
std::cout << ch;
else
std::cout << ++ch;
std::cin.get(ch);
}
std::cout << "nPlease excuse the slight confusionn";
return 0;
}
执行结果:

因为++ch,所以打印的是输入的下一个字符。
case 2:
使用ch+1代替++ch
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == 'n')
std::cout << ch;
else
std::cout << ch + 1;
std::cin.get(ch);
}
std::cout << "nPlease excuse the slight confusionn";
return 0;
}
执行结果:

发现输入的是字符,输出的是字符对应的ascii码,原因是加法操作使char型提升为int。
case 3:
打印ch
#include <iostream>
int main()
{
char ch;
std::cout << "Type, and I shall repeat.n";
std::cin.get(ch);
while (ch != '.')
{
if (ch == 'n')
std::cout << ch;
else
std::cout << ch;
std::cin.get(ch);
}
std::cout << "nPlease excuse the slight confusionn";
return 0;
}
结果:

转载于:https://my.oschina.net/u/2326611/blog/1491177
最后
以上就是勤奋蓝天最近收集整理的关于ifelse 测试-C++ Primer Plus该书中有一个好玩的打印的全部内容,更多相关ifelse内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复