第二章:开始学习C++
//ex2.1--display your name and address
#include
int main(void)
{
using namespace std;
cout<<"My name is liao chunguang and I live in hunan chenzhou.\n”;}
//ex2.2--convert the furlong units to yard uints-把浪单位换位码单位
#include
double fur2yd(double);
int main()
{
using namespace std;
cout<<"enter the distance measured by furlong units:";
double fur;
cin>>fur;
cout<<"convert the furlong to yard"< double yd; yd=fur2yd(fur); cout< return 0; } double fur2yd(double t) { return 220*t; } //ex2.3-每个函数都被调用两次 #include void mice(); void see(); using namespace std; int main() { mice(); mice(); see(); see(); return 0; } void mice() { cout<<"three blind mice"< } void see() { cout<<"see how they run"< } //ex2.4 #include int main() { using namespace std; cout<<"Enter your age:"; int age; cin>>age; int month; month=age*12; cout< return 0; } //ex2.5---convert the Celsius valve to Fahrenheit value #include double C2F(double); int main() { using namespace std; cout<<"please enter a Celsius value:"; double C; cin>>C; double F; F=C2F(C); cout< } double C2F(double t) { return 1.8*t+32; } //ex2.6---convert the light years valve to astronomical units--把光年转换为天文单位#include double convert(double);//函数原型 int main() { using namespace std; cout<<"Enter the number of light years:"; double light_years; cin>>light_years; double astro_units; astro_units=convert(light_years); cout< } double convert(double t) { return 63240*t;//1 光年=63240 天文单位 } //ex2.7--显示用户输入的小时数和分钟数 #include void show(); main() { using namespace std; show(); return 0; } void show() { using namespace std; int h,m; cout<<"enter the number of hours:"; cin>>h; cout<<"enter the number of minutes:"; cin>>m; cout<<"Time:"< } 第三章:处理数据 //ex3.1—将身高用英尺(feet)和英寸(inch)表示 #include const int inch_per_feet=12;// const常量--1feet=12inches--1英尺=12英寸 int main() { using namespace std; cout<<"please enter your height in inches:___\b\b\b";// \b表示为退格字符int ht_inch; cin>>ht_inch; int ht_feet=ht_inch/inch_per_feet;//取商 int rm_inch=ht_inch%inch_per_feet;//取余 cout<<"your height is "< < return 0; } //ex3.2--计算相应的body mass index(体重指数) #include const int inch_per_feet=12; const double meter_per_inch=0.0254; const double pound_per_kilogram=2.2; int main() { using namespace std; cout<<"Please enter your height:"< cout<<"First,enter your height of feet part(输入你身高的英尺部分):_\b"; int ht_feet; cin>>ht_feet; cout<<"Second,enter your height of inch part(输入你身高的英寸部分):_\b"; int ht_inch; cin>>ht_inch; cout<<"Now,please enter your weight in pound:___\b\b\b"; double wt_pound; cin>>wt_pound; int inch; inch=ht_feet*inch_per_feet+ht_inch; double ht_meter; ht_meter=inch*meter_per_inch; double wt_kilogram; wt_kilogram=wt_pound/pound_per_kilogram; cout< cout<<"Your pensonal body information as follows:"< cout<<"身高:"< <<"体重:"< double BMI; BMI=wt_kilogram/(ht_meter*ht_meter); cout<<"your Body Mass Index(体重指数) is "< return 0; } //ex3.3 以度,分,秒输入,以度输出 #include const int minutes_per_degree=60; const int seconds_per_minute=60; int main() { using namespace std; cout<<"Enter a latitude in degrees,minutes,and seconds:\n"; cout<<"First,enter the degrees:"; int degree; cin>>degree; cout<<"Next,enter the minutes of arc:"; int minute; cin>>minute; cout<<"Fianlly,enter the seconds of arc:"; int second; cin>>second; double show_in_degree; show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/mi nutes_per_degree/seconds_per_minute; cout< return 0; } //ex3.4 #include const int hours_per_day=24; const int minutes_per_hour=60; const int seconds_per_minute=60; int main() { using namespace std; cout<<"Enter the number of seconds:"; long seconds; cin>>seconds; int Day,Hour,Minute,Second; Day=seconds/seconds_per_minute/minutes_per_hour/hours_per_day; Hour=seconds/seconds_per_minute/minutes_per_hour%hours_per_day; Minute=seconds/seconds_per_minute%minutes_per_hour; Second=seconds%seconds_per_minute; cout< return 0; } //ex3.5 #include int main() { using namespace std; cout<<"Enter the world population:"; long long world_population; cin>>world_population; cout<<"Enter the population of the US:"; long long US_population; cin>>US_population; double percentage; percentage=(double)US_population/world_population*100; cout<<"The population of the US is "< return 0; } //ex3.6 汽车耗油量-美国(mpg)or欧洲风格(L/100Km) #include int main() { using namespace std; cout<<"Enter the miles of distance you have driven:"; double m_distance; cin>>m_distance; cout<<"Enter the gallons of gasoline you have used:"; double m_gasoline; cin>>m_gasoline; cout<<"Your car can run "< cout<<"Computing by European style:\n"; cout<<"Enter the distance in kilometers:"; double k_distance; cin>>k_distance; cout<<"Enter the petrol in liters:"; double k_gasoline; cin>>k_gasoline; cout<<"In European style:"<<"your can used "<<100*k_gasoline/k_distance<<" liters of petrol per 100 kilometers\n"; return 0; } //ex3.7 automobile gasoline consumption-耗油量--欧洲风格(L/100Km)转换成美国风格(mpg) #include int main() { using namespace std; cout<<"Enter the automobile gasoline consumption figure in\n" <<"European style(liters per 100 kilometers):"; double Euro_style; cin>>Euro_style; cout<<"Converts to U.S. style(miles per gallon):"< cout< return 0; } // Note that 100 kilometers is 62.14 miles, and 1 gallon is 3.875 liters. //Thus, 19 mpg is about 12.4 L/100Km, and 27 mpg is about 8.7 L/100Km. Enter the automobile gasoline consumption figure in European style(liters per 100 kilometers):12.4 Converts to U.S. style(miles per gallon): 12.4 L/100Km = 19.4187 mpg Press any key to continue // ex3.7 automobile gasoline consumption-耗油量--美国风格(mpg)转换成欧洲风格(L/100Km) #include int main() { using namespace std; cout<<"Enter the automobile gasoline consumption figure in\n" <<"U.S. style(miles per gallon):"; double US_style; cin>>US_style; cout<<"Converts to European style(miles per gallon):"< cout< return 0; } // Enter the automobile gasoline consumption figure in U.S. style(miles per gallon):19 Converts to European style(miles per gallon): 19 mpg = 12.6733L/100Km Press any key to continue 第四章复合类型 //ex4.1 display the information of student #include const int Asize=20; using namespace std; struct student//定义结构描述 { char firstname[Asize]; char lastname[Asize]; char grade; int age; }; void display(student);//函数原型放在结构描述后 int main() { cout<<"what is your first name?"< student lcg;//创建结构变量(结构数据对象) cin.getline(lcg.firstname,Asize); cout<<"what is your last name?"< cin.getline(https://www.wendangku.net/doc/c77503516.html,stname,Asize); cout<<"what letter grade do you deserve?"< cin>>lcg.grade; cout<<"what is your age?"< cin>>lcg.age; display(lcg); return 0; } void display(student name) { cout<<"Name: "< cout<<"Grade:"< cout<<"Age:"< } //ex4.2 use the string-class instead of char-array #include #include int main() { using namespace std; string name,dessert; cout<<"Enter your name: \n"; getline(cin,name); cout<<"Enter your favorite dessert: \n"; getline(cin,dessert); cout<<"I have some delicious "< cout<<" for you, "< return 0; } //有时候会遇到需要按下两次回车键才能正确的显示结果,这是vc++6.0的一个BUG,更改如下:else if (_Tr::eq((_E)_C, _D)) {_Chg = true; _I.rdbuf()->sbumpc();//修改后的 break; } ex4.3 输入其名和姓,并组合显示 #include #include const int Asize=20; int main() { using namespace std; char fname[Asize]; char lname[Asize]; char fullname[2*Asize+1]; cout<<"Enter your first name:";//输入名字,存储在fname[]数组中 cin.getline(fname,Asize); cout<<"Enter your last name:";//输入姓,存储在lname[]数组中 cin.getline(lname,Asize); strncpy(fullname,lname,Asize);//把姓lname复制到fullname空数组中 strcat(fullname,", ");//把“,”附加到上述fullname尾部 strncat(fullname,fname,Asize);//把fname名字附加到上述fullname尾部 fullname[2*Asize]='\0';//为防止字符型数组溢出,在数组结尾添加结束符 cout<<"Here's the information in a single string:"< return 0; } //ex4.4 使用string对象存储、显示组合结果 #include #include int main() { using namespace std; string fname,lname,attach,fullname; cout<<"Enter your first name:"; getline(cin,fname);//note:将一行输入读取到string类对象中使用的是getline(cin,str) //它没有使用句点表示法,所以不是类方法cout<<"Enter your last name:"; getline(cin,lname); attach=", "; fullname=lname+attach+fname; cout<<"Here's the information in a single string:"< return 0; } //ex4.5 declare a struct and initialize it 声明结果并创建一个变量 #include const int Asize=20; struct CandyBar { char brand[Asize]; double weight; int calory; }; int main() { using namespace std; CandyBar snack={"Mocha Munch",2.3,350}; cout<<"Here's the information of snack:\n"; cout<<"brand:"< cout<<"weight:"< cout<<"calory:"< return 0; } //ex4.6 结构数组的声明及初始化 #include const int Asize=20; struct CandyBar { char brand[Asize]; double weight; int calory; }; int main() { using namespace std; CandyBar snack[3]={ {"Mocha Munch",2.3,350}, {"XuFuJi",1.1,300}, {"Alps",0.4,100} }; for(int i=0;i<3;i++)//利用for循环来显示snack变量的内容{ cout< < < } return 0; } //ex4.7 pizza披萨饼 #include #include const int Size=20; struct pizza//声明结构 { char company[Size]; double diameter; double weight; }; int main() { using namespace std; pizza pie;//创建一个名为pie的结构变量 cout<<"What's the name of pizza company:"; cin.getline(https://www.wendangku.net/doc/c77503516.html,pany,Size); cout<<"What's the diameter of pizza:"; cin>>pie.diameter; cout<<"What's the weight of pizza:"; cin>>pie.weight; cout<<"company:"< cout<<"diameter:"< cout<<"weight:"< return 0; } //ex4.8 pizza pie 披萨饼使用new创建动态结构 #include #include const int Size=20; struct pizza//声明结构 { char company[Size]; double diameter; double weight; }; int main() { using namespace std; pizza *pie=new pizza;//使用new创建动态结构 cout<<"What's the diameter of pizza:"; cin>>pie->diameter; cin.get();//读取下一个字符 cout<<"What's the name of pizza company:"; cin.get(pie->company,Size); cout<<"What's the weight of pizza:"; cin>>pie->weight; cout<<"diameter:"< cout<<"company:"< cout<<"weight:"< delete pie;//delete释放内存 return 0; } //ex.4.9 使用new动态分配数组—方法1 #include #include using namespace std; struct CandyBar { string brand; double weight; int calory; }; int main() { CandyBar *snack= new CandyBar[3]; snack[0].brand="A";//单个初始化由new动态分配的内存snack[0].weight=1.1; snack[0].calory=200; snack[1].brand="B"; snack[1].weight=2.2; snack[1].calory=400; snack[2].brand="C"; snack[2].weight=4.4; snack[2].calory=500; for(int i=0;i<3;i++) { cout << " brand: " << snack[i].brand << endl; cout << " weight: " << snack[i].weight << endl; cout << " calorie: " << snack[i].calory << endl< } delete [] snack; return 0; } //ex.4.10 数组—方法1 #include int main() { using namespace std; const int Size = 3; int success[Size]; cout<<"Enter your success of the three times 40 meters running:\n"; cin >> success[0]>>success[1]>>success[2]; cout<<"success1:"< cout<<"success2:"< cout<<"success3:"< double average=(success[0]+success[1]+success[2])/3; cout<<"average:"< return 0; } //ex.4.10 array—方法2 #include #include int main() { using namespace std; array cout<<"Enter your success of the three times 40 meters running:\n"; cin >> ad[0]>>ad[1]>>ad[2]; cout<<"success1:"< cout<<"success2:"< cout<<"success3:"< ad[3]=(ad[0]+ad[1]+ad[2])/3; cout<<"average:"< return 0; } 第五章循环和关系表达式 //ex.5.1 #include int main() { using namespace std; cout<<"Please enter two integers: "; int num1,num2; cin>>num1>>num2; int sum=0; for(int temp=num1;temp<=num2;++temp)//or temp++ sum+=temp; cout<<"The sum from "< } //ex.5.2 #include #include int main() { using namespace std; array ad[1]=ad[0]=1L; for(int i=2;i<101;i++) ad[i]=i*ad[i-1]; for(int i=0;i<101;i++) cout< return 0; } //ex.5.3 #include int main() { using namespace std; cout<<"Please enter an integer: "; int sum=0,num; while((cin>>num)&&num!=0) { sum+=num; cout<<"So far, the sum is "< cout<<"Please enter an integer: "; } return 0; } //ex.5.4 #include int main() { using namespace std; double sum1,sum2; sum1=sum2=0.0; int year=0; while(sum2<=sum1) { ++year; sum1+=10; sum2=(100+sum2)*0.05+sum2; } cout<<"经过"< cout<<"此时,Cleo的投资价值为"< return 0; } //ex.5.5 #include const int MONTHS = 12; const char* months[MONTHS]={"January","February","March","April","May","June","July","August","Sept ember","October","November","December"}; int main() { using namespace std; int sales[MONTHS],sum=0; for(int i=0;i { cout<<"请输入在"< cin>>sales[i]; sum+=sales[i]; } cout<<"这一年中的C++ For Fools的总销售量为:"< return 0; } //ex.5.6 #include const int MONTHS = 12; const char* months[MONTHS]={"January","February","March","April","May","June","July","August","Sept ember","October","November","December"}; const char* years[3]={"第一年","第二年","第三年"}; int main() { using namespace std; int year_sale[3],sum=0,sales[3][MONTHS]; for(int i=0;i<3;i++) { int temp=0; cout< for(int j=0;j { cout<<"请输入"< cin>>sales[i][j]; temp+=sales[i][j]; } year_sale[i]=temp; sum+=year_sale[i]; } for(int i=0;i<3;i++) cout< return 0; } //ex.5.7 #include #include using namespace std; struct car{ string name; int year; }; int main() { cout<<"How many cars do you wish to catalog? "; int num; (cin>>num).get(); car* ps=new car[num]; for(int i=0;i { cout<<"Car #"< cout<<"Please enter the make: "; getline(cin,ps[i].name); cout<<"Please enter the year made: "; (cin>>ps[i].year).get(); } cout<<"Here is your collection:\n"; for(int i=0;i cout< delete [] ps; return 0; } //ex.5.8 #include #include int main() { using namespace std; char word[20]; int sum=0; cout<<"Enter words (to stop,type the word done):\n"; cin>>word; while(strcmp(word,"done")) { sum++; cin>>word; } cout<<"You entered a total of "< return 0; } //ex.5.9 #include #include int main() { using namespace std; string word; int sum=0; cout<<"Enter words (to stop, type the word done):\n"; cin>>word; while(word!="done") { sum++; cin>>word; } cout<<"You entered a total of "< return 0; } //ex.5.10 #include int main() { using namespace std; cout<<"Enter number of rows:"; int num; cin>>num; for(int i=0;i { for(int j=num-i;j>1;j--) cout<<"."; for(int k=0;k<=i;++k) cout<<"*"; cout< } return 0; }