//Ex2_01_main.cpp //A Simple Example of a Program #include <iostream>//标准输入输出流库文件 与C的“stdio”有区别,在C++中输入源和输出目的称为流 using std::cout; //ANSI C++所有标准库工具都被定义在名为“std”的命名空间,cout为标准输出流 using std::endl; //endl表示换行字符 int main() ...{ int apples,oranges; int fruit; apples=5;oranges=6; fruit=apples+oranges; cout<<endl;//“<<”这个符号表示将换行符号输出到输出流 cout<<"橘子不是唯一的水果---"<<endl<<"我们一共有"<<fruit<<"个水果!";//<<这样的写法是可以的。<<右边的字符都会输出到cout(输出流) return0; }
未完待续,马上陪老婆逛易初莲花...
继续:
Ex2_03.cpp
//Ex2_03.cppp //Exercising output #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::setw;//操作符号在头文件<iomanip>中定义,setw(n)的作用是输出的值在n个空格(字符)宽的字段中遵循右对齐。 int main() ...{ int num1=1234,num2=5678; cout<<endl; cout<<setw(6)<<num1<<setw(6)<<num2;//setw操作符:只对紧跟在它的插入运算符后的单个输入值起作用。 cout<<endl; return0; }
int main() { char newline='/n';///n的作用与endl一致 cout<<newline; cout<<"/"We/'ll make our escapes in sequence/",he said.";// “/"”代表字符“"” cout<<"/n/tThe program/'s over,it/'s time take make abeep beep./a/a/a/a/a";// “/n”代表换行“/t”代表跳到下一个制表符 // “/a”代表PC扬声器发“嘟”一声,牛! cout<<newline; return 0; }
//Ex2_05.cpp //Calculating how many rolls of wall paper are required for a room //计算你家墙用多少米的墙纸帖才够? //哈哈,老外的书很有意思。 #include <iostream> using std::cout; using std::cin;//cin是输入源,与cout相对 using std::endl; int main() ...{ double height=0.0,width=0.0,length=0.0;//Room dimensions 房间长宽高 double perimeter=0.0;//Room perimeter constdouble rollwidth=21.0; constdouble rolllength=12.0*33.0; int strips_per_roll=0; int strips_reqd=0; int nrolls=0; cout<<endl<<"Enter the height of the rom in inches:"; cin>>height; cout<<endl<<"Now enter the length and width in inches:"; cin>>length>>width; strips_per_roll=rolllength/height; perimeter=2.0*(length+width); strips_reqd=perimeter/rollwidth; nrolls=strips_reqd/strips_per_roll; cout<<endl<<"For your rom you need "<<nrolls<<" rolls of wallpaper."; }
Ex2_06
//Ex2_06.cpp //Exercising the comma operator逗号运算符号 #include <iostream> using std::cout; using std::endl; int main() ...{ long num1=0,num2=0,num3=0,num4=0; num4=(num1=10,num2=20,num3=30);//想想,如果写成:num4=num1=10,num2=20,num3=30 呢?num4的值为几? cout<<endl<<"The values of a series of expreesions is the value of the rightmost:"<<num4; cout<<endl; return0; }
变量的作用域:
//Ex2_07.cpp //Demonstrating variable scope 作用域对自动变量的影响 #include <iostream> using std::cout; using std::endl; int main() ...{ int count1=10; int count3=50; cout<<endl<<"Value of outer count1="<<count1<<endl; ...{ int count1=20; int count2=30; cout<<"value of inner count1="<<count1<<endl; count1+=3; count3+=count2; } cout<<"Value of outer count1="<<count1<<endl; cout<<"Value of outer count3="<<count3<<endl; //cout<<count2<<endl; } //最后的结果是,外层括号打出的count1不变,count3发生变化,注释部分代码如果有则报错,因为外层括号并不知道有count2
全局变量代码
//Ex2_08.cpp //Demonstrating variable scope #include <iostream> using std::cout; using std::endl; int count1=100;//此count1是全局变量 int main() ...{ int count1=10;//此count1是局部变量 int count3=50; cout<<endl <<"Value of outer count1="<<count1 <<endl; cout<<"Value of global count1="<<::count1 <<endl; ...{ int count1=20; int count2=30; cout<<"Value of inner count1="<<count1 <<endl; cout<<"Value of global count1="<<::count1 <<endl; count1+=3; count3+=count2; } cout<<"Value of outer count1="<<count1 <<endl <<"Value of outer count3="<<count3 <<endl; return0; } //本例子需要注意::count1这样的全局变量