2013年9月4日星期三

Download Timely, use Y2C4-PKXF-U0YT and get a theme for free! http://r.bitsp.in/Y2C4PKXFU0YT

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)


2012年5月31日星期四

你好,部落格还是很有意思的,为什么会乱码?

2012年5月30日星期三

从c++开始4

三、c++的处理数据
                命名规则 
c与c++的命名是随意性比较强的。名称中可以用数字digi、下划线underscore、大写字母等capital。但不能拿关键字keywords开玩笑,并且支持长字符。开始就用一个下划线或者两个underscores也合法,但是被实现reserved保留了,那就尽量避免触及到即可。

c++的整形integer
short ,int,long 是递增宽度width的无小数点部分并且有正负符号的类型。short就是 short int的简称并且可以单独使用。例如:short score; created a type short  integer variable创建了一个shor型的变量叫score。 int 、long同理。那么我们要用一个 include <limites>; 的头文件把一些对象召唤出来测量一下自己系统中不同整形的width宽度吧

// widthTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<climits>//预处理头文件被召唤 use limits.h for older systems
int main()
{
using namespace std;
int n_int=INT_MAX;//INT_MAX对象被召唤初始化变量
short n_short=SHRT_MAX;//SHRT_MAX对象被召唤初始化变量
long n_long=LONG_MAX;//LONG_MAX对象被召唤初始化变量

cout<<"short is "<<sizeof n_short<<endl;//操作符sizeof被召唤测量长度
cout<<"int is "<<sizeof n_int<<endl;//操作符sizeof被召唤测量长度
cout<<"long is "<<sizeof n_long<<endl;//操作符sizeof被召唤测量长度
cout<<endl;

cout<<"Maximum values:"<<endl;
cout<<"short:"<<n_short<<endl;
cout<<"int:"<<n_int<<endl;
cout<<"long:"<<n_long<<endl;
cout<<endl;

cout<<"Minimum int value ="<<INT_MIN<<endl;//直接使用INT_MIN对象
cout<<"Bits per byte ="<<CHAR_BIT<<endl;//直接使用CHAR_BIT对象

int m;//这个实现跟我没半毛钱关系
cin>>m;//这个实现跟我没半毛钱关系
}
如下就是实现内容:

short is 2
int is 4
long is 4

Maximum values:
short:32767
int:2147483647
long:2147483647

Minimum int value =-2147483648
Bits per byte =8

那个头文件#include<limits>; 有自己的一个成员对象list必须要在这里列述一下。但我就很反对必须二字,所以上网自己搜吧。一般必须的东西哪儿都能找到。

无符号类型,介绍过integer的三种类型 short、 int、 long 都是可以有正负符号的。有些数据是用不到负号的(人口),那么本来short是-32768 到 32767,则无符号表达就是0 到 65535,不信你数数看看跟有符号的short总数对不对?那么出现了另外一个关键字keywords:unsigned,是unsigned int 的缩写abb。例如:unsigned short n_short;就是定义了一无符号的变量n_short,他的值必须从0开始。
下面是根据 有符号short的取值范围从(-32768 到 32767),unsigned short的取值范围是(0 到 65535)的有趣例子。

#include "StdAfx.h"
#include <iostream>
#include <climits>
#define Zero 0//define是c的预处理命令在c++里一般用const。就是告诉编译器碰到ZERO就是0
int main()
{
using namespace std;
short sam = SHRT_MAX;//SHRT_MAX把有符号的short sam赋值32767
unsigned short sue = sam;//无符号的short,但是被变量sam赋值了,也是32767

cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl<<"Add $1 to each one."<<endl<<"Now!";

sam=sam+1;//变成-32768了,明白什么叫否极泰来么?取值范围的原因只能轮回了。
sue=sue+1;//32767+1
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited.\n Pool Sam!"<<endl;

sam=Zero;//归零 #define Zero 0
sue=Zero;//归零 #define Zero 0
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl;
cout<<"Take $1 from each one."<<endl<<"Now";

sam=sam-1;//有符号的 0-1就是-1
sue=sue-1;//没符号的就再次否极泰来是(0-1=65535)
cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;
cout<<" dollars deposited."<<endl<<"Luck Sue!"<<endl;

int m;//没半毛关系
cin>>m;//没半毛关系
}

 打印内容如下:

Sam has 32767 dollars and Sue has 32767 dollars deposited.

Add $1 to each one.
Now!Sam has -32768 dollars and Sue has 32768 dollars deposited.
 Pool Sam!
Sam has 0 dollars and Sue has 0 dollars deposited.
Take $1 from each one.
NowSam has -1 dollars and Sue has 65535 dollars deposited.
Luck Sue!

从c++开始练习1


我的代码:
1.编写代码用来显示姓名和地址;
// exercises2.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include <iostream>
int main()
{
using namespace std;
cout<<"My name is ChaAng and My addresses is GD";
int n;
cin>>n;
}
2.编写代码输入一个浪的距离,然后将它转化为码,1浪等于22码
#include "StdAfx.h"
#include <iostream>
double yd(double);
int main()
{
using namespace std;
double waves;
cout<<"How many waves do you know?"<<endl;
cin>>waves;
cout<<waves<<"Waves = ";
double yards = yd(waves);
cout<<yards<<"Yards";
int n;
cin>>n;
}
double yd(double wav )
{
return 22*wav;
}


3.编写代码,自定义两个函数。每一个都要被调用两次显示出一样的两段文字。
#include "StdAfx.h"
#include <iostream>
void mice(int);
void run(int);
int main()
{
using namespace std;
mice(1);
mice(2);
run(3);
run(4);
int m;
cin>> m;
}
void mice(int m)
{
using namespace std;
cout<<"Three blind mice."<<endl;
}
void run(int r)
{
using namespace std;
cout<<"See how they run."<<endl;
}

4.编写代码,由main()调用自定义函数将摄氏度转化为华氏度公式:华氏=摄氏×1.8+32.0

#include "StdAfx.h"
#include <iostream>
double Fh(double);
int main()
{
using namespace std;
double Cle;
cout<<"Please enter a Celsius value:";
cin>> Cle;
double fh = Fh(Cle);
cout<<endl;
cout<<Cle<<"degrees Clesius is "<<fh<<"degrees Fahrenheit.";
int n;
cin>>n;
}
double Fh(double cle)
{
return 1.8*cle+32;
}

5.编写代码,要求输入小时分钟数并传递给自定义void函数,被main()调用后以时间格式显示。
#include "StdAfx.h"
#include <iostream>
void hm(int,int);
int main()
{
using namespace std;
int hr;
cout<<"Enter the number of hours:";
cin>>hr;
int min;
cout<<"Enter the number of minutes:";
cin>>min;
int hrs = hr;
int mins = min;
hm(hrs,mins);
int n;
cin>>n;
}
void hm(int h,int m)
{
using namespace std;
cout<<"Time:"<<h<<":"<<m<<endl;
}