首页 > 其他分享 >实验六

实验六

时间:2022-12-07 08:33:19浏览次数:43  
标签:int ++ Vector 实验 words output size

实验六

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

相关文章

  • 实验六
    Task3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99......
  • Python实验报告——第6章 函数
    实例01:输出每日一帖(共享版) 在IDLE中创建一个名称为function_tips.py的文件,然后在该文件中创建一个名称为function_tips的函数,在该函数中,从励志文字列表中获取一条......
  • 实验六
    task3测试结果:      源代码:1#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingname......
  • 实验六 模板类与文件I/O
     task3task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89a......
  • 实验6 模板类和文件IO
    2022.11.30OOP实验实验6模板类和文件IO任务3task36_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespac......
  • Python实验报告——第5章 字符串及正则表达式
    实例01:使用字符串拼接输出一个关于程序员的笑话 在IDLE中创建一个名称为programmer_splice.py的文件,然后在该文件中定义两个字符串变量,分别记录两名程序说的话,再将......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......
  • 实验六
    实验三task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,......
  • 程序设计基础实验课 单元六的题-UVA10410 TreeReconstruction 树重建
    入门指南题面:  洛谷题面:   观看的题解:https://www.cnblogs.com/jerryRey/p/4622927.html  对样例区样例画的一些图:       题目的一些争议......
  • 实验六
    task3-1cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,......