首页 > 其他分享 >实验六

实验六

时间:2022-12-07 00:13:53浏览次数:36  
标签:std cout int Vector 实验 include size

task3

测试结果:

 

 

 

 

 

 

源代码:

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

 原因: 当array数组储存字符型时,读入文件内容”a b c d e f“将把空格也读成字符。

task4

测试结果:

 

源代码:

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

 

task5

测试结果:

 

 

源代码:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 int main(int argc, char const *argv[])
 7 {
 8     ofstream outFile;
 9     outFile.open("cipher_key.txt");
10     if (!outFile.is_open())
11     {
12         cout << "fail to open data1.dat\n";
13         return 1;
14     }
15     for (int i = 0; i < 27; i++)
16     {
17         if (!i)
18         {
19             cout << "   ";
20             outFile << "   ";
21             for (int j = 97; j < 123; j++)
22             {
23                 cout << char(j) << " ";
24                 outFile << char(j) << " ";
25             }
26             cout << endl;
27             outFile << endl;
28             continue;
29         }
30         cout << setiosflags(ios_base::left) << setw(2) << i << " ";
31         outFile << setiosflags(ios_base::left) << setw(2) << i << " ";
32         for (int j = i; j < i + 26; j++)
33         {
34             cout << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
35             outFile << setiosflags(ios_base::left) << setw(2) << char(65 + j % 26);
36         }
37         cout << endl;
38         outFile << endl;
39     }
40     outFile.close();
41     return 0;
42 }

 

标签:std,cout,int,Vector,实验,include,size
From: https://www.cnblogs.com/git-porn-hub/p/16961873.html

相关文章

  • 实验六 模板类与文件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,......
  • 实验六
    试验任务三task3_11#include<iostream>2#include<fstream>3#include<array>4#defineN556intmain(){7usingnamespacestd;89......
  • 实验六
    #include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    #pragmaonce#include<iostream>usingstd::cout;usingstd::endl;template<typenameT>classVector{public:Vector(Tn):size(n){p=newT[n];}V......