实验任务1:
(略)
实验任务2:
代码:
GradeCalc.hpp
查看代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
class GradeCalc: public vector<int> {
public:
GradeCalc(const string &cname, int size);
void input(); // 录入成绩
void output() const; // 输出成绩
void sort(bool ascending = false); // 排序 (默认降序)
int min() const; // 返回最低分
int max() const; // 返回最高分
float average() const; // 返回平均分
void info(); // 输出课程成绩信息
private:
void compute(); // 成绩统计
private:
string course_name; // 课程名
int n; // 课程人数
vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
vector<double> rates = vector<double>(5, 0); // 保存各分数段比例
};
GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {}
void GradeCalc::input() {
int grade;
for(int i = 0; i < n; ++i) {
cin >> grade;
this->push_back(grade);
}
}
void GradeCalc::output() const {
for(auto ptr = this->begin(); ptr != this->end(); ++ptr)
cout << *ptr << " ";
cout << endl;
}
void GradeCalc::sort(bool ascending) {
if(ascending)
std::sort(this->begin(), this->end());
else
std::sort(this->begin(), this->end(), std::greater<int>());
}
int GradeCalc::min() const {
return *std::min_element(this->begin(), this->end());
}
int GradeCalc::max() const {
return *std::max_element(this->begin(), this->end());
}
float GradeCalc::average() const {
return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n;
}
void GradeCalc::compute() {
for(int grade: *this) {
if(grade < 60)
counts.at(0)++;
else if(grade >= 60 && grade < 70)
counts.at(1)++;
else if(grade >= 70 && grade < 80)
counts.at(2)++;
else if(grade >= 80 && grade < 90)
counts.at(3)++;
else if(grade >= 90)
counts.at(4)++;
}
for(int i = 0; i < rates.size(); ++i)
rates.at(i) = counts.at(i) * 1.0 / n;
}
void GradeCalc::info() {
cout << "课程名称:\t" << course_name << endl;
cout << "排序后成绩: \t";
sort(); output();
cout << "最高分:\t" << max() << endl;
cout << "最低分:\t" << min() << endl;
cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
compute(); // 统计各分数段人数、比例
vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"};
for(int i = tmp.size()-1; i >= 0; --i)
cout << tmp[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl;
}
demo2.cpp
查看代码
#include "GradeCalc.hpp"
#include <iomanip>
void test() {
int n;
cout << "输入班级人数: ";
cin >> n;
GradeCalc c1("OOP", n);
cout << "录入成绩: " << endl;;
c1.input();
cout << "输出成绩: " << endl;
c1.output();
cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl;
c1.info();
}
int main() {
test();
}
运行结果:
问题1:
问题2:
问题3:
实验任务3:
代码:
GradeCalc.hpp
查看代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
class GradeCalc {
public:
GradeCalc(const string &cname, int size);
void input(); // 录入成绩
void output() const; // 输出成绩
void sort(bool ascending = false); // 排序 (默认降序)
int min() const; // 返回最低分
int max() const; // 返回最高分
float average() const; // 返回平均分
void info(); // 输出课程成绩信息
private:
void compute(); // 成绩统计
private:
string course_name; // 课程名
int n; // 课程人数
vector<int> grades; // 课程成绩
vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
vector<double> rates = vector<double>(5, 0); // 保存各分数段比例
};
GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {}
void GradeCalc::input() {
int grade;
for(int i = 0; i < n; ++i) {
cin >> grade;
grades.push_back(grade);
}
}
void GradeCalc::output() const {
for(int grade: grades)
cout << grade << " ";
cout << endl;
}
void GradeCalc::sort(bool ascending) {
if(ascending)
std::sort(grades.begin(), grades.end());
else
std::sort(grades.begin(), grades.end(), std::greater<int>());
}
int GradeCalc::min() const {
return *std::min_element(grades.begin(), grades.end());
}
int GradeCalc::max() const {
return *std::max_element(grades.begin(), grades.end());
}
float GradeCalc::average() const {
return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n;
}
void GradeCalc::compute() {
for(int grade: grades) {
if(grade < 60)
counts.at(0)++;
else if(grade >= 60 && grade < 70)
counts.at(1)++;
else if(grade >= 70 && grade < 80)
counts.at(2)++;
else if(grade >= 80 && grade < 90)
counts.at(3)++;
else if(grade >= 90)
counts.at(4)++;
}
for(int i = 0; i < rates.size(); ++i)
rates.at(i) = counts.at(i) *1.0 / n;
}
void GradeCalc::info() {
cout << "课程名称:\t" << course_name << endl;
cout << "排序后成绩: \t";
sort(); output();
cout << "最高分:\t" << max() << endl;
cout << "最低分:\t" << min() << endl;
cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
compute(); // 统计各分数段人数、比例
vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"};
for(int i = tmp.size()-1; i >= 0; --i)
cout << tmp[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl;
}
demo3.cpp
查看代码
#include "GradeCalc.hpp"
#include <iomanip>
void test() {
int n;
cout << "输入班级人数: ";
cin >> n;
GradeCalc c1("OOP", n);
cout << "录入成绩: " << endl;;
c1.input();
cout << "输出成绩: " << endl;
c1.output();
cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl;
c1.info();
}
int main() {
test();
}
运行结果:
问题1:
问题2:
实验任务4:
代码:
task4_1.cpp
查看代码
#include <iostream>
#include <string>
#include <limits>
using namespace std;
void test1() {
string s1, s2;
cin >> s1 >> s2; // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
void test2() {
string s1, s2;
getline(cin, s1); // getline(): 从输入流中提取字符串,直到遇到换行符
getline(cin, s2);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
void test3() {
string s1, s2;
getline(cin, s1, ' '); //从输入流中提取字符串,直到遇到指定分隔符
getline(cin, s2);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
}
int main() {
cout << "测试1: 使用标准输入流对象cin输入字符串" << endl;
test1();
cout << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "测试2: 使用函数getline()输入字符串" << endl;
test2();
cout << endl;
cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl;
test3();
}
task4_2.cpp
查看代码
#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;
void output(const vector<string> &v) {
for(auto &s: v)
cout << s << endl;
}
void test() {
int n;
while(cout << "Enter n: ", cin >> n) {
vector<string> v1;
for(int i = 0; i < n; ++i) {
string s;
cin >> s;
v1.push_back(s);
}
cout << "output v1: " << endl;
output(v1);
cout << endl;
}
}
int main() {
cout << "测试: 使用cin多组输入字符串" << endl;
test();
}
task4_3.cpp
查看代码
#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;
void output(const vector<string> &v) {
for(auto &s: v)
cout << s << endl;
}
void test() {
int n;
while(cout << "Enter n: ", cin >> n) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> v2;
for(int i = 0; i < n; ++i) {
string s;
getline(cin, s);
v2.push_back(s);
}
cout << "output v2: " << endl;
output(v2);
cout << endl;
}
}
int main() {
cout << "测试: 使用函数getline()多组输入字符串" << endl;
test();
}
运行结果:
问题1:
用途是读入多行字符串string,用于忽略输入流中的字符直到遇到换行符。
问题2:
用途是读入多行字符串string,n为几就读取几行。
实验任务5:
代码:
标签:const,int,void,GradeCalc,grade,实验,include From: https://www.cnblogs.com/xiarihua/p/18552981