首页 > 其他分享 >实验6

实验6

时间:2024-12-23 23:41:51浏览次数:3  
标签:std const get Complex 实验 return include

实验任务1:

Complex,hpp:

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<stdexcept>
 5 
 6 template<typename T>
 7 class Complex {
 8 public:
 9     Complex(T r = 0,T i = 0);
10     Complex(const Complex<T> &c);
11     
12     T get_real() const;
13     T get_imag() const;
14     
15     Complex<T>& operator+=(const Complex<T> &c);
16     
17     template<typename T1>
18     friend std::ostream& operator<<(std::ostream &out,const Complex<T1> &c);
19     
20     template<typename T1>
21     friend std::istream& operator>>(std::istream &in,Complex<T1> &c);
22     
23 private:
24     T real;
25     T imag;
26 };
27 
28 template<typename T>
29 Complex<T> operator+(const Complex<T> &c1,const Complex<T> &c2);
30 
31 template<typename T>
32 bool operator==(const Complex<T> &c1,const Complex<T> &c2);
33 
34 template<typename T>
35 Complex<T>::Complex(T r,T i):real{r},imag{i}{
36 }
37 
38 template<typename T>
39 Complex<T>::Complex(const Complex<T> &c):real{c.real},imag{c.imag}{
40 }
41 
42 template<typename T>
43 T Complex<T>::get_real() const {
44     return real;
45 }
46 
47 template<typename T>
48 T Complex<T>::get_imag() const {
49     return imag;
50 }
51 
52 template<typename T>
53 Complex<T>& Complex<T>::operator+=(const Complex<T> &c){
54     real += c.real;
55     imag += c.imag;
56     return *this;
57 }
58 
59 template<typename T1>
60 std::ostream& operator<<(std::ostream &out,const Complex<T1> &c){
61     if(c.imag >= 0)
62         out << c.real << " + " << c.imag << "i";
63     else
64         out << c.real << " - " << -c.imag << "i";
65     return out;
66 }
67 
68 template<typename T1>
69 std::istream& operator>>(std::istream &in,Complex<T1> &c){
70     in >> c.real >> c.imag;
71     return in;
72 }
73 
74 template<typename T>
75 Complex<T> operator+(const Complex<T> &c1,const Complex<T> &c2){
76     return Complex<T>(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
77 }
78 
79 template<typename T>
80 bool operator==(const Complex<T> &c1,const Complex<T> &c2){
81     return c1.get_real() == c2.get_real() && c1.get_imag() && c2.get_imag();
82 }
View Code

task1.cpp:

 1 #include"Complex.hpp"
 2 #include<iostream>
 3 #include<fstream>
 4 #include<stdexcept>
 5 
 6 void test1();
 7 void test2();
 8 
 9 int main(){
10     using namespace std;
11     
12     cout << "测试1:负数模版类测试" << endl;
13     test1();
14     
15     cout << "\n测试2:文件I/O测试" << endl;
16     test2();
17 }
18 
19 void test1(){
20     using namespace std;
21     Complex<double>c1{3.5,2},c2;
22     cout << "Enter c2: ";
23     cin >> c2;
24     cout << "c1 = " << c1 << endl;
25     cout << "c2 = " << c2 << endl;
26     cout << "c1 == c2:" << boolalpha << (c1 == c2) << endl;
27     
28     cout << "c1 + c2 = " << c1 + c2 << endl;
29     c1 += c2;
30     cout << "c1.real = " << c1.get_real() << endl;
31     cout << "c1.imag = " << c1.get_imag() << endl;
32     
33     cout << " c1 == c2: " << boolalpha << (c1==c2) << endl;
34 }
35 
36 void test2(){
37     using namespace std;
38     
39     Complex<int> c1{1,2},c2{9,-7};
40     ofstream out("ans.txt");
41     if(!out.is_open()){
42         cout << "fail to open file ans.txt to write\n";
43         return;
44     }
45     
46     out << "c1 = " << c1 << endl;
47     out << "c2 = " << c2 << endl;
48     out << "c1 + c2 = " << c1+c2 << endl;
49     out << "(c1 == c2) = " << boolalpha << (c1 == c2) << endl;
50     
51     out.close();
52     cout << "测试ok!" << endl;
53 }
View Code

实验任务2:

Contestant.hpp:

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <iomanip>
 5 #include <string>
 6 
 7 using std::string;
 8 using std::ostream;
 9 using std::istream;
10 using std::setw;
11 using std::setprecision;
12 using std::setiosflags;
13 using std::ios_base;
14 
15 class Contestant{
16 public:
17     Contestant() = default;
18     ~Contestant() = default;
19 
20     int get_num() const {return num;}
21     float get_time_usage() const {return time_usage;}
22 
23     friend ostream& operator<<(ostream &out, const Contestant &c);
24     friend istream& operator>>(istream &in, Contestant &c);
25 
26 private:
27     string no;          
28     string name;        
29     string major;      
30     int num;            
31     float time_usage;   
32 };
33 
34 ostream& operator<<(ostream &out, const Contestant &c){
35     out << setiosflags(ios_base::left);
36     out << setw(15) << c.no
37         << setw(15) << c.name
38         << setw(15) << c.major
39         << setw(5) << c.num
40         << setprecision(2) << c.time_usage;
41     
42     return out;
43 }
44 
45 istream& operator>>(istream &in, Contestant &c){
46     in >> c.no >> c.name >> c.major >> c.num >> c.time_usage;
47 
48     return in;
49 }
View Code

utils.hpp:

 1 #include "Contestant.hpp"
 2 #include <fstream>
 3 #include <iostream>
 4 #include <string>
 5 #include <vector>
 6 
 7 bool compare_by_solutionInfo(const Contestant &c1, const Contestant &c2){
 8     if(c1.get_num() > c2.get_num())
 9         return true;
10     
11     if(c1.get_num() == c2.get_num())
12         return c1.get_time_usage() < c2.get_time_usage();
13     
14     return false;
15 }
16 
17 void output(std::ostream &out, const std::vector<Contestant> &v){
18     for(auto &i: v)
19         out << i << std::endl;
20 }
21 
22 void save(const std::string &filename, std::vector<Contestant> &v){
23     using std::ofstream;
24 
25     ofstream out(filename);
26     if(!out.is_open()){
27         std::cout << "fail to open file to write\n";
28         return;
29     }
30 
31     output(out, v);
32     out.close();
33 }
34 
35 void load(const std::string &filename, std::vector<Contestant> &v){
36     using std::ifstream;
37 
38     ifstream in(filename);
39     if(!in.is_open()){
40         std::cout << "fail to open file to read\n";
41         return;
42     }
43 
44     std::string title_line;
45     getline(in, title_line);  
46 
47     int first_column;
48     Contestant t;
49     while(in >> first_column >> t) 
50         v.push_back(t);
51 
52     in.close();
53 }
View Code

task2.cpp:

 1 #include "Contestant.hpp"
 2 #include "utils.hpp"
 3 #include <iostream>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 
 8 void test(){
 9     using namespace std;
10 
11     vector<Contestant> v;
12 
13     load("data2.txt", v);  
14     sort(v.begin(), v.end(), compare_by_solutionInfo);
15     output(cout, v); 
16     save("ans.txt", v);
17 }
18 
19 int main(){
20     test();
21 }
View Code

实验任务3:

Triangle.hpp:

 1 #include <iostream>
 2 #include <stdexcept>
 3 #include <cmath>
 4 
 5 using namespace std;
 6 
 7 class Triangle{
 8 public:
 9     Triangle(double s1, double s2, double s3);
10     ~Triangle() = default;
11 
12     double area() const;
13 
14 private:
15     double a, b, c;
16 };
17 
18 Triangle::Triangle(double s1, double s2, double s3): a{s1}, b{s2}, c{s3}{
19     if(a <= 0 || b <= 0 || c <= 0)
20         throw invalid_argument("边长出现负值");
21         
22     if(a+b <= c || b+c <= a || a+c <= b) 
23         throw invalid_argument("不满足任意两边之和大于第三边");
24 }
25 
26 double Triangle::area() const { 
27     double s = (a + b + c)/2;
28     return sqrt(s*(s-a)*(s-b)*(s-c));
29 }
View Code

task3.cpp:

 1 #include "Triangle.hpp"
 2 #include <iostream>
 3 #include <fstream>
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     cout << "从文件读入三角形三边边长,计算面积" << endl;
 9 
10     ifstream in("data3.txt");
11     if(!in.is_open()){
12         cout << "fail to open file to read\n";
13         return;
14     }
15 
16     double a,b,c;
17     do{
18         cout << "三角形边长: ";
19         in >> a >> b >> c;
20         cout << a << " " << b << " " << c << endl;
21 
22         try{
23             Triangle t(a, b, c);
24             cout << "三角形面积: " << t.area() << endl << endl;
25         }catch(const exception &e){
26             cout << "error: " << e.what() << endl << endl;
27         }
28 
29         if(in.peek() == EOF)
30             break;
31     }while(1);
32 
33     in.close();
34 }
35 
36 int main(){
37     test();
38 }
View Code

实验任务4:

Vector.hpp:

 1 #pragma once
 2 #include<iostream>
 3 #include<stdexcept>
 4 #include<cmath>
 5  
 6 using namespace std;
 7  
 8 template<typename T>
 9 class Vector {
10 public:
11     Vector(int s ) :size{ s } {
12         if (s < 0)
13             throw std::length_error("vector constructor:negative size");
14         else
15             ptr = new T[size];
16     }
17     Vector(int s , T value ) :size{ s } {
18         if (s < 0)
19             throw std::length_error("vector constructor:negative size");
20         
21             ptr = new T[size];
22             for (int i = 0; i < size; ++i)
23                 ptr[i] = value;           
24         
25     }
26     Vector(const Vector<T>& v) :size{v.size}, ptr{new T[size]}{
27         for (int i = 0; i < size; ++i)
28             ptr[i] = v.ptr[i];
29     } 
30     ~Vector(){delete[]ptr;}
31  
32     int get_size(){ return size;}
33     
34     T& at(int index)const {
35         if (index < 0 || index >= size)
36             throw std::out_of_range("vector:index out of range");
37         return ptr[index];
38     }
39     T& operator[](int index){
40         if (index < 0 || index >= size)
41             throw std::out_of_range("vector:index out of range");
42  
43         return ptr[index];
44     }
45     
46     friend void output(const Vector<T>v){
47         for (int i = 0; i < v.size; i++)
48             cout << v.at(i) << ",";
49         cout << "\b\b\n";
50     }
51  
52 private:
53     int size;
54     T* ptr;
55 };
View Code

task4.cpp:

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test1() {
 5     using namespace std;
 6 
 7     int n;
 8     cout << "Enter n: ";
 9     cin >> n;
10     
11     Vector<double> x1(n);
12     for(auto i = 0; i < n; ++i)
13         x1.at(i) = i * 0.7;
14 
15     cout << "x1: "; output(x1);
16 
17     Vector<int> x2(n, 42);
18     const Vector<int> x3(x2);
19 
20     cout << "x2: "; output(x2);
21     cout << "x3: "; output(x3);
22 
23     x2.at(0) = 77;
24     x2.at(1) = 777;
25     cout << "x2: "; output(x2);
26     cout << "x3: "; output(x3);
27 }
28 
29 void test2() {
30     using namespace std;
31 
32     int n, index;
33     while(cout << "Enter n and index: ", cin >> n >> index) {
34         try {
35             Vector<int> v(n, n);
36             v.at(index) = -999;
37             cout << "v: "; output(v);
38         }
39         catch (const exception &e) {
40             cout << e.what() << endl;
41         }
42     }
43 }
44 
45 int main() {
46     cout << "测试1: 模板类接口测试\n";
47     test1();
48 
49     cout << "\n测试2: 模板类异常处理测试\n";
50     test2();
51 }
View Code

实验任务5:

 Contestant.hpp:

 1 #pragma once  
 2 #include <iostream>  
 3 #include <iomanip>  
 4 #include <string>  
 5 
 6 using std::string;  
 7 using std::ostream;  
 8 using std::istream;  
 9 using std::setw;  
10 using std::setprecision;  
11 using std::setiosflags;  
12 using std::ios_base;  
13 
14 class Contestant {  
15 public:  
16     Contestant() = default;  
17     ~Contestant() = default;  
18 
19     string get_no() const { return no; }  
20     string get_name() const { return name; }  
21     string get_major() const { return major; }  
22     float get_score() const { return score; }
23 
24     friend ostream& operator<<(ostream &out, const Contestant &c);  
25     friend istream& operator>>(istream &in, Contestant &c);  
26 
27 private:  
28     string no;      
29     string name;     
30     string major;     
31     double score; 
32 };  
33  
34 ostream& operator<<(ostream &out, const Contestant &c) {  
35     out << setiosflags(ios_base::left);  
36     out << setw(10) << c.no  
37         << setw(10) << c.name  
38         << setw(10) << c.major  
39         << setw(10) << c.score;  
40     return out;  
41 }  
42  
43 istream& operator>>(istream &in, Contestant &c) {    
44     in >> c.no >> c.name >> c.major >> c.score; 
45     return in;  
46 }
View Code

utils.hpp:

 1 #include "Contestant.hpp"  
 2 #include <fstream>  
 3 #include <iostream>  
 4 #include <string>  
 5 #include <vector>  
 6 #include <algorithm>  
 7 
 8 bool compare(const Contestant &c1, const Contestant &c2) {  
 9     if (c1.get_major() != c2.get_major())   
10         return c1.get_major() < c2.get_major(); 
11     return c1.get_score() > c2.get_score();  
12 }  
13  
14 void output(std::ostream &out, const std::vector<Contestant> &v) {  
15     for (const auto &i : v) {  
16         out << i << std::endl;  
17     }  
18 }  
19  
20 void save(const std::string &filename, const std::vector<Contestant> &v) {  
21     std::ofstream out(filename);  
22     if (!out.is_open()) {  
23         std::cout << "fail to open file to write\n";  
24         return;  
25     }  
26     output(out, v);  
27     out.close();  
28 }  
29  
30 void load(const std::string &filename, std::vector<Contestant> &v) {  
31     std::ifstream in(filename);  
32     if (!in.is_open()) {  
33         std::cout << "fail to open file to read\n";  
34         return;  
35     }  
36     std::string title_line;  
37     getline(in, title_line); 
38     Contestant t;  
39     while (in >> t) {  
40         v.push_back(t);  
41     }  
42     in.close();  
43 }
View Code

task5.cpp:

 1 #include "Contestant.hpp"  
 2 #include "utils.hpp"  
 3 #include <iostream>  
 4 #include <vector>  
 5 #include <algorithm>  
 6 
 7 using namespace std;  
 8 
 9 int main() {  
10     vector<Contestant> contestants;  
11     load("data5.txt", contestants);  
12     sort(contestants.begin(), contestants.end(), compare);  
13     output(cout,contestants); 
14     save("ans5.txt", contestants);  
15     return 0;  
16 }
View Code

 

标签:std,const,get,Complex,实验,return,include
From: https://www.cnblogs.com/guguabiu/p/18610837

相关文章

  • 数据结构实验
    (一)线性表1#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;multiset<int>ms;cin>>n;for(inti=1;i<=n;i++){intx;cin>>x;ms.insert(x);}intx;cin......
  • 数据结构实验
    (一)线性表1#include<bits/stdc++.h>usingnamespacestd;intmain(){intn;multiset<int>ms;cin>>n;for(inti=1;i<=n;i++){intx;cin>>x;ms.insert(x);}intx;cin......
  • 实验6
    4:1#pragmaonce23#include<iostream>4#include<string>5#include<stdexcept>6usingnamespacestd;78template<typenameT>9classVector{10private:11intsize;12T*ptr;13public:14Vector<......
  • 数据结构实验14-哈希查找&排序1
    目录【id:113】【20分】A.DS哈希查找--链地址法【id:114】【20分】B.DS哈希查找与增补【id:119】【10分】C.DS哈希查找—二次探测再散列【id:115】【20分】D.DS哈希查找—线性探测再散列【id:163】【10分】E.DS排序--直接插入排序【id:122】【10分】F.DS排序--希......
  • 实验7
    task.4代码:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#defineN80#defineM3voidwrite();voidculcuate();intmain(){printf("write\n");write();printf("算\n");culcuate();return0;}voidwrit......
  • 实验文档7
    关于第7次实验课作业实验结论task4.c1#define_CRT_SECURE_NO_WARNINGS2#include<stdio.h>3#defineN100004intmain(){5charch[N];6intline=1;7intn=0;8intj=0;9FILE*fp;10fp=fopen("data4.txt",......
  • 上海人工智能实验室:多模态实时流媒体模型
    ......
  • 实验6 模板类、文件I/O和异常处理
    task4代码:Vector.hpp1#pragmaonce2#include<iostream>34usingnamespacestd;56template<typenameT>7classVector{8public:9Vector(intn);10Vector(intn,Tvalue);11Vector(constVector<T>&......
  • flask毕设研究生实验室综合管理系统(程序+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容选题背景关于研究生实验室综合管理系统的研究,现有研究主要集中在高校实验室的信息化管理、资源调度及效率提升等方面。尽管这些研究在提升实验室运......
  • 实验7
    task4#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charfilename[]="data4.txt";charch;intlines=0;intcharacters=0;fp=fopen(filename,"r");if(fp==NULL){......