实验六
task4
vector.hpp
#pragma once
#include<iostream>
using namespace std;
template<typename T>
class Vector {
public:
Vector() {};
Vector(int n) :size{ n } {
p = new T[size];
}
Vector(int n, T value) :size{ n } {
p = new T[size];
for (auto i = 0; i < n; i++) {
p[i] = value;
}
}
Vector(const Vector<T>& v1) : size{ v1.size }
{
p = new T[size];
int i;
for (i = 0; i < size; i++)
p[i] = v1.p[i];
}
~Vector() {
delete[] p;
}
T& at(int index)
{
if (index >= 0 && index < size)
return p[index];
}
friend void output(Vector<T>& v)
{
int i;
for (i = 0; i < v.size; i++)
cout << v[i] << " ";
cout << endl;
}
int get_size()const
{
return size;
}
T& operator [](int index)
{
if (index >= 0 && index < size)
return p[index];
}
private:
int size;
T* p;
};
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();
}
task5
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
#define N 27
char words[N][N];
int number[N];
void Init_words(char words[][N], int n) {
words[0][0] = ' ';
for (int i = 1; i < N; i++) {
words[0][i] = 'a' + i - 1;
words[i][0] = i + '0';
number[i] = i;
}
for (int i = 1;i < N;i++)
for (int j = 1; j < n; j++) {
words[i][j] = words[0][j] + i + 'A' - 'a';
if (words[i][j] > 'Z')
words[i][j] -= 26;
}
}
void output(char words[][N], int n, ostream& out) {
for (int i = 0; i < N; i++) {
if (i == 0)
cout << " ";
else
cout << setw(2) << number[i] << ' ';
for (int j = 1; j < N; j++) {
cout << words[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < N; i++) {
if (i == 0)
out << " ";
else
out << setw(2) << number[i] << ' ';
for (int j = 1; j < N; j++) {
out << setw(2) << words[i][j];
}
out << endl;
}
}
int main() {
ofstream out;
out.open("cipher_key.txt");
Init_words(words, N);
output(words, N, out);
}
标签:int,++,Vector,实验,words,output,size
From: https://www.cnblogs.com/2003dingjiajie/p/16962017.html