1、C+ primer plus 编程练习25答案C+ primer plus第二章到第五章编程练习答案 第二章1:#include#define max 10using namespace std;void main() char namemax,dizhimax; coutname; coutdizhi; coutnamedizhiendl;2:#includeusing namespace std;void main() long juli; coutjuli; cout按照您输入的距离是:juli*220码endl; 3:#includeusing namespace std;void b
2、lind() coutThree blind micen;void run() coutSee how they runn;void main() for(int i=0;i2;i+) blind(); for(int j=0;j2;j+) run();4:#includeusing namespace std;void month(int age) cout该年龄一共包含age*12个月!n;void main() int age; coutage; month(age);5:#includeusing namespace std;double fahrenheit(double celsi
3、us) return 1.8*celsius+32.0;void main() double celsius; coutcelsius; coutcelsius degrees celsius is fahrenheit(celsius) degrees fahrenheit.n;6:#includeusing namespace std;double astronomical(double light) return 63240*light;void main() double light; coutlight; coutlight light years = astronomical(li
4、ght) astronomical units.n;7:#includeusing namespace std;void display(int hours,int minutes) coutTime: hours:minutesendl;void main() int hour,minute; couthour; coutminute; display(hour,minute); 第三章1:#includeusing namespace std;const float danwei=0.0833333;void iswap(int cun) cout您的身高为: cun*danwei 英尺!
5、endl;void main() int cun; coutcun; iswap(cun);2:#includeusing namespace std;const double yingchi=12;const double bang=2.2;const double memter=0.0245;void caculate(double chi,double cun,double weight) double BMI; double yingcun,mi,qianke; yingcun=cun+chi*yingchi; mi=yingcun*memter; qianke=weight/bang
6、; BMI=qianke/(mi*mi); cout您的BMI值为: BMIendl;void main() double chi,cun,weight; coutchicun; coutweight; caculate(chi,cun,weight);3:#includeusing namespace std;void main() double degrees,minutes,seconds,sum; coutEnter a latitude in degrees,minutes,and seconds:endl; coutdegrees; coutminutes; coutseconds
7、; sum=degrees+minutes/60+seconds/3600; coutdegrees degrees,minutes minutes,seconds seconds= sum degrees.endl;4:#includeusing namespace std;const long m=60;const long h=60;const long d=24;int sumday(long seconds) long hour,minute; minute=seconds/m; hour=minute/h; return hour/d;int sumhour(long second
8、s,int day) long minute; seconds=seconds-day*d*h*m; minute=seconds/m; return minute/h;int summinute(long seconds,int day,int hour) seconds=seconds-(day*d*h*m+hour*h*m); return seconds/m;int sumsecond(long seconds,int day,int hour,int minute) return seconds=seconds-(day*d*h*m+hour*h*m+minute*m);void m
9、ain() long seconds; int day,hour,minute,second; coutseconds; day=sumday(seconds); hour=sumhour(seconds,day); minute=summinute(seconds,day,hour); second=sumsecond(seconds,day,hour,minute); coutseconds seconds = day days,hour hours,minute minutes,second seconds.endl;5:#includeusing namespace std;void
10、main() double world,us; coutworld; coutus; double bilv; bilv=us/world; coutThe population of the us is bilv% of the world population.endl;6:#includeusing namespace std;void main() float memter,jialun; cout以美国风格还是欧洲风格显示耗油量?m为美国,o为欧洲!endl; coutc; if(c=m) coutmemter; coutjialun; cout汽车耗油量为:memter/jialu
11、nmpg.endl; else coutmemter; coutjialun; float ofg; ofg=(100*jialun)/memter; cout汽车耗油量为:ofgL/100Km.endl; 7:includeusing namespace std;void main() coutofg; float jialun; jialun=ofg/3.875; float haoyou; haoyou=62.14/jialun; cout转换成美国风格的耗油量(一加仑的里程,mpg):haoyoumpg.endl; 第四章1:#include#includeconst int num=
12、10;using namespace std;int main() coutWhats your first name?; char firstnum; cin.getline(first,num); coutlast; coutgrade; coutage; cout-endl; coutName: last,firstendl; coutGrade: char (grade+1)endl; coutAge: ageendl; return 0;2:#include#includeusing namespace std;int main() string name,dessert; cout
13、Enter your name:n; getline(cin,name); cin.get(); coutEnter your favorite dessert:n; getline(cin,dessert); coutI have some delicious dessert for you, name.n; return 0; 3:#include#includeusing namespace std;int main() coutfirst; coutlast; strcat(last, ); strcat(last,first); coutHeres the information i
14、n a single string: lastendl; return 0; 4:include#includeusing namespace std;int main() coutfirst; coutlast; last=last+, ; last=last+first; coutHeres the information in a single string: lastendl; return 0; 5:#include#includeusing namespace std;struct CandyBar char brand20; double weight; long calorie
15、s;int main() CandyBar snack= Mocha Munch,2.3,350 ; coutsnack.brandendl; coutsnack.weightendl; coutsnack.caloriesendl; return 0; 6:#include#includeusing namespace std;struct CandyBar char brand20; double weight; long calories;int main() CandyBar snack3= Mocha Munch,2.3,350 , caorui,3.6,456 , denger,4
16、.7,877 ; for(int i=0;i3;i+) cout-endl; coutsnacki.brandendl; coutsnacki.weightendl; coutsnacki.caloriesendl; cout-endl; return 0; 7:#include#include#includeusing namespace std;struct pizza string company; double diameter; double weight;int main() pizza p; coutPlease input the company of manufacture
17、pizza: ; getline(cin,pany); coutp.diameter; coutp.weight; coutName : pany,and the company name is form of pany.size() words.endl; coutDiameter: p.diameterendl; coutWeight: p.weightendl; return 0; 8:#include#include#includeusing namespace std;struct pizza string company; double diameter; double weigh
18、t;int main() pizza *p=new pizza; coutp-diameter; cin.get(); coutcompany); coutp-weight; coutName : company,and the company name is form of company.size() words.endl; coutDiameter: diameterendl; coutWeight: weightendl; return 0; 9:#include#includeusing namespace std;struct CandyBar char brand20; doub
19、le weight; long calories;int main() CandyBar *snack=new CandyBar3; strcpy(snack-brand,shanghai); snack-weight=1.2; snack-calories=7; strcpy(snack+1)-brand,beijing); (snack+1)-weight=2.3; (snack+1)-calories=8; strcpy(snack+2)-brand,guangzhou); (snack+2)-weight=3.4; (snack+2)-calories=9; for(int i=0;i
20、3;i+) cout-endl; coutsnacki.brandendl; coutsnacki.weightendl; coutsnacki.caloriesendl; cout-endl; return 0; 10:#include#includeusing namespace std;int main() double grade3; coutPlease input three grades of running.endl; for(int i=0;i3;i+) coutThe i+1gradei; cout一共跑了3次,平均成绩为: (grade0+grade1+grade2)/3
21、 码.endl; return 0; 第五章1:#includeusing namespace std;int main() int x,y; coutplease input two numbers.xy; double sum=0; for(int i=x;i=y;i+) sum+=i; coutx to y sums is: sumendl; system(pause); return 0;2:#includeusing namespace std;int main() long double jiechen=1; for(int i=2;i=100;i+) jiechen=i*jiechen; cout100!=jiechenendl; system(pause); return 0;3:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1