1.
#include<iostream>
usingnamespacestd;
constintinch_feet=12;
intmain()
{
cout<<"Pleaseinputyourheightininches:___\b\b\b";
intffeet;
cin>>ffeet;
intfinch;
finch=ffeet/inch_feet;
ffeet=ffeet%inch_feet;
cout<<"yourheightis"<<finch<<"inchesand"<<ffeet<<"feets."<<endl;
return0;
}
2.
#include<iostream>
usingnamespacestd;
constintinch_to_feet=12;
constfloatfeet_to_m=0.0254;
constfloatkg_to_pounds=2.2;
intmain()
{
intfinch,ffeet;
doublefpound;
cout<<"ThisisaprogramtogiveyourBMI"<<endl;
cout<<"pleaseinputyourheightininchesandfeet:"<<endl;
cout<<"inches:__\b\b";
cin>>finch;
cout<<"feet:__\b\b";
cin>>ffeet;
cout<<"Nowpleaseinputyourweighthinpounds:__\b\b";
cin>>fpound;
doublefh_m;
doublefkg;
fh_m=(finch*inch_to_feet+ffeet)*feet_to_m;
fkg=fpound/2.2;
doublebmi;
bmi=fkg/(fh_m*fh_m);
cout<<endl;
cout<<"yourBMIis"<<bmi<<endl;
cout<<"Thanksforyouruse!"<<endl;
return0;
}
其他的类似
第四章
1.
#include<iostream>
usingnamespacestd;
structPersoninfo
{
charfirstname[20];
charlastname[20];
chargrade;
intage;
};
voiddisplay(constPersoninfo&per);
intmain()
{
Personinfoone;
cout<<"Whatisyourfirstname?";
cin.getline(one.firstname,20);
cout<<endl<<"Whatisyourlastname?";
cin.getline(one.lastname,20);
cout<<endl<<"Whatyourgrade?";
cin>>one.grade;
cout<<endl<<"Whatisyourage?";
cin>>one.age;
display(one);
return0;
}
voiddisplay(constPersoninfo&per)
{
cout<<endl<<"Name:"<<per.lastname<<","<<per.firstname;
cout<<endl<<"Grade:"<<(char)(per.grade+1)\;
cout<<endl<<"Age:"<<per.age;
}
2.
#include<iostream>
usingnamespacestd;
structPersoninfo
{
stringfirstname;
stringlastname;
chargrade;
intage;
};
voiddisplay(constPersoninfo&per);
intmain()
{
Personinfoone;
cout<<"Whatisyourfirstname?";
getline(cin,one.firstname);
cout<<endl<<"Whatisyourlastname?";
getline(cin,one.lastname);
cout<<endl<<"Whatyourgrade?";
cin>>one.grade;
cout<<endl<<"Whatisyourage?";
cin>>one.age;
display(one);
return0;
}
voiddisplay(constPersoninfo&per)
{
cout<<endl<<"Name:"<<per.lastname<<","<<per.firstname;
cout<<endl<<"Grade:"<<(char)(per.grade+1);
cout<<endl<<"Age:"<<per.age;
}
3.
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
charfirstname[20];
charlastname[20];
charfullname[42];
cout<<"Enteryourfirstname:";
cin.getline(firstname,20);
cout<<endl<<"Enteryourlastname:";
cin.getline(lastname,20);
strcpy(fullname,lastname);
//strcpy(fullname,","); //copy擦除原来的字符串
strcat(fullname,",");
strcat(fullname,firstname);
cout<<endl<<"Hereistheinformationinasinglestring:"<<fullname;
return0;
}
4.
#include<iostream>
usingnamespacestd;
intmain()
{
stringfirstname;
stringlastname;
stringfullname;
cout<<"Enteryourfirstname:";
getline(cin,firstname);
cout<<endl<<"Enteryourlastname:";
getline(cin,lastname);
fullname=lastname+","+firstname;
cout<<endl<<"Hereistheinformationinasinglestring:"<<fullname;
return0;
}
5.
#include<iostream>
usingnamespacestd;
structCandyBar
{
stringName;
doubleweighth;
intKluli;
};
voiddisplay(constCandyBar&);
intmain()
{
CandyBarsnack={"MochaMunch",2.3,350};
display(snack);
return0;
}
voiddisplay(constCandyBar&candy)
{
cout<<endl<<"TheBrandis:"<<candy.Name;
cout<<endl<<"Theweightis:"<<candy.weighth;
cout<<endl<<"Thekululiis:"<<candy.Kluli;
}
6.
#include<iostream>
usingnamespacestd;
structCandyBar
{
stringName;
doubleweighth;
intKluli;
};
voiddisplay(constCandyBar&);
intmain()
{
CandyBarsnack[3]={{"MochaMunch",2.3,350},{"Fanlei",3.3,450},{"Blingjie",4.3,550}};
display(snack[0]);
display(snack[1]);
display(snack[2]);
return0;
}
voiddisplay(constCandyBar&candy)
{
cout<<endl<<"TheBrandis:"<<candy.Name;
cout<<endl<<"Theweightis:"<<candy.weighth;
cout<<endl<<"Thekululiis:"<<candy.Kluli;
}
9.
#include<iostream>
usingnamespacestd;
structCandyBar
{
stringName;
doubleweighth;
intKluli;
};
voiddisplay(constCandyBar&);
intmain()
{
CandyBar*p=newCandyBar[3];
*p={"MochaMunch",2.3,350};
*(p+1)={"Fanlei",3.3,450};//c++11可以这样列表初始化,若不允许,则各个元素分别赋值
*(p+2)={"Blingjie",4.3,550};
display(p[0]);
display(p[1]);
display(p[2]);
delete[]p;
return0;
}
voiddisplay(constCandyBar&candy)
{
cout<<endl<<"TheBrandis:"<<candy.Name;
cout<<endl<<"Theweightis:"<<candy.weighth;
cout<<endl<<"Thekululiis:"<<candy.Kluli;
}