首页 > 其他分享 >oop 实验6 模板类和文件I/O

oop 实验6 模板类和文件I/O

时间:2022-12-03 15:23:37浏览次数:32  
标签:int Vector 实验 oop template output include 模板 size

task3

程序源码

task3_1.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <fstream>
 4 #include <array>
 5 #define N 5
 6 using namespace std;
 7 
 8 
 9 int main() {
10     array<int, N> x{ 97, 98, 99, 100, 101 };
11     ofstream out;
12     out.open("data1.txt", ios::binary);
13     if (!out.is_open()) {
14         cout << "fail to open data" << endl;
15         return 1;
16     }
17     out.write(reinterpret_cast<char*>(&x), sizeof(x));
18     out.close();
19 }

task3_2.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <fstream>
 4 #include <array>
 5 #define N 5
 6 using namespace std;
 7 int main() {
 8     array<char, N> x;
 9     ifstream in;
10     in.open("data1.txt", ios::binary);
11     if (!in.is_open()) {
12         cout << "fail to open data.dat\n";
13         return 1;
14     }
15     in.read(reinterpret_cast<char*>(&x), sizeof(x));
16     in.close();
17     for (auto i = 0; i < N; i++) {
18         cout << x[i] << ",";
19     }
20     cout << "\b\b\n";
21     return 0;
22 }

程序运行截图:

因为char是一个字节,而int为4个字节,char相对少了3个字节,空出来留给下一次读取。

 

task4

程序源码:

Vector.hpp

 1 #pragma once
 2 #include <iostream>
 3 using namespace std;
 4 
 5 template<class T>
 6 class Vector {
 7 public:
 8     Vector(int size);
 9     Vector(int size, T value);
10     ~Vector();
11     Vector(const Vector& x);
12     int get_size() const { return size; }
13     T& at(int index);
14     T& operator[](int index);
15     template<typename T>
16     friend void output(const Vector<T> &v);
17     
18 private: 
19     int size;
20     T* a;
21 };
22 template<typename T>
23 void output(const Vector<T>& v) {
24     for (int i = 0; i < v.size; i++) {
25         cout << v.a[i] << ",";
26     }
27     cout << "\b\b\n";
28 }
29 template<class T>
30 Vector<T>::Vector(int size) : size(size) {
31     a = new T[size];
32 
33 }
34 template<typename T>
35 Vector<T>::Vector(int size, T value) : size(size) {
36     a = new T[size];
37     for (int i = 0; i < size; i++) {
38         a[i] = value;
39     }
40 }
41 template<typename T>
42 Vector<T>::~Vector() {
43     delete[] a;
44 }
45 template<typename T> 
46 Vector<T>::Vector(const Vector& x) {
47     this->size = x.size;
48     this->a = new T[size];
49     for (int i = 0; i < size; i++) {
50         this->a[i] = x.a[i];
51     }
52 }
53 
54 template<typename T>
55 T& Vector<T>::at(int index) {
56     return this->a[index];
57 }
58 
59 template<typename T> 
60 T& Vector<T>::operator[](int index) {
61     return this->a[index];
62 }

task4.cpp

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include "Vector.hpp"
 4 using namespace std;
 5 
 6 void test() {
 7     int n;
 8     cin >> n;
 9     Vector<double> x1(n);
10     for (auto i = 0; i < n; i++) {
11         x1.at(i) = i * 0.7;
12     }
13     Vector<int> x2(n, 42);
14     Vector<int> x3(x2);
15     output(x2);
16     output(x3);
17     x2.at(0) = 77;
18     output(x2);
19     x3[0] = 999;
20     output(x3);
21 
22 }
23 
24 int main() {
25     test();
26     return 0;
27 }

程序运行截图:

 

 

 task5

程序源码

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include <iostream>
 3 #include <fstream>
 4 #include <iomanip>
 5 using namespace std;
 6 void output(ostream& out) {
 7     cout << "  ";
 8     for (int i = 97; i < 97 + 26; i++) {
 9         cout << char(i) << " ";
10     }
11     cout << endl;
12     for (int i = 1; i <= 26; i++) {
13         cout << setw(2) << i ; 
14         for (int j = 65 + i; j <= 65 + 25; j++) {
15             cout << char(j) << " ";
16         }
17         for (int k = 65; k < 65 + i; k++) {
18             cout << char(k) << " ";
19         }
20         cout << endl;
21     }
22     
23     
24 
25 }
26 int main() {
27     ofstream ofs;
28     ofs.open("cipher_key.txt");
29     if (!ofs.is_open()) {
30         cout << "faile to open" << endl;
31     }
32     else {
33         ofs << setiosflags(ios::left) << setw(2) << "";
34         for (int i = 97; i < 97 + 26; i++) {
35             ofs << setiosflags(ios::left) <<setw(2) << char(i) << " ";
36         }
37         ofs << endl;
38         for (int i = 1; i <= 26; i++) {
39             ofs << setiosflags(ios::left) << setw(2) << i;
40             for (int j = 65 + i; j <= 65 + 25; j++) {
41                 ofs << setiosflags(ios::left) << setw(2) <<char(j) << " ";
42             }
43             for (int k = 65; k < 65 + i; k++) {
44                 ofs << setiosflags(ios::left) << setw(2) << char(k) << " ";
45             }
46             ofs << endl;
47         }
48 
49     }
50     ofs.close();
51     output(ofs);
52     return 0;
53 }

运行截图

 

 

 

 这里在txt文件一直大小写字母对不齐,于是我换了一个字体就成功了

标签:int,Vector,实验,oop,template,output,include,模板,size
From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16943037.html

相关文章

  • python实验报告(第13章)
    一、实验目的1.掌握Pygame的基础知识。二、实验环境python版本:3.10(64-bit)三、实验内容1.实例1  实验结果:  四、实验分析:1.掌握了Pygame的基础知识。......
  • 2022/12/3 Python实验报告
      实验报告1、实验目的和要求了解并掌握Pygame的基本应用2、实验环境笔记本与Python书本3、实验过程实例01制作一个跳跃的小球游戏创建一个游戏......
  • 组合数模板
    逆元定义:当x*y≡1(modp),y就是x在modp下的逆元应用:a/b≡a*x(modp),x就是a的乘法逆元我们可以将a/b≡a*x(modp)这个式子化简,两边同乘b-->a≡a*b*x(m......
  • 实验6
    实验6.3task3.11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89arr......
  • 接口文档模板
    标题:微信服务器获取access_token接口1、接口描述微信公众平台接口请求获取Token,用于验证身份2、调用方法HTTPS/GET3、接口路径https://api.weixin.qq.com/cgi-bin/to......
  • Python实验报告
    实验13:Pygame游戏编程一、实验目的和要求学会Pygame的基本应用二、Pygame的优点及应用  使用Python进行游戏开发的首选模块就是Pygame,专为电子游戏设计(包括图像、......
  • 【IDEA】测试类(test)的模板及定义
    人和动物的区别是:能制造和使用工具。因为,工具大大便利了我们的生活。我们在使用Idea开发java项目的过程中,有些代码是固定的,我们能不能只写几个字,就代表一连串的代码。例如......
  • 基于yolo进行目标检测的实验和研究【BLOG】
          根据我接触到的项目经验来看,需要我们进行检测的不是自然场景下的任意物体,而是特定场景下一类物体。典型的就是钢管识别,这些照片一般都是在厂区里面拍的、......
  • 大数据--Hadoop环境部署(2)主机映射和免密登录
    一.主机IP映射就是将虚拟机的IP地址和主机名进行映射,这样就可以直接通过root@主机名的方式找到对应的虚拟机(三台虚拟机都要建立三条映射关系)vim/etc/hosts192.168.121.......
  • 大数据--Hadoop环境部署(1)Linux环境搭建
    一.安装三台Linux虚拟机使用centos7系统,命名node_01,node_02,node_03,具体在VMware上的各种安装过程见其他博客二.虚拟机参数设置1.配置Linux系统网络及主机名创建完成的......