首页 > 编程语言 >C++PrimerPlus中文第六版第6章编程练习答案

C++PrimerPlus中文第六版第6章编程练习答案

时间:2022-11-13 23:44:23浏览次数:44  
标签:std main PrimerPlus cout int C++ 第六版 using include

1、

#include<iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter text:\n";
    while (cin.get(ch) && ch != '@')
    {
        if (isalpha(ch))
            ch = tolower(ch);
        if (isdigit(ch))
            continue;
        cout << ch;
    }

}

2、

#include<iostream>
#include<array>
using namespace std;

const int Max = 10;

int main()
{
    array<double, Max> donation;
    int i = 0, count = 0;
    double sum = 0;
    cout << "Enter a series of double(number up to 10 and terminate with non-number):\n";
    while ((cin >> donation[i]) and i < Max)
    {
        count++;
        sum += donation[i];
        i++;
    }

    cout << count << " number has been read.\n";
    double ave;
    cout << "The average is " << (ave = sum / count) << endl;
    int largers = 0;
    i = 0;
    while (i++ < count)
    {
        if (donation[i] > ave)
            largers++;
    }
    cout << largers <<" donations are larger than average.\n";
}

3、

#include<iostream>
#include<array>
using namespace std;

const int Max = 10;

int main()
{
    cout << "Please enter one of the following choice:\n";
    cout << "a)alpha\t\tc)carnivore\np)pianist\t\tt)tree\ng)game\n";
    char ch;
    while (cin.get(ch) && !('a' == ch || 'c' == ch || 'p' == ch || 't' == ch || 'g' == ch))
    {
        cout << "Please enter a, c, p, t or g: ";
    }
    cout << "A sample is a ";
    switch (ch)
    {
    case 'a': cout << "alpha\n"; break;
    case 'c': cout << "carnivore\n"; break;
    case 'p': cout << "pianist\n"; break;
    case 't': cout << "tree\n"; break;
    case 'g': cout << "game\n"; break;
    default: "error"; break;
    }

    
}

4、

#include<iostream>
using namespace std;

const int strsize = 20;

struct bop
{
    char fullname[strsize]; // real name
    char title[strsize]; // job title
    char bopname[strsize]; // secret BOP name
    int preference; // 0 = fullname, 1 = title, 2 = bopname
};

void printInfo(bop*, int, bool, bool, bool, bool);
void printInfoCond(bop*, int);

int main()
{
    bop members[5] =
    {
        {"Wimp Macho", "manager", "WM", 0},
        {"Raki Rhodes", "programmer001", "RR", 0},
        {"Celia Laiter", "programmer002", "CL", 0},
        {"Hoppy Hipman", "programmer003", "HH", 0},
        {"Pat Hand", "programmer004", "PH", 0}
    };

    cout << "Benevolent Order of Programmers Report\n";
    cout << "a. display by name\t\tb. display by title\nc. display by bopname\t\td.diaplay by preference\nq.quit\n";
    cout << "Enter your choice: ";
    char choice;
    while (!(cin >> choice) || (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'q'))
    {
        cout << "Enter a valid choice with a, b, c, d or q: ";
    }

    bool isa, isb, isc, isq;
    isa = isb = isc = isq = false;
    switch (choice)
    {
    case 'a': isa = true; break;
    case 'b': isb = true; break;
    case 'c': isc = true; break;
    case 'q': isq = true; break;
    default: break;
    }
    
    printInfo(members, 5, isq, isa, isb, isc);

}

void printInfo(bop* members, int n, bool isq, bool isa, bool isb, bool isc)
{
    if (isq)
    {
        cout << "Quit!\n";
        return;
    }

    bop* member = members;
    if (isa)
    {
        for (int i = 0; i < n; i++)
            cout << member++->fullname << endl;
    }
    else if (isb)
    {
        for (int i = 0; i < n; i++)
            cout << member++->title << endl;
    }
    else if (isc)
    {
        for (int i = 0; i < n; i++)
            cout << member++->bopname << endl;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            printInfoCond(member, member->preference);
            member++;
        }
    }

}

void printInfoCond(bop* member, int pref)
{
    switch (pref)
    {
    case 0: cout << member->fullname << endl; break;
    case 1: cout << member->title << endl; break;
    case 2: cout << member->bopname << endl; break;
    default: cout << "Preference " << pref << " doesn't match any information!\n"; break;
    }
}

5、

#include<iostream>
using namespace std;

double incomeTax(double);

int main()
{
    double earning;
    cout << "Enter the earning int tvarps: ";
    while (cin >> earning && earning >= 0)
    {
        cout << "Tax is " << incomeTax(earning) << " for earning " << earning << " tvarps\n";
        cout << "Enter the earning int tvarps: ";
    }
}

double incomeTax(double income)
{
    double tax;
    if (income <= 5000)
        tax = 0;
    else if (income <= 15000)
        tax = (income - 5000) * 0.1;
    else if (income <= 35000)
        tax = (income - 15000) * 0.15 + 10000 * 0.1;
    else
        tax = (income - 35000) * 0.2 + 20000 * 0.15 + 10000 * 0.1;

    return tax;
}

6、

#include<iostream>
#include<string>
using namespace std;

const int StrSize = 20;

struct contri
{
    char name[StrSize];
    double fund;
};
int main()
{
    int n;
    cout << "Please enter the number of contributers: ";
    (cin >> n).get();
    contri* contributors = new contri[n];
    for (int i = 0; i < n; i++)
    {
        cout << "\nContributor #" << i + 1 << endl;
        cout << "Name: ";
        cin.getline(contributors[i].name, StrSize);
        cout << "fund: ";
        (cin >> contributors[i].fund).get();
    }

    bool grandPatrons = false;
    cout << "Grand Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund >= 10000)
        {
            grandPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (!grandPatrons)
        cout << "none\n";


    //Other Patrons
    bool otherPatrons = false;
    cout << "Other Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund < 10000)
        {
            otherPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (not otherPatrons)
        cout << "none\n";

}

7、

#include<iostream>
using namespace std;

const int WordMaxLen = 20;

int main()
{
    char word[WordMaxLen];
    cout << "Enter words (q to quit):\n";
    int vowel = 0, consonant = 0, other = 0;
    while (cin >> word && strcmp(word, "q"))
    {
        if (isalpha(word[0]))
        {
            switch (word[0] = tolower(word[0]))
            {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u': vowel++; break;
            default: consonant++; break;
            }
        }
        else
            other++;
    }
    cout << vowel << " words beginning with vowels\n";
    cout << consonant << " words beginning with consonant\n";
    cout << other << " others\n";

}

8、

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ifstream fin;
    string filename = "chapter6question8.txt";
    fin.open(filename);
    if (!fin.is_open())
    {
        cout << filename << " doesn't exits! Programming exiting unexpectedly.\n";
    }
    char ch;
    int count = 0;
    while ((fin.get(ch)).good())
    {
        cout << ch;
        count++;
    }
    cout << count << " characters readed.\n";
}

9、

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

const int StrSize = 40;

struct contri
{
    char name[StrSize];
    double fund;
};
int main()
{
    ifstream fin;
    char filename[StrSize] = "chapter6question9.txt";
    fin.open(filename);
    if (!fin.is_open())
    {
        cout << "File " << filename << " doesn't exists, program exiting unexpectedly!\n";
        exit(EXIT_FAILURE);
    }

    int n;
    (fin >> n).get();
    contri* contributors = new contri[n];
    cout << "Reading files:";
    for (int i = 0; i < n; i++)
    {
        cout << "\nContributor #" << i + 1 << endl;
        fin.getline(contributors[i].name, StrSize);
        cout << "Name: " << contributors[i].name << endl;
        (fin >> contributors[i].fund).get();
        cout << "fund: " << contributors[i].fund << endl;
    }
    cout << "Reading over.\n";
    // Grand Patrons
    bool grandPatrons = false;
    cout << "\nGrand Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund >= 10000)
        {
            grandPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (!grandPatrons)
        cout << "none\n";


    // Other Patrons
    bool otherPatrons = false;
    cout << "\nOther Patrons:\n";
    for (int i = 0; i < n; i++)
    {
        if (contributors[i].fund < 10000)
        {
            otherPatrons = true;
            cout << "Name: " << contributors[i].name << ", fund: $" << contributors[i].fund << endl;
        }
    }
    if (not otherPatrons)
        cout << "none\n";

}

chapter6question9.txt

4
Sam Stone
2000
Freida Flass
100500
Tammy Tubs
5000
Rich Raptor
55000

注意最后一行有个回车。

标签:std,main,PrimerPlus,cout,int,C++,第六版,using,include
From: https://www.cnblogs.com/xinmind/p/16887734.html

相关文章

  • C++ 很有意思的自动相亲交往系统基础版
    Boy.h:#pragmaonce#include<string>#include<vector>usingnamespacestd;classGirl;classBoy{public:Boy();Boy(intage,stringname,intsalary......
  • c++ 基础语法
    一眼mophie,鉴定为:普及组。#include<bits/stdc++.h>#defineintlonglong#definepbpush_back#definempmake_pair#definepiipair<int,int>#definexfirst#de......
  • C/C++班主任管家系统
    C/C++班主任管家系统设计与开发班主任管家系统,巩固所学c语言基础知识,通过实际项目的开发过程培养综合解决问题的能力及编程能力等。班主任管家软件以学生信息﹑课程信息为......
  • C/C++中声明指针变量时星号是靠近变量名还是靠近数据类型?
    摘自<<C和指针>>3.23  int*a;int*a;两者意思相同且后者看上去更为清楚:a被声明为类型为int*的指针.但是,这并不是一个好技巧,原因如下:int*b,c,d;人们很......
  • C++中 vector容器的神奇用法
    1.可以用简单的数据类型作为参数:#include<iostream>#include<vector>usingnamespacestd;intmain(){vector<int>data;data.push_back(1);data......
  • C++设计模式-(创建模式)原型模式
     原型模式主要用于复制当前对象的副本 #include<iostream>classanimal{public:virtual~animal(){}virtualvoideat(){std::cout......
  • 按照C++语言程序结构组成数字电路进行计算的计算机
    按照C++语言程序结构组成数字电路进行计算的计算机按照C++语言程序结构组成数字电路进行计算的计算机是一种可以按照C++语言程序结构来安排加法器,输出显示电路,输入电路,的数......
  • 类和对象——C++运算符重载
    概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同数据类型。。重载的两类方法:1.类成员函数重载2.全局函数重载注意:运算符重载也可以实现函数重载引入:我们希望......
  • C++ template using for what?
    //Forclassusing,youcansetaseriousofmacrotopredictsomething////whenyoucallthismacro,thetemplatecouldmakeanewfunctionbyyourinput//templ......
  • C++运算符重载相关知识点
    1.运算符重载限制重载后的运算符必须至少有一个操作数是用户自定义的类型使用运算符时不能违反运算符原来的句法规则,也不能修改运算符的优先级。2.不能进行重载的运......