实验六
实验任务4
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) { // 多组输入,按
下Ctrl+Z终止
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();
}
vector.hpp
#pragma once
#include<iostream>
#include<stdexcept>
using namespace std;
template<typename T>
class Vector
{
private:
int size;
T* ptr;
public:
Vector(int n);
Vector(int n, T value);
Vector(const Vector<T>& v);
~Vector();
int get_size()
{
return size;
}
T& at(int index)const;
T& at(int index);
T& operator[](int index)const;
T& operator[](int index);
template<typename T1>
friend void output(const Vector<T1>& v);
};
template<typename T>
Vector<T>::Vector(int n) :size(n)
{
if (n < 0)
{
throw length_error("Vector constructor:negative size");
}
else
{
ptr = new T[size];
}
}
template<typename T>
Vector<T>::Vector(int n,T value):size(n)
{
if (n < 0)
{
throw length_error("Vector constructor:negative size");
}
else
{
ptr = new T[size];
for (int i = 0; i < size; i++)
{
ptr[i] = value;
}
}
}
template<typename T>
Vector<T>::Vector(const Vector<T>& v):size(v.size),ptr(new T[size])
{
for (int i = 0; i < size; i++)
{
ptr[i] = v.ptr[i];
}
}
template<typename T>
Vector<T>::~Vector()
{
delete[]ptr;
}
template<typename T>
T& Vector<T>::at(int index)const
{
if (index < 0 || index >= size)
{
throw out_of_range("Vector:index out of range");
}
else
{
return ptr[index];
}
}
template<typename T>
T& Vector<T>::at(int index)
{
return static_cast<const Vector*>(this)->at(index);
}
template<typename T>
T& Vector<T>::operator[](int index)const
{
if (index < 0 || index >= size)
{
throw out_of_range("Vector:index out of range");
}
else
{
return ptr[index];
}
}
template<typename T>
T& Vector<T>::operator[](int index)
{
return static_cast<const Vector*>(this)->operator[](index);
}
template<typename T>
void output(const Vector<T>& v)
{
for (int i = 0; i < v.size; i++)
{
cout << v.ptr[i] << ", ";
}
cout <<"\b\b " << endl;
}
实验任务5
task5.cpp
#include <fstream>
#include <iostream>
#include <string>
#include<vector>
using namespace std;
struct Student {
int id;
string name;
string major;
int score;
};
int main() {
auto fin = ifstream("data5.txt");
if (!fin) {
cout << "Failed to open file." << endl;
return 1;
}
string line;
getline(fin, line);
auto stus = vector<Student>();
while (!fin.eof()) {
int id, score;
string name, major;
fin >> id >> name >> major >> score;
stus.push_back({id, name, major, score});
}
auto fout = ofstream("ans5.txt");
for (auto &stu : stus) {
cout << stu.id << " " << stu.name << " " << stu.major << " " << stu.score
<< endl;
fout << stu.id << " " << stu.name << " " << stu.major << " " << stu.score
<< endl;
}
return 0;
}