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!

从c++开始6

值的类型将引导cin 和 cout 如何显示值

#include "stdafx.h"
#include <iostream>
int main()
{
                 using namespace std;
                 char c;//定义了一个character类新的变量没有赋值
                cout<< "Enter a character: " <<endl;
                cin>>c;//通过键盘输入一个字母
                cout<< "Hellow!" ;
                cout<< "Thank you for the " <<c<<"character." <<endl;//将该char类型的字母插入cout
                 int m;
                cin>>m;
    return 0;
}

如下显示: 
Enter a character:
m
Hellow!Thank you for the mcharacter.

cout.put() 是什么? cout 是类的对象,put()是类的成员函数,中间的句点是连接符。就是类的对象使用类的函数的意思。cout.put() 就是一种显示字符的方法,以前是用 << 操作符来执行的,是根据值的类型将引导cin 和 cout 如何显示值的定义。那么为何用到 cout.put ? 是有历史原因的,在早期的c++中处理这种情况跟c 一样的, cout 可以将字符变量显示出字符没问题,但是字符常量呢? 例如单引号的 'M' ,就会被显示成数字,并储存为integer类型.就是早期的 cout<< "M"  不能显示M 而是对应的ASCII 码77。

但是如今的c++ 修正了这一点. 也可以将字符常量显示为字符. 
貌似这个列子是多余的? 只不过是为了让大家了解" 类 , 对象, 成员函数" 的调用方法而已.

#include "stdafx.h"
#include <iostream>

int main()
{
                 using namespace std;
                 char c = 'M' ; //用单引号single quotes的 M 就是将其对应ASCII 码赋值给了变量c。
                 int i = c; //  int i 变量又使用了c作为它的值。
                cout << "The ASCII code for " <<c<<"is" <<i<<endl;
                cout << "Add one to the character code; " <<endl;
                c=c+1; // c+1 = 77+1 也就是大写N 对应的ASCII值
                i=c;
                cout<< "The ASCII code for " <<c<<" is " <<i<<endl;
                cout<< "Displaying char c using out.put(c): " ;
                cout.put(c); // 调用 cout.put 函数将c 也就是78转变成 N 
                cout.put( '!' ); // 直接用 cout.put 输出单引号的 !。
                cout<<endl<< "Done" <<endl;
                 int m;
                cin>>m;
                 return 0;
}

如下显示:
The ASCII code for Mis77
Add one to the character code;
The ASCII code for N is 78
Displaying char c using out.put(c): N!
Done

2012年6月1日星期五

从c++开始5


hex(hexadecimal); oct(octal); 这两个标识符在命名空间std中,也是由iostream类文件提供的。他两个的作用是以不同的整形integer面貌来显示数据。


#include "stdafx.h"
#include <iostream>


int main()
{
using namespace std;
int chest = 42;//十进制
int waist = 42;//十进制
int inseam = 42;//十进制


cout<<"Monsieur cuts a striking figure!"<<endl;


cout<<"chest = "<<chest <<" (Decimal)" <<endl;//十进制

cout<<hex <<"waist = "<<waist <<" (Hexadecimal)" <<endl;//hex的出现告诉后面的cout 要用十六进制的方式输出显示插入的数据


cout<<cout <<"inseam = "<<inseam <<" (Octal)" <<endl;//oct也一样,告知后面用八进制显示插入数据


int m;
cin>>m;


return 0;
}

如下显示:

Monsieur cuts a striking figure!
chest = 42 (Decimal)
waist = 2a (Hexadecimal)
inseam = 2a (Octal)