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

发布时间 2023-12-17 16:37:09作者: NFurioso

实验任务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

运行结果