首页 > 其他分享 >实验六

实验六

时间:2022-11-30 18:57:27浏览次数:39  
标签:index cout int Vector 实验 output include

task3_1

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace std;

  //  array<int, N> x {97, 98, 99, 100, 101};
    array<int, N> x {97,98,99,100,101};
    ofstream out;
    out.open("data1.dat", ios::binary);
    if(!out.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }

    out.write(reinterpret_cast<char *>(&x), sizeof(x));
    out.close();
}

  task3_2

#include <iostream>
#include <fstream>
#include <array>
#define N 5

int main() {
    using namespace std;
  //  array<int, N> x;
    array<char, N> x;

    ifstream in;
    in.open("data1.dat", ios::binary);
    if(!in.is_open()) {
        cout << "fail to open data1.dat\n";
        return 1;
    }

    in.read(reinterpret_cast<char *>(&x), sizeof(x));
    in.close();

    for(auto i = 0; i < N; ++i)
        cout << x[i] << ", ";
    cout << "\b\b \n";
}

  

改动后

 在写入文件时写入int类型,在内存中占4个字节,读入文件时读入char类型,在内存中占一个字节。

所以读入后会有三个字节的空缺空间。

Vector.hpp

#include<bits/stdc++.h>
using namespace std;
template <class T>
class Vector{
	public:
	Vector(int n);
	Vector(int n,T v);
	~Vector();
	Vector(Vector<T>& v);
	int get_size()const;
	T &at(int index);
	T &operator[](int index);
	 template<class A> friend void output(Vector<A> &xx);
	 private:
	 	int size;
	 	T *a;
}; 
template <class T>
Vector<T>::Vector(int n):size{n}{
   //  cout<<"constructor 1 called."<<endl;
     a = new T[n];
}
template <class T>
Vector<T>::Vector(int n,T v):size{n}{
  //  cout<<"constructor 2 called."<<endl;
    a = new T[n];
    for(int i=0;i<n;i++)
    a[i] = v;
}
template <class T>
Vector<T>::Vector(Vector<T>& v){
//	cout<<"copy constructor  called."<<endl;
	size = v.size;
	a = new T[size];
	for(int i=0;i<size;i++)
    a[i] = v[i];
}
template <class T>
Vector<T>::~Vector(){ 
      delete[] a;
     // cout<<"destructor called."<<endl;
}
template <class T>
int Vector< T>::get_size()const{
    return size;
}
template <class T>
T &Vector<T>::at(int index){
	return a[index];
}
template <class T>
T &Vector<T>::operator[](int index){
    assert(index >= 0 && index < size);
    return a[index];
}
template<class A>
void output(Vector<A> &xx){
    for(int i=0;i<xx.get_size();i++)
    cout<<xx[i]<<" ";
    cout<<endl;
}

  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();
}

  

task5.cpp

#include<bits/stdc++.h>
using namespace std;
void output(std::ofstream &out)
{   
   int i,j;
	out.open("cipher_key.txt");
	if(!out.is_open()) {
       cout << "fail to open cipher_key.txt\n";
       return;
    }
     for(i=0;i<27;i++){
       //
       if(i==0){
       cout<<setw(2)<<" ";
       out<<setw(2)<<" ";
       for(j=97;j<123;j++){  
       cout<<setw(2)<<char(j);
       out<<setw(2)<<char(j);
	   }
       cout << endl;
       out<< endl;  
		 
	   }
	   
    else{
    	for(j=0;j<27;j++){ 
	 if(j==0){
	   cout<<setw(2)<<i;
	   out<<setw(2)<<i;
	   continue;
	   }
	   cout<<setw(2)<<char(65+(i+j-1)%26);
		out<<setw(2)<<char(65+(i+j-1)%26);
	}
		cout<<endl;
		out<<endl;
    }
}
	
	out.close();	

}
int main(){
	ofstream out;
   	output(out);
}

  

小结

任务4中,对于友元函数的模板类声明必须换一个模板类字符。对每个成员函数类外定义前也要声明模板类。

任务五中,按行写入文件,基本思路是输出一行写一行。对于这种矩阵的基本循环控制也要熟悉。

标签:index,cout,int,Vector,实验,output,include
From: https://www.cnblogs.com/nitendoblog/p/16939441.html

相关文章

  • 实验6
    实验任务3:task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98......
  • 数据结构实验之栈与队列二:一般算术表达式转换成后缀式 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT100typedefstruct{  char*base;  ......
  • 数据结构实验之栈与队列四:括号匹配 sdut-oj
    #include<stdio.h>#include<stdlib.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstruct{  char*base;  ......
  • 实验六
    task4Vector.hpp:1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4template<typenameT>5classVector6{7private:8intsize;......
  • 数字逻辑实验 9 FPGA数字钟(Verilog)
    目录实验9FPGA数字钟实验分析:实现思路:硬件支持:硬件描述语言代码编写:1顶层模块2时钟分频,(正/倒)计时器模块3输入处理模块in_out.v5......
  • 实验6 模板类和文件I/O
    实验目的体验模板函数、模板类的编写,从多态角度理解模板函数和模板类(类型作为参数)体验标准I/O流类、文件I/O流类、字符串I/O流类的用法,能正确使用针对问题场景,能正确、......
  • web实验四(二)
    web实验四(二)基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服......
  • 实验四 Web服务器2
    基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用LinuxSocket实现:1.Web服务器的客户端服务器,提交程序运行截图2.实现GET即可,请求,响应要符合HTTP协议规范3.服务器部署到......
  • 焱融科技为国家重点实验室打造海量高性能存储
    中国科学院大气物理研究所大气科学和地球流体力学数值模拟国家重点实验室(英文缩写LASG)是国家级重点实验室。LASG主要研究方向为地球气候系统模式的研发与应用;天气气候动力......
  • 实验六
    task3task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,......