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;
}

时间: 2024-10-10 22:37:44

Accelerated C++ 学习笔记及题解----第三章的相关文章

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

关于C++的之前说过很多了,而且这本书也算是入门级别的书,因此,打算大概过一遍.前期的<字符,控制流等部分不会详细介绍,主要记录下题解就ok了. so,先从第零章开始. 第零章主要内容是以hello world为例子介绍了注释,#include命令,主函数main和函数以及输入返回语句转义字符等. 下面是部分题目题解: 0-2 #include <iostream> using namespace std; int main() { cout << "This (\

课本学习笔记4:第三章 20135115臧文君

第三章 进程管理 注:作者:臧文君,原创作品转载请注明出处. 一.进程 1.进程管理是所有操作系统的心脏所在. 2.进程:是处于执行期的程序以及相关的资源的总称,实际上,进程就是正在执行的程序代码的实时结果. 3.执行线程:简称线程thread,是在进程中活动的对象. 4.内核调度的对象是线程,而不是进程. 5.对Linux而言,线程是一种特殊的进程. 6.进程提供两种虚拟机制:虚拟处理器和虚拟内存. 同一个进程中的线程之间可以共享虚拟内存,但每个都拥有各自的虚拟处理器. 7.Linux系统中,

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

本节主要讲解的是: while语句 if语句 for语句 逻辑运算符. 本节设计的新类型有: bool   布尔值 unsigned 非负整数 short 至少16位整数 long size_t 无符号整数类型,可以保存任何对象的长度 string::size_type 无符号整数类型,可以存储任意字符串的长度 书中的源代码:frame.cpp #include <iostream> #include <string> // say what standard-library na

Accelerated C++学习笔记5—&lt;组织程序和数据&gt;

第4章  组织程序和数据 从前面的学习中,我们可以发现程序并不是我们所想的那么简短,他们都有一个共同的特性,那就是 1)都能解决某些特定类型的问题 2)与其他的大多数工具都互相独立 3)都具有一个自己的名称 C++中提供两种基本的方法来让我们组织大型的程序,函数(子程序)和数据结构. 1.组织计算 1)计算总成绩 子函数grade <span style="font-family:KaiTi_GB2312;">//根据学生的期中考试.期末考试.家庭作业成绩来计算总成绩 do

Accelerated C++学习笔记3—&lt;循环和计数&gt;

第2章 循环和计数 本节主要利用改进输出问候语的程序来改进如何支持循环和条件分支的. 1.使用循环输出一个周围带框架框住的问候语,且用户自己提供在框架与问候语之间的空格的个数. <span style="font-family:KaiTi_GB2312;">// lesson2_1.cpp : 定义控制台应用程序的入口点. //功能:使用循环 //时间:2014.5.8 #include "stdafx.h" #include "iostrea

【Android开发学习笔记】【第三课】Activity和Intent

首先来看一个Activity当中启动另一个Activity,直接上代码说吧: (1)首先要多个Activity,那么首先在res-layout下新建一个 Other.xml,用来充当第二个Activity的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

Accelerated C++学习笔记7—&lt;使用顺序容器并分析字符串&gt;

第6章  使用库算法 本章中主要教我们如何使用几个库算法来解决与处理字符串和学生成绩相关的问题. 1.分析字符串 使用一个循环来连接两幅字符图案 for(vector<string>::const_iterator it = bottom.begin(); it != bottom.end(); ++it) ret.push_back(*it);</span> 等价于 ret.insert(ret.end(), bottom.begin(), bottom.end());</

Accelerated C++学习笔记1—&lt;开始学习C++&gt;

第0章 开始学习C++ 1.每次学习一个新的语言,大家都是从Hello, world!开始 // lesson0_1.cpp : 定义控制台应用程序的入口点. //功能:编译并运行Hello,world //时间:2014.5.7 #include "stdafx.h" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { std::cout<< &

iOS学习笔记---oc语言第三天

继承.初始化方法 一.继承 继承的上层:父类  继承的下层:子类 继承是单向的,不能相互继承 继承具有传递性:A继承于B,B继承于C,A具有B和C的特征和行为 子类能继承父类全部的特征和行为(私有变量也继承过来了,只是不能访问) 面向对象提供了继承语法.能大大简化代码,把公共的方法和实例对象写在父类里.子类只需要写自己独有的实例变量和方法即可 继承既能保证类的完整,又能简化代码 继承特点 oc中只允许单继承 没有父类的类称为根类,oc中得根类是NSObject(祖宗) 继承的内容:所有的实例变量