2012年6月3日星期日

从c++开始7


char的常量。用单引号 single quotes 括起来的 ‘M’,就是直接用其对应的ASCII 码 77。字符 ‘5’ 对应的ASCII 码是 53 而不是5。空格‘ ’ 对应的ASCII 码是32。还有些不可能用键盘输入的字符,例如 Enter 键,只能用“转义序列”  Enter 在代码里可以用操作符manipulator: endl; 或者 标识符character: \n 来实现。那么作为character的 \n 就应该对应有ASCII 码值是10。还有水平制表符\t ;垂直制表符\v ; 退格\b 等,都有各自的ASCII 码值对应。


换行符\n 可以替代 endl; 是重启一行的意思。前面讲过在语句中使用 ‘\n’ 或者 “\n ” 都可以实现相同目的。
cout<< endl; //using the endl manipulator
cout<<'\n'; //using a character constant
cout<<"\n"; // using a string 

并且 \n 在特别是很长的语句中用起来很简洁。
cout<<endl<<endl<<"What next? "<<endl<<"Enter a number: "<<endl; 与 cout<<"\n\nWhat next?\nEnter a number: \n"; 是完全一样的。

结果如下:


What's next?
Enter a number:

转移序列的使用

#include "stdafx.h"
#include <iostream>
int main()
{
                 using namespace std;
                cout<< "\aOpeartion \"HyperHype\" is now activated!\n" //用到了转移序列中的\a,\",\n 。
                cout<< "Enter your agent code:_________\b\b\b\b\b\b\b\b" ; //用到了转义序列的\b。
                 long code;
                cin>>code;
                cout<< "\aYou entered " <<code<<"....\n" ; //用到了转义序列中的\a, \n。
                cout<< "\aCode verified! Proceed with Plan Z3!\n" ; //用到了转义序列的\a, \n。
                 int m;
                cin>>m;
                 return 0;
}
如下输出:
Opeartion "HyperHype" is now activated!
Enter your agent code:_123456789
You entered 123456789....
Code verified! Proceed with Plan Z3!

没有评论:

发表评论