C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 5-9

6.5
//caculate the income tax with excess progressive of tax rate
#include <iostream>
using namespace std;
const float Tax_Rate1=0.1;
const float Tax_Rate2=0.15;
const float Tax_Rate3=0.2;
const int Tax_Level1=5000;
const int Tax_Level2=15000;
const int Tax_Level3=35000;

int main()
{
double income,tax=0;
cout<<"Welcome to Kingdom Neutronia!\n";
cout<<"Please enter your income(tvarps): ";
while(!(cin>>income)||income<=0)
{
   cout<<"You entered the wrong data!\n";
   cout<<"Bye!!\n";
}

if(income<=5000)
cout<<"You don't have to pay tax\n";
else if(income<=15000){
tax=(income-Tax_Level1)*Tax_Rate1;
cout<<"You have to pay "<<tax<<" tvarps tax\n";
}
else if(income<=35000){
   tax=(Tax_Level2-Tax_Level1)*Tax_Rate1+(income-Tax_Level2)*Tax_Rate2;
   cout<<"You have to pay "<<tax<<" tvarps tax\n";
}
   
else {
   tax=(Tax_Level2-Tax_Level1)*Tax_Rate1+(Tax_Level3-Tax_Level2)*Tax_Rate2+(income-Tax_Level3)*Tax_Rate3;
   cout<<"You have to pay "<<tax<<" tvarps tax\n";
}
           
return 0;//dd 
}

6.6
//enter the patrons' name and the money they donate
//then select who donate more than 10000 and display them
#include <iostream>
#include <string>
using namespace std;

struct Patron

{
string name;
double amount;
};

int main()
{
int num;
cout<<"Enter the number of Patrons: ";
cin>>num;
cin.get();//don't forget this
Patron* patrons=new Patron[num];

for(int i=0;i<num;++i)
{
cout<<"The #"<<i+1<<" patron.\n";
cout<<"Name: ";
getline(cin,patrons[i].name);
cout<<"Amount: ";
cin>>patrons[i].amount;
cin.get();
  
}
int count;//count the number in case there's none.
    cout<<"Grand Patrons"<<endl;
    for(int i=0;i<num;++i)
    {  
    if(patrons[i].amount>10000){    
    cout<<patrons[i].name <<" donates "<<patrons[i].amount<<endl;
    ++count;
}
}
    
if(count==0)
    cout<<"None!";
    
count=0;
cout<<"\nPatrons"<<endl;
    for(int i=0;i<num;++i)
    {  
    if(patrons[i].amount<=10000){    
    cout<<patrons[i].name <<" donates "<<patrons[i].amount<<endl;
    ++count;
}
}
    
if(count==0)
    cout<<"None!";
return 0;//dd
 
}

6.7
//the program will read a word at one time until your entered 'q'
//then it will display the result that how many words that begin with
//vowels,consonants and others little

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
const int ArSize=20;


int main()
{
cout<<"Enter words (q to quit)\n";
char words[ArSize];
int vowels=0,consonants=0,others=0;
cin>>words;
while(strcmp(words,"q"))
{
  
if(isalpha(words[0])!=0)
{
switch(words[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':++vowels;break;
default: ++consonants;
}
}
else
   ++others;
cin>>words;
}
cout<<vowels<<" words beginning with vowels\n";
    cout<<consonants<<" words beginning with consonants\n";
    cout<<others<<" others\n";
cout<<"Bye!";
return 0; //dd
}
6.8
// you can enter a file name(.txt)
//the program will open the file 
//and it will read characters one by one until the EOF
//then it will count how many characters it contains 
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int ArSize=20;

int main()
{
char filename[ArSize];
ifstream inFile;
cout<<"Please enter a file name.\n";
cin.getline(filename,ArSize);
inFile.open(filename);
if(!inFile.is_open()){
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating";
exit(EXIT_FAILURE);
}
char ch;
int count=0;
inFile>>ch;
while(inFile.good())
{
++count;
inFile>>ch;
}
if(inFile.eof())
   cout<<"End of file reached.\n";
else if(inFile.fail())
   cout<<"Input terminated by data mismatch.\n";
else
   cout<<"Input terminated for unknown reason.\n";
cout<<"There are "<<count<<" characters includes in the file.";
inFile.close();
inFile.get();
inFile.get();
return 0;//dd
}

6.9
//this program is another vision of 6.6
//and it will open a file whose name is entered
//then read the file , display the treated result
#include <iostream>
#include <fstream>
#include <cstdlib> //suport for exit()
using namespace std;
const int ArSize=20;

struct Patron

{
string name;
double amount;
};

int main()
{
char filename[ArSize];
ifstream inFile;
cout<<"Enter name of data file\n";
cin.getline(filename,ArSize);
inFile.open(filename);
if(!inFile.is_open())//failed to open file
    {
    cout<<"Could not open the file"<<filename<<endl;
    cout<<"Program terminating.\n";
    exit(EXIT_FAILURE);
    }
    
int num;
inFile>>num;
inFile.get();//don't forget this
Patron* patrons=new Patron[num];

for(int i=0;i<num;++i)
{

getline(inFile,patrons[i].name);
inFile>>patrons[i].amount;
inFile.get();
  
}
int count;  //count the number in case there's none.
    cout<<"Grand Patrons"<<endl;
    for(int i=0;i<num;++i)
    {  
    if(patrons[i].amount>10000){    
    cout<<patrons[i].name <<" donates "<<patrons[i].amount<<endl;
    ++count;
}
}
    
if(count==0)
    cout<<"None!";
    
count=0;
cout<<"\nPatrons"<<endl;
    for(int i=0;i<num;++i)
    {  
    if(patrons[i].amount<=10000){    
    cout<<patrons[i].name <<" donates "<<patrons[i].amount<<endl;
    ++count;
}
}
    
if(count==0)
    cout<<"None!";
    inFile.close();
    return 0;//dd
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值