你好,部落格还是很有意思的,为什么会乱码?
2012年5月31日星期四
2012年5月30日星期三
从c++开始4
三、c++的处理数据
命名规则
打印内容如下:c与c++的命名是随意性比较强的。名称中可以用数字digi、下划线underscore、大写字母等capital。但不能拿关键字keywords开玩笑,并且支持长字符。开始就用一个下划线或者两个underscores也合法,但是被实现reserved保留了,那就尽量避免触及到即可。c++的整形integershort ,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 systemsint 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 2int is 4long is 4Maximum values:short:32767int:2147483647long:2147483647Minimum int value =-2147483648Bits 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就是0int main(){using namespace std;short sam = SHRT_MAX;//SHRT_MAX把有符号的short sam赋值32767unsigned short sue = sam;//无符号的short,但是被变量sam赋值了,也是32767cout<<"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+1cout<<"Sam has "<<sam<<" dollars and Sue has "<<sue;cout<<" dollars deposited.\n Pool Sam!"<<endl;sam=Zero;//归零 #define Zero 0sue=Zero;//归零 #define Zero 0cout<<"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就是-1sue=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;}
从c++开始3
二、c++的sentence
其程序是由不同的函数组成的,而每个函数都有自己的语句结构。声明变量及赋值
#include <iostream>int main(){using namespace std;int carrots;//声明整形变量definitioncarrots = 25;//给他赋值cout <<"I have ";cout << carrots;//声明变量在字符串中使用cout <<"carrots.";cout <<endl;//另起一行carrots = carrots - 1;//重新给变量赋值cout << "Crunch, crunch. Now I have"<<carrots <<"carrots." <<endl;return 0;}I have 25 carrots.Crunch, crunch. Now I have 24 carrots.“本来25个红萝卜,嚼b嚼b就剩24个了。”
定义声明 defining declaration(definition);引用声明 reference declaration 其两者区别在于引用是借用已经声明过的变量,定义是创建一个新的变量。定义就是为了告知编译器给变量分配适用的内存空间。
这个int carrots;的int 就是告知编译器变量carrots是整形,非小数点部分并且正负都可以的数据。
赋值语句 assign a value ,修改 modify the value
carrots = 25;= 是赋值操作符与插入操作符 << 一个类型。c与c++ 的赋值操作符也可以这么用
int a;int b;int c;
a = b = c = value;这个过程是自右向左的,value 赋给c值,然后是传递给了b,最后是a。代码中的carrots = carrots -1;就是 modify the value。但是这个modify使用了减法表达式 carrots-1 然而被modify后的carrots会发生什么?就是自现在开始新值24会代替原来的值25,储存在与对应内存空间中。
再说这个cout,程序中出现了同时向cout插入<< 字符串与变量的情况。“插入”听起来很形象。但插不同东西,打印出来的就不同。插一个带套子的“25”再插一个不带套子的25,编译器会把他们分别放入不同的储存空间的。“25”char型的2与5的ASC编码,而25却是个int型的二进制数值,一大堆1和0组成的东西。
cout打印出来:“嚼b嚼b就剩24个了”
cout << "Crunch, crunch. Now I have"<<carrots <<"carrots." <<endl;插入符<< 会把插入的东西整合,这就是典型的操作符重载。c++的cout 很智能是iostream 成员之一,而不像c中的printf需要提供转换说明。再举一个sentence的例子
#include <iostream>int main(){using namespace std;int carrots;cout<<"How many carrots do you have?"<<endl;cin>>carrots;//input输入cout<<"Here are two more.";carrots = carrots+2;cout<<"Now you have"<<carrots<<"carrots"<<endl;//concatenates outputreturn 0;}
How many carrots do you have?使用cin对象读取键盘输入数字:12Here are two more. Now you have 14 carrots.
使用cin。它跟cout一样也是iostream中的一个对象,操作符<<和>>是指信息流的方向,cin使用>>操作符提取输入数据。通常它的右边是一个变量 variable 。cin也是智能的,可以自动分辨 int carrots 变量的类型。就是输入的字符会被转换成变量可以接受的类型。类与oop Object-oriented programming
类是被定义的,可以描述他能够做什么,格式及用法。就像被声明的 类型和变量的关系int carrots。类定义描述的是数据格式用法,而对象则是根据这些内容创建实体。那么cout 和 cin 就是一个叫 iostream类的对象了。概念:类描述了一种数据类型及属性,对象是根据这些描述创建的实体。那么类是如何让对象工作的呢?有两种方式,操作符和函数的调用。cout<<"God bless u." 就是采用了操作符 << 。
函数
c++的函数分为两种 有返回值与没有返回值的。有返回值的会返回一个值赋予变量,这个表达式 X = sqrt(6.25)那么X就会被赋值2.5,因为函数sqrt()是一个能够返回平方根的函数return the value and assign it to variable,它会计算平方根。而表达式sqrt(6.25)就是调用函数sqrt() called function ->return value
sqrt() 函数的原型是:double sqrt(double_1) 这里面的double是sqrt() 函数返回值、是个带小数部分的实数。double_1 是参数,也是一个同类型的实数。那么例子里的6.25是参数,2.5是返回值。c++应当为每个使用的函数提供原型prototype #include cmath
double x;//declare x as a type double variablex=sqrt(6.25); 这里有分号结尾,表明这是条语句而不是函数头,如果省略分号,编译器会错误理解为函数头。两种方法提供原型prototype 1、是直接代码中写出来 double sqrt(double); 2、提前加上包含的头文件#include <cmath>,如同提前包含流文件一样 #include<iostream> 。当然是方法2 更好更准确。例如:
#include <iostream>#include<cmath>//添加函数头文件int main(){
using namespace std;double area;cout<<"Enter the floor area, in square feet, of your home.";cin>>area;double side;side = sqrt(area);cout<<"That's the equivalent of a square"<<"feet to the side."<<endl;cout<<"How fascinating!"<<endl;return 0;
}Enter the floor area, in square feet, of your home: 536That's the equivalent of a square 39.1918 feet to the side.How fascinating!
多参、无返回值或无参数的函数
1.多参函数,的参数间用逗号分开。例如 pow()有两个参数,第一个为底,第二个是幂。他的prototype是 double pow(double,double)2.不接受任何参数函数例如 rend() 他的prototype是 int rend(void)/ int rend()关键字void指出该函数不接受任何参数,但可以随机返回一个 整形 的数值。c++中函数即使无参数也必须有对括号。3.无返回值的函数例如 自定义了一个函数 rmb()可按照货币数值及符号显示数字。当传递进参数时,将会显示出类似¥30.36这样的信息。那么他的prototype是 void rmb(double); 因为不返回任何值所以不能调用在赋值语句,或者其他表达式里。必须使用纯粹的函数调用语句例如 rmb(30.36)在其他语言中这种函数会被称作过程procedure,或子程序subroutine
自定义无返回值函数 在使用之前必须提供原型prototype,通常会放在mian()函数顶部。还有必须提供函数的源代码放在main()函数后面
#include <iostream>void simon(int);//function prototype for simon 提供原型int main(){using namespace std;simon(3);//call the function第一次赋值调用cout<<"Pice an integer:";int count;//声明一个变量countcin>> count;// 用键盘给变量赋值simon(count);//call it again第二次变量调用cout<<"Done!"<<endl;return 0;}void simon(int n)//define the simon() function 提供源代码{using namespace std;cout<<"Simon says touch your toes"<<n<<"times"<<endl;// 调用后会发生什么}
Simon says touch your toes 3 times 第一次调用Pick an integer:250 cin输入Simon says touch your toes 250 times 第二次调用Done!
自定义函数格式:与main()函数格式相同,并且不能在一个函数定义嵌套中去定义另外一个函数。每个函数都是独立的,创建都是平等的。
type functionname(argumentlist){
statements
}
自定义有返回值函数:
#include <iostream>int stonetolb(int);//prototype for stonetolbint main(){using namespace std;int stone;//declared a variablecout<<"Enter the weight in stone:";cin>>stone;int pounds = stonetolb(stone);//declare a variable and call that function to assign it.cout<<stone<<"stone=";cout<<pounds<<"pounds."<<endl;return 0;}int stonetolb(int sts)//define the function{return 14*sts;// the function's procedure}Enter the weight in stone: xxxstone = xxxx pounds.
订阅:
博文 (Atom)