Accelerated C++ 学习笔记及题解----第三章
来源:互联网 发布:京瓷网络扫描设置 编辑:程序博客网 时间:2024/05/01 09:04
本章主要内容
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 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-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;}
2 1
- Accelerated C++ 学习笔记及题解----第三章
- Accelerated C++ 学习笔记及题解----第二章
- Accelerated C++ 学习笔记及题解----第零章
- Accelerated C++ 学习笔记及题解----第一章
- 【第0章—第三章】【初识C++ Accelerated C++ 学习笔记】
- 《Accelerated C++》 笔记摘录
- Accelerated C++学习笔记1—<开始学习C++>
- Accelerated C++ 第三章 3.3
- 《Accelerated C++》前面部分笔记
- 《accelerated c++》---------第六章
- c语言程序设计第三章题解
- Accelerated C++学习笔记 Ch1
- Accelerated C++学习笔记 Ch2
- Accelerated C++学习笔记 Ch3
- Accelerated C++ 学习笔记1
- Accelerated C++ 学习笔记2
- Accelerated C++ 学习笔记3
- Accelerated C++学习笔记4
- 高并发高流量网站架构
- lua单例
- 风の住む街 矶村由纪子
- PyQt挖地雷游戏学习笔记(6)
- 点退出弹出确认退出
- Accelerated C++ 学习笔记及题解----第三章
- 畅谈程序员接私活
- 《Head First Design Patterns》读书笔记之单例模式
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- Map集合类中保证线程安全的策略比较
- SVM 为什么说高斯核函数能把向量映射到无穷维
- 主要内容
- Substring with Concatenation of All Words[leetcode]
- Qt5.3 OpenCV 环境搭建(编译OpenCV)