实验6 C++
任务四:
Vector.hpp
#pragma once
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class Vector {
public:
Vector(int n, int p = 0);
Vector(const Vector<T> &v);
~Vector();
int get_size() const;
T& at(int i);
T& operator[](int i);
private:
int size;
T *a;
template <typename T1>
friend void output(const Vector<T1> &v);
};
template <typename T>
Vector<T>::Vector(int n, int p) : size{n} {
if(n < 0)
throw length_error("Vector constructor: negative size");
else {
a = new T [n];
for(int i = 0; i < n; i++)
a[i] = p;
}
}
template <typename T>
Vector<T>::Vector(const Vector<T> &v) {
size = v.size;
a = new T [size];
for(int i = 0; i < size; i++)
a[i] = v.a[i];
}
template <typename T>
Vector<T>::~Vector() {
delete [] a;
}
template <typename T>
int Vector<T>::get_size() const {return size;}
template < typename T>
T& Vector<T>::at(int i) {
if(i >= size || i < 0)
throw invalid_argument("Vector: index out of range");
else
return a[i];
}
template <typename T>
T& Vector<T>::operator[](int i) {
if(i >= size || i < 0)
throw invalid_argument("Vctor: index out of range");
else
return a[i];
}
template <typename T1>
void output(const Vector<T1> &v)
{
for(int i = 0; i < v.size - 1; i++)
cout << v.a[i] << ", ";
cout << v.a[v.size - 1] << endl;
}
task4.cpp
#include <iostream>
#include "Vector.hpp"
void test1() {
using namespace std;
int n;
cout << "Enter n: ";
cin >> n;
Vector<double> x1(n);
for(auto i = 0; i < n; ++i)
x1.at(i) = i * 0.7;
cout << "x1: "; output(x1);
Vector<int> x2(n, 42);
const Vector<int> x3(x2);
cout << "x2: "; output(x2);
cout << "x3: "; output(x3);
x2.at(0) = 77;
x2.at(1) = 777;
cout << "x2: "; output(x2);
cout << "x3: "; output(x3);
}
void test2() {
using namespace std;
int n, index;
while(cout << "Enter n and index: ", cin >> n >> index) {
try {
Vector<int> v(n, n);
v.at(index) = -999;
cout << "v: "; output(v);
}
catch (const exception &e) {
cout << e.what() << endl;
}
}
}
int main() {
cout << "测试1: 模板类接口测试\n";
test1();
cout << "\n测试2: 模板类异常处理测试\n";
test2();
}
运行结果:
任务五:
task5.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
class Stu {
public:
Stu() = default;
~Stu() = default;
string get_major() const {return major;}
int get_score() const {return score;}
friend ostream& operator<<(ostream &out, const Stu &s);
friend istream& operator>>(istream &in, Stu &s);
private:
string no;
string name;
string major;
int score;
};
ostream& operator<<(ostream &out, const Stu &s) {
out << setiosflags(ios_base::left);
out << setw(10) << s.no
<< setw(10) << s.name
<< setw(10) << s.major
<< setw(10) << s.score << endl;
return out;
}
istream& operator>>(istream &in, Stu &s) {
in >> s.no >> s.name >> s.major >> s.score;
return in;
}
bool cmp(const Stu &s1, const Stu &s2) {
if(s1.get_major() < s2.get_major())
return true;
if(s1.get_major() == s2.get_major())
return s1.get_score() > s2.get_score();
return false;
}
void output(ostream &out, const vector<Stu> &v) {
for(auto &i : v)
out << i ;
}
void load(const string &filename, vector<Stu> &v) {
ifstream in(filename);
if(!in.is_open()) {
cout << "fail to open file to read" << endl;
return;
}
string title_line;
getline(in, title_line);
Stu s;
while(in >> s)
v.push_back(s);
in.close();
}
void save(const string &filename, vector<Stu> &v) {
ofstream out(filename);
if(!out.is_open()) {
cout << "fail to open file to write" << endl;
return;
}
output(out, v);
out.close();
}
int main() {
vector<Stu> v;
load("data5.txt", v);
sort(v.begin(), v.end(), cmp);
output(cout, v);
save("ans5.txt", v);
}
运行结果: