首页 > 其他分享 >实验6 模板类、文件I/O和异常处理

实验6 模板类、文件I/O和异常处理

时间:2023-12-17 16:47:37浏览次数:35  
标签:std 文件 const get Complex 实验 include 模板 size

实验任务1

Complex.hpp

#pragma once

#include <iostream>
#include <stdexcept>

template<typename T>
class Complex {
public:
    Complex(T r = 0, T i = 0): real{r}, imag{i} {}
    Complex(const Complex<T> &c): real{c.real}, imag{c.imag} {}

    T get_real() const { return real; }
    T get_imag() const { return imag; }

    Complex<T>& operator+=(const Complex<T> c);    
    template<typename T1>
    friend std::ostream& operator<<(std::ostream &out, const Complex<T1> &c);  

    template<typename T1>
    friend std::istream& operator>>(std::istream &in, Complex<T1> &c); 

private:
    T real;
    T imag;
};

template<typename T>
Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2);

template<typename T>
bool operator==(const Complex<T> &c1, const Complex<T> &c2);

template<typename T>
Complex<T>& Complex<T>::operator+=(const Complex<T> c) {
    real += c.real;
    imag += c.imag;

    return *this;
}

template<typename T1>
std::ostream& operator<<(std::ostream &out, const Complex<T1> &c) {
    if(c.imag >= 0)
        out << c.real << " + " << c.imag << "i";
    else
        out << c.real << " - " << -c.imag << "i";
    
    return out;
}

template<typename T1>
std::istream& operator>>(std::istream &in, Complex<T1> &c) {
    in >> c.real >> c.imag;

    return in;
}

template<typename T>
Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2) {
    return Complex<T>(c1.get_real() + c2.get_real(), 
                      c1.get_imag() + c2.get_imag());
}

template<typename T>
bool operator==(const Complex<T> &c1, const Complex<T> &c2) {
    return c1.get_real() == c2.get_real() && 
           c1.get_imag() == c2.get_imag();
}
View Code

task1.cpp

#include "Complex.hpp"
#include <iostream>
#include <fstream>
#include <stdexcept>

void test1() {
    using namespace std;

    Complex<double> c1{1, 2}, c2;
    
    cout << "Enter c2: ";
    cin >> c2;
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;

    cout << "c1 + c2 = " << c1 + c2 << endl;
    c1 += c2;
    cout << "c1.real = " << c1.get_real() << endl;
    cout << "c1.imag = " << c1.get_imag() << endl;

    cout << boolalpha << (c1 == c2) << endl;
}

void test2() {
    using namespace std;

    Complex<int> c1{1, 5}, c2{3, -7};
    ofstream out("ans.txt");
    if(!out.is_open()) {
        cout << "fail to open file ans.txt to write\n";
        return;
    }

    out << "c1 = " << c1 << endl;
    out << "c2 = " << c2 << endl;
    out << "c1 + c2 = " << c1 + c2 << endl;

    out.close(); 
}


int main() {
    using namespace std;

    cout << "测试自定义复数模板类Complex<T>..." << endl;
    test1();

    cout << "\n测试文件流I/O..." << endl;
    test2();
}
View Code

运行结果

 

实验任务2

stu.hpp

#pragma once

#include <iostream>
#include <iomanip>
#include <string>

using std::string;
using std::ostream;
using std::istream;
using std::setw;
using std::setiosflags;
using std::ios_base;

class STU {
public:
    STU() = default;
    ~STU() = default;

    string get_name() const { return name; }
    string get_no() const { return no; }
    int get_score() const { return score; }

    friend ostream& operator<<(ostream &out, const STU &s);
    friend istream& operator>>(istream &in, STU &s);

private:
    string no;
    string name;
    int score;
};

ostream& operator<<(ostream &out, const STU &s) {
    out << setiosflags(ios_base::left) << setw(15) << s.no
        << setw(15) << s.name
        << s.score;

    return out;
}

istream& operator>>(istream &in, STU &s) {
    in >> s.no >> s.name >> s.score;

    return in;
}
View Code

task2.cpp

#include "stu.hpp"
#include "utils.hpp"

#include <vector>
#include <fstream>
#include <algorithm>

void test1() {
    using namespace std;

    vector<STU> stu;
    STU t;

    ifstream in;
    in.open("data.txt", ios::in);
    if(!in.is_open()) {
        cout << "fail to open file data.txt\n";
        return;
    }

    while(in >> t)
        stu.push_back(t);
    
    in.close();

    cout << "\n原始学生信息:\n";
    output(cout, stu);

    sort(stu.begin(), stu.end(), compare_by_no);
    save("data_by_no.txt", stu);

    sort(stu.begin(), stu.end(), compare_by_name);
    save("data_by_name.txt", stu);

    sort(stu.begin(), stu.end(), compare_by_score);
    save("data_by_score.txt", stu);

}

int main() {
    test1();
}
View Code

utils.hpp

#include "stu.hpp"
#include <vector>
#include <fstream>
#include <iostream>
#include <string>

bool compare_by_name(const STU &s1, const STU &s2) {
    return s1.get_name() < s2.get_name();
}

bool compare_by_no(const STU &s1, const STU &s2) {
    return s1.get_no() < s2.get_no();
}

bool compare_by_score(const STU &s1, const STU &s2) {
    return s1.get_score() > s2.get_score();
}

void output(std::ostream &out,  std::vector<STU> &v) {
    for(auto &i: v)
        out << i << std::endl;
}

void save(std::string filename, std::vector<STU> &v) {
    using std::ofstream;

    ofstream out;
    out.open(filename);
    if(!out.is_open()) {
        std::cout << "fail to open file " << filename << std::endl;
        return;
    }

    output(out, v);
    out.close();
}
View Code

运行结果

 

实验任务3

Triangle.hpp

#include <iostream>
#include <stdexcept>
#include <cmath>

using namespace std;

class Triangle {
public:
    Triangle(double s1, double s2, double s3);
    ~Triangle() = default;

    double area() const;

private:
    double a, b, c;
};

Triangle::Triangle(double s1, double s2, double s3): a{s1}, b{s2}, c{s3} {
    if(a <= 0 || b <= 0 || c <= 0)
        throw invalid_argument("边长出现负值");
        
    if(a+b <= c || b+c <= a || a+c <= b) 
        throw invalid_argument("不满足任意两边之和大于第三边");
}

double Triangle::area() const {
    double s = (a + b + c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}
View Code

task3.cpp

#include "Triangle.hpp"
#include <iostream>
#include <fstream>

void test1() {
    using namespace std;

    cout << "从文件读入三角形三边边长,计算面积" << endl;

    ifstream in("triangle_data.txt");
    if(!in.is_open()) {
        cout << "fail to open file to read\n";
        return;
    }

    double a,b,c;
    do {
        cout << "三角形边长: ";
        in >> a >> b >> c;
        cout << a << " " << b << " " << c << endl;

        try {
            Triangle t(a, b, c);
            cout << "三角形面积: " << t.area() << endl << endl;
        }catch(const exception &e) {
            cout << "error: " << e.what() << endl << endl;
        }

        if(in.peek() == EOF)
            break;
    } while(1);

    in.close();
}

int main() {
    test1();
}
View Code

运行结果

 

实验任务4

Vector.hpp

#pragma once

#include <iostream>
#include <stdexcept>

using namespace std;

template <typename T>

class Vector {
private:
    T* data;
    size_t size;

public:
    Vector(size_t sz) : size(sz) {
        data = new T[size];
    }

    Vector(size_t sz, const T& value) : size(sz) {
        data = new T[size];
        for (size_t i = 0; i < size; ++i)
        {
            data[i] = value;
        }
    }

    Vector(const Vector& other) : size(other.size) {
        data = new T[size];
        for (size_t i = 0; i < size; ++i)
        {
            data[i] = other.data[i];
        }
    }

    ~Vector() {
        delete[] data;
    }

    size_t get_size() const {
        return size;
    }

    T& at(size_t index) {
        if (index < size)
        {
            return data[index];
        }
        else
        {
            throw out_of_range("Index out of range");
        }
    }

    T& operator[](size_t index) {
        if (index < size)
        {
            return data[index];
        }
        else
        {
            throw out_of_range("Index out of range");
        }
    }

    friend void output(const Vector& vec) {
        for (size_t i = 0; i < vec.size; ++i)
        {
            cout << vec.data[i] << ' ';
        }
        cout << endl;
    }
};
View Code

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 * 1.414;

    output(x1);

    Vector<int> x2(n, 27);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 50;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main() {
    test();
}
View Code

运行结果

 

实验任务5

task5.cpp

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void output(ostream &out) {

    const int numLines = 26;

    for (int i = 0; i < numLines; ++i) {
        out << setw(2) << right << i + 1 << ' ';
        for (int j = 0; j < 26; ++j)
        {
            char ch = 'A' + (i + j) % 26;
            out << ch << ' ';
        }
        out << endl;
    }
}

int main() {

    cout << "输出矩阵:\n"
         << "   a b c d e f g h i j k l m n o p q r s t u v w x y z"
         << endl;

    output(cout);

    ofstream outFile("cipher_key.txt");

    if (outFile.is_open())
    {
        output(outFile);
        outFile.close();
    }
    else
    {
        return 1;
    }

    return 0;
}
View Code

运行结果

 

标签:std,文件,const,get,Complex,实验,include,模板,size
From: https://www.cnblogs.com/NFurioso/p/17909272.html

相关文章

  • 实验六
    4、试验任务4源代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales......
  • 23种设计模式——模板模式
    今天我们来学习23种设计模式中的模板模式。模板模式需要注意抽象类与具体子类之间的协作。它用到了虚函数的多态性技术以及“不用调用我,让我来调用你”的反向控制技术。概念:模板模式是定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法......
  • 实验7
    test4代码#include<stdio.h>#defineN2intmain(){char*x[N]={"0123456789-0123456789","nuist2023-nuist2024"};inti;FILE*fp;fp=fopen("data4.txt","w");......
  • 实验6
    1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;//销售......
  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献1简述你完成的工作作为组长,分配任务到各个小组组员上,组织统筹组员完成各项工作。参与组内博客和文档的编写工作。参与后端设计的代码编写和加密通信过程的实现2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为5535......
  • 实验6
    任务4#include<iostream>#include"Vector.hpp"usingnamespacestd;template<typenameT>voidoutput(constVector<T>&v){for(autoi=0;i<v.get_size();i++){cout<<v.at(i)<<",";......
  • 实验6 C语言结构体、枚举应用编程
    实验任务1源代码:1#include<stdio.h>2#include<string.h>3#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试45typedefstructstudent{6intid;//学号7charname[20];//姓名8......
  • 实验六
    #pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:intsize;T*ptr;public:Vector(intn):size{n}{if(n<0)throwstd::leng......
  • 实验7
    4.实验任务4task4.c源码:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf(&qu......
  • 实验6
    4.实验任务4task4.c源码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册......