C+Primer Plus第6章编程题(下)

原书为Stephen Prata所著《C++ Primer Plus(5th Edition)》的课后编程题。因为时间关系,在此不再重新附原题题干,大家可以在网上查找。

第五题:

/Chapter 6 Homework 5

#include

constdoublelevel1=0.1;

constdoublelevel2=0.15;

constdoublelevel3=0.2;

usingnamespacestd;

intmain(){

doubletvarp;

doublecus;

cout

cin>>tvarp;

while(cin.good()&&(tvarp>))

{

if(tvarp

cus=0.0;

elseif(tvarp>5000&&tvarp

cus=(tvarp-5000)*level1;

elseif(tvarp>15000&&tvarp

cus=10000*level1+(tvarp-15000)*level2;

elseif(tvarp>35000)

cus=10000*level1+20000*level2+(tvarp-35000)*level3;

cout

cout

cin>>tvarp;

}

cout

system("pause");

return;

}

第六题:

//Chapter 6 Homework 6

#include

#include

#include

usingnamespacestd;

structdonation

{

stringname;

doublemoney;

};

intmain(){

intnum;

intcount=;

cout

cin.get();//吃掉换行符

donation*donaer=newdonation[num];

for(inti=;i

{

cout

getline(cin,donaer[i].name);

cout

cin>>donaer[i].money;

cin.get();//吃掉换行符

}

cout

cout

cout

for(inti=;i

if(donaer[i].money>10000){

++count;

cout

cout

cout

}

}

if(count==)

{

cout

}

count=;

cout

cout

cout

for(inti=;i

if(donaer[i].money

++count;

cout

cout

cout

}

}

if(count==)

{

cout

}

delete[]donaer;

system("pause");

return;

}

第七题:

//Chapter 6 Homework 7

#include

#include

#include

usingnamespacestd;

intmain(){

stringword;

charch;

intyuan=;

intfu=;

intothers=;

cin>>word;

while(word!="q"){

ch=word[];

if(isalpha(ch))

{

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'

||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

++yuan;

else

++fu;

}

else

++others;

cin>>word;

}

cout

cout

cout

system("pause");

return;

}

第八题:

//Chapter 6 Homework 8

#include

#include

#include

usingnamespacestd;

constintMAXSIZE=50;

intmain(){

charfilename[MAXSIZE];

ifstreaminFile;

cout

cin.getline(filename,MAXSIZE);

inFile.open(filename);

if(!inFile.is_open()){

cout

exit(EXIT_FAILURE);

}

charch;

intcount=;

inFile>>ch;

while(inFile.good()){

++count;

inFile>>ch;

}

if(inFile.eof()){

cout

}

elseif(inFile.fail()){

cout

}

else

cout

if(count==){

cout

}

else{

cout

}

inFile.close();

system("pause");

return;

}

第九题:

//Chapter 6 Homework 9

#include

#include

#include

#include

usingnamespacestd;

constintMAXSIZE=50;

structdonation

{

stringname;

doublemoney;

};

intmain(){

charfilename[MAXSIZE];

ifstreaminFile;

cout

cin.getline(filename,MAXSIZE);

inFile.open(filename);

if(!inFile.is_open()){

cout

exit(EXIT_FAILURE);

}

intnum;

intcount=;

inFile>>num;

inFile.get();//吃掉换行符

donation*donaer=newdonation[num];

for(inti=;i

{

getline(inFile,donaer[i].name);

inFile>>donaer[i].money;

inFile.get();//吃掉换行符

}

cout

cout

cout

for(inti=;i

if(donaer[i].money>10000){

++count;

cout

cout

cout

}

}

if(count==)

{

cout

}

count=;

cout

cout

cout

for(inti=;i

if(donaer[i].money

++count;

cout

cout

cout

}

}

if(count==)

{

cout

}

delete[]donaer;

inFile.close();

system("pause");

return;

}

本题答案为本人自己编写或参考别人程序所写,在本机条件下运行成功,但未测试在其它操作系统、编译环境情况下的结果。因此仅供参考。

本人编译环境如下:

操作系统:Windows 10 x64(v1803)

编译器:Microsoft Visual Studio 2015(x64)

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181030G1KOV000?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券