我是靠谱客的博主 勤奋蓝天,这篇文章主要介绍ifelse 测试-C++ Primer Plus该书中有一个好玩的打印,现在分享给大家,希望可以做个参考。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

该书中有一个好玩的打印

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

执行结果:

095419_9RdN_2326611.png

因为++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;
}

执行结果:

095458_isMy_2326611.png

发现输入的是字符,输出的是字符对应的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;
}

 

结果:

095523_SsIi_2326611.png

 

转载于:https://my.oschina.net/u/2326611/blog/1491177

最后

以上就是勤奋蓝天最近收集整理的关于ifelse 测试-C++ Primer Plus该书中有一个好玩的打印的全部内容,更多相关ifelse内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(118)

评论列表共有 0 条评论

立即
投稿
返回
顶部