实验任务4
vector.hpp
#include<iostream>
#include<stdexcept>
#include <cassert>
using namespace std;
template<typename T>
class Vector
{
private:
T* ptr;
int size;
public:
Vector(int size)
{
if (size < 0)
throw std::length_error("vector constructor:negative size");
this->size = size;
ptr = new T[this->size];
}
Vector(int size, T value)
{
if (size < 0)
throw std::length_error("vector constructor:negative size");
this->size = size;
ptr = new T[size];
for (auto i = 0; i < size; i++)
{
ptr[i] = value;
}
}
Vector(const Vector<T>& x)
{
size = x.size;
ptr = new T[size];
for (auto i = 0; i < size; i++)
{
ptr[i] = x.ptr[i];
}
}
~Vector()
{
delete[] ptr;
}
int get_size()
{ return size;
}
T& at(int i)
{
assert(i >= 0 && i < size);
return(ptr[i]);
}
T& at(int i) const
{
assert(i >= 0 && i < size);
return(ptr[i]);
}
T& operator[](int i)
{
return ptr[i];
}
friend void output(Vector<T> x)
{
for (auto i = 0; i < x.get_size(); i++)
cout << x.at(i) << ",";
cout << "\b\b\n";
}
};
task4.cpp
#include <iostream>
#include "vector.hpp"
void test()
{
using namespace std;
int n;
cin >> n;
Vector<double> x1(n);
for (auto i = 0; i < n; ++i)
x1.at(i) = i * 0.7;
output(x1);
Vector<int> x2(n, 42);
Vector<int> x3(x2);
output(x2);
output(x3);
x2.at(0) = 77;
output(x2);
x3[0] = 999;
output(x3);
}
int main()
{
test();
}
实验任务5
task5.cpp
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void output(std::ostream& output)
{
output << " ";
for (int i = 97; i < 123; i++)
{
output << setw(2) << char(i);
}
output << endl;
for (int i = 1; i <= 26; i++)
{
output << setw(2) << i;
int j;
for (j = i; j < i + 26; j++)
{
output << setw(2) << char(65 + j % 26);
}
output << endl;
}
}
int main()
{
ofstream out;
output(cout);
out.open("cipher_key.txt");
output(out);
return 0;
}