Accelerated C + + learning notes and----third Chapter

Source: Internet
Author: User

main contents of this chapter

1. Type definition typedef

2. Local Variables

3. Formatting output initial involvement

4 vector vectors and basic methods


This chapter main program code

1. Calculate the average score

#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 had 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;}
Results:

2. Calculate the median value of a score

#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 grad    ES, "followed by End-of-file:";    Vector<double> homework; Double x;//invariant: ' Homework ' contains all the homework grades read so far while (CIN >> x) HOMEWORK.P Ush_back (x);//Check that the student entered some homework grades#ifdef _msc_ver typedef std::vector<double>::si Ze_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;}
Results:

Exercise Answer

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_SZi = integers.size () * 3/4; I < integers.size ();    ++i) cout << integers[i] << Endl; return 0;}
Results:

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

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 2using 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-fi        Le: ";        He number and sum of grades read so far int count = 0;        Double sum = 0; A variable into which toRead 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 Streamsi        Ze 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 had 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 * homewor    K_grade << setprecision (prec) << Endl; return 0;}


Accelerated C + + learning notes and----third Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.