1、第二章:开始学习C++
//ex2.1--display your name and address
#include
2、n()
{
using namespace std;
cout<<"enter the distance measured by furlong units:";
double fur;
cin>>fur;
cout<<"convert the furlong to yard"< 3、}
//ex2.3-每个函数都被调用两次
#include 4、am>
int main()
{
using namespace std;
cout<<"Enter your age:";
int age;
cin>>age;
int month;
month=age*12;
cout< 5、{
using namespace std;
cout<<"please enter a Celsius value:";
double C;
cin>>C;
double F;
F=C2F(C);
cout< 6、把光年转换为天文单位
#include 7、ical units."< 8、";
cin>>h;
cout<<"enter the number of minutes:";
cin>>m;
cout<<"Time:"< 9、ght 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 "< 10、
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:"< 11、r 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_k 12、ilogram=wt_pound/pound_per_kilogram;
cout< 13、x(体重指数) is "< 14、e;
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/minutes_per_degree/seconds_per_ 15、minute;
cout< 16、ter 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 17、per_minute;
cout< 18、out<<"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 "< 19、100Km)
#include 20、asoline<<" miles per gallon\n";
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 21、/k_distance<<" liters of petrol per 100 kilometers\n";
return 0;
}
//ex3.7 automobile gasoline consumption-耗油量--欧洲风格(L/100Km)转换成美国风格(mpg)
#include 22、 100 kilometers):";
double Euro_style;
cin>>Euro_style;
cout<<"Converts to U.S. style(miles per gallon):"< 23、out 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-耗油量--美国 24、风格(mpg)转换成欧洲风格(L/100Km)
#include 25、<" mpg = "<< 62.14*3.875/US_style<<"L/100Km\n";
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 s 26、tudent
#include 27、象)
cin.getline(lcg.firstname,Asize);
cout<<"what is your last name?"< 28、
cout<<"Name: "< 29、<"Enter your name: \n";
getline(cin,name);
cout<<"Enter your favorite dessert: \n";
getline(cin,dessert);
cout<<"I have some delicious "< 30、 {_Chg = true;
_I.rdbuf()->sbumpc();//修改后的
break; }
ex4.3 输入其名和姓,并组合显示
#include 31、our 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名字附加到上述ful 32、lname尾部
fullname[2*Asize]='\0';//为防止字符型数组溢出,在数组结尾添加结束符
cout<<"Here's the information in a single string:"< 33、out<<"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:"< 34、name< 35、the information of snack:\n";
cout<<"brand:"< 36、
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< 37、
//ex4.7 pizza披萨饼
#include 38、e);
cout<<"What's the diameter of pizza:";
cin>>pie.diameter;
cout<<"What's the weight of pizza:";
cin>>pie.weight;
cout<<"company:"< 39、new创建动态结构
#include 40、);//读取下一个字符
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:"< 41、 pie;//delete释放内存
return 0;
}
//ex.4.9 使用new动态分配数组—方法1
#include 42、ight=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;
c 43、out << " calorie: " << snack[i].calory << endl< 44、in >> success[0]>>success[1]>>success[2];
cout<<"success1:"< 45、ude 46、 47、num1;temp<=num2;++temp)//or temp++
sum+=temp;
cout<<"The sum from "< 48、1;i++)
ad[i]=i*ad[i-1];
for(int i=0;i<101;i++)
cout<
int main()
{
using namespace std;
cout<<"Please enter an integer: ";
int sum=0,num;
while((cin>>num)&&num!=0)
{
sum+=num;
49、 cout<<"So far, the sum is "<
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818