c++ primer plus第六章编程答案

#编程练习1
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    cout << "Enter something: ";
    char ch;
    while ( cin.get(ch) && ch != '@')
    {
        if (!isdigit(ch))
        {
             if (islower(ch))
                cout << char(toupper(ch)) ;
            else if(isupper(ch))
            cout <<  char(tolower(ch));
        }
    }
    cout << "Hello world!" << endl;
    return 0;
}
#编程练习2
#include <iostream>
#include <array>
const int ArSize = 10;
using namespace std;
int main()
{
    cout << "Enter donation.\n";
    int i=0;
    double sum = 0.0;
    cout << "#donation[" << i << "]: ";
   array<double,ArSize> donation;
   //double donation[ArSize];
    while (i < ArSize && cin >> donation[i])//注意要先判断i是否符合数组大小;
    {
        sum += donation[i];
          if(++i < ArSize)//这里需要再次确认i的范围
        cout << "#donation[" << i << "]: ";
    }
    double average;
    average = sum / i;
    cin >> fixed;
    cin.precision(6);
    cout << "Average is : " << average << endl;
    int count = 0;
    for (int j=0 ;j<i;j++)
    {
        if(donation[j]>average)
        count ++;
    }
    cout << "There are " << count << " number bigger than average.\n";
    return 0;
}
//编程练习3
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    cout << "Please enter one of the following choices:\n"
    << "c) carnivore\tp) pianist\n"
    << "t) tree\t\tg) game\n";
    char ch;
  while (cin >> ch)//输入一个字符
{
    switch (ch)
    {
    case 'c': cout << "carnivore\n";
            exit(EXIT_FAILURE);
            break;
    case 'p': cout << "pianist\n";
            exit(EXIT_FAILURE);
            break;
    case 't':cout << "A maple is a tree.\n";
            exit(EXIT_FAILURE);
            break;
    case 'g':cout << "This is a game.\n";
            exit(EXIT_FAILURE);
            break;
    default:cout << "Please enter a a,p ,t, or g:";
    }
}
    return 0;
}
//编程练习4 (复杂一点的可以用函数来实现)
#include <iostream>
void show();
void enter();
void display_fullname();
void display_title();
void display_bopname();
void display_preference();
const int strsize = 50;
const int ArSize = 5;
struct bop
{
    char fullname[strsize];
    char title[strsize];
    char bopname[strsize];
    int preference;//0 = fullname, 1 =title ;2 =bopname;
};
using namespace std;
 const bop Bopname[ArSize] =
                        {
                           {"ChengJie","CJ","Xcj",2},
                         {"HouJie","HJ","Xhj",0},
                         {"ChenYiHou","CYH","Xcyh",1},
                        {"BaoChen","BC","Xbc",2},
                       {"YangLuQian","YLQ","Xylq",1}
                        };
int main()
{
    show();
    char str;
    cout << "Enter your choice: ";
    cin >> str;
    while (cin && str!='q')
    {
    switch (str)
    {
     case 'a' :
        display_fullname();
        break;
     case 'b':
         display_title();
         break;
     case 'c':
         display_bopname();
         break;
     case 'd':
         display_preference();
         break;
    }
    enter();
    cin >>str;
    }
    if (str =='q')
        cout << "Bye!" << endl;
    return 0;
}
void show()
{
    cout << "Benevolent Order Programmers Report\n"
    <<"a. display by name\tb. display by title\n"
    << "c. display by bopname\td. display by preference\n"
    << "q. quit\n";
}
void enter()
{
        cout << "Next choice: ";
}
void display_fullname()
{
    for (int i=0;i<ArSize;i++)
                cout << Bopname[i].fullname <<endl;
}
void display_title()
{
     for (int j=0;j<ArSize;j++)
                cout << Bopname[j].title <<endl;
}
void display_bopname()
{
     for (int m=0;m<ArSize;m++)
                cout << Bopname[m].bopname <<endl;
}
void display_preference()
{
    for (int n=0;n<ArSize;n++)
               {
                if (Bopname[n].preference ==0 )
                    cout << Bopname[n].fullname<< endl;
                else if (Bopname[n].preference ==1)
                    cout <<Bopname[n].title <<endl;
                else if (Bopname[n].preference ==2)
                    cout << Bopname[n].bopname <<endl;
               }
}
//编程练习5
#include <iostream>
double pay(long num);
using namespace std;
int main()
{
    long income;
    double income_tax;
    cout << "Enter your income: ";
    while (cin >>income && income > 0 )
    {
        income_tax = pay (income);
        cout << income << " have " << income_tax <<" tvarps.\n";
        cout << "Enter your income: ";
    }
    cout << "end!\n";
    return 0;
}
double pay(long num)
{
    double tax;
    if (num >= 0 && num <=5000)
        {
            tax = 0;
            return tax;
            }
    else if (num >=5001 && num <= 15000)
        {
            tax = (num-5000)*0.10;
            return tax;
            }
    else if (num >= 15001 && num <=35000)
        {
            tax = (num-15000)*0.15 + 10000*0.1;
            return  tax;
            }
    else
        {
            tax = (num-35000)*0.2+ 20000*0.15 + 10000*0.1;
            return tax;
            }
}
//编程练习6
#include <iostream>
#include <string>
using namespace std;
struct donation
{
    string name;
    double money;
};
int main()
{
    int num;
    cout << "Please input the number of donor: ";
    cin >> num;
    cout << "Please input donor's name and fund.\n";
    donation *ps = new donation[num];
    for (int i = 0; i<num ;i++)
    {
        cin.get();
      cout << "#"<< i+1 <<": name: ";
      getline(cin,(ps+i)->name);
      cout << "\tfund: " ;
      cin >> (ps+i)->money;
    }
    int count =0;
    cout << "Grand Patrons:\n";
    for(int j=0;j<num;j++)
    {
        if (((ps+j)->money )> 10000)
        {
                count++;
                cout <<  (ps+j)->name << ":" << (ps+j)->money << endl;
        }
    }
    if (count == 0)
    {
       cout << "none\n";
    }
    cout << "\n";
    cout << "Patrons: \n";
    count = 0;
     for( int j =0;j<num;j++)
     {
         if (((ps+j)->money) <10000)
         {
             cout << (ps+j)->name <<endl;
             count ++;
         }
     }
    if (count==0)
    {
    cout << "none\n";
    }
    delete [] ps;
    return 0;
}
//编程练习7(注意是一个单词一个单词的检查)
#include <iostream>
#include <cctype>
const int ArSize = 50;
using namespace std;
int main()
{
    cout << "Enter words (q to quit): \n";
    char ch[ArSize];
    int count = 0;
    int  consonants = 0;
    int others = 0;
    cin >> ch;
    while(ch[0]!='q')
    {
        if(isalpha(ch[0]))
            switch(ch[0])
        {
            case 'a': count ++;break;
            case 'e': count ++;break;
            case 'i': count ++;break;
            case 'o': count ++;break;
            case 'u': count ++;break;
            default : consonants++;
        }
        else
        {
         others++;
        }
        cin >> ch;
    }
    cout<< count <<" words beginning with vowels\n ";
    cout << consonants << " words beginning with consonants\n";
    cout << others << " others\n";
    return 0;
}
//编程练习8
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    ifstream inFile;
    inFile.open("test.txt");
    if(!inFile.is_open())
    {
        cout << "Could not open the file!\n";
    exit (EXIT_FAILURE);
    }
    int ch;// char ch;  
    int count = 0;
    ch = inFile.get();// inFile >> ch;inFile.get(ch)
    while (ch!=EOF) //EOF 是-1,所以这里ch一定要是int型     // while (inFile.eof()==false)
    {
        count ++;
         cout << char(ch) ;
        ch = inFile.get();   //inFile >> ch;
    }
    cout << "The total words in this words are: "
    << count << endl;
    inFile.close();
    cin.get();
    return 0;
}
//编程练习9(利用文件输入,有必要时要用到cout显示)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct donation
{
    string name;
    double money;
};
int main()
{
    ifstream inFile;
    inFile.open("test.txt");
    if(!inFile.is_open())
    {
        cout << "Could not open the file!\n";
        exit (EXIT_FAILURE);
    }
     int num;
    cout << "Please input the number of donor: ";
    inFile >> num;
    cout << num <<endl;//比第6题多写一步cout
    cout << "Please input donor's name and fund.\n";
    donation *ps = new donation[num];
    while(inFile.eof()==false)
    {
        for (int i = 0; i<num ;i++)
    {
        inFile.get();
      cout << "#"<< i+1 <<": name: ";
      getline(inFile,(ps+i)->name);
      cout << (ps+i)->name<<endl;
      cout << "\tfund: " ;
      inFile >> (ps+i)->money;
      cout << (ps+i)->money << endl;
    }
    }
    int count =0;
    cout << "Grand Patrons:\n";
    for(int j=0;j<num;j++)
    {
        if (((ps+j)->money )> 10000)
        {
                count++;
                cout <<  (ps+j)->name << ":" << (ps+j)->money << endl;
        }
    }
    if (count == 0)
    {
       cout << "none\n";
    }
    cout << "\n";
    cout << "Patrons: \n";
    count = 0;
     for( int j =0;j<num;j++)
     {
         if (((ps+j)->money) <10000)
         {
             cout << (ps+j)->name <<endl;
             count ++;
         }
     }
    if (count==0)
    {
    cout << "none\n";
    }
    delete [] ps;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值