首页 > 其他分享 >实验一

实验一

时间:2023-10-15 11:14:50浏览次数:40  
标签:const int double void Complex 实验 Rect

任务一

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<array>
 5 template<typename T>
 6 void output1(const T &obj)
 7 {
 8     for(auto i:obj)
 9       std::cout<<i<<",";
10     std::cout<<"\b\b\n"; 
11 }
12 template<typename T>
13 void output2(const T &obj)
14 {
15     for(auto p=obj.begin();p!=obj.end();++p)
16         std::cout<<*p<<",";
17     std::cout<<"\b\b\n";
18 }
19 void test_array()
20 {
21     using namespace std;
22     array<int,5> x1;
23     cout<<"x1.size()="<<x1.size()<<endl;
24     x1.fill(42);
25     x1.at(0)=999;
26     x1[4]=-999;
27     cout<<"x1:";
28     output1(x1);
29     cout<<"x1:";
30     output2(x1);
31     array<int,5> x2(x1);
32     cout<<boolalpha<<(x1==x2)<<endl;
33     x2.fill(22);
34     cout<<"x2:";
35     output1(x2);
36     swap(x1,x2);
37     cout<<"x1:";
38     output1(x1);
39     cout<<"x2:";
40     output1(x2); 
41 }
42 void test_vector()
43 {
44     using namespace std;
45     vector<int>v1;
46     cout<<v1.size()<<endl;
47     cout<<v1.max_size()<<endl;
48     v1.push_back(55);
49     cout<<"v1:";
50     output1(v1);
51     vector<int> v2{1,0,5,2};
52     v2.pop_back();
53     v2.erase(v2.begin());
54     v2.insert(v2.begin(),999);
55     v2.insert(v2.end(),-999);
56     cout<<v2.size()<<endl;
57     cout<<"v2:";
58     output2(v2);
59     vector<int> v3(5,42);
60     cout<<"v3:";
61     output1(v3);
62     vector<int> v4(v3.begin(),v3.end()-2);
63     cout<<"v4:";
64     output1(v4);
65     
66 }
67 void test_string()
68 {
69     using namespace std;
70     string s1{"oop"};
71     cout<<s1.size()<<endl;
72     for(auto &i:s1)
73         i-=32;
74     s1+="2023";
75     s1.append(",hello");
76     cout<<s1<<endl;
77     
78 }
79 int main()
80 {
81     using namespace std;
82     cout<<"\n===========arry============="<<endl;
83     test_array();
84     cout<<"\n============vector=========="<<endl;
85     test_vector();
86     cout<<"\n===========string==========="<<endl;
87     test_string();
88 }
View Code1

 

任务二

 1 #include <iostream>
 2 #include <complex>
 3 void test_std_complex() 
 4 {
 5   using namespace std; 
 6   complex<double> c1{3, 4}, c2{4.5};
 7   const complex<double> c3{c2};
 8   cout << "c1 = " << c1 << endl;
 9   cout << "c2 = " << c2 << endl;
10   cout << "c3 = " << c3 << endl;
11   cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag()<< endl;
12   cout << "c1 + c2 = " << c1 + c2 << endl;
13   cout << "c1 - c2 = " << c1 - c2 << endl;
14   cout << "abs(c1) = " << abs(c1) << endl;
15   cout << boolalpha;
16   cout << "c1 == c2: " << (c1 == c2) << endl;
17   cout << "c3 == c2: " << (c3 == c2) << endl;
18   complex<double> c4 = 2;
19   cout << "c4 = " << c4 << endl;
20   c4 += c1;
21   cout << "c4 = " << c4 << endl; 
22 }
23 int main() 
24 {
25 test_std_complex();
26 }
View Code2

 

任务三

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class T 
 5 {
 6 public:
 7     T(int x = 0, int y = 0); 
 8     T(const T &t); 
 9     T(T &&t); 
10     ~T(); 
11     void set_m1(int x); 
12     int get_m1() const; 
13     int get_m2() const; 
14     void display() const; 
15     friend void func();
16 private:
17     int m1, m2;
18 public:
19     static void disply_count(); 
20 public:
21     static const string doc; 
22     static const int max_count; 
23 private:
24     static int count; 
25 };
26 const string T::doc{"a simple class"};
27 const int T::max_count = 99;
28 int T::count = 0;
29 T::T(int x, int y): m1{x}, m2{y}
30 {
31     ++count;
32     cout << "constructor called.\n";
33 }
34 T::T(const T &t): m1{t.m1}, m2{t.m2} 
35 {
36     ++count;
37     cout << "copy constructor called.\n";
38 }
39 T::T(T &&t): m1{t.m1}, m2{t.m2} 
40 {
41     ++count;
42     cout << "move constructor called.\n";
43 }
44 T::~T() 
45 {
46     --count;
47     cout << "destructor called.\n";
48 }
49 void T::set_m1(int x) 
50 {
51     m1 = x;
52 }
53 int T::get_m1() const 
54 {
55     return m1;
56 }
57 int T::get_m2() const 
58 {
59     return m2;
60 }
61 void T::display() const 
62 {
63     cout << m1 << ", " << m2 << endl;
64 }
65 void T::disply_count() 
66 {
67     cout << "T objects: " << count << endl;
68 }
69 void func() 
70 {
71     T t1;
72     t1.set_m1(55);
73     t1.m2 = 77; 
74     t1.display();
75 }
76 void test() 
77 {
78     cout << "T class info: " << T::doc << endl;
79     cout << "T objects max_count: " << T::max_count << endl;
80     T::disply_count();
81     T t1;
82     t1.display();
83     t1.set_m1(42);
84     T t2{t1};
85     t2.display();
86     T t3{std::move(t1)};
87     t3.display();
88     t1.display();
89     T::disply_count();
90 }
91 int main() 
92 {
93     cout << "============类T============" << endl;
94     test();
95     cout << endl;
96     cout << "============友元函数func()============" << endl;
97     func();
98 }
View Code

 

任务四

 1 #include <iostream>
 2 #include <string>
 3 #include <iomanip>
 4 using namespace std;
 5 class Rect
 6 {
 7 public:
 8     static const string doc;
 9     Rect(double x=2.0,double y=1.0);
10     Rect(Rect &r);
11     double len(){return length;}
12     double wide(){return width;}
13     double area(){return(length*width);}
14     static int size_info(){return size;}
15     double circumference(){return 2*(length+width);}
16     void resize(int times);
17     void resize(int l_times,int w_times);
18     ~Rect();
19 private:
20     double length,width;
21     static int size;    
22 }; 
23 const string Rect::doc("a simple rect class");
24 int Rect::size=0;
25 Rect::Rect(double x,double y)
26 {
27     length=x;
28     width=y;
29     size++;
30 }
31 void Rect::resize(int times)
32 {
33     length=length*times;
34     width=width*times;
35 }
36 void Rect::resize(int l_times,int w_times)
37 {
38     length=length*l_times;
39     width=width*w_times; 
40 }
41 Rect::Rect(Rect &r)
42 {
43     length=r.length;
44     width=r.width;
45     size++;
46 }
47 Rect::~Rect()
48 {
49     --size;
50 }
51 void output(const Rect &r) 
52 {
53     cout << "矩形信息: " << endl;
54     cout << fixed << setprecision(2); 
55     Rect a;
56     a=r;
57     cout<<"长度"<<a.len()<<endl;
58     cout<<"宽度"<<a.wide()<<endl; 
59     cout<<"周长"<<a.circumference()<<endl;
60     cout<<"面积"<<a.area()<<endl; 
61 }
62 void test() 
63 {
64     cout << "矩形类信息: " << Rect::doc << endl;
65     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
66     Rect r1;
67     output(r1);
68     Rect r2(4, 3);
69     output(r2);
70     Rect r3(r2);
71     r3.resize(2);
72     output(r3);
73     r3.resize(5, 2);
74     output(r3);
75     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
76 }
77 int main() 
78 {
79     test();
80     cout << "当前矩形对象数目: " << Rect::size_info()<< endl;
81 }
View Code

 

 

任务五

 

 1 #include <iostream>
 2 #include <cmath>
 3 using namespace std;
 4 class Complex
 5 {
 6 public:
 7     Complex(double a = 0, double b = 0) :a(a), b(b) {}
 8     Complex(const Complex& x) ;
 9     double get_real() { return a; }
10     double get_imag() const { return b; }
11     void show() const;
12     void add(const Complex &z);
13     ~Complex(){};
14     friend Complex add(const Complex &k,const Complex &l);
15     friend bool is_equal(const Complex &asd,const Complex &zxc);
16     friend double abs(const Complex &x);
17 
18 private:
19     double a, b;
20 };
21 Complex::Complex(const Complex& x)
22 {
23     a = x.a;
24     b = x.b;
25 }
26 void Complex::show() const
27 {
28     if (b > 0)
29         cout << a << "+" << b << "i" ;
30     else if (b < 0)
31         cout << a << b << "i" ;
32     else
33         cout << a ;
34 }
35 void Complex::add(const Complex &z)
36 {
37     a = a + z.a;
38     b = b + z.b;
39 }
40 Complex add(const Complex &k,const Complex &l)
41 {
42     Complex s;
43     s.a=k.a+l.a;s.b=k.b+l.b;
44     return (s);
45 }
46 bool is_equal(const Complex &asd,const Complex &zxc)
47 {
48     if (asd.a == zxc.a && asd.b == zxc.b)
49         return (true);
50     else
51         return (false);
52 }
53 double abs(const Complex &x)
54 {
55     return(sqrt(x.a * x.a + x.b * x.b));
56 }
57 
58 
59 void test()
60 {
61     using namespace std;
62     Complex c1(3, -4);
63     const Complex c2(4.5);
64     Complex c3(c1);
65     cout << "c1 = ";
66     c1.show();
67     cout << endl;
68     cout << "c2 = ";
69     c2.show();
70     cout << endl;
71     cout << "c2.imag = " << c2.get_imag() << endl;
72     cout << "c3 = ";
73     c3.show();
74     cout << endl;
75     cout << "abs(c1) = ";
76     cout << abs(c1) << endl;
77     cout << boolalpha;
78     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
79     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
80     Complex c4;
81     c4 = add(c1, c2);
82     cout << "c4 = c1 + c2 = ";
83     c4.show();
84     cout << endl;
85     c1.add(c2);
86     cout << "c1 += c2, " << "c1 = ";
87     c1.show();
88     cout << endl;
89 }
90 int main()
91 {
92     test();
93 }
View Code

 

 

标签:const,int,double,void,Complex,实验,Rect
From: https://www.cnblogs.com/dmsx/p/17763738.html

相关文章

  • 实验三 互斥锁
    不加锁的多线程售票系统存在的问题售票系统实现代码#include<stdio.h>#include<pthread.h>#include<unistd.h>intticketAmout=2;//票的数量:全局变量void*ticketAgent(void*arg){intt=ticketAmout;if(t>0){printf("Oneticketsold\n");t--;}el......
  • 实验四报告: 熟悉Python字典、集合、字符串的使用
    实验目标本实验的主要目标是熟悉Python中字典、集合、字符串的创建和操作,包括字典的创建、访问、修改和合并,集合的创建、访问以及各种集合运算,以及字符串的创建、格式化和常用操作。实验要求通过编写Python代码,验证以下要求:熟悉Python字典的创建、访问、修改、合并。熟悉Pyt......
  • 可实现加、减、乘、除、开平方的计算器软件的实验设计
    1、思路代码:#include<stdio.h>#include<math.h>//牛顿迭代法计算平方根doublesqrt_newton(doublex){doubleguess=x/2.0;//初始猜测值为x的一半doubledelta=0.000001;//误差范围while(fabs(guess*guess-x)>delta){guess=(guess+x/guess)/2.0;......
  • 5381: C++实验:STL之search
    描述  使用STL中的search函数,判断一个序列是否是另一个序列的子序列。部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。  C++intmain(){vector<int>vec1,vec2;intn,m,a;cin>>n>>m;while(n--){cin>>a;......
  • 5383: C++实验:STL之multimap
    描述  使用STL中的multimap记录用户的所有电话号码,yuyu想查询用户有多少个电话号码,crq则想查询时输出所有的号码。部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。 C++intmain(){ multimap<string,string>sm; stringname,phone; intn; cin>>......
  • GPU实验室-在阿里云云上部署ChatGLM2-6B大模型
    实验室地址:https://developer.aliyun.com/adc/scenario/f3dc63dc55a543c3884b8dbd292adcd5一、先买机器并开通对应安全组8501端口规格族:GPU计算型gn6i实例规格:ecs.gn6i-c4g1.xlarge安全组新增规则入方向端口范围:8501/8501授权对象:0.0.0.0/0二、最好是安装系统的时候把安装nvidi......
  • 实验5实验6_102101310_黄心怡
    实验5实验6_102101310_黄心怡实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络应用程序,进一步熟悉POX控制器流表下发的方法。二......
  • matalab实验imread函数和FFT使用!【图像的傅立叶变换实验】
    解决的问题:Imread函数一般不要用动态的图,否则会如下的报错!步骤:随便搞一张.png图片—>新建一个文件夹—>存放你找的图片和你建的工程(matalab工程命名不可以使用空格)—>把当前文件夹设置成改你设置的文件【如图】:如果没有后缀出现(点击查看,打开显示后缀名,修改成.gif,如果实验要求是。p......
  • 实验1 类和对象
    实验任务1task1.cpp源码1//标准库string,vector,array基础用法2#include<iostream>3#include<string>4#include<vector>5#include<array>67//函数模板8//对满足特定条件的序列类型T对象,使用范围for输出9template<typenameT>10voidoutp......
  • 实验1 类和对象
    //标准库string,vector,array基础用法#include<iostream>#include<string>#include<vector>#include<array>//函数模板//对满足特定条件的序列类型T对象,使用范围for输出template<typenameT>voidoutput1(constT&obj){for(autoi:obj)......