首页 > 代码库 > Accelerated C++ 学习笔记及题解----第三章

Accelerated C++ 学习笔记及题解----第三章

本章主要内容

1.类型定义typedef

2.局部变量

3.格式化输出初涉

4.向量vector及基本方法


本章主要程序代码

1.计算平均成绩

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

int main()
{
// ask for and read the student's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;
// ask for the homework grades
    cout << "Enter all your homework grades, "
         "followed by end-of-file: ";
// the number and sum of grades read so far
    int count = 0;
    double sum = 0;
// a variable into which to read
    double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
    while (cin >> x)
    {
        ++count;
        sum += x;
    }
// write the result
    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
         << setprecision(prec) << endl;
    return 0;
}
结果:

2.计算成绩中值

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
// ask for and read the student's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;
// ask for and read the homework grades
    cout << "Enter all your homework grades, "
         "followed by end-of-file: ";
    vector<double> homework;
    double x;
// invariant: `homework' contains all the homework grades read so far
    while (cin >> x)
        homework.push_back(x);
// check that the student entered some homework grades
#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif
    vec_sz size = homework.size();
    if (size == 0)
    {
        cout << endl << "You must enter your grades. "
             "Please try again." << endl;
        return 1;
    }
// sort the grades
    sort(homework.begin(), homework.end());
// compute the median homework grade
    vec_sz mid = size/2;
    double median;
    median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2
             : homework[mid];
// compute and write the final grade
    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * median
         << setprecision(prec) << endl;
    return 0;
}
结果:

习题答案

3.2

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> integers;
    cout << "Integers: ";
    int x;
    while (cin >> x)
        integers.push_back(x);
    if (integers.size() == 0)
    {
        cout << endl << "No integers!" << endl;
        return 1;
    }
    sort(integers.rbegin(), integers.rend());
    typedef vector<int>::size_type vec_sz;
    cout << "1st quartile" << endl;
    for (vec_sz i = 0; i < integers.size() / 4; ++i)
        cout << integers[i] << endl;
    cout << "2nd quartile" << endl;
    for (vec_sz i = integers.size() / 4; i < integers.size() / 2; ++i)
        cout << integers[i] << endl;
    cout << "3rd quartile" << endl;
    for (vec_sz i = integers.size() / 2; i < integers.size() * 3 / 4; ++i)
        cout << integers[i] << endl;
    cout << "4th quartile" << endl;
    for (vec_sz i = integers.size() * 3 / 4; i < integers.size(); ++i)
        cout << integers[i] << endl;
    return 0;
}
结果:

3.3

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    typedef vector<string>::size_type vec_sz;
    vector<string> words;
    vector<int> counts;
    cout << "Words: ";
    string s;
    while (cin >> s)
    {
        bool found = false;
        for (vec_sz i = 0; i < words.size(); ++i)
        {
            if (s == words[i])
            {
                ++counts[i];
                found = true;
            }
        }
        if (!found)
        {
            words.push_back(s);
            counts.push_back(1);
        }
    }
    for (vec_sz i = 0; i < words.size(); ++i)
        cout << words[i] << " appeared " << counts[i] << " times" << endl;
    return 0;
}
结果:

3.4

#include <iostream>
#include <string>

using namespace std;

int main()
{
    typedef string::size_type str_sz;
    string longest;
    str_sz longest_length = 0;
    string shortest;
    str_sz shortest_length = 0;
    cout << "Words: ";
    string s;
    while (cin >> s)
    {
        if (longest_length == 0 || s.size() > longest_length)
        {
            longest = s;
            longest_length = s.size();
        }
        if (shortest_length == 0 || s.size() < shortest_length)
        {
            shortest = s;
            shortest_length = s.size();
        }
    }
    cout << "Longest: " << longest << endl;
    cout << "Shortest: " << shortest << endl;
    return 0;
}

3.5

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
#define NUM_HOMEWORK 2
using std::vector;
int main()
{
    vector<string> names;
    vector<double> final_grades;
    bool done = false;
    while (!done)
    {
        // ask for and read the student's name
        cout << "Please enter your first name: ";
        string name;
        cin >> name;
        cout << "Hello, " << name << "!" << endl;
        names.push_back(name);
        // ask for and read the midterm and final grades
        cout << "Please enter your midterm and final exam grades: ";
        double midterm, final;
        cin >> midterm >> final;
        // ask for the homework grades
        cout << "Enter both your homework grades, "
             "followed by end-of-file: ";
        // he number and sum of grades read so far
        int count = 0;
        double sum = 0;
        // a variable into which to read
        double x;
        // invariant:
        // we have read `count' grades so far, and
        // `sum' is the sum of the first `count' grades
        while (count < NUM_HOMEWORK)
        {
            ++count;
            cin >> x;
            sum += x;
        }
        double final_grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count;
        final_grades.push_back(final_grade);
        cout << "More? (Y/N) ";
        string s;
        cin >> s;
        if (s != "Y")
            done = true;
    }
    for (vector<string>::size_type i = 0; i < names.size(); ++i)
    {
        // write the result
        streamsize prec = cout.precision();
        cout << names[i] << "'s final grade is " << setprecision(3)
             << final_grades[i]
             << setprecision(prec) << endl;
    }
    return 0;
}

3.6

#include <iomanip>
#ifndef __GNUC__
#include <ios>
#endif
#include <iostream>
#include <string>
using namespace std;
int main()
{
// ask for and read the student's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;
// ask for the homework grades
    cout << "Enter all your homework grades, "
         "followed by end-of-file: ";
// the number and sum of grades read so far
    int count = 0;
    double sum = 0;
// a variable into which to read
    double x;
// invariant:
// we have read `count' grades so far, and
// `sum' is the sum of the first `count' grades
    while (cin >> x)
    {
        ++count;
        sum += x;
    }
    double homework_grade = (count > 0) ? sum / count : 0.0;
// write the result
    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * homework_grade
         << setprecision(prec) << endl;
    return 0;
}


Accelerated C++ 学习笔记及题解----第三章