Accelerated C++ 5 Using sequential containers and analyzing strings
来源:互联网 发布:linux tar 解包 编辑:程序博客网 时间:2024/06/14 01:38
The<cctype>header provides useful functions for manipulating charactor data:
isspace(c)isalpha(c)isdigit(c)isalnum(c)ispunct(c) // punctuation charactorisupper(c)islower(c)toupper(c)tolower(c)
I.
split.h
#ifndef GUARD_split_h#define GUARD_split_h#include <vector>#include <string>std::vector<std::string> split(const std::string&);#endif
split.cpp
//source file split-related#include<cctype>#include "split.h"using std::string; using std::vector;vector<string> split(const string& s) { vector<string> ret; typedef vector<string>::size_type s_sz; s_sz i = 0; s_sz j = 0; while (i != s.size()) { while (i != s.size() && isspace(s[i])) ++i; j = i; while (j != s.size() && !isspace(s[j])) ++j; if (i != j) { ret.push_back(s.substr(i,j - i)); i = j; } } return ret;}
I.
Student_info.h
Student_info.cpp
grade.h(median.h)
grade.cpp(median.cpp)
see Note 4
extract_fails.h
#ifndef GUARD_extract_fails_h#define GUARD_extract_fails_h#include <list>#include "Student_info.h"bool fgrade(const Student_info&);bool pgrade(const Student_info&);std::list<Student_info> extract_fails(std::list<Student_info>&); #endif
extract_fails.cpp
#include "grade.h"#include "extract_fails.h"#include "fopgrade.h"#include "Student_info.h"using std::list;bool fgrade(const Student_info& s) { return grade(s) < 60;}bool pgrade(const Student_info& s) { return grade(s) >= 60;}list<Student_info> extract_fails(list<Student_info>& students) { list<Student_info> fails; list<Student_info>::iterator iter = students.begin(); while(iter != students.end()){ if (fgrade(*iter)) { fails.push_back(*iter); iter = students.erase(iter); } else { ++iter; } } return fails;}
main.cpp
#include <algorithm>#include <iomanip> // setprecision#include <ios> // streamsize #include <iostream>#include <stdexcept>#include <string>#include "Student_info.h"#include "grade.h"#include "extract_fails.h"using std::list; using std::sort; using std::string; using std::endl;using std::domain_error; using std::cin; using std::cout; using std::max;using std::streamsize; using std::setprecision; using std::list; using std::setw;int main(){ list<Student_info> students; Student_info record; list<Student_info> fail_stus; string::size_type maxlen = 0; // read and store all the students' data // Invariant: students contains all the student records read so far // maxlen contains the length of the longest name in students while (read(cin, record)) { // find length of longest name maxlen = max(maxlen, record.name.size()); students.push_back(record); } // alphabetize the student records sort(students.begin(), students.end(), compare); // write the names and grades for (list<Student_info>::iterator it = students.begin(); it != students.end(); ++it){ // write the name, padded on the right to maxlen + 1 characters record = (*it); cout << record.name << string(maxlen + 1 - record.name.size(), ' '); // compute and write the grade try { double final_grade = grade(record); streamsize prec = cout.precision(); cout << setprecision(3) << final_grade << setprecision(prec); } catch (domain_error e) { cout << e.what(); } cout << endl; } cout << "print the students who fail the exam!" << endl; fail_stus = extract_fails(students); for (list<Student_info>::iterator it = fail_stus.begin(); it != fail_stus.end(); ++it) { record = *it; cout << record.name << string(maxlen + 1 - record.name.size(), ' '); try { streamsize prec = cout.precision(); cout << setw(4) << setprecision(3) << grade(record) << setprecision(prec); } catch (domain_error e) { cout << e.what(); } cout << endl; } return 0;}
阅读全文
0 0
- Accelerated C++ 5 Using sequential containers and analyzing strings
- 【primer】chapter9 sequential containers
- Chapter 9 Sequential Containers
- Chapter 9 Sequential Containers
- Chapter 9. Sequential Containers
- Accelerated 13 Using inheritance and dynamic binding
- Accelerated C++<4-5>
- C++ Primer4 Chapter 9. Sequential Containers
- !!!Chapter 9 Sequential Containers (9.1 ~ 9.3)
- !!!Chapter 9 Sequential Containers (9.4 ~ 9.7)
- Using UTF-8 as the internal representation for strings in C and C++ with Visual Studio
- Analyzing Display and Performance
- Strings in C and C++
- 477 C. Dreamoon and Strings
- 【CODEFORCES】 C. Dreamoon and Strings
- Codeforces Round #208 (Div. 2) C. Dima and Containers
- Linearizability and Sequential Consistency
- 《C++ Primer第五版》读书笔记(7)--SEQUENTIAL CONTAINERS
- Java实现单例模式的3中实现方式
- MFC 窗体大小控制
- Struts2-参数封装
- 剑指Offer-59
- 《深入理解java虚拟机(第二版)》学习笔记6——类加载机制
- Accelerated C++ 5 Using sequential containers and analyzing strings
- 30 多年的编码经验浓缩成的 10 条最佳实践
- 每日一诗词 —— 将进酒
- JavaScript为unicode编码转换为中文
- 牛客网错题集锦5
- 服务器硬件升级解决方案
- 洛谷p1908逆序对
- Dagger2 大全
- 51nod 1082 与7无关的数